View previous topic :: View next topic |
Author |
Message |
Mamat
Joined: 20 Jan 2011 Posts: 9
|
xbee |
Posted: Wed May 25, 2011 2:19 am |
|
|
This receive xbee code. Why when I want to receive char 'aa' cannot be successful (x == 'a') ?
Code: |
#include <18f4620.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1) // you can use any pins for software uart...
#use rs232(baud=9600, UART1) // hardware uart much better; uses RC6/TX and RC7/RX
// characters transmitted faster than the pic eats them will cause UART to hang.
#include <stdlib.h>
char x;
void main() {
while (true) {
if (kbhit()) { x = getc();}
if (x=='a'){output_high(pin_a0);delay_us(100);}
else output_low(pin_a0);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed May 25, 2011 2:31 am |
|
|
First, add the word 'ERRORS' to your hardware UART declaration. This adds code to recover if the UART overflows and avoid hanging....
Then. "aa", is two characters not one.
You need to look for each in turn.
Code: |
#include <stdlib.h>
char x;
int1 have_first_a=FALSE;
void main() {
x=rs232_errors; //This prevents error message from adding 'ERRORS'
do {
if (kbhit()) {
//You should only test when you have a character - hence include here
//not called continuously as in original code.
x = getc();
if (x=='a') {
if (have_first_a==TRUE) {
//Here have seen _two_ consecutive 'a' characters
output_high(pin_a0);
delay_us(100);
have_first_a=FALSE;
}
else {
have_first_a==TRUE; //Flag that one 'a' has been seen
}
}
else {
have_first_a=FALSE;
output_low(pin_a0);
}
}
} while(TRUE); //This avoids compiler error message from the while loop
}
|
Best Wishes
Last edited by Ttelmah on Wed May 25, 2011 7:16 am; edited 1 time in total |
|
|
Mamat
Joined: 20 Jan 2011 Posts: 9
|
|
Posted: Wed May 25, 2011 2:47 am |
|
|
when i compile it cannot work |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed May 25, 2011 7:15 am |
|
|
It needs your header added, _with_ the errors keyword.
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed May 25, 2011 9:05 am |
|
|
Code: | #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors,STREAM=UART1) |
This will set you serial communications port to use the hardware that's built into the PIC and call it UART1. If you are new to designing with PICs then I strongly advise you use the hardware and not attempt to use software. Use the interrupt:
Code: | #int_RDA
void RDA_isr(void)
{
// Put code here
} |
to capture your data. Remember, you can only retrieve ONE character at a time, each time the interrupt is entered.
Ronald |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed May 25, 2011 10:11 am |
|
|
He is already using the hardware UART - UART1 sets this up.
You don't need streams unless you have multiple UARTs (software or hardware) in use.
For just looking for "aa", the interrupt is an unnecessary complication. Ideal if he latter wants to do other things at the same time, but for what is shown, gains nothing.
The settings you give are the defaults. It is always 'safer' to use them, but not needed.
Best Wishes |
|
|
|