AT89C51,ADC0808数码管显示实验代码
时间: 2023-11-15 13:05:54 浏览: 70
以下是AT89C51与ADC0808数码管显示实验的代码,仅供参考:
```
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DIO=P1^0;
sbit CLK=P1^1;
sbit CS=P1^2;
sbit OE=P1^3;
sbit A0=P1^4;
sbit A1=P1^5;
sbit A2=P1^6;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //0-9的数码管码表
};
uint adc; //存放ADC0808采集值
void delay(uint i) //延时函数
{
while(i--);
}
void write_74hc595(uchar dat) //74hc595写数据函数
{
uchar i;
for(i=0;i<8;i++)
{
DIO=dat&0x80; //从高位开始写
CLK=0;
CLK=1;
dat<<=1; //左移一位
}
}
void write_adc0808(uchar cmd) //ADC0808写命令函数
{
uchar i;
OE=1;
CS=0;
for(i=0;i<8;i++)
{
DIO=cmd&0x80; //从高位开始写
CLK=0;
CLK=1;
cmd<<=1; //左移一位
}
OE=0;
}
uchar read_adc0808() //ADC0808读数据函数
{
uchar i,dat;
OE=1;
CS=1;
dat=0;
for(i=0;i<8;i++)
{
CLK=0;
dat<<=1; //左移一位
dat|=DIO; //读数据
CLK=1;
}
OE=0;
return dat;
}
void display(uint dat) //数码管显示函数
{
write_74hc595(0x00); //清空
write_74hc595(table[dat/1000]); //千位
A0=1;
delay(1);
A0=0;
write_74hc595(table[dat%1000/100]); //百位
A1=1;
delay(1);
A1=0;
write_74hc595(table[dat%100/10]); //十位
A2=1;
delay(1);
A2=0;
write_74hc595(table[dat%10]); //个位
delay(1);
}
void main()
{
while(1)
{
write_adc0808(0x88); //选择通道0并启动转换
delay(100);
adc=read_adc0808(); //读取ADC0808采集值
display(adc); //数码管显示采集值
}
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)