atmega8+rs232+sim800

Thread Starter

devjeetmandal

Joined May 7, 2017
48
i have an atmega8 and a sim800. now i send some command to sim800 module and want to receive the data at terminal. note i dnt want to send command from terminal but from atmega8. but recieve at terminal. baud set 9600 at both end atmega and terminal.
 

Thread Starter

devjeetmandal

Joined May 7, 2017
48
basically i have a gsm module sim800 and i m connecting it with pc via rs232 to check weather its working or not.
and its working fine.



but i can't interface it with atmega8.

C:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>

#define buffer_size 64

void usart_init(unsigned int value);
void transmit_char(unsigned char cmd);
void transmit_string(char * cmd);
void blinkLed();

char buffer[buffer_size];
uint8_t readpos = 0;
uint8_t writepos = 0;



int main(void)
{
    _delay_ms(1000);
    DDRB = 0x01;
    blinkLed();
    usart_init(8);
    sei();
 
    transmit_string("ATE0\r");
    _delay_ms(2000);
 
    transmit_string("AT+IPR=9600\r");
    _delay_ms(2000);
    usart_init(103);
 
    OK:
    writepos = 0;
    transmit_string("AT\r");
    _delay_ms(2000);
    if(strstr(buffer,"OK"))
     blinkLed();
    else
     goto OK;
    _delay_ms(1000);
 
    while (1)
    {
    }
}

void blinkLed()
{
    PORTB=0xfe;
    _delay_ms(1000);
    PORTB=0x01;
    _delay_ms(1000);
}

void usart_init(unsigned int value)
{
    UCSRB |= (1<<RXEN) | (1<<TXEN) | (1<<RXCIE);
    UCSRC |= (1<<UCSZ0) | (1<<USBS) | (1<<URSEL);
    UBRRH = (unsigned char)(value>>8);
    UBRRL = (unsigned char)value;
 
}

void transmit_char(char cmd)
{
    while ((UCSRA & (1 << UDRE)) == 0);
    UDR = cmd;
 
}

void transmit_string(char *cmd)
{
    char length;
 
    length = strlen(cmd);
    for(int i=0;i<length;i++)
    transmit_char(cmd[I]);
  
}

ISR(USART_RXC_vect)
{
    buffer[writepos] = UDR;
    writepos++;

    if(writepos>=buffer_size)
    {
        int i;
        writepos = 0;
        for( i=0;i<buffer_size;i++)
          buffer[I] = 0;
        buffer[I] = '\0';
    }
}
Moderators note : used code tags
 
Last edited by a moderator:
Top