/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.4a Advanced
Automatic Program Generator
� Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 2010/07/11
Author  : NeVaDa
Company : 
Comments: 


Chip type               : ATmega32L
Program type            : Application
AVR Core Clock frequency: 6.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega32.h>

#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm

#include <lcd.h>
#include <stdio.h>
#include <delay.h>

char Enter=13;
char rx_buffer[255];
unsigned char rx_index=0;
static char k;


interrupt [USART_RXC] void usart_rx_isr(void)
{
char data=0;  
data=UDR;
rx_buffer[rx_index] = data;
if(++rx_index == 255)
rx_index=0;  
}


void main(void)
{

PORTB=0xff;
DDRB=0x00;

PORTC=0x00;
DDRC=0xF0;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;

ACSR=0x80;
SFIOR=0x00;

lcd_init(20);

printf("ATE0%c",Enter);
delay_ms(50);
printf("AT+CMGF=1%c",Enter);
delay_ms(50);

#asm("sei")
while (1)
      {       
         if(PINB.0==0)
            {                
               printf("AT+CMGR=2\n\r");
               delay_ms(100);                                       
            }                          
        
         if(PINB.1==0)
            {                
               #asm("cli")
               sprintf(rx_buffer,"%s");                  
               lcd_clear();
               lcd_puts(rx_buffer);
               delay_ms(300);
              // for(k=0;k<255;k++)
             //  rx_buffer[k]=0;
               #asm("sei")           
            }
            
      };
}