补齐这段C51代码,完成ADC0808的数字电压表虚拟仿真,在LCD1602上显示电压(格式:v=X.XXV,其中“X”对应具体值): #include "reg52.h" #include "intrins.h" typedef unsigned int uint; typedef unsigned char uchar; sbit P3^2=CLOCK; sbit P3^3=EOC; sbit P3^4=START; sbit P3^5=OE; sbit P1^0=OUT8; sbit P1^1=OUT7; sbit P1^2=OUT6; sbit P1^3=OUT5; sbit P1^4=OUT4; sbit P1^5=OUT3; sbit P1^6=OUT2; sbit P1^7=OUT1; sbit P0^0=D0; sbit P0^1=D1; sbit P0^2=D2; sbit P0^3=D3; sbit P0^4=D4; sbit P0^5=D5; sbit P0^6=D6; sbit P0^7=D7; sbit P2^6=RS; sbit P2^5=RW; sbit P2^7=E;
时间: 2024-04-02 22:34:02 浏览: 58
基于51单片机+ADC0808的八路数字电压表仿真设计资料 包含程序、PCB原理图、仿真文件
void delay(uint i) {
while(i--);
}
void write_com(uchar com) {
P0 = com;
P2^6 = 0;
P2^5 = 0;
P2^7 = 1;
_nop_();
P2^7 = 0;
delay(10);
}
void write_data(uchar dat) {
P0 = dat;
P2^6 = 1;
P2^5 = 0;
P2^7 = 1;
_nop_();
P2^7 = 0;
delay(10);
}
void init_lcd() {
write_com(0x38);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
delay(5);
write_com(0x01);
delay(5);
}
void init_adc() {
P3^4 = 1;
P3^5 = 1;
}
uint read_adc() {
uint value;
P3^4 = 0;
P3^5 = 0;
P3^4 = 1;
while(P3^3);
for(int i=0;i<8;i++) {
P3^2 = 0;
P3^2 = 1;
value = value << 1;
value |= OUT1;
}
P3^5 = 1;
return value;
}
void main() {
init_lcd();
init_adc();
while(1) {
uint adc_value = read_adc();
float voltage = adc_value * 5.0 / 255.0;
write_com(0x80);
write_data('v');
write_data('=');
write_data((int)voltage+'0');
write_data('.');
write_data((int)(voltage*10)%10+'0');
write_data((int)(voltage*100)%10+'0');
write_data('V');
}
}
阅读全文