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

AT Commands help

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



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

AT Commands help
PostPosted: Thu Jul 10, 2008 11:22 am     Reply with quote

I'm trying to write a program that will receive commands from a modem, and then call certain functions in response to the commands, and I am having trouble with the AT Commands (I believe, never really used them). I wrote a very simple code that lights an LED when the message '*1' is send to the PIC, but that isn't working. Code is below:

Code:

/*#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
*/ //This is all included in the protoalone.h file called below

#include <protoalone.h>
#include <utility.c> //contains the function flash_leds() used later on

#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;           // Buffer full !!
}

#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);
}

void main() {

int i, flag = 0;

enable_interrupts(int_rda);
enable_interrupts(global);

while(TRUE) {
   printf("AT+SBDWT\r");
   flash_leds();
   
   do {
   delay_ms(4000);
      for(i = 0; i < BUFFER_SIZE; i++) {
         if(buffer[i] == '*') {
            if(buffer[i+1] == '1') {
               light_one_led(GREEN);
               flag = 1;
            }
         }
      }
   } while(flag == 0);
}
}


Unfortunately, as stated above, the code isn't working properly, and I think there is something wrong with how I'm using the AT Command, I suppose, I don't really have a ton of knowledge about them. Any help would be greatly appreciated!

Thanks,
-Nick


Last edited by milkman41 on Thu Jul 10, 2008 12:35 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 11:49 am     Reply with quote

Your program is incomplete. There is no main(). Do you have int_rda
and global interrupts enabled ? We can't tell. In addition to that, you
are using the interrupt-driven receive buffer incorrectly. You are
supposed to call bkbhit(), and if it returns TRUE, then you call bgetc()
to get an individual character from the buffer. You can then put in
a linear buffer, or test it, or do something with the character. Besides
all of that, there is a very obvious bug in your code. The int_rda buffer
is a circular buffer. You could very easily have the '*' character be
at the physical end of the buffer, and have the '1' character be at the
start. That's completely possible with a circular buffer. But you are
checking the buffer in a linear manner, so your test could fail. All of
those problems can be prevented if you use it in the correct manner,
as I described above.
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 12:43 pm     Reply with quote

yes, sorry, somehow that part of the code hadn't gotten copied into the post, haha, but is there, the void main and interrupts enabled, but it is now in the code in the ifrst post. As for the circular buffer, I have other programs working with it (one for modem signal strength) that work just fine, however, I understand your point and it is a good one, how could I go about fixing it (I am pretty much a total newbie when it comes to programming and whatnot, especially this interrupts stuff and whatnot).

And as for the bkbhit()/bgetc() part of your response, I'm not entirely certain what you mean, I basically adapted this code (and the aforementioned modem signal strength program which is nearly identical to this code excepts it searches for numbers) from EX_SISR.c, and slightly altered it.

I know you know aloooot more about this than I do, so I look forward to your suggestions/assistance Very Happy

Thanks,
-Nick
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 12:53 pm     Reply with quote

You have got the routines in there for bgetc() and bkbhit() but you
don't call the routines to get the characters out of the recieve buffer.
You're just reading the buffer directly. Ex_sisr.c shows how to do this.
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 12:59 pm     Reply with quote

If I understand you correctly I should transfer the characters out of the recieve buffer into some other array and read from that one instead of reading straight from the recieve buffer? Something along the lines of:

Code:

...define etc...

char array[BUFFER_SIZE];

while(bkbhit()) {
    array[i] = bgetc();
    i++;
}


Or am I misunderstanding you? I'm sorry if these are dumb questions, but like I said, pretty much a total newbie with this programming stuff.

Thanks,
-Nick
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 1:35 pm     Reply with quote

Sample code to detect serial command strings:

From Ttelmah:
http://www.ccsinfo.com/forum/viewtopic.php?t=31144
From srhoar:
http://www.ccsinfo.com/forum/viewtopic.php?t=28159
pattousai



Joined: 23 Aug 2006
Posts: 37

View user's profile Send private message

PostPosted: Thu Jul 10, 2008 7:32 pm     Reply with quote

Hi PCM programmer.

Well, I was reading this post and went checking the codes you mentioned above. I must admit that I can't really understand the Ttelmah code, but the srhoar code I could get, and I was wondering, "this is a efficient code?" I mean, this is done in a reasonable time?

Well... I have troubles to measure in a PIC what is fast enough and what isn't.

Thanks
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