file:///C:UserschenluAppDataLocalTempksohtmlwps1945.tmp.jpg#include <reg52.h> #define uchar unsigned char#define uint unsigned intsbit lcden=P2^1;sbit lcdrs=P2^0;sbit lcdrw=P2^2;
sbit adwr=P3^7;//sbit adcs=P3^0; //oe ==gndsbit eoc=P2^3;
uchar code table[]="the illus
tion is";uchar code table1[]="lx";uint num,a,d,qian,bai,shi,ge,num1;uint illusion,temp;void init(void); //初始化函数void delay(uint a); //延时函数void write_com(uchar com); //写指令函数void write_data(uchar date); //写数据函数 void display(uint d); //显示函数
void main(){ void init(); //初始化 EA=1; //开总中断 ET0=1; //开定时器中断 TR0=1; //启动定时器
lcden=0;
/***********对1602液晶的模式进行设置***************/ write_com(0x38); //设置16*2显示,5*7点阵,8位数据接口 write_com(0x0c); //设置开显示,不显示光标 write_com(0x06); //写一个字符后地址指针加一 write_com(0x01); //显示清零,数据指针清零
while(1) {/*******开始不断扫描P1引脚是否有信号,并且对信号进行计算和显示********/ if(P1!=0) { delay(10); if(P1!=0)//防止误判 { adwr=0; delay(5); adwr=1; delay(5); adwr=0; while(eoc); for(a=10;a>0;a--) {temp=illusion*4; temp=1000; display(temp);//显示照度 }};};}}/**********************初始化程序************************/void init(void){ TMOD=0x01; //定时器0工作于计数方式1 TH0=(65536-5000)/256; TL0=(65536-5000)%256; //定时时间为50ms num=0;// adcs=0;// sign=0;// temp=0;write_com(0x80);}/*************延时函数每次延时50ms********************/void delay(uint a){ uint b,c; for(b=a;b>0;b--) for(c=110;c>0;c--);}/************定时器中断函数*****************/void to_time() interrupt 1{ TH0=(65536-5000)/256; //中断函数里重新赋初值 TL0=(65536-5000)%256; num++; if(num==20) //每20*50ms计算一次,并且将速度显示 { num=0; //标志位 illusion=P1; }}/***********写指令函数************/void write_com(uchar com){ lcdrs=0; lcdrw=0; P0=com; //delay(5); lcden=1; delay(20); lcden=0;}/*********写数据函数*************/void write_data(uchar date){ lcdrs=1; //delay(5); lcdrw=0; P0=date;// delay(5); lcden=1; delay(20); lcden=0;}/*************显示函数********************/void display(uint illusion) { qian=illusion/1000; bai=illusion%1000/100; shi=illusion%100/10; ge=illusion%10; //速度范围为0至9999. for(num1=0;num1<20;num1++) //显示前面一段字符 { write_data(table[num1]); delay(20); };
write_com(0x80+0x41); //显示光照强度 write_data(0x30+qian); write_com(0x80+0x42); write_data(0x30+bai);
write_com(0x80+0x43); write_data(0x30+shi); write_com(0x80+0x44); write_data(0x30+ge);
write_com(0x80+0x47); for(num1=0;num1<2;num1++) { write_data(table1[num1]); delay(20); } write_com(0x80); }
-
#define LCD1602_DB P0 //data bus 数据总线
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;
void InitLcd1602();
void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str);
void main()
{
unsigned char str[] = "Welcome To Lcd";
InitLcd1602();
LcdShowStr(0,0,str);
while(1);
}
void Read_Busy() //忙检测函数,判断bit7是0,允许执行;1禁止
{
unsigned char sta; //
LCD1602_DB = 0xff;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_EN = 1;
sta = LCD1602_DB;
LCD1602_EN = 0; //使能,用完就拉低,释放总线
}while(sta & 0x80);
}
void Lcd1602_Write_Cmd(unsigned char cmd) //写命令
{
Read_Busy();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_EN = 1;
LCD1602_EN = 0;
}
void Lcd1602_Write_Data(unsigned char dat) //写数据
{
Read_Busy();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_EN = 1;
LCD1602_EN = 0;
}
void LcdSetCursor(unsigned char x,unsigned char y) //坐标显示
{
unsigned char addr;
if(y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
Lcd1602_Write_Cmd(addr|0x80);
}
void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) //显示字符串
{
LcdSetCursor(x,y); //当前字符的坐标
while(*str != ' ')
{
Lcd1602_Write_Data(*str++);
}
}
void InitLcd1602() //1602初始化
{
Lcd1602_Write_Cmd(0x38); //打开,5*8,8位数据
Lcd1602_Write_Cmd(0x0c);
Lcd1602_Write_Cmd(0x06);
Lcd1602_Write_Cmd(0x01); //清屏
}
参考一下这个,我亲自用过的
一周热门 更多>