|
简易电压表测量设备的制作proteus仿真及其C语言程序,供参考
#include <reg52.h>
#define TIMEFRQ 480 //定义定时器定时频率
#define CLOCK 22118400 //系统时钟
#define LED_SEG P0 //定义数码管接口
#define LED_BIT P2
#define num 100 //50次测量求平均值
sbit key0 = P3^4;
sbit key1 = P3^5;
sbit wr = P3^6;
sbit rd = P3^7;
unsigned char segcode[]=
{
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0
};//共阴极
unsigned char led_buf[]={0,0,0,0,0,0,0}; //缓冲数组
delay(unsigned int t)
{
while(--t);
}
//定时器初始化
void Init_T1(void)
{
TMOD = (TMOD & 0X0F) | 0X10; //定时器1工作在模式1,16Bit定时器模式
TH1 = 255-CLOCK/TIMEFRQ/12/256;
TL1 = 255-CLOCK/TIMEFRQ/12%256;
EA = 1; //开总中断
ET1 = 1;
TR1 = 1;//开启定时器和中断
}
//读取ADC0804结果函数
unsigned char get_8bit()
{
unsigned int temp = 0;
wr=1;
rd=0;//ADC数据一直输出
wr=0;
wr=1;//启动ADC
delay(1000);//延时等待ADC结束
temp = P1;
return(temp);
}
main()
{
unsigned long int SUM = 0;
unsigned int counter;
unsigned char j;
j = num ; //num=50
Init_T1(); //定时器初始化
while(1)
{
/***更新显示缓冲区***/
led_buf[2] = counter%10;
led_buf[3] = counter/10%10;
led_buf[4] = (counter/100)%10;
led_buf[5] = (counter/1000)%10+10;//+10就是加小数点
led_buf[0] = 20;//熄灭
led_buf[1] = 20;//熄灭
/*************************************/
SUM += get_8bit()*2; //从P1口读二进制数据
j--;
if(j==0) //采集50次数据之后取平均值
{
counter = SUM/num;
counter = counter*2.50*1000/255;
SUM = 0;
j = num ;
}
}
}
/*定时器中断,完成数码管显示刷新*/
void time() interrupt 3
{
static unsigned char i=0;
TH1 = 255-CLOCK/TIMEFRQ/12/256;
TL1 = 255-CLOCK/TIMEFRQ/12%256;
i=++i%6;
LED_BIT = 0x0; //关所有数码管
LED_SEG = segcode[led_buf[i]];
LED_BIT = (1<<i);
}
|
|