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 support@ccsinfo.com

Measuring Start Bit of incomming byte using HUART

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







Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 8:15 am     Reply with quote

Hello,
I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
Thanks
Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12564
Jamie Huckerby
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 9:41 am     Reply with quote

Try using an Oscilliscope to measure any bit you can see. Adjust the time base so that 1 bit = 1 division. Then its just a simple case of calculation!

Hope this is some help.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12568
Sherpa Doug
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 11:16 am     Reply with quote

:=Hello,
:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:= Thanks
:=Ezequiel

If you want the PIC to do this automatically you need to read the input as a port pin, bypassing the UART. Wait for the starting edge, then poll the input at times representing the candidate baud rates. I would suggest measuring several bits, both 1's and 0's in case there is noise on the line or the transmission cable is not quite symetrical.

___________________________
This message was ported from CCS's old forum
Original Post ID: 12573
davidpk
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 12:13 pm     Reply with quote

I think you're trying to do an auto detect of the baud rate. So things like bench testing are out.

Determining the rate can be done in a couple of ways. The first to come to mind would be to route the RX pin to one of the port-B interrupt-on-change pins. When you get an interrupt use the ISR to start a fast timer, when you get the second interrupt, use the ISR to read the timer. If you assume the interrupt latency is equal on both interrupts then you could be fairly accurate. If you assume standard baud rates you can guess even better.

There will be some speed limits based on how fast your PIC is running compared to your UART signal. Normally you need a clock 16-times faster than your fastest baud rate but you may need to be running even faster to get a good rate.

:=Hello,
:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:= Thanks
:=Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12575
Acetoel
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 2:03 pm     Reply with quote

Hi,
that is what I want to do! (Autobaud rate detection). The terminal that I connect to the PIC can communicate at 1200 (Min Speed), 2400,4800,9600,10400,10800 (Max Speed). I need to determine the speed with this Sync Byte (0x55), I cannot use an interrupt pin, as I have all the pins on the PIC used. I have to do this using the RXPin (RB.7). Can anybody help me with the code?
Thanks
Ezequiel

:=I think you're trying to do an auto detect of the baud rate. So things like bench testing are out.
:=
:=Determining the rate can be done in a couple of ways. The first to come to mind would be to route the RX pin to one of the port-B interrupt-on-change pins. When you get an interrupt use the ISR to start a fast timer, when you get the second interrupt, use the ISR to read the timer. If you assume the interrupt latency is equal on both interrupts then you could be fairly accurate. If you assume standard baud rates you can guess even better.
:=
:=There will be some speed limits based on how fast your PIC is running compared to your UART signal. Normally you need a clock 16-times faster than your fastest baud rate but you may need to be running even faster to get a good rate.
:=
:=:=Hello,
:=:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:=:= Thanks
:=:=Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12581
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 4:16 pm     Reply with quote

:=Hi,
:= that is what I want to do! (Autobaud rate detection). The terminal that I connect to the PIC can communicate at 1200 (Min Speed), 2400,4800,9600,10400,10800 (Max Speed). I need to determine the speed with this Sync Byte (0x55), I cannot use an interrupt pin, as I have all the pins on the PIC used. I have to do this using the RXPin (RB.7). Can anybody help me with the code?
:=Thanks
:=Ezequiel
---------------------------------------------------------
I think Sherpa Doug said how to do it. If you're using a
hardware USART, then temporarily disable the USART by clearing
the appropriate bit(s) in the PIC's hardware registers.
Then make the into an input pin and poll the pin, while looking
for an edge (change from 0 to 1, or 1 to 0). Do this in a
loop, and count up the number of loops that occur between
edges. If your PIC is running at 20 MHz, then the loop might
be 3 us long. So for 9600 baud, a bit time is 104 us, and
the loop count might be approximately 34 or 35.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12587
Acetoel
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 4:43 pm     Reply with quote

Hi, Do you think this code is Okey. Take a look at it.
This rutine also include a Timeout error (that I need)

TimeOut = 0;
BaudRate = 0;
while (RXPin && (++Timeout<250000)) // 2.5 Segundos
delay_us(5);
If (!RXPin) {
While (!RXPin) {
delay_us(1);
BaudRate = BaudRate + 1;
}
BaudRate = 1 / BaudRate;
If ((Baudrate > 1100) && (Baudrate < 1400)) {Set_Uart_Speed(1200); printf(lcd_putc,"1200bps"); Break; }
If ((Baudrate > 2300) && (Baudrate < 2500)) {Set_Uart_Speed(2400); printf(lcd_putc,"2400bps"); Break; }
If ((Baudrate > 4350) && (Baudrate < 5100)) {Set_Uart_Speed(4800); printf(lcd_putc,"4800bps"); Break; }
If ((Baudrate > 9400) && (Baudrate < 9800)) {Set_Uart_Speed(9600); printf(lcd_putc,"9600bps"); Break; }
If ((Baudrate > 10000) && (Baudrate < 10850)) {Set_Uart_Speed(10400); printf(lcd_putc,"10400bps"); Break; }
}

Else { Show_TimeoutError(); Goto StartUp;}

Any other Idea. I was thinking of setting the speed at 9600bps and then guess using this method: If I rcv 0x55 at 10400bps and I'm waiting it at 9600, then I will get 0x95, and not 0x55. So I know that the speed is 10400bps. The point is that this is not very versatile.
Any help with the code will be very appreciate.
Thanks
Ezequiel



:=:=Hi,
:=:= that is what I want to do! (Autobaud rate detection). The terminal that I connect to the PIC can communicate at 1200 (Min Speed), 2400,4800,9600,10400,10800 (Max Speed). I need to determine the speed with this Sync Byte (0x55), I cannot use an interrupt pin, as I have all the pins on the PIC used. I have to do this using the RXPin (RB.7). Can anybody help me with the code?
:=:=Thanks
:=:=Ezequiel
:=---------------------------------------------------------
:=I think Sherpa Doug said how to do it. If you're using a
:=hardware USART, then temporarily disable the USART by clearing
:=the appropriate bit(s) in the PIC's hardware registers.
:=Then make the into an input pin and poll the pin, while looking
:=for an edge (change from 0 to 1, or 1 to 0). Do this in a
:=loop, and count up the number of loops that occur between
:=edges. If your PIC is running at 20 MHz, then the loop might
:=be 3 us long. So for 9600 baud, a bit time is 104 us, and
:=the loop count might be approximately 34 or 35.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12589
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Wed Mar 12, 2003 5:18 pm     Reply with quote

:=Hi, Do you think this code is Okay. Take a look at it.
-------------------------------------------

The break statements won't do anything, because break
only works with a loop statement, such as for(;;) or
while(), or a switch-case statement.

Also, you're assuming that the code in your loops
takes no time to execute. It might take several usec,
depending on your crystal frequency. This is especially
true if you're incrementing or testing a 16-bit value.

You also need to add some code to handle the boundary
conditions. ie., less than 1100 and greater than 10400 counts.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12591
Acetoel
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Thu Mar 13, 2003 3:39 am     Reply with quote

Hi,
Could somebody help me with a piece of code? Thanks very much, Ezequiel


:=:=Hi, Do you think this code is Okay. Take a look at it.
:=-------------------------------------------
:=
:=The break statements won't do anything, because break
:=only works with a loop statement, such as for(;;) or
:=while(), or a switch-case statement.
:=
:=Also, you're assuming that the code in your loops
:=takes no time to execute. It might take several usec,
:=depending on your crystal frequency. This is especially
:=true if you're incrementing or testing a 16-bit value.
:=
:=You also need to add some code to handle the boundary
:=conditions. ie., less than 1100 and greater than 10400 counts.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12607
Acetoel
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Thu Mar 13, 2003 10:58 am     Reply with quote

Hi,
Could you help me with a piece of code? thanks
Ezequiel

:=:=Hello,
:=:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:=:= Thanks
:=:=Ezequiel
:=
:=If you want the PIC to do this automatically you need to read the input as a port pin, bypassing the UART. Wait for the starting edge, then poll the input at times representing the candidate baud rates. I would suggest measuring several bits, both 1's and 0's in case there is noise on the line or the transmission cable is not quite symetrical.
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 12625
R.J.Hamlett
Guest







Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Thu Mar 13, 2003 4:48 pm     Reply with quote

:=Hi,
:= Could you help me with a piece of code? thanks
:=Ezequiel
:=
:=:=:=Hello,
:=:=:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:=:=:= Thanks
:=:=:=Ezequiel
:=:=
:=:=If you want the PIC to do this automatically you need to read the input as a port pin, bypassing the UART. Wait for the starting edge, then poll the input at times representing the candidate baud rates. I would suggest measuring several bits, both 1's and 0's in case there is noise on the line or the transmission cable is not quite symetrical.
:=:=
The attached is a 'starting point'. I have probably got the timings wrong, and repeating the test would be a very good idea...
It is also only 'part there', done as if it was part of a 'main' block, but needing quite a few other things added.

//This will all depend on the clock - I have assumed 20MHz
int error;
int16 val;
int16 baud;
setup_TIMER_0(T1_INTERNAL|T1_DIV_8);

//Have also assumed you have standard definitions for things
//like T1IF
set_timer1(0);
T1IF=FALSE;
//Only needed if these interrupts are enabled for something else
disable_interrupts(INT_TIMER1);
//reset the error flag.
error=FALSE;

//You can wait as long as you want here - add a longer timeout
// if required
while (RXpin) {
if(T1IF) {
error=TRUE;
break;
}
}

//This gives the minimum latency to resetting the clock
set_timer1(0);

if (error) Show_timeout_error();
else {
error=FALSE;
T1IF=FALSE;
//Here you have a pulse edge downwards (start bit). If you
//send 0x55 as the start pattern, or any other byte which
//has the low bit set, the duration being measured will be
//two bit times
//(start bit+first low bit).
while (!RXpin) {
if(T1IF) {
error=TRUE;
break;
}
}
val=get_timer1();
//The error here, is that the second edge took longer than
//about 1/10th second (625000/65536)
if (error) Show_timeout_error();
else {
//Here you have a 'count', from which you need
//to 'bracket'
//the actual baud rate.
baud=1250000l/(int32)val;
//You can use a 'lookup' test based on a constant table,
//or a switch statement, to give the rates selected.

}
}


You should remember that you should only accept values within perhaps +/-5\% of the design times, or something is wrong...

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 12645
adrian



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

Re: Measuring Start Bit of incomming byte using HUART
PostPosted: Fri Mar 14, 2003 6:15 am     Reply with quote

:=Hello,
:= I need to measure the start bit of a RS232 communication byte, to determine the baudrate. I receive a syncbyte 0x55 (10101010) and wants to measure the Bit Period, so 1/Baudrate = Bit Period, and I can determine the baudrate. I'm using the PIC16F76 Hardware RS232 Port. Can anybody help me with this.
:= Thanks
:=Ezequiel

have a look at this link
<a href="http://www.e-insite.net/ednmag/index.asp?layout=article&articleId=CA238424&pubdate=08/22/2002" TARGET="_blank">http://www.e-insite.net/ednmag/index.asp?layout=article&articleId=CA238424&pubdate=08/22/2002</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 12666
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