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 Command Problem Between Mobile & PIC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gokayk



Joined: 25 Mar 2012
Posts: 8

View user's profile Send private message

AT Command Problem Between Mobile & PIC
PostPosted: Sun Mar 25, 2012 10:45 am     Reply with quote

Hi eveyone,

I've a problem about sending AT commands to PIC,
For exp. I've already tried and got the OK response from mobile by using hyper terminal. Also i've tested my circuit (PIC) with hyper terminal by using max 232.

Here my codes;

Code:
#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, STREAM = PC)
#use rs232 (baud=9600, xmit=pin_C4, rcv=pin_C5, STREAM = PHONE)

char mobile_phone[80];

while(1)
{
      fprintf(PHONE,"AT");  //sending AT code to mobile phone
      delay_ms(100);
      fputc(0x0d,PHONE);  //ENTER command should be sent
      delay_ms(1000);
      fprintf(PC, "\n\rHere is the code for sending to mobile phone: AT\n"); //at command shown in hyper terminal
      fgets(mobile_phone,PHONE);  //getting data from mobile phone
      fprintf(PC, "\n\rData from mobile: %s\n",mobile_phone);  //data from mobile phone is shown in hyper terminal
}


When i send AT command, it is waiting for receiving datas from mobile. But it never stops and program can not pass to the other step.

There is a problem but i really don't know what it is :S

Can you help me?

Thanks in advance.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 25, 2012 1:06 pm     Reply with quote

First problem is that after sending the AT-command to the phone you have a delay of 1000ms before you start listening for the response. I don't see the code for hardware flow control, so the phone will not wait for you to be listening and has long since sent a reply. Now the phone is waiting for a new command while you are waiting for the phone's response. Deadlock.

Some minor suggestions:
- With the hardware UART always add the 'ERRORS' directive to the #use rs232 line. This will add code to automatically clear overflow error conditions.
- Why do you have a delay between sending 'AT' and the newline character?
- Easier to read is when you combine the 'AT' command and newline into a single:
Code:
fprintf(PHONE,"AT\n");  //sending AT code to mobile phone
Note the \n. Some phone require '\n\r'.
gokayk



Joined: 25 Mar 2012
Posts: 8

View user's profile Send private message

PostPosted: Sun Mar 25, 2012 1:55 pm     Reply with quote

Thanks for quick reply.

New code:

Code:
#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, STREAM = PC, ERRORS)
#use rs232 (baud=9600, xmit=pin_C4, rcv=pin_C5, STREAM = PHONE, ERRORS)

char mobile_phone[];

while(1)
{
      fprintf(PHONE,"AT\r\n");  //sending AT code to mobile phone
      fprintf(PC, "\n\rHere is the code for sending to mobile phone: AT\n"); //at command shown in hyper terminal
      fgets(mobile_phone,PHONE);  //getting data from mobile phone
      fprintf(PC, "\n\rData from mobile: %s\n",mobile_phone);  //data from mobile phone is shown in hyper terminal
}


I've tried these too:
Code:
fprintf(PHONE,"AT\n");  //sending AT code to mobile phone
fprintf(PHONE,"AT\r");  //sending AT code to mobile phone
fprintf(PHONE,"AT\r\n");  //sending AT code to mobile phone
fprintf(PHONE,"AT\n\r");  //sending AT code to mobile phone


But still no response. Still waiting in fgets() Sad
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Sun Mar 25, 2012 10:44 pm     Reply with quote

The problem is the PIC can only buffer one or two characters. By the time you get to the fgets(), the fprintf to the PC has taken so long you've missing the response from the modem.

The simple option would be to fprintf to the PC first, then the modem, then the fgets, then the final fprintf.

In the long run, it would be even better to forget fprintf and fgets entirely and write your own based on interrupt-driven buffers. See the CCS examples EX_SISR.C (receiving) and EX_STISR.C (transmitting).
_________________
Andrew
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Mar 26, 2012 2:22 am     Reply with quote

In addition to Andrew's comments. I've built a similar application a long time ago and I can fully recommend the interrupt driven approach. However... only the hardware UART can generate an interrupt and most PIC processors have only one hardware UART. It would have made my project a lot easier when I had a PIC with two UARTS. You are lucky because these do exist now, for example the PIC18F23K22 (28 pin, PDIP and SMD housings, $1.34 at 5000 pieces), or the PIC16F1526 (64 pin, SMD only, $1.47 at 5000 pieces).
And then, of course, there are the PIC24 and many ARM processors that have 2 or more UARTs and are similar priced (or cheaper) then these PICs but require you to use other compilers and programmer tools.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Mar 26, 2012 7:24 am     Reply with quote

Hi,

I can bet you 5 bucks your problem can be solved like this:

Swap the tx/rx lines from your the PIC to your phone.

here is why:
http://en.wikipedia.org/wiki/Data_terminal_equipment


ask yourself, who is who?

read my last comment on my post:
http://www.ccsinfo.com/forum/viewtopic.php?t=42527

that should take care of 90% of your startup problems.


Write a small, short program that only tests for proper response to the AT command.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093


Last edited by Gabriel on Mon Mar 26, 2012 7:39 am; edited 2 times in total
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Mar 26, 2012 7:31 am     Reply with quote

you dont show your input buffer or your ISR for the USART.

that is very important as well.
G
_________________
CCS PCM 5.078 & CCS PCH 5.093
gokayk



Joined: 25 Mar 2012
Posts: 8

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 12:54 pm     Reply with quote

First of all i'd like to say thank you for your all beneficial comments,

I've modified my program with reference to Gabriel's program.

Here it is:

Code:
#if defined(__PCM__)
#include <18F452.h>
#include <string.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOCPD                    //No EE protection
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)

#elif defined(__PCH__)
#include <18F452.h>
#include <string.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOCPD                    //No EE protection
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
#endif

#define green_led pin_c2
#define blue_led pin_c3

volatile int counter_search=0;   //counter to traverse the array
volatile int counter_read=0;  //counter to fill the array

volatile char recieve_string[70];   //buffer for incoming data
////////////////////////////////////////////////////////////////////////////////
#int_RDA
void  RDA_isr(void)
{
   recieve_string[counter_read]=getchar();
   counter_read++;
   if(counter_read==69)
   {
      counter_read=0;
   }
}
////////////////////////////////////////////////////////////////////////////////
void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   output_low(green_led);
   output_low(blue_led);
   
   set_tris_c(0b10100000);
   
   output_high(green_led);
   delay_ms(500);
   output_low(green_led);
   delay_ms(500);
   output_high(blue_led);
   delay_ms(500);
   output_low(blue_led);
   delay_ms(500);
   
   while(1)
   {
////////////////////////////////////////////////////////////////////////////////
      counter_read=0;
     
      printf("AT\n");
      putchar(0x0D);
     
      output_high(blue_led);
      while(counter_read<8)
      { 
      }
      output_low(blue_led);
     
      counter_read=0;
      delay_ms(500);
////////////////////////////////////////////////////////////////////////////////
      counter_search=0;
     
      while((recieve_string[counter_search]!='O')&&(counter_search<10))
      {
         counter_search++;
      }
     
      if((recieve_string[counter_search]=='O')&&(recieve_string[counter_search+1]=='K'))
      {
         output_high(green_led);
         counter_search=0;
      }
      else
      {
         output_low(green_led);
         counter_search=0;
      }
////////////////////////////////////////////////////////////////////////////////     
   }

}


I've tried this program with hyper terminal, it is waiting me to entry datas should include O and K somewhere in 8 bytes, i can see the result on my circuit (turn on green light).

However; when i try it with mobile (sony ericsson t290i), blue led never stops it means program still waiting for datas until 8 byte completes.

I've tried changing rx/tx cables from mobile side, it didn't work

I'm sendig my circuit picture too. Just have a look please.

Can you something wrong? Or do you have any idea about my still-continuing problem?

Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 1:26 pm     Reply with quote

Ah! you are using the same cell as my program!

I see you modified the code a bit...

Code:
printf("AT\n");

no need for the '\n' character... take it off.

also:
Code:
while(counter_read<8)

my code was:
Code:
while(counter_read<=8)

im expecting "A T \R \R \N O K \R \N" from the phone...
however note that the above string includes the echoed characters..
if you disabled the local echo "ATE=0", you will only get:
"\R \N O K \R \N" which is less than 8 chars...so you will be stuck in the loop... waiting for nothing.

I know I advised to turn off local echos... however the code in the post is designed with local echo enabled.... it was my first attempt at GSM.

You can change the loop to wait for (1)one character (if local echo is off!)... and then give it
a 500ms (Completly arbitrary, however reasonable for testing) delay.

Also.. I am assuming since you are using the T290i that you have the serial cable (DB9 connector on the end).... and that you are using a MAX232 chip.... you swap the tx/rx lines between the MAX chip and the DB9 connector on the cable (pins 2 and 3)

You must swap the tx/rx lines, and pull pin 4 high if don't remember correctly (i tied it directly to VCC however a 10k pull up i think does the job ).... you need to make your own cable... that goes from the breadboard to the cell cable's DB9 connector... or use one of those breadboard DB9 adapters...


G
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 2:18 pm     Reply with quote

Just to be clear...

You need to pull high pin 4 on the DB9 connector of the CELL.
Swap the tx rx lines between the Cell and your MAX232.

g.
_________________
CCS PCM 5.078 & CCS PCH 5.093
gokayk



Joined: 25 Mar 2012
Posts: 8

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 3:08 pm     Reply with quote

Gabriel wrote:
Ah! you are using the same cell as my program!

I see you modified the code a bit...

Code:
printf("AT\n");

no need for the '\n' character... take it off.

also:
Code:
while(counter_read<8)

my code was:
Code:
while(counter_read<=8)

im expecting "A T \R \R \N O K \R \N" from the phone...
however note that the above string includes the echoed characters..
if you disabled the local echo "ATE=0", you will only get:
"\R \N O K \R \N" which is less than 8 chars...so you will be stuck in the loop... waiting for nothing.

I know I advised to turn off local echos... however the code in the post is designed with local echo enabled.... it was my first attempt at GSM.

You can change the loop to wait for (1)one character (if local echo is off!)... and then give it
a 500ms (Completly arbitrary, however reasonable for testing) delay.

Also.. I am assuming since you are using the T290i that you have the serial cable (DB9 connector on the end).... and that you are using a MAX232 chip.... you swap the tx/rx lines between the MAX chip and the DB9 connector on the cable (pins 2 and 3)

You must swap the tx/rx lines, and pull pin 4 high if don't remember correctly (i tied it directly to VCC however a 10k pull up i think does the job ).... you need to make your own cable... that goes from the breadboard to the cell cable's DB9 connector... or use one of those breadboard DB9 adapters...


G


The best way of learning something new, try to understand how it is written ;)

I will change them, then i will try again. And i think i wont disable the local echo until i can't find any new solutions.

Sometimes when i try to send AT command to phone by using hyper terminal, then few attempts fails. I mean I can't send AT all the time. But after i can send AT commands, it gives OK responses right away. I don't know why is like that. The problem can be because of this issue. What do you think?

I bought USB data cable (it is difficult to find data cable with DB9 connector in Turkey) then i cut the USB side includes its own circuit. Then i soldered new control cable to the connector (phone side), i am using 3 cable (rx,tx & gnd) for my max232 circuit.

So which pin should i pull-up by using resistor from phone side?
http://pinouts.ru/CellularPhones-P-W/erics_t28_pinout.shtml

Sorry about too many questions Confused
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 3:21 pm     Reply with quote

Your diagram does not show the phone ground connected to the PIC ground.

Without this it won't work. Perhaps the diagram is wrong or you really don't have a ground ??
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 3:44 pm     Reply with quote

I'm not able to see your diagram... link?


Ok, so i guess you are hooking up the pic directly to the phone? and are only using the connector to get physical access to the pone pins?

Then you do not need the Max 232... however the phone is operating at 3.3V..... a level converter should be used probably. in this case a voltage divider on the PIC tx line should do the trick.

If you connect directly then you don't need to swap the lines... just make sure you have tx to Rx and rx to Tx accordingly.

Which pin on the cell needs to be high?
http://en.wikipedia.org/wiki/IEEE_802.11_RTS/CTS ignore this...

Edit... its this link...
Quote:
http://en.wikipedia.org/wiki/RS-232_RTS/CTS#RTS.2FCTS_handshaking

That should give you a hint.

However.... check the AT command list and see if it is possible to turn off the Hardware flow control.... if so, then all you need is GND. RX and TX!


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093


Last edited by Gabriel on Tue Mar 27, 2012 4:01 pm; edited 2 times in total
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 3:52 pm     Reply with quote

Quote:
Sometimes when i try to send AT command to phone by using hyper terminal, then few attempts fails. I mean I can't send AT all the time. But after i can send AT commands, it gives OK responses right away. I don't know why is like that. The problem can be because of this issue. What do you think?


The above worries me alot.

How are you connecting the phone to hyperterminal?

The PC is using +/-12V signals... if you hook this up the phone directly.. you probably fried the cell... and thats why you only have intermittent functionality if any.


G
_________________
CCS PCM 5.078 & CCS PCH 5.093
gokayk



Joined: 25 Mar 2012
Posts: 8

View user's profile Send private message

PostPosted: Wed Mar 28, 2012 2:29 am     Reply with quote

temtronic wrote:
Your diagram does not show the phone ground connected to the PIC ground.

Without this it won't work. Perhaps the diagram is wrong or you really don't have a ground ??


Phone ground & pic ground & Max232 circuit ground & power supply ground are all connected to common ground.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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