Tinkering TI MSP430F5529

Alphanumerical LCD

Alphanumerical LCDs are probably the easiest devices to quickly project information visually. They are easy to use and cheap. To be able to use them, we do not need any additional hardware feature of a micro or any special communication hardware like I2C and SPI. Digital I/Os are what we need to use these displays. Just like my other tutorials, this tutorial includes three LCD examples – one with software SPI communication, one with software I2C communication and one with direct DIO-based 4-bit LCD interface.

Be sure to use 3.3V compatible LCD because most of them don’t operate at 3.3V power level. Most of the LCD are designed for 5V operation. It is okay to use 5V power supply for powering LCD if there is no other option. LCDs accept 0V – 3.3V logic level. However, there should be no way with which a 5V power supply/device can get connected with MSP430’s 3.3V power bus.

Code Example

lcd.h

#include "driverlib.h"
#include "delay.h"

#define LCD_RS_PORT           GPIO_PORT_P8
#define LCD_RS_PIN            GPIO_PIN1

#define LCD_EN_PORT           GPIO_PORT_P8
#define LCD_EN_PIN            GPIO_PIN2

#define LCD_D4_PORT           GPIO_PORT_P2
#define LCD_D4_PIN            GPIO_PIN3

#define LCD_D5_PORT           GPIO_PORT_P3
#define LCD_D5_PIN            GPIO_PIN7

#define LCD_D6_PORT           GPIO_PORT_P2
#define LCD_D6_PIN            GPIO_PIN6

#define LCD_D7_PORT           GPIO_PORT_P4
#define LCD_D7_PIN            GPIO_PIN0

#define LCD_RS_HIGH           GPIO_setOutputHighOnPin(LCD_RS_PORT, LCD_RS_PIN)
#define LCD_RS_LOW            GPIO_setOutputLowOnPin(LCD_RS_PORT, LCD_RS_PIN)

#define LCD_EN_HIGH           GPIO_setOutputHighOnPin(LCD_EN_PORT, LCD_EN_PIN)
#define LCD_EN_LOW            GPIO_setOutputLowOnPin(LCD_EN_PORT, LCD_EN_PIN)

#define LCD_DB4_HIGH          GPIO_setOutputHighOnPin(LCD_D4_PORT, LCD_D4_PIN)
#define LCD_DB4_LOW           GPIO_setOutputLowOnPin(LCD_D4_PORT, LCD_D4_PIN)

#define LCD_DB5_HIGH          GPIO_setOutputHighOnPin(LCD_D5_PORT, LCD_D5_PIN)
#define LCD_DB5_LOW           GPIO_setOutputLowOnPin(LCD_D5_PORT, LCD_D5_PIN)

#define LCD_DB6_HIGH          GPIO_setOutputHighOnPin(LCD_D6_PORT, LCD_D6_PIN)
#define LCD_DB6_LOW           GPIO_setOutputLowOnPin(LCD_D6_PORT, LCD_D6_PIN)

#define LCD_DB7_HIGH          GPIO_setOutputHighOnPin(LCD_D7_PORT, LCD_D7_PIN)
#define LCD_DB7_LOW           GPIO_setOutputLowOnPin(LCD_D7_PORT, LCD_D7_PIN)

#define clear_display         0x01
#define goto_home             0x02

#define cursor_direction_inc  (0x04 | 0x02)
#define cursor_direction_dec  (0x04 | 0x00)
#define display_shift         (0x04 | 0x01)
#define display_no_shift      (0x04 | 0x00)

#define display_on            (0x08 | 0x04)
#define display_off           (0x08 | 0x02)
#define cursor_on             (0x08 | 0x02)
#define cursor_off            (0x08 | 0x00)
#define blink_on              (0x08 | 0x01)
#define blink_off             (0x08 | 0x00)

#define _8_pin_interface      (0x20 | 0x10)
#define _4_pin_interface      (0x20 | 0x00)
#define _2_row_display        (0x20 | 0x08)
#define _1_row_display        (0x20 | 0x00)
#define _5x10_dots            (0x20 | 0x40)
#define _5x7_dots             (0x20 | 0x00)

#define DAT                   1
#define CMD                   0

#define dly                   2

void LCD_DIO_init(void);
void LCD_init(void);
void LCD_send(unsigned char value, unsigned char mode);
void LCD_4bit_send(unsigned char lcd_data);
void LCD_putstr(char *lcd_string);
void LCD_putchar(char char_data);
void LCD_clear_home(void);
void LCD_goto(unsigned char x_pos, unsigned char y_pos);
void toggle_EN_pin(void);
void toggle_io(unsigned char lcd_data, unsigned char bit_pos, unsigned int PORT, unsigned int PIN);

lcd.c

#include "lcd.h"

void LCD_DIO_init(void)
{
    GPIO_setAsOutputPin(LCD_RS_PORT, LCD_RS_PIN);
    GPIO_setAsOutputPin(LCD_EN_PORT, LCD_EN_PIN);
    GPIO_setAsOutputPin(LCD_D4_PORT, LCD_D4_PIN);
    GPIO_setAsOutputPin(LCD_D5_PORT, LCD_D5_PIN);
    GPIO_setAsOutputPin(LCD_D6_PORT, LCD_D6_PIN);
    GPIO_setAsOutputPin(LCD_D7_PORT, LCD_D7_PIN);

    _delay_cycles(100);
}

void LCD_init(void)
{
    LCD_DIO_init();

    LCD_RS_LOW;
    delay_ms(dly);
    toggle_EN_pin();

    LCD_send(0x33, CMD);
    LCD_send(0x32, CMD);

    LCD_send((_4_pin_interface | _2_row_display | _5x7_dots), CMD);
    LCD_send((display_on | cursor_off | blink_off), CMD);
    LCD_send((clear_display), CMD);
    LCD_send((cursor_direction_inc | display_no_shift), CMD);
}

void LCD_send(unsigned char value, unsigned char type)

{
    switch(type)
    {
      case DAT:
      {
          LCD_RS_HIGH;
         break;
      }
      default:
      {
         LCD_RS_LOW;
         break;
      }
    }

    LCD_4bit_send(value);
}

void LCD_4bit_send(unsigned char lcd_data)
{
    toggle_io(lcd_data, 7, LCD_D7_PORT, LCD_D7_PIN);
    toggle_io(lcd_data, 6, LCD_D6_PORT, LCD_D6_PIN);
    toggle_io(lcd_data, 5, LCD_D5_PORT, LCD_D5_PIN);
    toggle_io(lcd_data, 4, LCD_D4_PORT, LCD_D4_PIN);

    toggle_EN_pin();

    toggle_io(lcd_data, 3, LCD_D7_PORT, LCD_D7_PIN);
    toggle_io(lcd_data, 2, LCD_D6_PORT, LCD_D6_PIN);
    toggle_io(lcd_data, 1, LCD_D5_PORT, LCD_D5_PIN);
    toggle_io(lcd_data, 0, LCD_D4_PORT, LCD_D4_PIN);

    toggle_EN_pin();
}

void LCD_putstr(char *lcd_string)
{
    do
    {
        LCD_send(*lcd_string++, DAT);
    }while(*lcd_string != '\0');
}

void LCD_putchar(char char_data)
{
    LCD_send(char_data, DAT);
}

void LCD_clear_home(void)
{
    LCD_send(clear_display, CMD);
    LCD_send(goto_home, CMD);
}

void LCD_goto(unsigned char x_pos, unsigned char y_pos)
{
    if(y_pos == 0)
    {
        LCD_send((0x80 | x_pos), CMD);
    }
    else
    {
        LCD_send((0x80 | 0x40 | x_pos), CMD);
    }
}

void toggle_EN_pin(void)
{
    LCD_EN_HIGH;
    delay_ms(dly);
    LCD_EN_LOW;
    delay_ms(dly);
}

void toggle_io(unsigned char lcd_data, unsigned char bit_pos, unsigned int PORT, unsigned int PIN)
{
    unsigned char temp = 0x00;

    temp = (0x01 & (lcd_data >> bit_pos));

    switch(temp)
    {
        case 0:
        {
            GPIO_setOutputLowOnPin(PORT, PIN);
            break;
        }

        default:
        {
            GPIO_setOutputHighOnPin(PORT, PIN);
            break;
        }
    }
}

main.c

#include "driverlib.h"
#include "delay.h"
#include "lcd.h"

void clock_init(void);
void show_value(unsigned char value);

void main(void)
{
    unsigned char s = 0x00;

    char txt1[] = {"MICROARENA"};
    char txt2[] = {"SShahryiar"};
    char txt3[] = {"MSP430F5529LP"};
    char txt4[] = {"Launchpad!"};

    WDT_A_hold(WDT_A_BASE);

    clock_init();

    LCD_init();

    LCD_clear_home();

    LCD_goto(3, 0);
    LCD_putstr(txt1);
    LCD_goto(3, 1);
    LCD_putstr(txt2);
    delay_ms(4000);

    LCD_clear_home();

    for(s = 0; s < 13; s++)
    {
        LCD_goto((1 + s), 0);
        LCD_putchar(txt3[s]);
        delay_ms(60);
    }
    for(s = 0; s < 10; s++)
    {
        LCD_goto((3 + s), 1);
        LCD_putchar(txt4[s]);
        delay_ms(60);
    }
    delay_ms(4000);

    s = 0;
    LCD_clear_home();

    LCD_goto(3, 0);
    LCD_putstr(txt1);

    while(1)
    {
        show_value(s);
        s++;
        delay_ms(400);
    };
}

void clock_init(void)
{
    PMM_setVCore(PMM_CORE_LEVEL_3);

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,
                                               (GPIO_PIN4 | GPIO_PIN2));

    GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5,
                                                (GPIO_PIN5 | GPIO_PIN3));

    UCS_setExternalClockSource(XT1_FREQ,
                               XT2_FREQ);

    UCS_turnOnXT2(UCS_XT2_DRIVE_4MHZ_8MHZ);

    UCS_turnOnLFXT1(UCS_XT1_DRIVE_0,
                    UCS_XCAP_3);

    UCS_initClockSignal(UCS_FLLREF,
                        UCS_XT2CLK_SELECT,
                        UCS_CLOCK_DIVIDER_4);

    UCS_initFLLSettle(MCLK_KHZ,
                      MCLK_FLLREF_RATIO);

    UCS_initClockSignal(UCS_SMCLK,
                        UCS_REFOCLK_SELECT,
                        UCS_CLOCK_DIVIDER_1);

    UCS_initClockSignal(UCS_ACLK,
                        UCS_XT1CLK_SELECT,
                        UCS_CLOCK_DIVIDER_1);
}

void show_value(unsigned char value)
{
   unsigned char ch = 0x00;

   ch = ((value / 100) + 0x30);
   LCD_goto(6, 1);
   LCD_putchar(ch);

   ch = (((value / 10) % 10) + 0x30);
   LCD_goto(7, 1);
   LCD_putchar(ch);

   ch = ((value % 10) + 0x30);
   LCD_goto(8, 1);
   LCD_putchar(ch);
}

Hardware Setup

Explanation

Since alphanumerical displays utilize GPIOs, there’s hardly anything to explain here. Try not to use GPIOs that have alternate roles because you may never know when would you need their alternative functions.

Most popularly alphanumerical LCDs are driven in 4-bit data mode and this is what that has been done here. First let’s see some typical LCD instructions.
Since alphanumerical displays utilize GPIOs, there’s hardly anything to explain here. Try not to use GPIOs that have alternate roles because you may never know when would you need their alternative functions.

Most popularly alphanumerical LCDs are driven in 4-bit data mode and this is what that has been done here. First let’s see some typical LCD instructions.

#define LCD_RS_PORT           GPIO_PORT_P8
#define LCD_RS_PIN            GPIO_PIN1

#define LCD_EN_PORT           GPIO_PORT_P8
#define LCD_EN_PIN            GPIO_PIN2

#define LCD_D4_PORT           GPIO_PORT_P2
#define LCD_D4_PIN            GPIO_PIN3

#define LCD_D5_PORT           GPIO_PORT_P3
#define LCD_D5_PIN            GPIO_PIN7

#define LCD_D6_PORT           GPIO_PORT_P2
#define LCD_D6_PIN            GPIO_PIN6

#define LCD_D7_PORT           GPIO_PORT_P4
#define LCD_D7_PIN            GPIO_PIN0

This declaration in the LCD header file is very important as it states GPIO pin purposes.

Initialization of the LCD is done by going through the steps in the following program flow graph and manipulating the LCD pins accordingly.

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

Related Posts

10 comments

Leave a Reply to Surya k s Cancel reply

Your email address will not be published. Required fields are marked *