|
|
View previous topic :: View next topic |
Author |
Message |
milkman41
Joined: 16 Jun 2008 Posts: 30
|
AT Commands help |
Posted: Thu Jul 10, 2008 11:22 am |
|
|
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
|
|
Posted: Thu Jul 10, 2008 11:49 am |
|
|
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
|
|
Posted: Thu Jul 10, 2008 12:43 pm |
|
|
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
Thanks,
-Nick |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 10, 2008 12:53 pm |
|
|
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
|
|
Posted: Thu Jul 10, 2008 12:59 pm |
|
|
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
|
|
|
pattousai
Joined: 23 Aug 2006 Posts: 37
|
|
Posted: Thu Jul 10, 2008 7:32 pm |
|
|
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 |
|
|
|
|
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
|