ADC数模转换——ARM的Proteus实验

实验原理

使用ARM微处理器内置的AD转换,将电压值转换为数字量后直接输出到串口UART0。

Proteus仿真电路图

proteus仿真ARM的ADC转换电压值

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++);
}
}
菜鸟的ARM学习笔记(第一阶段)
  1. LED闪烁——ARM的Proteus实验
  2. 开关控制LED——ARM的Proteus实验
  3. LCD——ARM的Proteus实验
  4. UART——ARM的Proteus实验
  5. Eint1外部中断——ARM的Proteus实验
  6. 多个外部中断——ARM的Proteus实验
  7. 中断结合串口——ARM的Proteus实验
  8. 定时器——ARM的Proteus实验
  9. SPI通信——ARM的Proteus实验
  10. SPI通信(多从设备)——ARM的Proteus实验
  11. ADC数模转换——ARM的Proteus实验

 

返回单片机目录, ARM目录, 返回首页