51 microcontroller A/D conversion
Components
Components | name |
---|---|
exclusion | RESPACK-8 |
51 microcontroller | AT89C51 |
Digital Tube | 7SEG-MPX4-CA-BLUE |
ADC chip | ADC0808 |
sliding rheostat | POT-HG |
Schematic part
ADC0808 pin function
project | Value |
---|---|
ADDA-C | Select IN channel |
IN0-7 | Analog input |
VREF+ | ADC reference voltage |
VREF- | ADC reference ground |
CLOCK | clock |
VREF+ | ADC reference voltage |
OUT1-8 | Digital signal output |
START | start signal |
ARE YOU | Output enable |
EOC | end signal |
ADC0808 initialization function
According to the timing diagram, first is the START signal, which goes from low to high and then back to low, and then looks at the EOC signal, which goes from high to low, and then to high again. The OUTPUT ENABLE(OE) signal goes high, then the output signal is read, and then the output OUTPUT ENABLE(OE) goes low to turn off the output.
u8 ADC_Init(void)
{
u8 ad_result=0;
//首先是START信号,由低变高再变低
START=0;
START=1;
START=0;
//然后看EOC信号,由高到低
while(EOC==0);
//OUTPUT ENABLE(OE)信号变高,然后读取输出信号
OE=1;
ad_result=P1;
OE=0;
return ad_result;
}
code
main.c
code show as below:
#include <reg51.h>//头文件
#define u8 unsigned char //宏定义
#define u16 unsigned int
sbit START=P2^5;
sbit EOC=P2^6;
sbit OE=P2^7;
void SMG_output(void);
void delay_ms(u16 ms);
u8 ADC_Init(void);
u8 tab[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //数码管码值
u8 SMG[8]={
10,10,10,10,10,10,10,10};//存储数码管要显示的码
u8 ad=0;
void main(void)
{
while(1)
{
ad=ADC_Init();
SMG[0]=10;
SMG[1]=ad/100;
SMG[2]=ad%100/10;
SMG[3]=ad%10;
SMG_output();//数码管显示
}
}
u8 ADC_Init(void)
{
u8 ad_result=0;
START=0;
START=1;
START=0;
while(EOC==0);
OE=1;
ad_result=P1;
OE=0;
return ad_result;
}
//数码管显示函数
void SMG_output(void)
{
u8 i; //定义数码管控制位
for(i=0;i<=4;i++) //八次循环
{
P2=1<<i; //第一次选第一根数码管,第二次第二根。。。。 1 0000 0001 0000 0010
P0=tab[SMG[i]];//显示数码管数组对应的值 p0=tab[smg[0]]=tab[2]=2
delay_ms(1); //延时1ms == 显示1ms
}
P2=0xff; //数码管消隐
P0=0xff; //数码管消隐
}
//延时函数
void delay_ms(u16 ms)
{
u16 i,j;
for(i=0;i<ms;i++)
for(j=71;j>0;j--);
}