单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RS=P2^0; /***LCD1602工作端口定义***/
- sbit E=P2^2;
- sbit RW=P2^1;
- //sbit qp=P3^5; /***清屏按键***/
- sbit cs=P1^4; /***adc0832端口定义***/
- sbit clk=P1^5;
- sbit di=P1^6;
- sbit DO1=P1^7;
- char table5[5]; /***用来存储数字转换成字符的***/
- uint temp,i,time; /***速度、角度、循环/定时时间变量定义***/
- uchar data2; //char型
- void delay(uint z) /***延时子程序***/
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void write_com(uchar com) /***LCD1602写命令子程序***/
- { RW=0;
- RS=0;
- P0=com;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void write_data(uchar dat) /***LCD1602写数字子程序***/
- { RW=0;
- RS=1;
- P0= dat;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void LCD_write_char(uchar x,uchar y,uchar Data1) /***LCD1602在指定位置写字符子程序***/
- {
- if (y == 0)
- {
- write_com(0x80 + x);
- }
- else
- {
- write_com(0xC0 + x);
- }
- write_data( Data1);
- }
- void init() /***LCD1602初始化子程序***/
- {
- E=0;
- write_com(0x38);
- write_com(0x0e);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80+ 0x1);
- }
- void convector_num(uint x) /***把数字 转化成 字符串***/
- {
- if(x<100)
- {
- table5[0]=x/10+0x30;
- table5[1]=x%10+0x30;
- table5[2]=' ';
- table5[3]=' ';
- table5[4]=' ';
- }
- else if(x>=100)
- {
- table5[0]=x/100+0x30;
- table5[1]=(x/10)%10+0x30;
- table5[2]=x%10+0x30;
- table5[3]=' ';
- table5[4]=' ';
- }
- }
- void convector_float(float x) /***把数字 转化成 float***/
- {
- uint y=x*10;
- table5[0]=y/10+0x30;
- table5[1]='.';
- table5[2]=y%10+0x30;
- table5[3]=' ';
- table5[4]=' ';
- }
- void zhczfc(uint x) /***把数字 转化成 字符串***/
- {
- if(x<10)
- {
- table5[0]=x/1000+0x30;
- table5[1]= '.';
- table5[2]=x%1000/100+0x30;
- table5[3]= x%100/10+0x30;
- table5[4]= x%10+0x30;
- }
- if(x>=10)
- {
- table5[0]=x/10000+0x30;
- table5[1]=x%10000/1000+0x30;
- table5[2]= '.';
- table5[3]=x%1000/100+0x30;
- table5[4]= x%100/10+0x30;
- }
- }
- readadc0832() /***adc0832数据量读取***/
- {
- uchar j;
- data2=0;
- data2<<=3;
- data2|=3;
- cs=0;
- for(i=0;i<3;i++)
- {
- clk=0;
- delay(5);
- di=data2;
- data2>>=1;
- delay(5);
- clk=1;
- di=0 ;
- }
- clk=0;
- delay(5);
- clk=1;
- for(j=0;j<8;j++)
- {
- clk=0;
- delay(5);
- if(DO1==1)data2|=0x01;
- data2<<=1;
- delay(5) ;
- clk=1;
- }
- cs=1;
- }
- float voltage; //电压值
- void main()
- {
- init(); /***lcd1602初始化***/
- delay(100);
- while(1)
- {
- readadc0832();
- convector_num(data2);
- for(i=0;i<5;i++)
- {
- LCD_write_char(i,0,table5[i]);
- delay(20);
- }
- voltage=data2*5.0/255; /***500为5*100,100为保留2位小数***/
- //zhczfc(data2);
- convector_float(voltage);
- for(i=0;i<5;i++)
- {
- LCD_write_char(i,1,table5[i]);
- delay(20);
- }
- }
- }
复制代码
|