STC15W401AS自带AD 4位电压表AD000.0-500.0。单片机直接驱动4位共阴数码管!采用定时中断调AD!附件有很清楚的注释!
单片机源程序如下:
- #include "STC15W401AS.h"
- #include "intrins.h"
-
- sbit GND1=P1^4;
- sbit GND2=P1^3;
- sbit GND3=P1^2;
- sbit GND4=P1^1;
-
- sbit LEDa=P1^5;
- sbit LEDb=P1^6;
- sbit LEDc=P3^3;
- sbit LEDd=P3^1;
- sbit LEDe=P3^0;
- sbit LEDf=P1^7;
- sbit LEDg=P3^2;
- sbit LEDp=P3^4;
- unsigned char count;
- unsigned char PWM=9;
- float dianya; // 电压
- int dianya1; // 电压 1
- unsigned char code numtab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- //unsigned char code numtab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- /*-------------------------------------延时函数-------------------------------------*/
- void delay (unsigned int a)
- {
- unsigned int i;
- while( --a != 0)
- {
- for (i = 0; i < 12; i++);
- }
- }
- /*-------------------------------------数码管消隐-------------------------------------*/
- void displayoff (void)
- {
- GND1=1;
- GND2=1;
- GND3=1;
- GND4=1;
-
- LEDa=0;
- LEDb=0;
- LEDc=0;
- LEDd=0;
- LEDe=0;
- LEDf=0;
- LEDg=0;
- LEDp=0;
- delay(10-PWM);
- }
- /*-------------------------------------个位码显-------------------------------------*/
- void bit1 (unsigned char d)
- {
- unsigned char i;
- i=d&0x01;
- if (i==0x01){
- GND1=0;LEDa=1;}delay(PWM);displayoff();
- i=d&0x02;
- if (i==0x02){
- GND1=0;LEDb=1;}delay(PWM);displayoff();
- i=d&0x04;
- if (i==0x04){
- GND1=0;LEDc=1;}delay(PWM);displayoff();
- i=d&0x08;
- if (i==0x08){
- GND1=0;LEDd=1;}delay(PWM);displayoff();
- i=d&0x10;
- if (i==0x10){
- GND1=0;LEDe=1;}delay(PWM);displayoff();
- i=d&0x20;
- if (i==0x20){
- GND1=0;LEDf=1;}delay(PWM);displayoff();
- i=d&0x40;
- if (i==0x40){
- GND1=0;LEDg=1;}delay(PWM);displayoff();
- i=d&0x80;
- if (i==0x80){
- GND1=0;LEDp=1;}delay(PWM);displayoff();
- }
- /*-------------------------------------十位码显-------------------------------------*/
- void bit2 (unsigned char d)
- {
- unsigned char i;
- i=d&0x01;
- if (i==0x01){
- GND2=0;LEDa=1;}delay(PWM);displayoff();
- i=d&0x02;
- if (i==0x02){
- GND2=0;LEDb=1;}delay(PWM);displayoff();
- i=d&0x04;
- if (i==0x04){
- GND2=0;LEDc=1;}delay(PWM);displayoff();
- i=d&0x08;
- if (i==0x08){
- GND2=0;LEDd=1;}delay(PWM);displayoff();
- i=d&0x10;
- if (i==0x10){
- GND2=0;LEDe=1;}delay(PWM);displayoff();
- i=d&0x20;
- if (i==0x20){
- GND2=0;LEDf=1;}delay(PWM);displayoff();
- i=d&0x40;
- if (i==0x40){
- GND2=0;LEDg=1;}delay(PWM);displayoff();
- i=d&0x80;
- if (i==0x80){
- GND2=0;LEDp=1;}delay(PWM);displayoff();
- }
- /*-------------------------------------百位码显-------------------------------------*/
- void bit3 (unsigned char d)
- {
- unsigned char i;
- i=d&0x01;
- if (i==0x01){
- GND3=0;LEDa=1;}delay(PWM);displayoff();
- i=d&0x02;
- if (i==0x02){
- GND3=0;LEDb=1;}delay(PWM);displayoff();
- i=d&0x04;
- if (i==0x04){
- GND3=0;LEDc=1;}delay(PWM);displayoff();
- i=d&0x08;
- if (i==0x08){
- GND3=0;LEDd=1;}delay(PWM);displayoff();
- i=d&0x10;
- if (i==0x10){
- GND3=0;LEDe=1;}delay(PWM);displayoff();
- i=d&0x20;
- if (i==0x20){
- GND3=0;LEDf=1;}delay(PWM);displayoff();
- i=d&0x40;
- if (i==0x40){
- GND3=0;LEDg=1;}delay(PWM);displayoff();
- i=d&0x80;
- if (i==0x80){
- GND3=0;LEDp=1;}delay(PWM);displayoff();
- }
- /*-------------------------------------百位码显-------------------------------------*/
- void bit4 (unsigned char d)
- {
- unsigned char i;
- i=d&0x01;
- if (i==0x01){
- GND4=0;LEDa=1;}delay(PWM);displayoff();
- i=d&0x02;
- if (i==0x02){
- GND4=0;LEDb=1;}delay(PWM);displayoff();
- i=d&0x04;
- if (i==0x04){
- GND4=0;LEDc=1;}delay(PWM);displayoff();
- i=d&0x08;
- if (i==0x08){
- GND4=0;LEDd=1;}delay(PWM);displayoff();
- i=d&0x10;
- if (i==0x10){
- GND4=0;LEDe=1;}delay(PWM);displayoff();
- i=d&0x20;
- if (i==0x20){
- GND4=0;LEDf=1;}delay(PWM);displayoff();
- i=d&0x40;
- if (i==0x40){
- GND4=0;LEDg=1;}delay(PWM);displayoff();
- i=d&0x80;
- if (i==0x80){
- GND4=0;LEDp=1;}delay(PWM);displayoff();
- }
- /*-------------------------------------数码管位显-------------------------------------*/
- void display(unsigned char l,unsigned char d)
- {
- switch (l)
- {
- case 1:
- bit1(d);
- break;
- case 2:
- bit2(d);
- break;
- case 3:
- bit3(d);
- break;
- case 4:
- bit4(d);
- break;
- }
- }
- /*--------------------------------A/D转换初始化函数--------------------------------*/
- void Init_ADC()
- {
- P1ASF|=0X03;//设定P1.0,P1.1为模拟量电压输入端
- ADC_RES=0;//清除缓存寄存器
- ADC_CONTR=ADC_PWR+ADC_SPD1;//启动ADC电源,设定转换速率
- _nop_();
- }
- //========================================================================//
- uint ReadAdc(uchar Chx)//读取ADC数据
- {
- uint ADCDAT;
- Init_ADC();
- ADC_CONTR=ADC_PWR+ADC_SPD1+Chx+ADC_START;//启动转换
- _nop_();
- _nop_();
- _nop_();
- _nop_();//短暂延时
- while((ADC_CONTR&ADC_FLAG)==0);//等待转换完毕
- //ADC_CONTR&=~ADC_FLAG;//清除转换完成标志
- ADC_CONTR &= 0xE7;//1111,0111 清 ADC_FLAG 位, 关闭A/D转换,
- ADCDAT=ADC_RES;//获取ADC数值
- ADCDAT<<=2;
- ADCDAT|=ADC_RESL;
- return ADCDAT;//返回数值
- }
- /*--------------------------------------主函数--------------------------------------*/
- void main (void)
- {
- P1M0=0x01; P1M1=0xe0;
- P3M0=0x00; P3M1=0x1f;
- TMOD=0x01;//定时器工作方式1
- TH0=0x3C;//定时器初始产生50ms时基信号
- TL0=0xB0;//定时器初始产生50ms时基信号
-
- EA=1;//开总中断
- ET0=1;//开定时器中断
- TR0=1;//启动定时器
- while(1)
- {
- if (count==10)
- {
- /* dianya=ReadAdc(0)*(5.0/4500.0); // 取电压
- dianya=dianya*100;
- dianya1=(int)dianya;
- */
- dianya=ReadAdc(0);
- dianya1=dianya;
- count=0;
- }
- /* P2=0x01;
- P0=shuzu[dianya1/100]; // 取百位
- DELAY_MS(5);
- P2<<=1;
- P0=shuzu[dianya1%100/10];// 取十位
- DELAY_MS(5);
- P2<<=1;
- P0=shuzu[dianya1%100%10]&0x7f;// 取个位
- DELAY_MS(5);
- */
- display (1,numtab[dianya1/1000 + '0']); //取百位
- display (2,numtab[dianya1%1000/100 + '0']); //取十位
- display (3,numtab[dianya1%100/10 + '0']); //取个位
- display (3,numtab[dianya1%10 + '0']); //取小数点后一位
- }
- }
- /*-----------------------------------中断服务程序-----------------------------------*/
- void Timer0_Routine(void) interrupt 1
- {
- TH0=0x3C;
- TL0=0xB0;
- count++;
- }
复制代码
所有资料51hei提供下载:
500电压表.rar
(29.56 KB, 下载次数: 51)
|