Wednesday 19 October 2011

Interfacing 16x2 LCD to 8051 core mcu

How would you interface devices to a microcontroller?
The most important thing is "Data sheet" of the device.And it is not mandatory to read each and every part of data sheet,reading data sheet is also an art form.You must know your expectation of  devices specifications, read through data sheet of the device and make sure it satisfies that to some extend.Electrical specification will matter a lot when you are interfacing a device.You must know the power rating,input voltage supply,input current etc.,
We know that lot of  16x2 LCDs are available in the market so it is your job to find out the data sheet of the LCD your interfacing.But the instruction sets for almost all LCD are same,so below i've put instruction set.



Here is a program for interfacing t JHD162A LCD to 8051 core mcu:

 HEADER FILE


 #ifndef __LCD_H__
#define __LCD_H__

void lcd_init();
void lcd_printc(char a_char);  //print a character
void lcd_prints(char *string);  //print a string
void lcd_clear_row(char row);
void lcd_clear(void);
void lcd_cursor_on(void);
void lcd_cursor_off(void);
void lcd_move_cursor(unsigned char row, unsigned char column);
void lcd_move_cursor_left(void);
void lcd_move_cursor_right(void);


#endif

LIBRARY FILE
#include <reg51.h>
#define LCD_RS P2_4
#define LCD_WR P2_5
#define LCD_EN P2_6

#define LCD_MAX_ROWS    2
#define LCD_MAX_COLS    16

#define LCD_CMD_CLEAR        0x01
#define LCD_CMD_CURSOR_HOME    0x02
#define LCD_CMD_CURSOR_LEFT    0x10
#define LCD_CMD_CURSOR_RIGHT     0x14
#define LCD_CMD_CURSOR_ON    0x0D
#define LCD_CMD_CURSOR_OFF    0x0C

static void lcd_write_data(unsigned char val);
static void lcd_write_cmd(unsigned char cmd);
static char lcd_is_busy();



void lcd_init(void)
{
    delay(15);        // LCD Power on delay
    lcd_write_cmd(0x38);    // 8BIT, 2 LINE , 5x7 DOT Format font
    delay(5);        // Small dealy
    lcd_write_cmd(0x38);
    lcd_write_cmd(0x06);
    lcd_write_cmd(0x0c);
    lcd_write_cmd(0x0E);
    lcd_clear();        // Display ON, Cursor ON, Blink OFF

    lcd_cursor_off();
    lcd_clear();
}

void lcd_printc(char a_char)
{
    lcd_write_data(a_char);
}

void lcd_prints(char *string)
{
    while (*string)
        lcd_printc(*string++);
}

void lcd_clear(void)
{
    lcd_write_cmd(LCD_CMD_CLEAR);
}

void lcd_clear_row(char row)
{
    char i = 0;
    lcd_move_cursor(row, 0);
    while (i < LCD_MAX_COLS) {
        lcd_printc(' ');
        i++;
    }
    lcd_move_cursor(row, 0);
}

void lcd_cursor_on(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_ON);
}

void lcd_cursor_off(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_OFF);
}

void lcd_move_cursor(unsigned char row, unsigned char column)
{
    switch (row) {
    case 0:
        lcd_write_cmd(0x80 + column);
        break;
    case 1:
        lcd_write_cmd(0xc0 + column);
        break;
    default:
        break;
    }
}

void lcd_move_cursor_left(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_LEFT);
}

void lcd_move_cursor_right(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_RIGHT);
}

void lcd_move_cursor_home(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_HOME);
}

static char lcd_is_busy()
{
    unsigned char c;
    P0 = 0xFF;
    LCD_WR = 1;
    LCD_RS = 0;        // SET LCD_RS for DATA
    LCD_EN = 0;
    LCD_EN = 1;        // Pulse LCD_EN
    c = P0;
    LCD_EN = 0;
    return c & 0x80;    // return busy bit
}

static void lcd_write_cmd(unsigned char cmd)
{
    while (lcd_is_busy());
    LCD_WR = 0;
    LCD_EN = 0;
    LCD_RS = 0;        // Reset LCD_RS for Command
    P0 = cmd;
    LCD_EN = 1;        // Pulse LCD_EN
    LCD_EN = 0;
    LCD_WR = 1;
}

static void lcd_write_data(unsigned char val)
{
    while (lcd_is_busy());
    LCD_WR = 0;
    LCD_EN = 0;
    LCD_RS = 1;        // SET LCD_RS for DATA
    P0 = val;
    LCD_EN = 1;        // Pulse LCD_EN
    LCD_EN = 0;
    LCD_WR = 1;
}




Tuesday 4 October 2011

Keil tutorials

this video helps you to create a new project and debug,if any error encounters.