XMEGA tutorial: LCD display (05)

|

Blinking diodes mentioned in the previous part of our tutorial is cool, however not very communicative. That's why before moving on to discussing particular peripherals of XMEGA microcontrollers, we will learn how to operate 16x2 LCD text display with HD44780 driver.

This paragraph’s aim is to acquaint the reader with basic knowledge on how to control the display, although we are not going to create it’s function library from scratch. Instead, we will use a template provided by Radosław Kwiecień and available on his website radzio.dxp.pl. I have adjusted his function library to work with XMEGA microcontrollers, so that we can use the display in following parts of the preparation course. We will see how new libraries can be added to the Atmel Studio project by the way.


After creating a new project (I have described the whole procedure in the second part of the course) we need to add display control library which can be downloaded below.
Download:
Next, in the project tree visible on the right side of the Atmel Studio window, we indicate the highest position and right-click it. From the pop-up menu we choose Add and Existing Item afterwards.



We select hd44780.c and hd44780.h files, which have been downloaded earlier from the Leon Instruments' server. They should show up in the project tree.

Then, according to the C Language rules, we should add a library’s header file using #include directive.
    
    #include <avr/io.h>
    #include <util/delay.h>
    #include "hd44780.h"
 
Now, I will briefly introduce functions responsible for display control:
  • void LcdInit(void) – display controller initialization
  • void LcdClear(void) – cleaning the display
  • void LcdData(unsigned char data) – displaying single character on the display in the cursor’s actual position
  • void LcdGoto(unsigned char x, unsigned char y) – cursor’s movement into set position, where x is the number of column starting from the left, while y is the number of the line counting from the top. (remember: we count from 0!)
  • void LcdWrite(char * text) – displaying text stored in RAM. This function should be used with caution, as all of the texts used by this function will be stored in RAM memory, which can be very quickly overloaded
  • #define Lcd(tekst) LcdWriteProgmem(PSTR(tekst)) – macro to display a text stored on a Flash memory, sparing space for additional texts in the very valuable RAM memory
  • #define Lcd2 LcdGoto(0,1) – movement to the second line
  • void LcdDec(uint32_t liczba) – displaying the number
It is worth having a look at the file hd44780.h, as in this file we need to define which processor's pin are connected to the display.
    
    #define LCD_PORT        PORTC
    #define LCD_RS_bm       PIN2_bm
    #define LCD_E_bm        PIN3_bm
    #define LCD_D4_bm       PIN4_bm
    #define LCD_D5_bm       PIN5_bm
    #define LCD_D6_bm       PIN6_bm
    #define LCD_D7_bm       PIN7_bm
 
Having the header file defined, we can move on to constructing the circuit on the breadboard, shown on the schematic.


Exeptional attention should be put to the fact, that some of the older displays are designed to work with 5V voltage, while XMEGA microcontrollers are designed to 3.3V voltage. Remember that processor’s pins cannot be input higher voltage than the power supply voltage. In this circuit, communication between the processor and the display is one way only. Willing to read the data from the display for e.g. busy flag, the circuit should be equipped with a voltage translator, at least some resistor voltage divider, because direct connection might damage processor’s pins.

Now we can step forward to programming. After turning on the power supply, the display should provide us a welcome announcement, and afterwards the program should count how many times did we press FLIP button on the X3-DIL64 board. The source code of the program is simple, and after having this article read you shouldn’t find it difficult to understand.

 
#define F_CPU 2000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "hd44780.h"

int main(void) {
    
    // FLIP configuration
    PORTE.DIRCLR    =  PIN5_bm;             // pin E5 as input
    PORTE.PIN5CTRL  =  PORT_OPC_PULLUP_gc;  // pull-up to the power supply
    
    // welcome message
    LcdInit();                              // display initialization
    Lcd(" XMEGA TUTORIAL");                 // display the text
    Lcd2;                                   // go to the second line
    Lcd("Leon Instruments");                // display the text

    // counter variable
    uint8_t counter = 0;
    
    while(1) {
        if(!(PORTE.IN & PIN5_bm)) {         // if FLIP pressed
            counter++;                      // increment the counter
            LcdClear();                     // clear the display
            Lcd("counter:");                // display the text
            Lcd2;                           // przejście do drugiej linii
            LcdDec(counter);                // display the number
            _delay_ms(100);                 // wait 100ms
        }
    }
}


1 comments :

Unknown said...

Thanks lot, you saved my time, this library and code working fine with ATXMEGA32A4U

Post a Comment

Post a comment