Question

I have a problem in connecting ATmega128 serial port to computer via USB-Serial converter. The USB-Serial converter is verified as I have connected computer to CDMA modem using it. However when I try to connect it with atmega128 I can't figure out the problem. I have connected it to serial LCD (CLCD) and it works fine.Even in simulation with virtual terminal there is no problem. I would like to know if I have missed anything related to serial port. I have already checked baud rate in hardware options and in virtual terminal. Here is the code.

#include<avr/io.h>
#include<util/delay.h>

char str1[]="AT\r\n";

char str2[]="AT+CMGF=1\r\n";

char str3[]="AT+CMGS=\"01068685673\"\r\n";

char str4[]="hello\x1A\r\n";


int i;

void TX_CHAR(char ch)
{

    while(!(UCSR1A&0x20));
    UDR1=ch;

}

int main()
{

    UBRR1H=0; UBRR1L=103; UCSR1B=0x08;
    UCSR1C=0b00000110;

    while(1)
    {
        i=0; while(str1[i])TX_CHAR(str1[i++]);
        _delay_ms(200);
        i=0; while(str2[i])TX_CHAR(str2[i++]);
        _delay_ms(200);
        i=0; while(str3[i])TX_CHAR(str3[i++]);
        _delay_ms(200);
        i=0; while(str4[i])TX_CHAR(str4[i++]);
        _delay_ms(3000);
    }
}
Was it helpful?

Solution

Things to check:

  • hardware - wiring
  • value of M103C fuse (= compatibility mode)
  • XTAL frequency and prescalers, as the formula for BAUD depends on it: BAUD = Fosc/16(UBRR+1)
  • USART double speed flag (UCSRA)
  • frame format
  • UDREn flag set

You also may get better insight into your code if you use predefined symbolic values, e.g.

/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);

see the examples in the data sheet (pg 176ff)

On the frame format I understand you are set to async, 8-bit (UCSR1B:2 = 0, UCSR1C:2,1 = 11), parity disabled, 1 stop bit

In void TX_CHAR(char ch) I understand you are checking the status of bit 7 (RXC1) by using mask 0x20H. On the other hand you dont have the RX enabled (RXEN1 meaning UCSR1B:4=0)

I wonder if you shouldn't better check bit 6 (TXC1). Again ... using the symbolic values would help to better understand the code.

Hope this helps ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top