|
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit LCD_RS= P2^6;//定义LCD引脚
- sbit LCD_RW= P2^5;
- sbit LCD_E= P2^7;
- uchar codeDIS[]={"NE555-------TEST"};//NE555 测试;
- uchar codedsptab[]={'0','1','2','3','4','5','6','7','8','9'};//字符代表码;
- uchari,c[4],a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uint s;
- /*====================延时子程序====================*/
- void delay(uint z)
- {
- uchar m;
- for(;z>0;z--)
- for(m=124;m>0;m--);
- }
- void delay400ms(void)////400ms延时
- { uchar z = 5;
- uintm;
- while(z--)
- {m=7269;
- while(m--);
- }
- }
- /*====================LCD1602子程序====================*/
- /*-- 读状态 --*/
- //读状态子程序;有返回值;返回值类型为CHAR型;
- //读回的状态通过RETURN返回;
- uchar read_estate(void) //定义有返回值的函数;
- { P0=0Xff; //把LCD端口全置1方便读取信号;
- LCD_RS=0; //RS置0;
- LCD_RW=1; //RW置1;
- LCD_E=0; //E端置0;
- delay(10); //短延时;
- LCD_E=1; //E端置1;以锁存数据;
- while(P0&0x80); //检测忙,则一直循环;
- return(P0); //返回读取的信号;
- }
- /*-- 写数据 --*/
- //写数据子程序;无返回值;输入变量I;
- //I为要写入LCD中的数据;
- //数据类型CHAR形;
- void write_data(uchar i) //定义输入变量值I;
- {
- read_estate(); //检测忙信号;
- P0=i; //把I中数据送到LCD数据端;
- LCD_RS=1; //RS置1;
- LCD_RW=0; //RW置0;
- LCD_E=0; //E置0;
- delay(10); //短延时;
- LCD_E=1; //E置1;以锁存数据;
- }
- /*-- 写指令 --*/
- //写指令子程序;无返回值;输入二个变量I和J.
- //I为要写入LCD的指令;J为判断要不要检测忙.
- //如果J为0则不判断检测忙;
- //如果J为1则判断检测忙;
- void write_dictate(uchar i,j) //定义二个变量;
- {
- if(j)read_estate(); //根据需要检测忙;
- P0=i; //把要写入的数据送到LCD数据端;
- LCD_RS=0; //RS置0;
- LCD_RW=0; //RW置0;
- LCD_E =1; //E端置0;
- delay(10); //延时;
- LCD_E =0; //E端置1;以锁存数据;
- }
- /*-- LCD初始化 --*/
- //LCD初始化程序;主要作用初始化LCD,对LCD进行复位以及设置;
- void initialization(void) //定义函数;
- {
- delay(50); //延时5MS;
- write_dictate(0x38,0); //写指令38H;不检测忙;
- delay(50); //延时5MS;
- write_dictate(0x38,0); //写指令38H;不检测忙;
- delay(50); //延时5MS;
- write_dictate(0x38,0); //写指令38H;不检测忙;
- delay(50);
- write_dictate(0x38,1); //显示模式设置;检测忙;
- write_dictate(0x08,1); //关闭显示;检测忙;
- write_dictate(0x01,1); //显示清屏;检测忙;
- write_dictate(0x06,1); //显示光标移动设置;检测忙;
- write_dictate(0x0C,1); //显示开及光标设置;检测忙;
- }
- /*-- 在指定位置显示一个字符 --*/
- void displayonechar(uchar x, y,ddata)
- {
- y &= 0x01;
- x &= 0x0f; //限制X不能大于15,Y不能大于1
- if (y) x+= 0x40; //当要显示第二行时地址码+0x40;
- x+= 0x80; //算出指令码
- write_dictate(x,0); //这里不检测忙信号,发送地址码
- write_data(ddata);
- }
- /*-- 在指定位置显示一串字符 --*/
- void displaylistchar(uchar x,y,uchar code*ddata)
- {uchar a=0;
- y&=0x01;
- x&=0xf;
- while(ddata[a]>0x20)
- {if(x<=0xff)
- {
- displayonechar(x, y,ddata[a]);
- a++;
- x++;
- }
- }
- }
- /*====================MAIN主程序====================*/
- void main()
- {
- delay400ms();//延时400MS
- initialization();//LCD复位;
- displaylistchar(0,0,DIS);//显示NE555 测试;
- displayonechar(0,1,'F'); //在LCD是显示F=____Hz
- displayonechar(1,1,'=');
- displayonechar(9,1,'H');
- displayonechar(10,1,'z');
- TMOD=0X51; //定义T0为方式一(十六位进制),T1为计数器(16位)
- TH0=0X3C; //50 ms
- TL0=0XB0;
- TH1=0X00; //初始值
- TL1=0X00;
- TR0=1;
- TR1=1;
- ET0=1;
- ET1=0;
- EA=1;
- i=0;
- /* 显示 */ // 调用显示子程序(显示位,显示行,显示数据)注:显示行 “0”为第一行,“1为第二行”
- while(1)
- {
- if(s/10000>=1)
- {
- displayonechar(2,1,dsptab[s/10000]);//万
- displayonechar(3,1,dsptab[s/1000%10]);//千
- displayonechar(4,1,'.');//点
- displayonechar(5,1,dsptab[s/100%10]);//百
- displayonechar(6,1,dsptab[s/10%10]);//十
- displayonechar(7,1,dsptab[s%10]); //个
- displayonechar(8,1,'K');
- }
- else if(s/1000>=1)
- {
- displayonechar(2,1,' ');
- displayonechar(3,1,dsptab[s/1000%10]);//千
- displayonechar(4,1,'.'); //点
- displayonechar(5,1,dsptab[s/100%10]);//百
- displayonechar(6,1,dsptab[s/10%10]); //十
- displayonechar(7,1,dsptab[s%10]); //个
- displayonechar(8,1,'K');
- }
- else if(s/100>=1)
- {
- displayonechar(2,1,' ');
- displayonechar(3,1,' ');
- displayonechar(4,1,' '); //万
- displayonechar(5,1,' '); //千
- displayonechar(6,1,dsptab[s/100%10]);//百
- displayonechar(7,1,dsptab[s/10%10]); //十
- displayonechar(8,1,dsptab[s%10]); //个
- }
- else if(s/10>=1)
- {
- displayonechar(2,1,'');
- displayonechar(3,1,'');
- displayonechar(4,1,''); //万
- displayonechar(5,1,''); //千
- displayonechar(6,1,' ');//百
- displayonechar(7,1,dsptab[s/10%10]); //十
- displayonechar(8,1,dsptab[s%10]); //个
- }
- else
- {
- displayonechar(2,1,'');
- displayonechar(3,1,'');
- displayonechar(4,1,''); //万
- displayonechar(5,1,''); //千
- displayonechar(6,1,'');//百
- displayonechar(7,1,dsptab[s/10%10]);//十
- displayonechar(8,1,dsptab[s%10]); //个
- }
- }
- }
- /* 中断 */
- void t0()interrupt 1
- {
- TH0=0X3C;
- TL0=0XB0;
- i++;
- if(i==20) //1 s时间到时
- {
- s=TH1; //555信号发生器的输出口接P3.5(T1) 15脚
- s=s<<8;
- s=s+TL1;
- i=0; //清零
- TH1=0X00;
- TL1=0X00;
- }
- }
- /*----------------------------------------------------------------*/
- /*----------------- 1602液晶数据读取子程序 -----------------------*/
- //读数据子程序;有返回值,返回值类型为CHAR型;
- /*uchar read_data(void) //定义有返回值的子函数;
- {
- LCD_DATA=0Xff; //LCD数据端口置1;
- LCD_RS=1; //RS置1;
- LCD_RW=1; //RW置1;
- LCD_E=0; //E置0;
- delay(10); //短延时;
- LCD_E=1; //E置1;以锁存数据;
- return(LCD_DATA); //返回读取的值;
- }
- /*----------------------------------------------------------------*/
复制代码
|
-
-
测试系统.zip
36.36 KB, 下载次数: 13, 下载积分: 黑币 -5
Proteus仿真图
|