Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

position of Address in a frame

Status
Not open for further replies.

treyz

Member level 5
Joined
Sep 26, 2012
Messages
93
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,065
Hi Everyone,

I am writting my code on c18 compiler from Mplab,
I am sending a frame: stx-address-data-checksum-etx. The frame is done and I am able to send it via RS232 from PC to a PIC 18f4680.

The things is according to the value of "address",the position of "address" might changes such as if address is equal to zero, send out (from the PIC via SPI)the frame as it is, but if it is greater than 0(between 1 and 99), send it out like that : address-stx-data-checksum-etx.

I don't know how to do it in C, Does anyone have a template that I can use, or something similar to that then I can start from scratch?

Thanks
 

Code:
if(addr == 0) {
	//send data as "STX-address-data-checksum-ETX"
}
else if((addr >=1) && (addr <= 99)) {

	//send data as address-STX-data-checksum-ETX"
}

Do you want to send data from PIC to PC or PC to PIC?

Code:
unsigned char addr, data, checksum, stx, etx;

if(addr == 0) {
	
	//send data as "STX-address-data-checksum-ETX"
	result[0] = stx;
	result[1] = addr;
	result[2] = data;
	result[3] = checksum;
	result[4] = etx;
	
}
else if((addr >=1) && (addr <= 99)) {

	//send data as address-STX-data-checksum-ETX"
	result[0] = addr;
	result[1] = stx;
	result[2] = data;
	result[3] = checksum;
	result[4] = etx;


}

for(i=0;i<5;i++) {

        WriteUSART(result[i]);

}
 
Last edited:

Hey!

I thought you had forgotten about me,
Tha data is sent fron the PC, goes through a 1st PIC and then the second PIC for a system via RS485. There is where I need to check "address" and change the order of the frame.
 

What is the problem with the above code? How is PIC to PIC communicating? Using UART?
 

No, we don't have such of thing in here. Too expensive I think....
 

yes please, that would be helpful!:)
 

You can have as many Virtual Terminals as you want to send and receive data in Proteus. Also you have I2C and SPI debuggers. You can debug your source code if you create .cof files.


Assuming the first PIC receives data as "STX-address-data-checksum-ETX"

Code:
unsigned char result[32];


if(result[1] == 0) {
	
	//send data as "STX-address-data-checksum-ETX"
	WriteUSART(result[0]);
	WriteUSART(result[1]);
	WriteUSART(result[2]);
	WriteUSART(result[3]);
	WriteUSART(result[4]);


	
}
else if((result[1] >=1) && (result[1] <= 99)) {

	//send data as address-STX-data-checksum-ETX"
	WriteUSART(result[1]);
	WriteUSART(result[0]);
	WriteUSART(result[2]);
	WriteUSART(result[3]);
	WriteUSART(result[4]);

}
 
Last edited:

OK. Are you leaving? If yes, then see you tomorrow.

Code:
unsigned char result[32];
unsigned int count = 0;

if(result[1] == 0) {
	
	//send data as "stx-address-data-checksum-etx"

	for(count=0;count<5;count++) {
		WriteUSART(result[count]);
		
	}
	
}

else if((result[1] >=1) && (result[1] <= 99)) {

	//send data as address-stx-data-checksum-etx"

	WriteUSART(result[1]);
	WriteUSART(result[0]);
	for(count=2;count<5;count++) {
		WriteUSART(result[count]);
		
	}
}
 
Last edited:

Hi,

I have been thinking about your code, actually, I am not writing to the USART, I am just want to send them out(via RS485), using TX pin from the PIC.
 

Yes, it Has

For a rs485 communication, the software doesn't change, all the changes are on the hardware side which is already done(using a MAX1487).
 

It just on the code about the conditions on "address", that you have already given me....so I am going to work on it and get back to you within 2 hours,alright?
 

Actually, I don't see how can I change the orders of my frame, because,the order is set from the PC....Do you have any idea?
 

Are you saying that "stx-address-data-checksum-etx" , "address-stx-data-checksum-etx" is set from PC?

can data be 0x02 or 0x03 i.e. stx or etx?
 

The order is from the PC(STX-ADDRESS-DATA-CHECKSUM-ETX), this frame goes to the 1st PIC via RS232 byte by bytes(does it mean that result[0] = stx and result[1]= address etc...automatically?), this PIC analyses it and perform a checksum, check "address" if it is at 0 or between 1 and 99. According to it, the 1st PIC will send out the frame in the right order.
 

Check for result[j] == 0x03. If result[j] == 0x03 condition is passed, then checksum is calculated after that check "address" if it is at 0 or between 1 and 99. According to it, send the frame as "STX-ADDRESS-DATA-CHECKSUM-ETX" or "ADDRESS-STX-DATA-CHECKSUM-ETX"
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top