使用51单片机制作简易数字电压表教程
首先打开proteus软件导入各个元器件,并连线。
接下来介绍下adc0808的导入方法
频率发生器的导入方法:
接下来就要编写c程序了,代码都是经过测试的,可以方型复制使用。
#include"reg52.h" // 引入头文件
#define uint unsigned int // 把无符号整型简写成 uint
sbit START = P3^0; // START接到P3.0口
sbit EOC = P3^1; // EOC接到P3.1口
sbit OE = P3^2; // OE接到P3.2口
sbit RS = P3^3; // RS接到P3.3口
sbit RW = P3^4; // RW接到P3.4口
sbit E = P3^5; // E接到P3.5口
uint dianya=0; // 定义 电压=0
unsigned char str[]={"0123456789.V"}; // 定义lcd显示需要的字符
// 延时函数(不精确的延时)
void delay(uint t){
uint i=0, j=0;
for(i=0; i<t; i++){
for(j=0; j<120; j++);
}
}
// 模拟转数字 ADC函数 根据时序表来写
void adc(){
START=0;
START=1;
delay(5);
START=0;
while(EOC != 1);
OE = 1;
dianya =P2;
OE = 0;
}
// lcd写数据函数 写dat首字母
void xdat(unsigned char dat){
RS = 1;
RW = 0;
E = 0;
P1 = dat;
delay(5);
E = 1;
E = 0;
}
// lcd写命令函数 写命令首字母xml
void xml(unsigned char ml){
RS = 0;
RW = 0;
E = 0;
P1 = ml;
delay(5);
E = 1;
E = 0;
}
// lcd初始化函数
void lcdinit(){
xml(0x38);
xml(0x0c);
xml(0x06);
xml(0x01);
}
// 更新显示函数
void gengxin(){
uint v1=0, v2=0, v3=0; // 定义电压的个位,十分位,百分位变量
dianya=dianya*100/51; // 根据电压显示和实际的比例做出先扩大100倍在缩小51倍
v1 = dianya/100; // 个位是电压值÷100的整数部分
v2 = (dianya%100)/10; // 十分位是电压值÷100的余数在÷10的整数部分
v3 = dianya%10; // 百分位是电压值÷10的余数
xml(0x80+0x04); // 写lcd从第一行第5个字符开始写
delay(5); // 延时5毫秒
xdat(str[v1]); // 写字符串str的第v1位
delay(5); // 延时5毫秒
xdat(str[10]); // 写字符串str的第10位(.)
delay(5); // 延时5毫秒
xdat(str[v2]); // 写字符串str的第v2位
delay(5); // 延时5毫秒
xdat(str[v3]); // 写字符串str的第v3位
delay(5); // 延时5毫秒
xdat(str[11]); // 写字符串str的第11位(V)
delay(5); // 延时5毫秒
}
// 主函数
void main(){
lcdinit(); // lcd初始化
while(1){
adc(); // 读电压值
gengxin(); // 更新显示电压值
}
}
最后就是编译成hex文件并导入proteus中仿真了,然后调节滑动变阻器,就可以发现电压表的值和lcd的值是一样的了,肯定会有误差,而且这个误差没有办法消除。