#include<reg52.h> #include<intrins.h> #include<stdio.h> #include<math.h> #define uchar unsigned char #define uint unsigned int #define ulong unsigned long uchar code table[]="cm^2"; sbit lcden=P2^2; sbit lcdrs=P2^0; sbit lcdwr=P2^1; sbit adwr=P3^6; sbit adrd=P3^7; sbit adcs=P2^5; uchar num,ad_val; void delay(uint z) //延时子函数 { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void write_com(uchar com) //写命令 { lcdrs=0; lcdwr=0; P0=com; delay(5); lcden=1; delay(5); lcden=0; } void write_data(uchar date) //写数据 { lcdrs=1; lcdwr=0; P0=date; delay(5); lcden=1; delay(5); lcden=0; } void init() //液晶初始化 { lcden=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); } uchar get_ad() //A/D 启动和转换程序 { uchar ad_val; adwr=1; _nop_(); adwr=0; //一个向下的脉冲,启动 A/D 转换 _nop_(); adwr=1; delay(5); //延迟 5 微秒,给 A/D 转换留一定的时间 P1=0xff; //读取 P1 口之前先给其全写 1 adrd=1; //选通 ADCS _nop_(); adrd=0; _nop_(); ad_val=P1; //AD 数据赋给 P1 口 adrd=1; return ad_val; } void main() //主函数 { uint A1,A2,A3,A4; //uint 的范围是 0-65535 ulong ad_val,ad_vo; //ulong 的范围是 0~2^32-1 adcs=0; //AD 的片选信号选通*/ ad_val=get_ad(); delay(10); ad_val=ad_val*10000; ad_vo= (45574-ad_val)/3522; //本来是 y=(4.5574-x)/0.3522 ad_vo=ad_vo*10000; //浮点数不能差分,把它们扩大 10000 倍 A1=ad_vo/100000; A2=ad_vo%100000/10000; A3=ad_vo%10000/1000; A4=ad_vo%1000/100; init(); write_com(0x80); { write_data(A1+48); write_data(A2+48); write_data('.'); write_data(A3+48); write_data(A4+48); delay(5); } write_com(0x80+0x40); for(num=0;num<4;num++) { write_data(table[num]); delay(5); } while(1); }
十位数显示乱码其他正常。求大神解救
|