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 CCS Technical Support

xbee

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



Joined: 20 Jan 2011
Posts: 9

View user's profile Send private message Yahoo Messenger ICQ Number

xbee
PostPosted: Wed May 25, 2011 2:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 25, 2011 2:31 am     Reply with quote

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

View user's profile Send private message Yahoo Messenger ICQ Number

PostPosted: Wed May 25, 2011 2:47 am     Reply with quote

when i compile it cannot work
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed May 25, 2011 7:15 am     Reply with quote

It needs your header added, _with_ the errors keyword.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed May 25, 2011 9:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 25, 2011 10:11 am     Reply with quote

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
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