单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit rs=P2^0;
- sbit rw=P2^1;
- sbit en=P2^2;
- sbit START=P2^3;
- sbit ALE=P2^3;
- sbit CLK=P2^4;
- sbit OE=P2^5;
- sbit EOC=P2^6;
- uchar code table[]="Digital Ohmmeter";
- uchar code bby[]="(R)=";
- //uchar code abc[]={0,1,2,3,4,5,6,7,8,9};
- uchar num=0;
- uint temp,getdata;
- uchar qian, bai, shi, ge;
- void delay(uint z)
- {
- uint x,y;
- for(z=x;x>0;x--)
- for(y=110;y>0;y--);
- }
- bit lcd_busy_check() //1602状态检测是否忙碌
- {
- bit result;
- rs=0;
- rw=1;
- en=1;
- delay(10);
- result=(bit)(P0&0x80);
- en=0;
- return result;
- }
- void write_comd(uchar command) //写LCD命令
- {
- while(lcd_busy_check());
- rw=0;
- rs=0;
- en=0;
- P0=command;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
- void write_data(uchar dat) //向1602发送数据
- {
- while(lcd_busy_check());
- rs=1;
- rw=0;
- en=0;
- P0=dat;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
- lcd_init() //LCD1602初始化
- {
- write_comd(0x38); //显示模式设置为16X2显示
- write_comd(0x0c); //开显示屏,关光标
- write_comd(0x06); //字符进入模式:屏幕不动,字符后移
- write_comd(0x01); //清屏
- write_comd(0x80);
- }
- lcd_display()
- {
- uint a,b;
- for(a=0;a<16;a++)
- {
- write_data(table[a]);
- }
- //delay(200);
- write_comd(0x80+0x40+2);
- for(b=0;b<4;b++)
- {
- write_data(bby[b]);
- }
- //delay(200);
- write_comd(0x80+0x40+13);
-
- write_data(0xF4);//显示欧姆符号
-
- // delay(200);
-
- }
- adc0808_tra() // AD转换
- {
- uint getdata;
-
- START=0;
- START=1;
- delay(1);
- START=0;
- delay(5);
- EOC=1;
- while(EOC==0);
- OE=1;
-
- getdata=P1;
- OE=0;
- return getdata;
-
- }
-
-
- void data_display()
- {
- temp=getdata*1.0/255*500;
- qian=temp/1000;
- bai=temp%1000/100;
- shi=temp%100/10;
- ge=temp%10;
-
- write_comd(0x80+0x40+7);
- write_data(qian+0x30); //千位
- delay(5);
- write_comd(0x80+0x40+8);
- write_data(bai+0x30);//百位
- delay(5);
- write_comd(0x80+0x40+9);
- write_data(shi+0x30); //十位
- delay(5);
- write_comd(0x80+0x40+10);
- write_data('.');
- delay(5);
- //write_comd(0x80+0x40+11);
- write_data(ge+0x30); //小数一位
- }
- void T0init()
- {
- TMOD=0x01;
- TH0=(65536-200)/256;
- TL0=(65536-200)%256;
- EA=1;
- ET0=1;
- TR0=1;
- //delay(20);
- }
- void main()
- {
-
- T0init();
- lcd_init();
- lcd_display();
- while(1)
- {
- adc0808_tra();
- data_display();
- }
- }
- void timer() interrupt 1
- {
- TH0=(65536-200)/256;
- TL0=(65536-200)%256;
- CLK=~CLK;
- }
复制代码
|