1 实验现象
2 实验原理
3 系统设计
4 硬件设计
5 软件设计
5.1 主程序
#include
#include "DisplaySmg.h"
#include "ADC0809.h"
#include "Timer0.h"
#include "Timer1.h"
unsigned char adc_result = 0;
int adc_result_show = 0;
void disp_num(void) //显示四位十进制数
{
LedBuf[0]= 23; //千位,不显示
LedBuf[1]= adc_result_show/100; //百位
LedBuf[2]= adc_result_show/10%10;//十位
LedBuf[3]= adc_result_show%10; //个位
}
void main()
{
Timer0_Init(); //定时/计数器T0初始化
Timer1_Init(); //定时/计数器T1初始化
EA=1; //中断总开关
DotDig1=1; //点亮第二个数码管的小数点
while(1)
{
adc_result = ADC_Conv();
adc_result_show = adc_result*1.0*100*5/255; //数据变换处理(线性标度变换)
disp_num(); //显示数据
}
}
void Timer0_ISR(void) interrupt 1
{
TR0=0; //关闭定时器
DisplaySmg(); //每过1ms,刷新一次数码管显示函数
TL0 = 0x66; //设置定时初始值,定时1ms
TH0 = 0xFC; //设置定时初始值,定时1ms
TR0=1; //打开定时器
}
5.2 数码管显示模块
#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;
}
}
5.3 ADC0809模数转换模块
#ifndef __ADC0809_H__
#define __ADC0809_H__
#include
//#include "DelayXms.h"
#define ADC_DATA P3
sbit ADDR_A = P1^0;
sbit ADDR_B = P1^1;
sbit ADDR_C = P1^2;
sbit CLK = P1^3;
sbit START = P1^4;
sbit EOC = P1^5;
sbit OE = P1^6;
unsigned char ADC_Conv(void);
#endif
#include "ADC0809.h"
/*ADC0809的工作过程:
首先输入3位地址,并使ALE=1,将地址输入地址锁存器中。此地址经
译码选通8路模拟输入之一到比较器。START上升沿将逐次逼近寄存器复位。
下降沿启动ADC转换,之后EOC输出信号变低,指示转换正在进行。直到ADC
转换完成,EOC变为高电平,指示ADC转换结束,结果数据已存入锁存器,
这个信号可用作中断申请。当OE输入高电平时,输出三态门打开,转换结果的
数字量输出到数据总线上。*/
unsigned char ADC_Conv()
{
unsigned char adc_result;
OE = 0; //数据输出允许信号,高电平有效
START = 0; //ADC转换启动信号,高电平有效, 电路中与ALE(地址锁存允许信号)连在一起
ADDR_A = 1; //3位地址输入线
ADDR_B = 1; //用于选通8路模拟输入中的一路
ADDR_C = 0;
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;
}
//定时器T1产生500KHz时钟信号,即2us, 定时器每1us中断一次
void Timer1_ISR(void) interrupt 3