0
\$\begingroup\$

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:

  1. I've used an Arduino instead of a bare processor. It works fine sending and receiving commands, so the SIM module is fine.

  2. I've disconnected the SIM module and the echo program works well.

  3. 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.

\$\endgroup\$
4
  • \$\begingroup\$ What is your terminal software on the computer side? \$\endgroup\$
    – asdfex
    Commented Sep 27, 2020 at 9:26
  • \$\begingroup\$ @asdfex Arduino IDE - Serial monitor \$\endgroup\$
    – malloc
    Commented Sep 27, 2020 at 9:44
  • \$\begingroup\$ Your program has a memory for just one character (recieved_byte). It's not possible that it by itself repeats a longer string over and over again. The problem has to be elsewhere. \$\endgroup\$
    – asdfex
    Commented Sep 27, 2020 at 10:33
  • \$\begingroup\$ @asdfex That issue is solved, The module cross the RX/TX inside(Update1) but I can't get any response from module. \$\endgroup\$
    – malloc
    Commented Sep 27, 2020 at 10:40

1 Answer 1

2
\$\begingroup\$

You cannot have the same UART wired to both the PC via USB serial and also the mobile modem, without causing such mixups.

You need a second UART, either hardware or software as typical Arduino examples for your platform in this role use.

You also do not want to use delays in serial code.

Basically, start over from a working example and only attempt to reimplement by hand once you thoroughly understand that example.

Ideally you would also switch to a better processor with at least two hardware UARTs. Likely few but the Arduino folks would try to force this one to do that job when there are better, cheaper choices for an application needing two UARTs.

\$\endgroup\$
2
  • \$\begingroup\$ I've seen this before(using same wire for multiple UART) and I'm totally aware of the issues it could cause. this is because the Arduino have one TX/RX pins and I've connected all sort of modules to it, but the thing is it create some problems but it works somehow, I know that it is a must to put UART switch something like max232 or use a MCUs with multiple UART or software UART which could cause some transition speed lost , but before that I need to see a signal that it is alive, in my situation , it is like the MCU is totally dead and it put nothing on the Tx wire. \$\endgroup\$
    – malloc
    Commented Sep 27, 2020 at 17:59
  • \$\begingroup\$ No, wiring all three things together is NOT going to work. You need to redesign the system with two UART solutions. \$\endgroup\$ Commented Sep 27, 2020 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.