Help in interfacing ATmega32 with GSM modem

Go To Last Post
32 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there..

I used this program using codevision AVR. It tests AT and AT+CMGF=1 command..

I have uploaded the program in text file since I had trouble coding it and replying.. I was getting error message "Method not implemented".

As you can see the program tests AT and AT+CMGF=1
An LED connected to PORTA.0
a) Blinks if AT works and AT+CMGF=1 doesn't
b) Becomes ON if both AT and AT+CMGF=1 work
c) Remains OFF if both AT and AT+CMGF=1 work.

When I tested this program on ATmega32 with 16Mhz oscillator, the LED remained OFF. Connections are fine and GSM modem supports both commands when tested on PC & works on 9600 Baud rate.

Can someone figure out what might have gone wrong ?? Sorry for starting new thread, I had problems replying in old one.. Please reply.. Thanks..

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First of all, are you sure you are running at 16MHZ (fuses set properly???). Can you talk to a PC instead of the GSM Modem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ya.. 16MHz sure.. Fuses set properly.. I tested blinking LED program with 1sec delay, worked fine.. Ya.. With hyperterminal in PC, modem responds perfectly.. Please check the program and let me know the flaws.. Thanks..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

With hyperterminal in PC, modem responds perfectly

How about the AVR+PC combination? See FAQ #3

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did not get when you meant AVR+PC combination.. Anyway, I m using 16 MHz crystal oscillator. UBRR=(fosc/(16xBaud_rate))-1=((16x10^6)/(16x9600))-1 = 103 (approx).
So is this how I should store it ?? You think that was problem :?:

void baudinit()
{
unsigned int baudr= 103; /*  Value in UBRR for 9600 Baud  */
/* Set baud rate */
UBRRH = (unsigned char)(baudr>>8);
UBRRL = (unsigned char)baudr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

In above calculation, I get 0.3% Baud error.. If I change baud rate to 1200 keeping oscillator frequency same, I get 0% error.. So do you suggest I should baud rate from 9600 to 1200 ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you have 3 communicating devices. A modem, a PC and an AVR. So far you know:

AVR+modem = not work
modem+PC = work

I'd just like to know what result you get with:

AVR+PC = ?

IOW if you send some characters out of the UART of the AVR and up a cable into the RS232 of a PC (with a suitable level converter/inverter) do you see the same characters on the screen in Hyperterminal and if you type in Hyperterminal are the same characters receive on the AVR.

My suspicion is that the AVR is not running at the speed you think and so it can talk to neither PC nor modem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

My suspicion is that the AVR is not running at the speed you think and so it can talk to neither PC nor modem.

As I said I tried running a blinking LED program with 1 sec delay with settings for 16MHz and it worked fine..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But if you would just test the UART AVR -> PC you could eliminate one possible error - regardless if your LED blinks with 1sec. delay... The problem is not that big and it brings you assurance that the communication works just fine.

Problems could possibly be that your hardware is not set up right. Which RS232 transceiver do you use? Did you connect the right PINs from the ATmega32 to the transceiver?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please confirm you are using atmega32 and not atmega32L?

when i was developing a project involving communication between a Gsm Modem and an avr, i also used a FT232RL chip to provide another communications port to debug whats going on between the avr atmega128 and gsm modem. I still have spares of this PCB if you are interested(for a small fee offcourse), since you are in India i could have it couriered to you, you will then have to solder a SMD atmega128, an ft232rl, a max3232 and some other passive smds. the pcb has provision for a 8bit LCD and 2 inputs for ADC... let me know if your intersted.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kumarprabhatn wrote:
I did not get when you meant AVR+PC combination.. Anyway, I m using 16 MHz crystal oscillator. UBRR=(fosc/(16xBaud_rate))-1=((16x10^6)/(16x9600))-1 = 103 (approx).
So is this how I should store it ?? You think that was problem :?:

void baudinit()
{
unsigned int baudr= 103; /*  Value in UBRR for 9600 Baud  */
/* Set baud rate */
UBRRH = (unsigned char)(baudr>>8);
UBRRL = (unsigned char)baudr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

In above calculation, I get 0.3% Baud error.. If I change baud rate to 1200 keeping oscillator frequency same, I get 0% error.. So do you suggest I should baud rate from 9600 to 1200 ??


Any reason not to use a 14.7456 MHz or 18.432 MHz crystal instead of a 16 MHz?
These frequencies are meant for serial communitation and will give you 0% timing error even at much higher baud rates.

Anyway I typed in your settings in this AVRcalc Baud Calculator and it only gave a 0.160% error at 16 MHz: http://b9.com/elect/avr/kavrcalc

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there.. I edited the program to test only AT.
As you see from the attachment, in the program either the LED must glow or it must blink. But when the program was tested LED did neither glowed nor blinked, it remained OFF !! This makes me think that the program is waiting for some reply.. Isn't it??

It is ATMega32 for sure.

I m planning to change oscillator to 14.7456 MHz.

Can someone tell or give link, procedure to communicate with PC using AVR ??

In DB9 female connector, which connects modem to microcontroller, do we need to short 1,4,6 pins and 7,8 pins ??

Please reply..

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just need ground, rx to tx, tx to rx

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Just need ground, rx to tx, tx to rx

Then what do you think is wrong with the code ?? It is definitely stopping in between, program isn't executing till end..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking at your code (has someone stolen your TAB key by the way?), your for() loop sends FOUR characters from at[] but there are only 3 characters in it ("AT\r"). The fourth element will be NUL, not sure what the modem will make of that?

You then receive four characters. Are you SURE it's four that will be returned? I'm assuming you expect to see "OK" but is that "OK\n" or "OK\r\n". It'll only get out of the second for() loop with the latter. I presume the intention of the test there is to consider it "valid" if you see the 'O' of "OK"?

I have a feeling the reason you don't see the LED is because it's stuck in that second for() loop. If it were me I'd only expect the O of OK so don't bother with a for() loop at all but just read ONE character and if it is "O" it's "valid" and not valid otherwise.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i assume that you have a max232 on board. did you try connecting the avr to the comp and check whats coming in hyperterminal?

till you get hold of a crystal why dont you run off the internal crystal of 1mhz and baud rate of 2400. its not reliable.. but its fine for testing. once you get your crystal you can shift to external clock.

could you post some schematics of your board? just to rule out hardware issues

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Looking at your code (has someone stolen your TAB key by the way?), your for() loop sends FOUR characters from at[] but there are only 3 characters in it ("AT\r"). The fourth element will be NUL, not sure what the modem will make of that?

My bad.. I forgot to edit here.. I have put at[3] in the program when I programmed it but forgot to edit the text file I uploaded..

Quote:
I presume the intention of the test there is to consider it "valid" if you see the 'O' of "OK"?

Exactly !!

I m looping 4 times because I m not sure of exact format the modem replies, I mean, it may be "\r\nOK\r\n" or just "OK" or something like that. So to be on safer side I just looped 4 times because within that O must come.

May be you are right, its probably getting stuck there since I m receiving less than 4 characters, you have any solution for that??
Because if I accept only 1st character, it might be \r also, right, if the format is "\r\nOK" or something like that.

rodney_alm wrote:
i assume that you have a max232 on board. did you try connecting the avr to the comp and check whats coming in hyperterminal?

Thats why I asked for a procedure or link to communicate.

Quote:
could you post some schematics of your board? just to rule out hardware issues

I will upload here soon.

Last Edited: Sat. May 16, 2009 - 03:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I m not understanding why it must get stuck there because after 4 loops it must come out, right ??

No, not if the reply is ONLY "OK\n" - that's three characters.

Rather than guess at this try two things:

1) read the modem's datasheet - it *should* tell you the exact format of the responses but if that fails...

2) connect the modem direct to a PC terminal that will output hex codes of received characters (so 0x0D for \r and 0x0A for \n) to find out the EXACT format of the response.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

No, not if the reply is ONLY "OK\n" - that's three characters.

You are right !!

Quote:
connect the modem direct to a PC terminal that will output hex codes of received characters (so 0x0D for \r and 0x0A for \n) to find out the EXACT format of the response.

How ??

Because when I use hyperterminal, I type AT and press enter, hence I dont know if \n is required or not. With echo enabled, I get reply AT and in next line OK with cursor in next line.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hyperterminal is not the world's greatest piece of sofwtare (though it could be on the short list for worst ;-)). Maybe download "Bray's terminal" as that will display Hex codes if you ask it nicely.

http://braypp.googlepages.com/te...

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so can we presume that serial communiations is fine and stable?

avr<--->computer communications established?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rodney_alm wrote:
so can we presume that serial communiations is fine and stable?

avr<--->computer communications established?

Nope.. Sorry.. Today is saturday.. Can't try until monday in college.. I'll post as soon as I can.. But a tutorial or something on how to communicate would be really helpful.. Thanks anyway..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Hyperterminal is not the world's greatest piece of sofwtare (though it could be on the short list for worst ;-)).

Maybe that's why Microsoft doesn't include HyperTerminal in Windows anymore (or maybe it's just because most newer PCs doesn't have COM ports).
http://en.wikipedia.org/wiki/Hyp...
Quote:
In 1995 Hilgraeve licensed a low-end version of HyperACCESS, known as HyperTerminal (essentially a "Lite" version) to Microsoft for use in their set of communications utilities. It was initially bundled with Windows 95, and subsequently all versions of Windows up to and including Windows XP. Windows Vista and Windows 7 does not include HyperTerminal, though the commercial products HyperTerminal Private Edition (latest version being 6.4) and HyperACCESS (latest version being 8.4) both support Vista.

clawson wrote:
Maybe download "Bray's terminal" as that will display Hex codes if you ask it nicely. http://braypp.googlepages.com/te...

Yes much more powerful tool than HyperTerminal and it doens't require installation either, runs just from a single .exe file. There's probably many other terminal programs for Windows better than HyperTerminal too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

1) read the modem's datasheet - it *should* tell you the exact format of the responses but if that fails...

According to datasheet of modem, it is given,
A serial link handler is set with these default values (factory settings) :
- autobaud, 8 bits data, 1 bit stop, no parity, RTS/CTS flow control.

The commands always start with AT (means Attention !) and finish with a
character.

Responses start and end with , except with the ATV0 (DCE response
format) and ATQ1 (result code suppression) commands.

According to this, the response received should be "\r\nOK\r\n", right?? which means my program is correct and it should not hang at 2nd for loop.
Since it requires RTS,CTS control does that mean I must short 1,4,6 and 7,8 since I m not controlling RTS,CTS in µcontroller ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, in AT+IFC=,
AT+IFC? command gives result +IFC: 2,2

< DCE_by_DTE >
0 : none (Supported)
1 : Xon/Xoff local circuit 103 (not supported)
2 : RTS (Supported)
3 : Xon/Xoff global on circuit 103 (not supported)

< DTE_by_DCE >
0 : none (Supported)
1 : Xon/Xoff circuit 104 (not supported)
2 : CTS (Supported)

Is it possible that this is the problem, because it relies on RTS and CTS. Should I change settings using AT+ICF=0,0

Please reply.. Thanks..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is that make of the modem? is it a i300 chipset?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I m not sure.. My modem is Wavecom G240 3R based on Wavecom module Q2403A..

In my tests using Bray's terminal, when I directly connected modem to PC, I couldn't get output for command AT unless I enabled handshaking RTS/CTS.
Also, the format of reply was "\r\nOK\r\n"

Any conclusion from this might help me.. I think this was the problem since I did not take care of handshaking.. Anyone agreeing ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Someone please reply.. Is it problem of RTS/CTS.. Can I disable it by command
AT+IFC=0,0

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the modem uses CTS/RTS and you don't want to generate this from the AVR then hardware the CTS of the modem so it's always told that it's Clear To Send.

But the unknown in this is whether the AVR is actually communicating to the modem correctly at all. You will only know if it is by forgetting the modem and connecting the AVR to the PC so you can SEE what it is generating. My suspicion (as always - see FAQ#3) is a timing error (whatever your LED tests may have told you)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kumarprabhatn :
According to your answers I assume that you don't know much about the RS232 interface. I'll try to explain that shortly. The RS232 UART is a voltage controlled interface. A voltage between -3V and -15V means a logical one or high, a voltage between +3V and +15V means a logical zero or low. A voltage in between -3V and +3V is undefined. Yor AVR can only produce voltages from low (nearly 0V) to high (max. UCC -> 3,3V or 5V). So you could never communicate with another device through the UART or RS232. This is why you need a TRANSCEIVER like the Maxim MAX232 or similar devices. It "translates" the TTL logic the AVR uses into the RS232 the PC or your modem understands.

So, if you do NOT have some kind of MAX232 or another transceiver, your serial link will never work - unless you have a dual voltage power supply and solve the problem with the help of transistors.

I hope this helps a little.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ya.. I understood that and I have used MAX232 chip..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Finally got my program to work
1. Added while(1); at end of program
2. In codevisiion avr, format is slightly different from C. For example, in C it is while {......} but in codevision avr it is while {......};
3. Disabled RTS/CTS using AT+IFC=0,0
4. Shorted 1,4,6 and 7,8 pins just in case

After correcting these, I tried connecting AVR to PC. It worked. Then tried checking AT command program and got it to work.

Thanks everybody for your help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I m having trouble with looping for received message. So I have decided to USART RX Complete interrupt. I have understood most of its functioning but I wanted to know what happens if I get receive complete interrupt when I m processing the a receive complete ISR ?? Please help..