https://static.assets-stash.eet-china.com/album/old-resources/2008/10/28/4780217a-c7a5-4728-8c6c-2f8469f1be8e.rarADC0804+LED数码管显示
//ADC0804驱动头文件
//File: ADC0804_1.H
//Date: 08-7-15
//Time: 14:45
//Note: 8 Bits Analog to Digital Conversion
/*Chip:ADC0804
/*-------------------------------------------------------------*/
#ifndef __ADC0804_1_H__
#define __ADC0804_1_H__
#include<intrins.h>
/*-------------------------------------------------------------*/
unsigned int Volt; //电压换算并扩大1000倍
//unsigned char LCD[7]; //电压数据转化成液晶字符显示
unsigned char LED[4]; //电压数据转换成数码管数字
unsigned char code SEG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//0 1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------*/
void Data_Conversion(void); //电压换算并扩大1000倍
//void Volt_To_LCD(void); //数据转化成液晶字符显示
void Volt_To_LED(void); //数据转换成数码管显示数字
void delayms(void); //延时
/*-------------------------------------------------------------*/
void delayms(void)
{
unsigned char i,j;
for(j=10;j>0;j--)
for(i=248;i>0;i--)
;
}
/*-------------------------------------------------------------*/
void Data_Conversion(void)
{
unsigned char AD_Data;
AD_Data = P1;
Volt = 5.0 / 256 * AD_Data * 1000;
}
/*-------------------------------------------------------------*/
/*
//数据转化成液晶字符显示
void Volt_To_LCD(void)
{
Data_Conversion(); //电压换算并扩大1000倍
LCD[0] = Volt / 1000 + '0'; //千位
LCD[1] = '.'; //小数点
LCD[2] = Volt / 100 % 10 + '0'; //百位
LCD[3] = Volt / 10 % 10 + '0'; //十位
LCD[4] = Volt % 10 + '0'; //个位
LCD[5] = 'V'; //字符V
LCD[6] = '\0'; //字符串结束标志符
}
/*-------------------------------------------------------------*/
//数据转换成数码管显示数字
void Volt_To_LED(void)
{
Data_Conversion(); //电压换算并扩大1000倍
LED[0] = SEG[Volt / 1000] & 0x7f; //千位加小数点
LED[1] = SEG[Volt / 100 % 10]; //百位
LED[2] = SEG[Volt / 10 % 10]; //十位
LED[3] = SEG[Volt % 10]; //个位
}
/*-------------------------------------------------------------*/
#endif
//主函数
/*
测试: ADC0804测量电压
说明: ADC0804数据线接在P1口
LED数码管片选为P0_0~P0_3
LED数码管段码为P2口
*/
/*-------------------------------------------------------------*/
#include<at89x52.h>
#include"ADC0804_1.H"
/*-------------------------------------------------------------*/
void Display(void)
{
P2 = 0xff; //断码清零
P0 = P0 & 0xf0; //低四位清零
P2 = LED[0];
P0_0 = 1;
delayms();
P0_0 = 0;
P2 = 0xff;
P2 = LED[1];
P0_1 = 1;
delayms();
P0_1 = 0;
P2 = 0xff;
P2 = LED[2];
P0_2 = 1;
delayms();
P0_2 = 0;
P2 = 0xff;
P2 = LED[3];
P0_3 = 1;
delayms();
}
/*-------------------------------------------------------------*/
void main(void)
{
while(1)
{
Volt_To_LED();
Display();
}
}
文章评论(0条评论)
登录后参与讨论