#include <reg52.h>
typedef unsigned char uchar8;
typedef unsigned int uint16;
uchar8 code TAB1[] = "l do what l do";
uchar8 code TAB2[] = "1711";
sbit RS = P3^5; //数据or命令选择端(H/L)
sbit RW = P3^4; //读/写选择端(H/L)
sbit EN = P3^3; //使能信号(电平)
sbit BIT_ = P1^6;
sbit SEG_ = P1^7;
void DelayMS(uint16 val)
{
uint16 i, j;
for(i = 0; i < val; i++)
for(j = 0; j < 113; j++);
}
/*************************************
*函数名称:DectectBusyBit()
*函数功能:检测状态标志位(忙or闲)
***************************************/
void DectectBusyBit(void)
{
P0 = 0xff; //读取状态值时,先赋高电平
RS = 0;
RW = 1;
EN = 1;
DelayMS(1);
while(P0 & 0x80); //若LCD忙,则停止在这里,否则走起
EN = 0; //之后将EN初始化为低电平
}
/****************************************
*函数名称:WrComLCD()
*函数功能:为LCD写指令
*入口参数:指令(ComVal)
*****************************************/
void WrComLCD(uchar8 ComVal)
{
DectectBusyBit();
RS = 0;
RW = 0;
EN = 1;
P0 = ComVal;
DelayMS(1);
EN = 0;
}
/*******************************************
*函数名称:WrDatLCD()
*函数功能:为LCD写数据
*入口参数: 数据(DatVal)
********************************************/
void WrDatLCD(uchar8 DatVal)
{
DectectBusyBit();
RS = 1;
RW = 0;
EN = 1;
P0 = DatVal;
DelayMS(1);
EN = 0;
}
/******************************************
*函数名称:LCD_Init()
*函数功能:初始化LCD
*******************************************/
void LCD_Init(void)
{
WrComLCD(0x38);
DelayMS(1);
WrComLCD(0x38);
WrComLCD(0x01);
WrComLCD(0x06);
DelayMS(1);
WrComLCD(0x0c);
}
void main(void)
{
uchar8 ucval;
SEG_ = 1;
P0 = 0x00;
SEG_ = 0;
BIT_ = 1;
P0 = 0xff;
BIT_ = 0;
LCD_Init();
DelayMS(5);
WrComLCD(0x80);
while(TAB1[ucval] != '\0')
{
WrDatLCD(TAB1[ucval]);
ucval++;
}
ucval = 0;
WrComLCD(0xc0);
while(TAB2[ucval] != '\0')
{
WrDatLCD(TAB2[ucval]);
ucval++;
}
while(1);
}
|