how to program GSM module in avr...??
how to program GSM module in avr...??
Depends on the module used. Since your questions says absolutely nothing at all about that, the answer will be on the same level of abstraction:
Write code.
i am using SIM300 module and i am getting problem to read new arrived message from module....
Show code.
Describe problem more specifically than just "getting problem".
HERE IS THE CODE FOR YOU . HERE ATMGA168 IS USED,, WHEN MSG IS SENT LED WILL TURN ON.
#ifndef F_CPU
#define F_CPU 1000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
//Functions initialization
char *compare();
void send();
// Global Variable Initialization
char *data1,str1[100],str2[100],y[10],*data2,index;
int s,e;
void uart_init(){
UBRR0H=0x00;
UBRR0L= 12;
UCSR0A=(0<<RXC0) | (0<<TXC0) | (0<<UDRE0) | (0<<FE0) | (0<<DOR0) | (1<<U2X0) | (0<<MPCM0);
UCSR0B=(0<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0) | (1<<RXEN0) | (1<<TXEN0) | (0<<UCSZ02) | (0<<RXB80) | (0<<TXB80);
UCSR0C=(0<<UMSEL00) | (0<<UPM01) | (0<<UPM00) | (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00) | (0<<UCPOL0);
}
void uart_transmit (char data){
while (!( UCSR0A & (1<<UDRE0))); // wait while register is free
UDR0 = data; // load data in the register
}
void string_transmit(const char *str){
unsigned char s=0;
while (str[s]!=0){
uart_transmit (str[s]);
s++;
}
}
unsigned char uart_receive (){
while((UCSR0A&(1<<RXC0)) == 0); // Return received data
return UDR0;
}
char * string_receive1(){
unsigned char x,i = 0; //receive the characters until "," character comes
while((x = uart_receive())!=','){
str2[i++] = x; //and store the received characters into the array string[] one-by-one
}
str2[i] = '\0'; //insert NULL to terminate the string
return str2;
}
char * string_receive2()
{
unsigned char x,i = 0;
//receive the characters until "K" character comes
while((x = uart_receive())!='K')
{
str2[i++] = x; //and store the received characters into the array string[] one-by-one
}
str2[i] = '\0'; //insert NULL to terminate the string
return str2;//return the received string
}
void gsm_init()
{
string_transmit("\r\nAT\r\n");
_delay_ms(500);
string_transmit("ATE1\r\n");
_delay_ms(500);
string_transmit("AT+CMGF=1\r\n");
_delay_ms(500);
string_transmit("AT+CMGD=1,4\r\n");
_delay_ms(500);
string_transmit("AT+CNMI=1,1,0,1\r\n");
}
char *compare(){
int i = 0;
// s = start of new string
// e = end of new string
while(s<e ){
y[i++] = data1[s++]; //and store the received characters into the array string[] one-by-one
}
y[i] = '\0'; //insert NULL to terminate the string
return(y); //return the received string
}
//Main function
int main()
{
DDRC=0xff;
uart_init();
gsm_init();
while(1){
PORTC ^= (1 << 0);
_delay_ms(300);
data1=string_receive1();
index=uart_receive();
_delay_ms(300);
s=5;e=9;
data2=compare();
if(strncmp(data2,"+CMTI",4)==0){ PORTC ^= (1 << 1); }
string_transmit("AT+CMGR=");
uart_transmit(index);
string_transmit("\r\n");
data1=string_receive2();
_delay_ms(300);
s=78;e=86;
data2=compare();
_delay_ms(300);
if(strncmp(data2,"Yellow",6)==0)
{if ((PORTC & (1 << 4) ) == 0){
PORTC |= 1 << 4;
}
else{
PORTC &= ~(1 << 4);
}}
if(strncmp(data2,"Green",5)==0){
if ((PORTC & (1 << 2))==0){
PORTC |= 1 << 2;
}
else{
PORTC &= ~(1 << 2);
}
}
_delay_ms(2700);
string_transmit("AT+CMGD=1,4\r\n");
}
return 0;
}
Thanks for the contribution but the OP was 8 years ago.
Continuing the necromancy theme ...
Do not do this:
string_transmit("\r\nAT\r\n");
_delay_ms(500);
string_transmit("ATE1\r\n");
_delay_ms(500);
string_transmit("AT+CMGF=1\r\n");
_delay_ms(500);
string_transmit("AT+CMGD=1,4\r\n");
_delay_ms(500);
string_transmit("AT+CNMI=1,1,0,1\r\n");
All AT commands give a response - "OK" or an Error - to indicate when the unit has finished handling one command, and is ready for the next.
You need to pay attention to these responses; not just blindly ignore them with delays, as above - any such code is going to come unstuck sooner or later when a command takes even a little longer than expected, or fails completely.
See:
https://www.avrfreaks.net/commen...
https://www.avrfreaks.net/commen...
https://www.avrfreaks.net/commen...
https://www.avrfreaks.net/commen...
https://www.avrfreaks.net/commen...
etc, etc, ...