View previous topic :: View next topic |
Author |
Message |
brahme.girish
Joined: 20 Jun 2010 Posts: 7
|
pic 18f452 and serial communication problem |
Posted: Mon Jun 21, 2010 11:24 pm |
|
|
dear all,
I developed a project using Pic18f452. I am using CCS version 4.084.
In my project I am using two timers interrupt for different purpose.
And serial interrupt #int_RDA for data transmission from pc.
The code is like this
Code: |
#use rs232(baud =9600,xmit = Pin_c6, rcv = pin_c7)
#int_RDA
void RDA_isr()
{
data[i++] = getc();
if(i>=50)
{
i = 0;
}
}
void main()
{
}
|
Usually my code including all above works very fine. But when I switch ON or OFF my pc controller get hang. As its rcv pin get low (normally when pc is it is at -9v). I use Max232 for communication. I also try to use enable pin but the problem is same.
I can't use watchdog as if controller get hang then watchdog will reset. And both my timers will get reset. And I don't want to reset my timers. As after reseting timer all my other events will be restarted, which should be restarted only when user (customer) want.
So please any one will help me? Please I am at end of my project.
Thanx in advance,
girish. |
|
|
CC
Joined: 22 Jun 2010 Posts: 2
|
|
Posted: Tue Jun 22, 2010 1:51 am |
|
|
Try putting
if (!kbhit())
return;
in your INT_RDA routine. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Tue Jun 22, 2010 2:06 am |
|
|
Won't help.
INT_RDA, will only get called when the interrupt flag is set, which means a character has been received. kbhit will be true. If you return without reading the character, you will just get another interrupt... :(
Key thing, add the keyword 'ERRORS' to the #use RS232 statement. I suspect this is what is 'missing'. Without this, if a framing error or overrun error occurs, the RX side of the UART _will_ get hung.
Then modify the interrupt handler as:
Code: |
#int_RDA
void RDA_isr(void)
{
int8 temp;
temp=getc();
if ((RS232_ERRORS & 6)!=0) return;
data[i++] = temp;
if(i>=50)
{
i = 0;
}
}
|
This will throw away incoming characters, if the framing error, or overrun error bits get set, avoiding possible problems with the receive code while this is happening. Depending on where in the character the PIC 'is' when PC power goes off, one or both of these will be set for the garbage characters being received, when the line at the PIC is in the 'break' condition (dates from the wire being 'broken', which is what the chip sees if the other end is off).
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jun 22, 2010 8:46 am |
|
|
FYI-
Microchip considers the 18F452 a mature product and recommends using the 18F4520 in new designs. It is pin-for-pin compatible.
I always put ERRORS in my #use rs232() declaration to make sure it doesn't hang up. |
|
|
brahme.girish
Joined: 20 Jun 2010 Posts: 7
|
|
Posted: Wed Jun 23, 2010 12:36 am |
|
|
hi Ttelmah,
Thanx for your valuable reply. I have done exactly same like below as you suggested. It is working very fine. No matter how many times you switch OFF or ON computer.
Code: |
#int_RDA
void RDA_isr(void)
{
int8 temp;
temp=getc();
if ((RS232_ERRORS & 6)!=0) return;
data[i++] = temp;
if(i>=50)
{
i = 0;
}
}
|
Thanx once again,
Regards
girish. |
|
|
|