#define F_CPU 16000000UL
#include<avr/io.h>
#include<avr/interrupt.h>
#include <util/delay.h>
#include"LCD.h"
#define BAUD 9600
#define MYUBRR ((( F_CPU / ( BAUD * 16UL))) - 1)
#define rx_on UCSRB|=(1<<RXCIE);
#define rx_off UCSRB&=~(1<<RXCIE);
void uart_init(unsigned int ubrr);
void send_string(unsigned char *str);
void send_data(unsigned char back);
void reset_flush(void);
unsigned char ReceivedByte[200];
unsigned char count,i=0,j,k,data,AT_ack=1;
int main(void)
{
DDRB=0xFF;
DDRA=0xFF;
DDRC=0xFF;
DDRD=0xFE;
PORTD=0x00;
MCUCSR|=(1<<JTD);
MCUCSR|=(1<<JTD);
lcd_init();
uart_init(MYUBRR);
_delay_ms(500);
lcd_send_R1(0,"GSM_init");
sei();
_delay_ms(50);
rx_off;
_delay_ms(500);
while(1)
{
/*******************************************************************************/
// AT+CMGR=1 //
/*******************************************************************************/
send_string("AT+CMGR=1"); //send "AT" to GSM
send_data(0x0D);
send_data(0x0A);
rx_on; // checking response from GSM
_delay_ms(10000);
rx_off;
for(k=0;k<16;k++)
{
lcd_cmd(0x80+k);
lcd_data1(ReceivedByte[k]);
_delay_ms(500);
}
for(k=16;k<32;k++)
{
k=k-16;
lcd_cmd(0xC0+k);
lcd_data1(ReceivedByte[k]);
_delay_ms(500);
}
for(k=32;k<48;k++)
{
k=k-32;
lcd_cmd(0x80+k);
lcd_data1(ReceivedByte[k]);
_delay_ms(500);
}
reset_flush();
_delay_ms(5000);
/*******************************************************************************/
}
return 0;
}
/*******************************************************************************/
// UART Initialization //
/*******************************************************************************/
void uart_init(unsigned int ubrr)
{
UBRRH = (uint16_t)(ubrr>>8);
UBRRL = (uint16_t)ubrr;
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
UCSRC = (1<<URSEL)|(3<<UCSZ0);
}
/*******************************************************************************/
// char send to UART //
/*******************************************************************************/
void send_data(unsigned char back)
{
while( !( UCSRA & (1<<UDRE)) ) ;
UDR=back;
}
/*******************************************************************************/
// String send to UART //
/*******************************************************************************/
void send_string(unsigned char *str)
{
while(*str)
{
send_data(*str++);
}
}
/*******************************************************************************/
// Reset Buffer //
/*******************************************************************************/
void reset_flush(void)
{
i=0;
count=0;
for(j=0;j<200;j++)
{
ReceivedByte[j]='\0';
}
}
ISR ( USART_RXC_vect )
{
count=UDR;
if(count!=0x0D && count!=0x0A && count!='>' && count!='"') //Do not Print \r\n and ">"
{
ReceivedByte[i]=count;
i++;
}
if(i==200)
{
i=0;
}
}