Serial communication programming in atmega328p on arduino

Thread Starter

thebeginner

Joined Jun 19, 2016
2
I am trying to program arduino's atmega328p serial communication registers myself. After referring to several resources on the internet I have arrived at the following code:

Code:
#define BAUDRATE(BAUD) (((F_CPU/(BAUD*16UL)))-1)
    class serials
    {
              serials()
        {
            UBRR0H = BAUDRATE(9600) >> 8;
            UBRR0L = BAUDRATE(9600);

            UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(TXCIE0);
            UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
        }
        void transmit(unsigned char);
};
void serials::transmit(unsigned char data)
    {
           
              while(!(UCSR0A & _BV(UDRE0)));
                  UDR0 = data;
                    
       
           
    }
Code:
void loop()
{
serials lcdtransmit;

  lcdtransmit.transmit(254);
  lcdtransmit.transmit(1);
  lcdtransmit.transmit(254);
  lcdtransmit.transmit(128);
  lcdtransmit.transmit('a');
while(1);
}
I am trying to transmit 'a' to a serial lcd. Though the serial lcd displays 'a', the data on the lcd resets continuously.
Is there any solution to this issue ? Is there any error in the code written.
Is this problem specific to arduino.
 

MMcLaren

Joined Feb 14, 2010
861
Hi TheBeginner:

Your code looks similar to the code in my very first Arduino program from six or seven weeks ago. My code worked fine using a terminal app' on my PC.

You might add comments to your 'loop' section. Can I assume you're sending a '254' character ahead of each LCD 'command' character? In this case you're sending a 0x01 (clear screen) command and a 0x80 (set DDRAM address) command?

Regards, Mike

Code:
/*******************************************************************
 *                                                                 *
 *     Project: UART_Echo                                          *
 *      Author: Mike McLaren, K8LH                                 *
 *        Date: 28-Apr-16                                          *
 *                                                                 *
 *   Arduino Nano USART Echo Test (9600 baud) tries to avoid       *
 *   using Arduino core function calls.                            *
 *                                                                 *
 *       Board: Arduino Nano                                       *
 *         IDE: Arduino 1.6.8                                      *
 *                                                                 *
 *******************************************************************/

 #include <avr/io.h>

//#define F_CPU 16000000UL          // provided by boards.txt

/*******************************************************************
 *  constants & helper macros                                      *
 *******************************************************************/

 #define _BAUD    9600                 // baud rate
 #define _UBRR    (F_CPU/16)/_BAUD-1

 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~(1<<bit))
 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= 1<<bit)

/*******************************************************************
 *  variables                                                      *
 *******************************************************************/
/*******************************************************************
 *  functions                                                      *
 *******************************************************************/

 char usart_GetSer(void)            // GetSer (blocking)
 { while(!(UCSR0A & 1<<RXC0));      // wait char available 
   return UDR0;                     //
 }                                  //
/*******************************************************************/
 void usart_PutSer(char data)       // PutSer (blocking)
 { while(!(UCSR0A & 1<<UDRE0));     //
     UDR0 = data;                   //
 }                                  //
/*******************************************************************/
 void usart_PutStr(char * data)     // PutStr
 { while(*data!=0)                  //
     usart_PutSer(*data++);         //
 }                                  //
/*******************************************************************/
 void usart_PutNib(char work)       // PutNib (hex nibble to ASCII)
 { work &= 0x0F;                    // lo nibble only
   if(work > 9) work += 7;          // 'A'..'F' values
   usart_PutSer(work + '0');        // put ascii nibble
 }                                  //
/*******************************************************************/
 void usart_PutHex(char value)      // PutHex
 { usart_PutNib(value >> 4);        //
   usart_PutNib(value);             //
 }                                  //

/*******************************************************************
 *  main setup                                                     *
 *******************************************************************/
 int main (void)                    //
 { UBRR0H = (_UBRR) >> 8;           // setup USART port
   UBRR0L = (_UBRR);                //  "
 //UCSR0B |= _BV(TXEN0);            //  "
 //UCSR0B |= _BV(RXEN0);            //  "
 //sbi(UCSR0B, TXEN0);              //  "
 //sbi(UCSR0B, RXEN0);              //  "
   UCSR0B |= 1<<TXEN0 | 1<<RXEN0;   //  "

 //usart_PutStr("Hello world ");    // test
 //usart_PutHex(0x3C);              // test
 //usart_PutSer(' ');               // test

/*******************************************************************
 *  main loop                                                      *
 *******************************************************************/
   while(1)                         //
   { usart_PutSer(usart_GetSer());  // echo to terminal
   }                                //
 }                                  //
 
Last edited:
Top