|
單片機(jī)源程序如下:
搜狗截圖20171231182336.jpg (467.51 KB, 下載次數(shù): 79)
下載附件
2017-12-31 18:18 上傳
- #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]; /***用來存儲數(shù)字轉(zhuǎn)換成字符的***/
- uint temp,i,time; /***速度、角度、循環(huán)/定時(shí)時(shí)間變量定義***/
- uchar data2; //char型
- void delay(uint z) /***延時(shí)子程序***/
- {
- 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寫數(shù)字子程序***/
- { 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) /***把數(shù)字 轉(zhuǎn)化成 字符串***/
- {
- 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) /***把數(shù)字 轉(zhuǎn)化成 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) /***把數(shù)字 轉(zhuǎn)化成 字符串***/
- {
- 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數(shù)據(jù)量讀取***/
- {
- 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位小數(shù)***/
- //zhczfc(data2);
- convector_float(voltage);
- for(i=0;i<5;i++)
- {
- LCD_write_char(i,1,table5[i]);
- delay(20);
- }
- }
- }
復(fù)制代碼
|
|