|
|
View previous topic :: View next topic |
Author |
Message |
chingB
Joined: 29 Dec 2003 Posts: 81
|
Serial data timeout help |
Posted: Wed Jun 23, 2004 7:22 am |
|
|
Hello,
I have an application wherein the PC can sends data to my PIC18F452. I amde a test program such that I could check if the PIC receive the correct data.
My concern is how can my PIC determine if it has receive the last byte if my serial packets is of variable byte size.
I have a packet that will range from 8 character to 40 character? can anybody provide me a snippet or any help on how can I determine that the PC has send the last character.
Thank u. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Jun 23, 2004 7:45 am |
|
|
It will take two interrupt routines. Use the UART receive interrupt and clear a timer-variable on reception of the first character. Increment the timer-variable in the timer1 interrupt. Clear the timer-variable in the serial receive interrupt routine upon receipt of each character. After the last character is received, there is nothing to clear the timer-variable and you can just check that variable until it reaches a certain value, then you will know the last character must have been received. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Serial data timeout help |
Posted: Wed Jun 23, 2004 8:02 am |
|
|
chingB wrote: | Hello,
I have an application wherein the PC can sends data to my PIC18F452. I amde a test program such that I could check if the PIC receive the correct data.
My concern is how can my PIC determine if it has receive the last byte if my serial packets is of variable byte size.
I have a packet that will range from 8 character to 40 character? can anybody provide me a snippet or any help on how can I determine that the PC has send the last character.
Thank u. |
Have a look at this.
http://www.ccsinfo.com/forum/viewtopic.php?t=19306 |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Jun 23, 2004 8:55 am |
|
|
Code: | #include <18F452.h>
#device ICD=TRUE
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, STVREN, NODEBUG, LVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
char rcv;
int rcvto;
#int_TIMER1
TIMER1_isr()
{
rcvto++;
}
#int_RDA
RDA_isr()
{
rcv = getc();
rcvto = 0;
enable_interrupts(INT_TIMER1);
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(1){
while(rcvto==0){
//do something..wait for first character
output_high(pin_b5);
}
while(rcvto<10){
//do something... wait for timeout after last character
output_low(pin_b5);
}
#use rs232(debugger)
printf("Received All");
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
disable_interrupts(INT_TIMER1);
rcvto=0;
}
} |
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jun 23, 2004 9:06 am |
|
|
The usual way of doing this is to either have a byte at the start of the packet telling how long the packet will be, or a special character to indicate the end of the packet. Usually the end of packet character is 0x0D so you can type a packet on a terminal and use the ENTER key at the end.
Trying to use timers to determine the end of the packet is fraught with peril as the timing can be disrupted if the transmitter has an interrupt. Also it requires you to wait some worst-case time interval before accepting the data.
An end of packet character will save code size, complexity, execution speed, and grey hair!
Sherpa Doug _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|
|
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
|