Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Tue Aug 07, 2012 1:24 am |
|
|
Do you have control of what is sent from the other end?.
The PIC AutoBaud feature, only works reliably,, if 0x55 is sent as the first character after it is enabled. Basically all you do, is set the ABDEN bit, and then the BRG automatically synchronises to the next character received. You can test the ABDOVF bit to verify that a legitimate rate was seen, and test the ABDEN bit (which goes clear when the detection is finished). The fact that the BRG contents get changed, the compiler doesn't care about, and existing code carries on happily working at the new rate.
So:
Code: |
//For UART1
#byte BAUDCON1=(getenv("SFR:BAUDCON1")
#bit ABDOVF1=BAUDCON1.7
#bit ABDEN1=BAUDCON1.1
#define trigger_baud_detection1() ABDEN1=1
#define baud_has_been_found1() (ABDEN1==0)
#define baud_error1() (ABDOVF1==1)
//Basic usage as:
int8 ctr='0', tick=255;
while (TRUE) {
printf(lcd_putc,"\fAttach serial");
trigger_baud_detection();
while (!baud_has_been_found()) {
lcd_gotoxy(1,1);
printf("waiting %c");
delay_ms(1);
if (tick) --tick;
else {
tick=255;
if (++ctr==':') ctr='0';
}
}
if (!baud_error()) break;
else {
printf(lcd_putc,"\fError try again");
lcd_gotoxy(1,2);
printf(lcd_putc,"# to continue");
do {
} while (kchr!='#');
}
}
|
No guarantees.
This is 'snipped and trimmed' from some existing code I use on a different PIC with the same feature. This has it's own keypad, and lcd, and in the code, if an error occurs in the baud detection, asks you to press '#' to try again. The system at the other end, has 'break' detection on the serial, and automatically sends a 0x55 sync character, when the cable is attached.
I think I have left all the bits that matter.
Best Wishes |
|