CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pic 18F442 and #int_rda

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Patrick Frank
Guest







Pic 18F442 and #int_rda
PostPosted: Tue Feb 03, 2004 8:31 am     Reply with quote

Hi,
i am using PCH 3.151 with a 18F442 @ 40Mhz
I try to communicate over the integrated RS232 with a MAX487 Chip which create a RS485 Signal. The Baudrate is 38400.
#use rs232(baud=38400,xmit=pin_c6,rcv=pin_c7,parity=n,bits=8,enable=pin_e2,Errors)

This is my interrupt function who read the bytes
#INT_RDA //IRQ f�r seriellen Emfpang. L�st aus wenn Bytes am Port
void serial_isr()
{
Empfangsbit=1;
Buffer=getc(); //Statusbyte einlesen
Paketlaenge=(Buffer & 0xE0)>>5; //Paketl�nge vordere 3 Bit
Statusbyte =Buffer & 0x1F; //Statusbyte hintere 5 Bit
if (Paketlaenge !=1) //wenn Datenpakete dabei sind einlesen
{
if (Paketlaenge ==3) {Data1=getc(); Data2=getc();}
if (Paketlaenge ==2) {Data1=getc(); Data2=0;}
}
Pruefsumme =getc(); //Pr�fsumme einlesen
}

When the int_rda occurs i read 2-5 Bytes from the RS232 Buffer. I think the buffer is only 3 Bytes long, so i use getc() to wait for the other bytes.

Now my problems and questions. Very Happy

When did the int_rda occur, only when the 3 Byte Buffer is full, or everytime a byte is at the port?

If i put the power on at my board and there is no communication partner at the MAX487 chip it seems that i got some peaks to the pic which are stored in the RS232 Buffer. If i now activate the enable_interrupts(INT_RDA); it jumps directly in the interrupt routine and try to read the bytes. But in this moment is in the buffer only garbage, so my Pic hangs in the getc() routine.
What can i do, any proposals? This happens also if i cut the connection from the MAX487 Chip to his communication partner.

I can`t use the wdt, because i must store values in the eeprom if an error occur or i put the power off.
I try to use the kbhit() function in the int_rda, but then i don`t recieve all bytes. My program is very time-critical, the RS232 Routine should only run when needed, i can`t use big delay routines.

Thank you and best regards
Patrick Frank
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Feb 03, 2004 11:45 am     Reply with quote

Quote:

When did the int_rda occur, only when the 3 Byte Buffer is full, or everytime a byte is at the port?

Every time a falling edge (start-bit) is detected in the Rcv_PIN.


Quote:

If i put the power on at my board and there is no communication partner at the MAX487 chip it seems that i got some peaks to the pic which are stored in the RS232 Buffer. If i now activate the enable_interrupts(INT_RDA); it jumps directly in the interrupt routine and try to read the bytes. But in this moment is in the buffer only garbage, so my Pic hangs in the getc() routine.
What can i do, any proposals? This happens also if i cut the connection from the MAX487 Chip to his communication partner.


If the MAX487 biased correctly, it will output in the Rx pin -TTL side- a steady H state, without any glitches.
I usually connect a 10K pull-up in this line. Be sure to get a stable H iddle state in the PIN_C7
You must get this solved before looking for errors in your code.


Quote:


if (Paketlaenge !=1) //wenn Datenpakete dabei sind einlesen
{
if (Paketlaenge ==3) {Data1=getc(); Data2=getc();}
if (Paketlaenge ==2) {Data1=getc(); Data2=0;}
}
Pruefsumme =getc(); //Pr�fsumme einlesen



This will not work as you expected. Every time you use getc() the UART will be waiting for a start bit....

Try to give us a brief explanation of what you want to do.

Best regards,

Humberto
Patrick Frank
Guest







PostPosted: Wed Feb 04, 2004 2:45 am     Reply with quote

Hello,
thank you for your response.

Quote:
Quote:

If i put the power on at my board and there is no communication partner at the MAX487 chip it seems that i got some peaks to the pic which are stored in the RS232 Buffer. If i now activate the enable_interrupts(INT_RDA); it jumps directly in the interrupt routine and try to read the bytes. But in this moment is in the buffer only garbage, so my Pic hangs in the getc() routine.
What can i do, any proposals? This happens also if i cut the connection from the MAX487 Chip to his communication partner.

If the MAX487 biased correctly, it will output in the Rx pin -TTL side- a steady H state, without any glitches.
I usually connect a 10K pull-up in this line. Be sure to get a stable H iddle state in the PIN_C7
You must get this solved before looking for errors in your code.

I measure the Rx signal on TTL Side and it has a L state when i cut the connection to the communication partner of the MAX487. On my old board i have a H state in this situation. If the communicatio partner is plugged in i have a H State at Rx. I use a Pull-Up (10k) in the Rx from PIC to MAX487.
On RS485 side i us 120R as termination.
But i don`t work. I search whos the problem.

Quote:
Quote:

if (Paketlaenge !=1) //wenn Datenpakete dabei sind einlesen
{
if (Paketlaenge ==3) {Data1=getc(); Data2=getc();}
if (Paketlaenge ==2) {Data1=getc(); Data2=0;}
}
Pruefsumme =getc(); //Pr�fsumme einlesen


This will not work as you expected. Every time you use getc() the UART will be waiting for a start bit....

Try to give us a brief explanation of what you want to do.

Best regards,

Humberto

I try to explain my communication.
I communicate with a pc, who deliver commands, which can be 2-5 Bytes long. First he send a Statusbyte, then 1-2 Data or only a CRC. I read the bytes and jump in a routine which analyse the command and react.
As you can see at my code the pic jumps in the int_rda function if the first Byte is on the port ( in my case the Statusbyte ). In the Statusbyte is encoded how much data bytes i must recieve. If there are no data bytes i read only the CRC. If there are 1 or 2 data bytes i read them. Thats the variable "Paketlaenge" Then i wait with the getc() for the other bytes.
I hope you can understand this.
I thinki have a problem if no more bytes are transmitted, or an error occure. Then my pic hangs in the getc() routine?
Have you got another solution?

Greetings
Patrick Frank
Patrick Frank
Guest







PostPosted: Wed Feb 04, 2004 2:59 am     Reply with quote

Sorry, i made a mistake in my post.
I recieve only 2-4 bytes.
Everytime the Buffer (would be the statusbyte) and the CRC (Pruefsumme).
If there is 1 or 2 data bytes i recieve them and store in DATA1/DATA2.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Feb 04, 2004 7:41 am     Reply with quote

Hello Patrick,

1) First of all, you must solve the Rx line in the MAX487.


2)
Quote:

In the Statusbyte is encoded how much data bytes i must recieve. If there are no data bytes i read only the CRC. If there are 1 or 2 data bytes i read them. Thats the variable "Paketlaenge" Then i wait with the getc() for the other bytes.


Just my opinion, it is not a good practice to handle this issue with a string of variable size. For this kind of applications, fixed size packet strings is by far the best way, it�s easy to code and track.

Make your own protocol and assign to each byte to be transmitted different meannings such "address", "data", "command", "value", "Pruefsumme", etc and transmit the whole packet each time, regardless of the task.
As you say, "if there are no data bytes i read only the CRC." In this case just put a know value in each byte to be transmitted and in the destination side, only take care of the byte corresponding to the Pruefsumme. I will do that it in this way and forget your Statusbyte.

Hope this help you,

Regards

Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group