I have an Atmega328 micrprocessor.
I connected its RX/TX pins to an SIM808 module and uploaded this code to the processor to communicate with it and send commands:
#include <util/delay.h>
#define F_CPU 12000000UL
#include<avr/io.h>
#define USART_BAUDRATE 1200
#define BAUD_PRESCALE (((F_CPU/(USART_BAUDRATE*16UL)))-1)
int main(void){
// default settings
char recieved_byte;
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
UBRR0H = (BAUD_PRESCALE >> 8);
UBRR0L = BAUD_PRESCALE;
for(;;){
// wait until a byte is ready to read
while( ( UCSR0A & ( 1 << RXC0 ) ) == 0 ){}
// grab the byte from the serial port
recieved_byte = UDR0;
// wait until the port is ready to be written to
while( ( UCSR0A & ( 1 << UDRE0 ) ) == 0 ){}
// write the byte to the serial port
UDR0 = recieved_byte;
_delay_ms(100);
}
return 0; /* never reached */
}
It gets trapped in a loop, for example after typing at in the serial terminal it prints this back and it continuous until restarted:
OK
atOK
OK
atOKatOKatOKatOKatOK
I've a feeling that the code must be changed, but I'm new to this and I couldn't find a simple example.
PS:
I test these:
I've used an Arduino instead of a bare processor. It works fine sending and receiving commands, so the SIM module is fine.
I've disconnected the SIM module and the echo program works well.
I've removed the echo part (I think) UDR0 = recieved_byte; but no luck.
Update 1:
I realized that the SIM808 works fine with an Arduino and/or other USB to serial if one connects RX to RX and TX to TX, it must be crossed inside the module. It doesn't help if I use this code and a bareare AVR MCU and it only echos back commands which the MCU gets, not the response from SIM module.