目標(biāo):LCD1602的編碼
- lcd.c
#include "lcd.h"
void Lcd1602_Delay1ms(uint c) //延時(shí)'c'ms
{
uchar a,b;
for(;c>0;c--){
for(b=199;b>0;b--){
for(a=1;a>0;a--);
}
}
}
#ifndef LCD1602_4PINS //如果定義的不是四位的話
void LcdWriteCom(uchar com){ //傳輸命令的函數(shù)
LCD1602_E = 0; //使能端為低,可以進(jìn)行數(shù)據(jù)改變轧抗,在這時(shí)把數(shù)據(jù)放如數(shù)據(jù)端口(DATAPINS)
LCD1602_RS = 0; //命令
LCD1602_RW = 0; //寫入
LCD1602_DATAPINS = com; //數(shù)據(jù)放入數(shù)據(jù)端
Lcd1602_Delay1ms(1); //延遲1ms,也可以不延遲
LCD1602_E = 1; //使能端變高,可以把數(shù)據(jù)輸送到液晶
Lcd1602_Delay1ms(5); //給數(shù)據(jù)傳輸?shù)揭壕У臅r(shí)間
LCD1602_E = 0; //為下次傳輸時(shí)間做準(zhǔn)備
}
#else
void LcdWriteCom(uchar com)
{
LCD1602_E = 0;
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DATAPINS = com; //由于4位的接線是接到P0口的高四位妆够,所以傳送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
//Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //發(fā)送低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
#ifndef LCD1602_4PINS //如果定義的不是四位的話
void LcdWriteData(uchar dat) //傳輸數(shù)據(jù)的函數(shù)
{
LCD1602_E = 0;
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
// Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //發(fā)送低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
#ifndef LCD1602_4PINS
void LcdInit()
{
LcdWriteCom(0x38); //開顯示
LcdWriteCom(0x0c); //開顯示不顯示光標(biāo)
LcdWriteCom(0x06); //寫一個(gè)指針加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針的起點(diǎn)
}
#else
void LcdInit()
{
LcdWriteCom(0x32); //將8位總線轉(zhuǎn)為4位總線
LcdWriteCom(0x28); //在四位線下的初始化
LcdWriteCom(0x0c); //開顯示不顯示光標(biāo)
LcdWriteCom(0x06); //寫一個(gè)指針加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針的起點(diǎn)
}
#endif
- main.c
#include "reg52.h"
#include "lcd.h"
typedef unsigned int u16;
typedef unsigned char u8;
u8 Disp1[] = " KAI TUO ZHE V2 "; //第一排要顯示的字符
u8 Disp2[] = " Hello World! "; //第二排要顯示的字符
void main()
{
u8 i;
LcdInit();
for(i=0;i<16;i++){
LcdWriteData(Disp1[i]); //從指針的起始位置開始傳輸,自動(dòng)加1
}
LcdWriteCom(0x80+0x40); //第二排起始地址
for(i=0;i<16;i++){
LcdWriteData(Disp2[i]);
}
while(1); //數(shù)據(jù)已經(jīng)傳輸過去了丸凭,可以一直顯示枯冈,就保持在這就行了
}
- lcd.h
#ifndef _LCD_H_
#define _LCD_H_
//#define LCD1602_4PINs //當(dāng)使用的四位數(shù)據(jù)傳輸?shù)臅r(shí)候定義,使用8位時(shí)注釋此句話
#include<reg52.h>
#ifndef uint
#define uint unsigned int
#endif
#ifndef uchar
#define uchar unsigned char
#endif
//PIN口定義
#define LCD1602_DATAPINS P0
sbit LCD1602_E = P2^7; //使能端
sbit LCD1602_RW = P2^5; //選擇寫還是讀
sbit LCD1602_RS = P2^6; //命令數(shù)據(jù)選擇端
//函數(shù)定義
void Lcd1602_Delay1ms(uint c); //延時(shí)'c'ms 4
void LcdWriteCom(uchar com); ///*LCD1602寫入8位命令子函數(shù)*/
void LcdWriteData(uchar dat); /*LCD1602寫入8位數(shù)據(jù)子函數(shù)*/
void LcdInit(); /*LCD1602初始化子程序*/
#endif