简易数字电压表+ADC0809+程序查询方式实现8路数据转换
2024-07-26 来源:cnblogs
1 实验现象(每间隔1s自动切换模拟信号采集通道,通过数码管显示测量的电压值)
2 硬件设计
3 程序设计
3.1 主函数
#include #include 'DisplaySmg.h' #include 'ADC0809.h' #include 'Timer0.h' unsigned char adc_result = 0; //ADC转换后直接采集的数据 int adc_result_show = 0; //线性标度变化 unsigned char adc_flag = 1; //1s时间的标志信号,用来自动切换通道 unsigned char channel = 0; //通道号0~7 void disp_num(void) //显示四位十进制数 { LedBuf[0]= channel; //通道号 LedBuf[1]= adc_result_show/100; //百位 LedBuf[2]= adc_result_show/10%10; //十位 LedBuf[3]= adc_result_show%10; //个位 } void main() { Timer0_Init(); //定时/计数器T0初始化 EA=1; //中断总开关 DotDig1=1; //点亮第二个数码管的小数点 while(1) { if(adc_flag==1) //1S时间标志信号 { adc_flag = 0; //清零,使用Timer0计时 adc_result = ADC_Conv(channel); //通过ADC采集通道channel输入端数据 adc_result_show = adc_result*1.0*100*5/255; //数据变换处理(线性标度变换) disp_num(); //显示数据 channel++; //切换下一个通道 if(channel>7) channel = 0; } } } //1ms定时,动态刷新数码管,同时每隔1000ms刷新adc_flag=1 void Timer0_ISR(void) interrupt 1 { static unsigned int timer0cnt; TR0=0; //关闭定时器 timer0cnt++; if(timer0cnt>=1000) { timer0cnt = 0; adc_flag = 1; } DisplaySmg(); //每过1ms,刷新一次数码管显示函数 TL0 = 0x66; //设置定时初始值,定时1ms TH0 = 0xFC; //设置定时初始值,定时1ms TR0=1; //打开定时器 } 3.2 ADC0809模数转换函数 #ifndef __ADC0809_H__ #define __ADC0809_H__ #include #define ADC_DATA P3 sbit ADDR_A = P1^0; sbit ADDR_B = P1^1; sbit ADDR_C = P1^2; sbit START = P1^4; sbit EOC = P1^5; sbit OE = P1^6; unsigned char ADC_Conv(unsigned char channel); #endif #include 'ADC0809.h' void ADC_SetChannel(channel) { switch(channel) { case 0: ADDR_A = 0; ADDR_B = 0; ADDR_C = 0; break; case 1: ADDR_A = 1; ADDR_B = 0; ADDR_C = 0; break; case 2: ADDR_A = 0; ADDR_B = 1; ADDR_C = 0; break; case 3: ADDR_A = 1; ADDR_B = 1; ADDR_C = 0; break; case 4: ADDR_A = 0; ADDR_B = 0; ADDR_C = 1; break; case 5: ADDR_A = 1; ADDR_B = 0; ADDR_C = 1; break; case 6: ADDR_A = 0; ADDR_B = 1; ADDR_C = 1; break; case 7: ADDR_A = 1; ADDR_B = 1; ADDR_C = 1; break; default: break; } } unsigned char ADC_Conv(unsigned char channel) { unsigned char adc_result; OE = 0; //数据输出允许信号,高电平有效 START = 0; //ADC转换启动信号,高电平有效, 电路中与ALE(地址锁存允许信号)连在一起 ADC_SetChannel(channel); //选通道 START = 1; //上升沿,同时将ADC内部的寄存器清零 START = 0; //产生一定的脉冲,Typ=100ns,下降沿启动AD转换 while(EOC==0); //查询方式,ADC转换结束信号,EOC=1,转换结束 // DelayXms(1); //延时等待方式 OE = 1; //数据更新到输出端口 adc_result = ADC_DATA; //单片机读取数据 OE = 0; //本次过程结束 return adc_result; } 3.3 数码管动态显示函数 #ifndef __DisplaySmg_H__ #define __DisplaySmg_H__ #include #define GPIO_SEG P0 //段选端 #define GPIO_SEL P2 //位选端 extern unsigned char LedBuf[]; //外部变量声明 extern unsigned char DotDig0,DotDig1,DotDig2,DotDig3; void DisplaySmg(void); #endif #include 'DisplaySmg.h' unsigned char code LedData[]={ //共阴型数码管的段码表,字符,序号 0x3F, //'0',0 0x06, //'1',1 0x5B, //'2',2 0x4F, //'3',3 0x66, //'4',4 0x6D, //'5',5 0x7D, //'6',6 0x07, //'7',7 0x7F, //'8',8 0x6F, //'9',9 0x77, //'A',10 0x7C, //'B',11 0x39, //'C',12 0x5E, //'D',13 0x79, //'E',14 0x71, //'F',15 0x76, //'H',16 0x38, //'L',17 0x37, //'n',18 0x3E, //'u',19 0x73, //'P',20 0x5C, //'o',21 0x40, //'-',22 0x00, //熄灭 23 }; unsigned char DotDig0=0,DotDig1=0,DotDig2=0,DotDig3=0; //小数点控制位 unsigned char code LedAddr[]={0xfe,0xfd,0xfb,0xf7}; //数码管位选 unsigned char LedBuf[]={22,22,22,22}; //显示缓存区 void DisplaySmg() //四位数码管,考虑小数点 { unsigned char i; //等价于 'static unsigned char i = 0;' unsigned char temp; switch(i) { case 0: { GPIO_SEG = 0x00; //消影 if(DotDig0==1) //小数点 { temp = LedData[LedBuf[0]] | 0x80; //点亮小数点 } else { temp = LedData[LedBuf[0]]; } GPIO_SEG = temp; //段码 GPIO_SEL = LedAddr[0]; //位选 i++; break; } case 1: GPIO_SEG = 0x00; if(DotDig1==1) //小数点 { temp = LedData[LedBuf[1]] | 0x80; } else { temp = LedData[LedBuf[1]]; } GPIO_SEG = temp; GPIO_SEL = LedAddr[1]; i++; break; case 2: GPIO_SEG = 0x00; if(DotDig2==1) //小数点 { temp = LedData[LedBuf[2]] | 0x80; } else { temp = LedData[LedBuf[2]]; } GPIO_SEG = temp; GPIO_SEL = LedAddr[2]; i++; break; case 3: GPIO_SEG = 0x00; if(DotDig3==1) //小数点 { temp = LedData[LedBuf[3]] | 0x80; } else { temp = LedData[LedBuf[3]]; } GPIO_SEG = temp; GPIO_SEL = LedAddr[3]; i=0; break; default:break; } } 3.4 定时器T0 #ifndef __Timer0_H__ #define __Timer0_H__ #include void Timer0_Init(void); #endif #include 'Timer0.h' void Timer0_Init(void) //1毫秒@11.0592MHz { TMOD &= 0xF0; //设置定时器模式 TMOD |= 0x01; //设置定时器模式 TL0 = 0x66; //设置定时初始值 TH0 = 0xFC; //设置定时初始值 TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 ET0 = 1; //定时器0中断开关 // EA = 1; //中断总开关 } 4 参考来源 (1)单片机应用——ADC0809查询方式实现8路模拟信号的AD转换_哔哩哔哩_bilibili;