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

Auto Detect baud rate of 16f877a

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



Joined: 12 Oct 2013
Posts: 4

View user's profile Send private message

Auto Detect baud rate of 16f877a
PostPosted: Sat Oct 12, 2013 7:42 am     Reply with quote

hi. my work is monitoring RS232 between 2 computer by hyperterminal program.

my problem is auto detecting baud rate such as hyper terminal is communicated at 1200 baud rate then i want 16f877a changing auto baud rate at 1200.so i have no idea to use any function please help me T_T

Blog diagram : http://s24.postimg.org/n0zi41np1/Untitled.png
my code is

Code:
#include <16f877A.h>
#fuses NOWDT,HS, NOPROTECT,NOLVP
#use delay (clock=20000000)
#use rs232(baud=9600,rcv=PIN_B4,stream=A)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=B)
#include "lcd.c"

char c[50],d[50];
int s1,s2;
int push0=0,push1=0;
int da=0,db=0;
int buad=0;
#INT_RB
void IntRB_isr(void){
c[s1]=getc(A);
lcd_init();
lcd_gotoxy(1,1); printf(lcd_putc,"%c  1",c[s1]);
s1++;
if(input(PIN_B1)){
disable_interrupts(GLOBAL);
disable_interrupts(INT_RDA);
disable_interrupts(INT_RB);break;}
}
#INT_RDA
void IntRDA_isr(){
d[s2]=getc(B);
lcd_init();
lcd_gotoxy(1,2); printf(lcd_putc,"%c  2",d[s2]);
s2++;
if(input(PIN_B1)){
disable_interrupts(GLOBAL);
disable_interrupts(INT_RDA);
disable_interrupts(INT_RB);break;}

}
void main(void)
{

lcd_init();
lcd_gotoxy(1,1); printf(lcd_putc,"select buad rate");
while(1)
{
if(input(PIN_A1)){
while(1){

enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
if(input(PIN_B1)){
disable_interrupts(GLOBAL);
disable_interrupts(INT_RDA);
disable_interrupts(INT_RB);break;}
}
                }



if(input(PIN_A2))
       {
      lcd_init();
      lcd_gotoxy(1,1); printf(lcd_putc,"A is :\n%c%c%c%c%c",c[push0],c[push0+1],c[push0+2],c[push0+3],c[push0+4]);
     delay_ms(200);
       push0++;   
                  }
if(input(PIN_A3))
       {
      lcd_init();
      lcd_gotoxy(1,1); printf(lcd_putc,"A is :\n%c%c%c%c%c",c[push0],c[push0+1],c[push0+2],c[push0+3],c[push0+4]);
     delay_ms(200);
       push0--;   
                  }
if(input(PIN_A4))
       {
      lcd_init();
      lcd_gotoxy(1,1); printf(lcd_putc,"B is :\n%c%c%c%c%c",d[push1],d[push1+1],d[push1+2],d[push1+3],d[push1+4]);
      delay_ms(200);
       push1++;   
                  }
if(input(PIN_A5))
       {
      lcd_init();
      lcd_gotoxy(1,1); printf(lcd_putc,"B is :\n%c%c%c%c%c",d[push1],d[push1+1],d[push1+2],d[push1+3],d[push1+4]);
      delay_ms(200);
       push1--;   
                  }
}
}
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 8:05 am     Reply with quote

Unless you are monitoring an ongoing communication with known format between 2 devices, or an incoming known stream to your pic, its impossible to determine a machines baud.... kinda

Ive done auto baud for my GSM driver (not yet posted) and basically all i do is:

Code:
Set baud 1
Send request to target device
check for response
if response not received, set next baud.
repeat until proper response.


if you want to plug a device to listen to comunications between 2 other devices and you want to adjust to the baud being used.. you would need to know what the conversation is about and know what strings to look for to confirm you are at the right baud..

remember that when you connect 2 uarts at diferent speeds sometimes you get garbage characters.... so you need a specific string or command to check against your received buffer...


I hope i explained my self correctly...

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 8:09 am     Reply with quote

oh my, i see you are using Software Uarts...

I too like to live dangerously.



by the way your code shows no attempt at auto bauding....
_________________
CCS PCM 5.078 & CCS PCH 5.093
nathapol1992



Joined: 12 Oct 2013
Posts: 4

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 8:15 am     Reply with quote

Gabriel wrote:
oh my, i see you are using Software Uarts...

I too like to live dangerously.


by the way your code shows no attempt at auto bauding....


Yes my code is no attempt auto baud rate. I have no idea to check receive buffer because i used code of head program
Code:
#use rs232(baud=9600,rcv=PIN_B4,stream=A)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=B)

is fixed at 9600 only.
Any code or function to me please.
thanks
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 8:33 am     Reply with quote

One "advantage" of a software uart is that you do have access to the receive pin in software. In this case, you can have an auto-baud routine that just sits there and watches the pin. Start counting at the start of a bit and measure the bit length (until the next edge). You will find some "bits" may actually be multiple "bits" - like "111", however, if you do it for a number of bits, you will find the shortest one (the others will be multiples of that short one). You can then determine the baud rate based on that bit length. It will get confused if the signal is noisy though. I usually just pick a standard rate and use that, but this method should allow you to pick the correct rate. Something like an upper case "U" would give a good detect since it is "01010101" ( http://www.ascii-code.com/ ) You may find you get better results if you use a scheme like that to get you close then pick from a table of standard divisors so you are right on. You'll need to experiment a bit for the best results.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
nathapol1992



Joined: 12 Oct 2013
Posts: 4

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 8:44 am     Reply with quote

gpsmikey wrote:
One "advantage" of a software uart is that you do have access to the receive pin in software. In this case, you can have an auto-baud routine that just sits there and watches the pin. Start counting at the start of a bit and measure the bit length (until the next edge). You will find some "bits" may actually be multiple "bits" - like "111", however, if you do it for a number of bits, you will find the shortest one (the others will be multiples of that short one). You can then determine the baud rate based on that bit length. It will get confused if the signal is noisy though. I usually just pick a standard rate and use that, but this method should allow you to pick the correct rate. Something like an upper case "U" would give a good detect since it is "01010101" ( http://www.ascii-code.com/ ) You may find you get better results if you use a scheme like that to get you close then pick from a table of standard divisors so you are right on. You'll need to experiment a bit for the best results.

mikey


Does ccs code on #use232(baud=....) can change baud rate again at main program ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 10:52 am     Reply with quote

Several comments:

Generically, things supporting 'auto baud', normally require the other device to send a sequence of specific characters. So normally something like LF LF LF, immediately after starting, to 'trigger' the detection.

It is actually vastly harder to do with a software UART, than using the hardware UART. Problem is that each different baud rate, will require different code. With the hardware UART, you can just use setup_uart to change the rate. Even if you restrict the choices to a dozen rates, there will be a dozen copies of #use RS232, and a dozen different sets of code. Understand that #use RS232 generates it's code at compile time, so cannot be used to make a dynamic change to the rate.

Consider switching chips. Many of the PIC18's, have built in hardware to do this.

Using the hardware UART, the sequence is:

1) Turn the UART off.
2) Wait for the RX line to drop. Record the time. T1.
3) Wait for the RX line to rise. Record the time. T2.

Depending on the character chosen, the time between T2, and T1, is one or more bit times. Calculate the baud rate from this, re-enable the UART, and set it's baud rate to the required value.

Best Wishes
nathapol1992



Joined: 12 Oct 2013
Posts: 4

View user's profile Send private message

PostPosted: Sat Oct 12, 2013 11:56 am     Reply with quote

Ttelmah wrote:
Several comments:

Generically, things supporting 'auto baud', normally require the other device to send a sequence of specific characters. So normally something like LF LF LF, immediately after starting, to 'trigger' the detection.

It is actually vastly harder to do with a software UART, than using the hardware UART. Problem is that each different baud rate, will require different code. With the hardware UART, you can just use setup_uart to change the rate. Even if you restrict the choices to a dozen rates, there will be a dozen copies of #use RS232, and a dozen different sets of code. Understand that #use RS232 generates it's code at compile time, so cannot be used to make a dynamic change to the rate.

Consider switching chips. Many of the PIC18's, have built in hardware to do this.

Using the hardware UART, the sequence is:

1) Turn the UART off.
2) Wait for the RX line to drop. Record the time. T1.
3) Wait for the RX line to rise. Record the time. T2.

Depending on the character chosen, the time between T2, and T1, is one or more bit times. Calculate the baud rate from this, re-enable the UART, and set it's baud rate to the required value.

Best Wishes


So, auto baud rate detecting cant possible because i using software and hardware uart that separated baud rate?

thanks
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Oct 13, 2013 7:24 am     Reply with quote

Quote:
So, auto baud rate detecting cant possible because i using software and hardware uart that separated baud rate?
I think you're not understanding what you're being told.

I believe you are being told that you CAN do auto-baud detection with either software or hardware UART.

In either case you monitor the RX pin, and time the period of one, or more, pulses.

I've just done some simple tests on a hardware UART.
The results of several runs were within a few machine cycles of the expected values.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Sun Oct 13, 2013 8:47 am     Reply with quote

Exactly.

However with a software UART, you can only do it with a number of fixed rates, and the code will get _very_ bulky, with separate streams or #use statements needed for each baud rate. So for a dozen baud rates, a dozen copies of the serial driver. _Big_.

Hardware UART, much easier.

Best Wishes
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Oct 13, 2013 9:12 am     Reply with quote

Here is some good info from another thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=22640
_________________
Google and Forum Search are some of your best tools!!!!
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