Serial communication programming in atmega328p on arduino

Discussion in 'Embedded Systems and Microcontrollers' started by thebeginner, Jun 19, 2016.

  1. thebeginner

    Thread Starter New Member

    Jun 19, 2016
    2
    0
    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 (Text):
    1. #define BAUDRATE(BAUD) (((F_CPU/(BAUD*16UL)))-1)
    2.     class serials
    3.     {
    4.               serials()
    5.         {
    6.             UBRR0H = BAUDRATE(9600) >> 8;
    7.             UBRR0L = BAUDRATE(9600);
    8.  
    9.             UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(TXCIE0);
    10.             UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
    11.         }
    12.         void transmit(unsigned char);
    13. };
    14. void serials::transmit(unsigned char data)
    15.     {
    16.            
    17.               while(!(UCSR0A & _BV(UDRE0)));
    18.                   UDR0 = data;
    19.                    
    20.        
    21.            
    22.     }
    23.  
    Code (Text):
    1. void loop()
    2. {
    3. serials lcdtransmit;
    4.  
    5.   lcdtransmit.transmit(254);
    6.   lcdtransmit.transmit(1);
    7.   lcdtransmit.transmit(254);
    8.   lcdtransmit.transmit(128);
    9.   lcdtransmit.transmit('a');
    10. while(1);
    11. }
    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.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,397
    497
    Do you have Arduino board?
    What model of Arduino board do you have?
     
  3. thebeginner

    Thread Starter New Member

    Jun 19, 2016
    2
    0
    Arduino uno
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,397
    497
  5. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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 (Text):
    1.  
    2. /*******************************************************************
    3.  *                                                                 *
    4.  *     Project: UART_Echo                                          *
    5.  *      Author: Mike McLaren, K8LH                                 *
    6.  *        Date: 28-Apr-16                                          *
    7.  *                                                                 *
    8.  *   Arduino Nano USART Echo Test (9600 baud) tries to avoid       *
    9.  *   using Arduino core function calls.                            *
    10.  *                                                                 *
    11.  *       Board: Arduino Nano                                       *
    12.  *         IDE: Arduino 1.6.8                                      *
    13.  *                                                                 *
    14.  *******************************************************************/
    15.  
    16.  #include <avr/io.h>
    17.  
    18. //#define F_CPU 16000000UL          // provided by boards.txt
    19.  
    20. /*******************************************************************
    21.  *  constants & helper macros                                      *
    22.  *******************************************************************/
    23.  
    24.  #define _BAUD    9600                 // baud rate
    25.  #define _UBRR    (F_CPU/16)/_BAUD-1
    26.  
    27.  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~(1<<bit))
    28.  #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= 1<<bit)
    29.  
    30. /*******************************************************************
    31.  *  variables                                                      *
    32.  *******************************************************************/
    33. /*******************************************************************
    34.  *  functions                                                      *
    35.  *******************************************************************/
    36.  
    37.  char usart_GetSer(void)            // GetSer (blocking)
    38.  { while(!(UCSR0A & 1<<RXC0));      // wait char available
    39.    return UDR0;                     //
    40.  }                                  //
    41. /*******************************************************************/
    42.  void usart_PutSer(char data)       // PutSer (blocking)
    43.  { while(!(UCSR0A & 1<<UDRE0));     //
    44.      UDR0 = data;                   //
    45.  }                                  //
    46. /*******************************************************************/
    47.  void usart_PutStr(char * data)     // PutStr
    48.  { while(*data!=0)                  //
    49.      usart_PutSer(*data++);         //
    50.  }                                  //
    51. /*******************************************************************/
    52.  void usart_PutNib(char work)       // PutNib (hex nibble to ASCII)
    53.  { work &= 0x0F;                    // lo nibble only
    54.    if(work > 9) work += 7;          // 'A'..'F' values
    55.    usart_PutSer(work + '0');        // put ascii nibble
    56.  }                                  //
    57. /*******************************************************************/
    58.  void usart_PutHex(char value)      // PutHex
    59.  { usart_PutNib(value >> 4);        //
    60.    usart_PutNib(value);             //
    61.  }                                  //
    62.  
    63. /*******************************************************************
    64.  *  main setup                                                     *
    65.  *******************************************************************/
    66.  int main (void)                    //
    67.  { UBRR0H = (_UBRR) >> 8;           // setup USART port
    68.    UBRR0L = (_UBRR);                //  "
    69.  //UCSR0B |= _BV(TXEN0);            //  "
    70.  //UCSR0B |= _BV(RXEN0);            //  "
    71.  //sbi(UCSR0B, TXEN0);              //  "
    72.  //sbi(UCSR0B, RXEN0);              //  "
    73.    UCSR0B |= 1<<TXEN0 | 1<<RXEN0;   //  "
    74.  
    75.  //usart_PutStr("Hello world ");    // test
    76.  //usart_PutHex(0x3C);              // test
    77.  //usart_PutSer(' ');               // test
    78.  
    79. /*******************************************************************
    80.  *  main loop                                                      *
    81.  *******************************************************************/
    82.    while(1)                         //
    83.    { usart_PutSer(usart_GetSer());  // echo to terminal
    84.    }                                //
    85.  }                                  //
    86.  
    87.  
    88.  
     
    Last edited: Jun 19, 2016
Loading...