补齐这段C51代码,完成ADC0808的数字电压表虚拟仿真,在LCD1602上显示电压(格式:v=X.XXV,其中“X”对应具体值): #include "reg52.h" #include "intrins.h" typedef unsigned int uint; typedef unsigned char uchar; sbit CLOCK=P3^2; sbit EOC=P3^3; sbit START=P3^4; sbit OE=P3^5; sbit OUT8=P1^0; sbit OUT7=P1^1; sbit OUT6=P1^2; sbit OUT5=P1^3; sbit OUT4=P1^4; sbit OUT3=P1^5; sbit OUT2=P1^6; sbit OUT1=P1^7; sbit D0=P0^0; sbit D1=P0^1; sbit D2=P0^2; sbit D3=P0^3; sbit D4=P0^4; sbit D5=P0^5; sbit D6=P0^6; sbit D7=P0^7; sbit RS=P2^6; sbit RW=P2^5; sbit E=P2^7;
时间: 2024-04-02 13:33:47 浏览: 127
C51 stc 通过xpt获取电压值,并将电压显示到1602
5星 · 资源好评率100%
uchar code table[]="0123456789"; // 数码管字符表
void DelayUs2x(uint t)
{
while(t--);
}
void DelayMs(uint t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void WriteCommand(uchar com)
{
RS=0;
RW=0;
P0=com;
E=1;
_nop_();
_nop_();
_nop_();
E=0;
}
void WriteData(uchar dat)
{
RS=1;
RW=0;
P0=dat;
E=1;
_nop_();
_nop_();
_nop_();
E=0;
}
void InitLcd()
{
WriteCommand(0x38);
DelayMs(5);
WriteCommand(0x38);
DelayMs(5);
WriteCommand(0x38);
DelayMs(5);
WriteCommand(0x38);
WriteCommand(0x08);
WriteCommand(0x01);
WriteCommand(0x06);
WriteCommand(0x0c);
}
uchar ReadADC0808(uchar chn) // 读取ADC0808采集的电压值
{
uchar dat=0;
OE=1;
START=1;
CLOCK=0;
CLOCK=1;
dat|=((D7<<7)|(D6<<6)|(D5<<5)|(D4<<4)|(D3<<3)|(D2<<2)|(D1<<1)|D0);
OE=0;
return dat;
}
void main()
{
uchar value;
float voltage;
InitLcd();
WriteCommand(0x80);
WriteData('v');
WriteData('=');
WriteData(' ');
while(1)
{
value=ReadADC0808(0); // 采集通道0的电压值
voltage=value*5.0/255; // 获取实际电压值
WriteCommand(0x87); // 设置光标位置
WriteData(table[(int)voltage]); // 显示整数部分
WriteData('.'); // 显示小数点
voltage=voltage-(int)voltage; // 获取小数部分
voltage*=100;
WriteData(table[(int)voltage]); // 显示小数部分的整数部分
WriteData(table[(int)(voltage*10)%10]); // 显示小数部分的小数部分
WriteData('V'); // 显示单位
DelayMs(500);
}
}
阅读全文