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

Entering XBee AT Command using Motion Sensor

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



Joined: 20 Apr 2010
Posts: 6

View user's profile Send private message

Entering XBee AT Command using Motion Sensor
PostPosted: Thu Apr 26, 2012 8:37 am     Reply with quote

Hi..
I really need help with this source code. What i'm trying to do here is that whenever an external interrupt from sensor occurs, xbee will display its serial low value by using ATSL command. What I'm expecting from the display is:
PIC Alive
OK
(ATNI value)
OK

What I get from this is:

PIC Alive
OK

Here is my source code. Hope anyone can help me with this. Many thanks.
Code:

#include "18f2525.h"
#fuses NOLVP,NOWDT,PUT,BROWNOUT,INTRC_IO
#use delay(clock=2000000)
#use rs232(baud=9600, xmit=pin_C6, rcv=pin_C7, ERRORS, PARITY=N, BITS=8, STOP=1)

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_rda
void serial_isr() {
   int t;
   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;     
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;
   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

#int_ext
void isr_ext(void)
{
      enable_interrupts(int_rda);
      delay_ms(1000);
      printf("+++");
     
      delay_ms(1000);
      printf("ATSL\r");
     
      delay_ms(1000);
      printf("ATCN\r");
      delay_ms(1000);
      while(bkbhit)
      putc( bgetc() ); 
   
   output_high(pin_b3);
   delay_ms(500);
   output_low(pin_b3);
}


void main()
{
   printf("PIC Alive\r");
   SET_TRIS_B(0b00000001);
   
   ext_int_edge(L_to_H);            ////when interrupt goes low to high (rising edge)
   enable_interrupts(INT_EXT);    //enable INT0
   enable_interrupts(global);    //enable global interrupt
   delay_ms(2000);
   printf("PIC Alive\r");
 
   while(true);
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Apr 26, 2012 9:03 am     Reply with quote

Hi,

Your strategy of using the UART interrupt and the external interrupt is probably not going to work. Post a link to the sensor you are using, so that we can help you determine the best way to use it!

Thanks,

John
farah0103



Joined: 20 Apr 2010
Posts: 6

View user's profile Send private message

PostPosted: Thu Apr 26, 2012 8:12 pm     Reply with quote

I'm using MN530-02S:

http://www.comus-intl.com/productpages/movement_vibration_switches_us.asp
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Apr 27, 2012 8:02 am     Reply with quote

Hi,

OK, I was under the impression that your sensor had a serial interface from your first post, but that is clearly not the case. Can you provide a description of what exactly you want your code to do? I appears that you want to sent some 'AT' commands to something (what??), and then receive a serial reply which is then displayed? What is the serial device?

A couple of comments: You generally want to keep your interrupt routines as short as possible, and generally NEVER want to put things like delays and printf's inside your ISR's as they just take too long and defeat the whole purpose of using interrupts. The preferred technique is to set flags inside the interrupt, and then process things outside the ISR.

I think the external interrupt is really not needed for this project, although you could use it if you don't mind the added complexity. Rather, I would just use a tight loop in main to 'poll' your sensor, and then do what you want once it is tripped.

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Apr 27, 2012 9:47 am     Reply with quote

I'll give you a _guide_ to what you need to do. There are some problems though. You have the clock set to 20MHz, but the internal RC oscillator which you select, doesn't allow this speed. I've set it to a speed it supports. Beware also though that this oscillator is 'borderline', for running RS232. OK at room temperature, but likely to go wrong if the temperature moves by much - perhaps you should be thinking about using a better oscillator.....

Code:

#include "18f2525.h"
#fuses NOLVP,NOWDT,PUT,BROWNOUT,INTRC_IO
#use delay(clock=1600000)
#use rs232(baud=9600, xmit=pin_C6, rcv=pin_C7, ERRORS, PARITY=N, BITS=8, STOP=1)

int1 old_input;
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_rda
void serial_isr() {
   int t;
   buffer[next_in]=getc();
   t=next_in++;
   if (next_in==BUFFER_SIZE) next_in=0; //faster and handles non binary
   //buffer sizes
   if(next_in==next_out)
     next_in=t;     
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;
   while(!bkbhit) ;
   c=buffer[next_out++];
   if (next_out== BUFFER_SIZE) next_out=0;
   return(c);
}

void main(void) {
   printf("PIC Alive\r");
   old_input=input(PIN_B0);
   enable_interrupts(INT_RDA);
   enable_interrupts(global);    //enable global interrupt

   do {
      //Now check for the input signal
      if (input(PIN_B0)) {
         if (old_input==FALSE) {
             //Here input has gone high
             old_input=TRUE;
             delay_ms(1000);
             printf("+++");
     
             delay_ms(1000);
             printf("ATSL\r");
     
             delay_ms(1000);
             printf("ATCN\r");
             delay_ms(1000);
             while(bkbhit)
                 putc( bgetc() );
   
             output_high(pin_b3);
             delay_ms(500);
             output_low(pin_b3);
         }
     }
     else
         old_input=FALSE;
   }
}

Key point is that delays etc., are out in the 'main' code. I've not even bothered with interrupts to detect the incoming edge, just checking for the line being high, when it was low. You could use the interrupt and set a flag equally well.
Key thing to understand, is that with the output code inside the interrupt handler, INT_RDA, is not serviced.

Best Wishes
farah0103



Joined: 20 Apr 2010
Posts: 6

View user's profile Send private message

PostPosted: Mon May 14, 2012 8:56 pm     Reply with quote

Thanks very much...
It works...
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