设计51单片机和ADC0809的接口电路,使用中断方式顺序采集4路模拟量,依次存入内部RAM的4个单元中,循环往复,不断采集。 资源分配: (1)、线选地址线:P2.0 (2)、模拟量:IN4--IN7 (3)、内部RAM地址:44H --47H,用C51单片机编写该程序
时间: 2023-06-23 15:07:55 浏览: 245
以下是设计51单片机和ADC0809的接口电路,并使用中断方式顺序采集4路模拟量,依次存入内部RAM的4个单元中,循环往复,不断采集的C51单片机程序:
```
#include <reg51.h>
#define ADC0809_PORT P1
#define ADC0809_WR P2_1
#define ADC0809_RD P2_2
#define ADC0809_CS P2_3
#define ADC0809_INTR P3_2
#define RAM_START_ADDR 0x44
unsigned char code ADC0809_CHNL[] = {0x84, 0xC4, 0x94, 0xD4};
unsigned char ADC0809_DATA;
unsigned char RAM_ADDR = RAM_START_ADDR;
void ADC0809_Init() { // 初始化ADC0809
ADC0809_INTR = 1;
ADC0809_CS = 1;
ADC0809_WR = 1;
ADC0809_RD = 1;
}
void ADC0809_Read(unsigned char chnl) { // 读取指定通道的模拟量
ADC0809_PORT = chnl;
ADC0809_CS = 0;
ADC0809_WR = 0;
ADC0809_WR = 1;
while(ADC0809_INTR); // 等待转换完成
ADC0809_RD = 0;
ADC0809_DATA = ADC0809_PORT;
ADC0809_RD = 1;
ADC0809_CS = 1;
}
void ADC0809_ISR() interrupt 0 { // ADC0809中断服务程序
ADC0809_INTR = 1;
ADC0809_Read(ADC0809_CHNL[RAM_ADDR-RAM_START_ADDR]);
*(unsigned char xdata *)(RAM_ADDR++) = ADC0809_DATA; // 将数据存入RAM
if(RAM_ADDR > 0x47) RAM_ADDR = RAM_START_ADDR; // 循环到RAM起始地址
}
void main() {
ADC0809_Init(); // 初始化ADC0809
IE = 0x81; // 开启中断并允许外部中断0
while(1);
}
```
该程序中,我们首先定义了ADC0809的控制引脚和端口,以及RAM的起始地址和ADC0809的通道号。然后在`ADC0809_Init()`函数中初始化ADC0809,`ADC0809_Read()`函数中读取指定通道的模拟量。在`ADC0809_ISR()`函数中,我们通过中断方式顺序采集4路模拟量,并依次存入内部RAM的4个单元中,循环往复,不断采集。最后在主函数中开启中断并进入死循环。
阅读全文