ADC数模转换——ARM的Proteus实验
实验原理
使用ARM微处理器内置的AD转换,将电压值转换为数字量后直接输出到串口UART0。
Proteus仿真电路图
C语言源程序
main.c
#include <LPC21XX.H>
#include "uart0.h"
typedef unsigned int uint32;
void delay(void) {
unsigned volatile long i,j;
for(i=0;i<10000;i++)
for(j=0;j<50;j++)
;
}
void adcRead (void) {
unsigned int val;
ADCR |= 0x01000000; /* 开始AD转换 */
do {
val = ADDR; /* 读取AD转换数据寄存器 */
} while ((val & 0x80000000) == 0); /* 等待AD转换结束 */
ADCR &= ~0x01000000; /* 结束AD转换 */
val = (val >> 6) & 0x03FF; /* 设置数据格式并且按照16进制输出 */
putstr ("\nAIN0 Result = 0x");
puthex((val >> 8) & 0x0F);
puthex((val >> 4) & 0x0F);
puthex (val & 0x0F);
}
int main(void)
{
ADCR = 0x002E0401; /* Setup A/D: 10-bit AIN0 @ 3MHz */
PINSEL0 = 0x20000005; /*引脚选中EINT1功能,开串口UART0*/
PINSEL1 = 0x00000001; /*引脚选中EINT0功能*/
uart0Init();
while (1) {
adcRead();
delay();
}
}
uart0.c
#include <LPC21XX.H>
#include "uart0.h"
#define CR 0x0D
int putchar (int ch) { /* 向串口输出一个字符 */
if (ch == '\n') {
while (!(U0LSR & 0x20));
U0THR = CR;
}
while (!(U0LSR & 0x20));
return (U0THR = ch);
}
void serialPuts(char *p){ /* 向串口输出字符串 */
while (*p != '\0'){
putchar(*p++);
}
putchar('\n');
}
void uart0Init(void){
U0LCR = 0x83; /* 8位数据,无效验,一个停止位 */
U0DLL = 97; /* VPB 15MHz的时候波特率为9600 */
U0LCR = 0x03; /* DLAB = 0 */
}
void puthex (int hex) { /* Write Hex Digit to Serial Port */
if (hex > 9) putchar('A' + (hex - 10));
else putchar('0' + hex);
}
void putstr (char *p) { /* Write string */
while (*p) {
putchar(*p++);
}
}