0% found this document useful (0 votes)
4 views4 pages

Thư Viện Lcd

The document contains C code for interfacing with an LCD display using a microcontroller. It defines various control commands and functions for initializing the LCD, sending data, and managing display settings. Additionally, it includes delay functions and a union structure for handling register bits and bytes.

Uploaded by

nguyenb2104438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Thư Viện Lcd

The document contains C code for interfacing with an LCD display using a microcontroller. It defines various control commands and functions for initializing the LCD, sending data, and managing display settings. Additionally, it includes delay functions and a union structure for handling register bits and bytes.

Uploaded by

nguyenb2104438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "intrinsics.

h"
#include "string.h"
#include "stdio.h"
union reg {
struct bit {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}_BIT;
unsigned char _BYTE;
};
union reg* P2_DIR=(union reg*)0x2a; //khai bao dia chi o nho
union reg* P2_OUT=(union reg*)0x29;
union reg* P2_SEL=(union reg*)0x2e;
union reg* P2_IN=(union reg*)0x28;

union reg* P1_SEL=(union reg*)0x26;


union reg* P1_DIR=(union reg*)0x22; //khai bao dia chi o nho
union reg* P1_OUT=(union reg*)0x21;
union reg* P1_IN=(union reg*)0x20;
//
===================================================================================
===================================
//==============================================================================
//DINH NGHIA LCD:
#define MCLK_F 1 // frequency of Master Clock in MHz
/****************************************************************************
* HARD DEFINITIONS
******************************************************************************/
/* Display ON/OFF Control definitions */
#define DON 0x0F /* Display on */
#define DOFF 0x0B /* Display off */
#define CURSOR_ON 0x0F /* Cursor on */
#define CURSOR_OFF 0x0D /* Cursor off */
#define BLINK_ON 0x0F /* Cursor Blink */
#define BLINK_OFF 0x0E /* Cursor No Blink */

/* Cursor or Display Shift definitions */


#define SHIFT_CUR_LEFT 0x04 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0x05 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0x06 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0x07 /* Display shifts to the right */

/* Function Set definitions */


#define FOUR_BIT 0x2C /* 4-bit Interface */
#define EIGHT_BIT 0x3C /* 8-bit Interface */
#define LINE_5X7 0x30 /* 5x7 characters, single line */
#define LINE_5X10 0x34 /* 5x10 characters */
#define LINES_5X7 0x38 /* 5x7 characters, multiple line */
//==============================================================
#define LCD_RS P2_OUT -> _BIT.b2
#define LCD_RS_DIR P2_DIR -> _BIT.b2
#define LCD_EN P2_OUT -> _BIT.b3
#define LCD_EN_DIR P2_DIR -> _BIT.b3
#define LCD_DATA_4 P2_OUT -> _BIT.b4
#define LCD_DATA_4_DIR P2_DIR -> _BIT.b4
#define LCD_DATA_5 P2_OUT -> _BIT.b5
#define LCD_DATA_5_DIR P2_DIR -> _BIT.b5
#define LCD_DATA_6 P2_OUT -> _BIT.b6
#define LCD_DATA_6_DIR P2_DIR -> _BIT.b6
#define LCD_DATA_7 P2_OUT -> _BIT.b7
#define LCD_DATA_7_DIR P2_DIR-> _BIT.b7
//==============================================================================
//CHUONG TRINH DELAY:
void delay_ms(unsigned int ms){
while(ms--){
__delay_cycles(1000);
}
}

unsigned int value;


//================================
void lcd_delay_us (unsigned long t)
{
int i;
for (i = 0; i<t; i++ )
__delay_cycles(MCLK_F);
}
void lcd_delay_ms (unsigned long t)
{
int i;
for (i = 0; i<t; i++ )
__delay_cycles(MCLK_F*1000);
}
//==============================================================================
//THU VIEN LCD 1602:
void lcd_put_byte(unsigned char rs, unsigned char data)
{
LCD_RS = 0;
if(rs) LCD_RS = 1;
lcd_delay_us(20);
LCD_EN = 0;

// send the high nibble


if (data&BIT4) LCD_DATA_4 = 1;
else LCD_DATA_4 = 0;
if (data&BIT5) LCD_DATA_5 = 1;
else LCD_DATA_5 = 0;
if (data&BIT6) LCD_DATA_6 = 1;
else LCD_DATA_6 = 0;
if (data&BIT7) LCD_DATA_7 = 1;
else LCD_DATA_7 = 0;

lcd_delay_us(20);
LCD_EN = 1;
lcd_delay_us(20);
LCD_EN = 0;

// send the low nibble


if (data&BIT0) LCD_DATA_4 = 1;
else LCD_DATA_4 = 0;
if (data&BIT1) LCD_DATA_5 = 1;
else LCD_DATA_5 = 0;
if (data&BIT2) LCD_DATA_6 = 1;
else LCD_DATA_6 = 0;
if (data&BIT3) LCD_DATA_7 = 1;
else LCD_DATA_7 = 0;

lcd_delay_us(20);
LCD_EN = 1;
lcd_delay_us(20);
LCD_EN = 0;
}
void lcd_init(void)
{
LCD_RS_DIR = 1;
LCD_EN_DIR = 1;
LCD_DATA_4_DIR = 1;
LCD_DATA_5_DIR = 1;
LCD_DATA_6_DIR = 1;
LCD_DATA_7_DIR = 1;
LCD_RS = 0;
LCD_EN = 0;
lcd_delay_ms(200);

lcd_put_byte(0,0x30);
lcd_delay_ms(50);
lcd_put_byte(0,0x30);
lcd_delay_ms(50);
lcd_put_byte(0,0x32);
lcd_delay_ms(200);

lcd_delay_ms(2); // wait for LCD


lcd_put_byte(0,FOUR_BIT & LINES_5X7); // Set LCD type
lcd_delay_ms(2); // wait for LCD

lcd_put_byte(0,DOFF&CURSOR_OFF&BLINK_OFF); // display off


lcd_delay_ms(2); // wait for LCD
lcd_put_byte(0,DON&CURSOR_OFF&BLINK_OFF); // display on
lcd_delay_ms(2); // wait for LCD

lcd_put_byte(0,0x01); // clear display and move cursor to home


lcd_delay_ms(2); // wait for LCD
lcd_put_byte(0,SHIFT_CUR_LEFT); // cursor shift mode
lcd_delay_ms(2); // wait for LCD
lcd_put_byte(0,0x01); // clear display and move cursor to home
lcd_delay_ms(2); // wait for LCD
}
void lcd_clear(void)
{
lcd_put_byte(0,0x01); // display off
lcd_delay_ms(2); // wait for LCD
}
void lcd_gotoxy(unsigned char col, unsigned char row)
{
unsigned char address;

if(row!=0)
address=0x40;
else
address=0;
address += col;
lcd_put_byte(0,0x80|address);
lcd_delay_ms(2); // wait for LCD
}
void lcd_putc(char c)
{
switch(c){
case '\f':
lcd_put_byte(0, 0x01);
lcd_delay_ms(2); // wait for LCD
break;
case '\n':
lcd_gotoxy(0, 0x01);
break;
default:
lcd_put_byte(1, c);
lcd_delay_ms(2); // wait for LCD
break;
}
}
void lcd_puts(const char* s)
{
while(*s){
lcd_putc(*s++);
__delay_cycles(50000);
}
}
void lcd_3n(unsigned char x)
{ lcd_putc((x/100)+48);
lcd_putc(((x/10)%10)+48);
lcd_putc((x%10)+48); }

You might also like