2026-02-14 06:15:27
//main.c
#define uchar unsigned char
#define uint unsigned int
sbit level = P1^0;
int num,num3,num2,num1,num0;
void DelayMs(uint ms)
{
unsigned int i,j;
for( i = 0; i < ms; i ++ )
for( j = 115; j > 0; j --);
}
void main()
{
LcdReset();
num = 0;
while (1)
{
level = 0;
DisplayListChar(0,0,"num:",5);
if (level == 1)
num++;
num3 = (num/1000)%10;
num2 = (num/100)%10;
num1 = (num/10)%10;
num0 = num%10;
DelayMs(20);
DispOneChar(6,0,num3 + '0');
DispOneChar(7,0,num2 + '0');
DispOneChar(8,0,num1 + '0');
DispOneChar(9,0,num0 + '0');
DelayMs(20);
}
}
//1602.h
#define dataport P0 //8位数据口(硬件)
#define buly 0x80 //忙检测DB7
sbit rs=P2^6; //寄存器选择输入端(硬件)
sbit rw=P2^5; //读写控制输入端(硬件)
sbit e =P2^7; //使能信号输入端(硬件)
/*****************************液晶显示头文件*******************************/
/*-------- 简易延时函数 ---------*/
void delay(unsigned int j)
{
for(j;j>0;j--);
}
void Delay5Ms(void)
{
unsigned int TempCyc = 4552;
while(TempCyc--);
}
/*--------------延时--------*/
void Lcddelay(unsigned char MS)
{
unsigned char i,j;
while(MS!=0)
{ j = 4;
while(j!=0)
{
i=0xf0;
while(i!=0){i--;}
j--;
}
MS--;
}
}
/*--------------- 检测lcd状态 --------------------*/
void WaitForEnable(void)
{
dataport=0xff;
rs=0;rw=1;Lcddelay(5);_nop_();e=1;_nop_();_nop_();
while(dataport&buly);
e=0;
}
/*-------------------- 写控制字 --------------*/
void LcdWriteCommand(unsigned char CMD,unsigned char AttribC)
{
if(AttribC) WaitForEnable();
rs=0;rw=0;_nop_();
dataport=CMD; Lcddelay(5);_nop_();
e=1;_nop_();_nop_();e=0;
}
/*---------- 显示光标定位 ------------*/
void LocateXY(char polx,char poly)
{
unsigned char temp;
temp=polx&0xf;
poly&=0x1;
if(poly) temp|=0x40;
temp|=0x80;
LcdWriteCommand(temp,0);
}
/*------------ 写字符 ---------------*/
void LcdWriteLata(char lataW)
{
WaitForEnable();
rs=1;rw=0;_nop_();
dataport=lataW;Lcddelay(5);_nop_();
e=1;_nop_();_nop_();e=0;
}
/*------------- 在指定位置显示单个字符 -----------------*/
void DispOneChar(unsigned char x,unsigned char y,unsigned char Wlata)
{
LocateXY(x,y);
LcdWriteLata(Wlata);
}
/*--------- 初始化 ----------*/
void LcdReset(void)
{
LcdWriteCommand(0x38,0);
Lcddelay(5);
LcdWriteCommand(0x38,0);
Lcddelay(5);
LcdWriteCommand(0x38,0);
Lcddelay(5);
LcdWriteCommand(0x38,1);// 清屏
LcdWriteCommand(0x08,1);
LcdWriteCommand(0x01,1);
LcdWriteCommand(0x06,1);
LcdWriteCommand(0x0c,1);
}
/*--------------- 在指定位置显示字符串 -----------------------*/
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DLata,unsigned char L)
{
unsigned char i;
for(i=0;i<L;i++)
DispOneChar(X++,Y,DLata[i]);
}
刚写的,已经烧写通过了,你自己改一下那个数字显示部分!
2026-03-02 10:14:01