如何在ARM7中进行A/D转换

控制/MCU

1882人已加入

描述

主程序:/******************************************************************************/

/* This file is part of the uVision/ARM development tools */

/* Copyright KEIL ELEKTRONIK GmbH 2002-2004 */

/******************************************************************************/

/* */

/* BLINKY.C: LED Flasher */

/* */

/******************************************************************************/

#include /* LPC21xx definitions */

extern void init_serial (void); /* Initialize Serial Interface */

extern int putchar (int ch); /* Write character to Serial Port */

extern int getchar (void); /* Read character from Serial Port */

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++);

}

}

void delay (void) { /* Delay function */

unsigned int cnt;

unsigned int val;

ADCR |= 0x01000000; /* Start A/D Conversion */

do {

val = ADDR; /* Read A/D Data Register */

} while ((val & 0x80000000) == 0); /* Wait for end of A/D Conversion */

ADCR &= ~0x01000000; /* Stop A/D Conversion */

val = (val 》》 6) & 0x03FF; /* Extract AIN0 Value */

putstr (“\nAIN0 Result = 0x”); /* Output A/D Conversion Result */

puthex((val 》》 8) & 0x0F); /* Write 1. Hex Digit */

puthex((val 》》 4) & 0x0F); /* Write 2. Hex Digit */

puthex (val & 0x0F); /* Write 3. Hex Digit */

val = (val 》》 2) 《《 12; /* Adjust Delay Value */

for (cnt = 0; cnt 《 val; cnt++); /* Delay */

}

int main (void) {

unsigned int n;

IODIR1 = 0x00FF0000; /* P1.16..23 defined as Outputs */

ADCR = 0x002E0401; /* Setup A/D: 10-bit AIN0 @ 3MHz */

init_serial(); /* Initialize Serial Interface */

IOSET1=0x00ff0000;

while (1) { /* Loop forever */

for (n = 0x00010000; n 《= 0x00800000; n 《《= 1) {

/* Blink LED 0, 1, 2, 3, 4, 5, 6, 7 */

IOCLR1 = n; /* Turn on LED */

delay(); /* Delay */

IOSET1 = 0x00FF0000; /* Turn off LEDs */

}

}

}

串口程序:

/******************************************************************************/

/* This file is part of the uVision/ARM development tools */

/* Copyright KEIL ELEKTRONIK GmbH 2002-2004 */

/******************************************************************************/

/* */

/* SERIAL.C: Low Level Serial Routines */

/* */

/******************************************************************************/

#include /* LPC21xx definitions */

#define CR 0x0D

void init_serial (void) { /* Initialize Serial Interface */

PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */

U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */

U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */

U1LCR = 0x03; /* DLAB = 0 */

}

int putchar (int ch) { /* Write character to Serial Port */

if (ch == ‘\n’) {

while (!(U1LSR & 0x20));

U1THR = CR; /* output CR */

}

while (!(U1LSR & 0x20));

return (U1THR = ch);

}

int getchar (void) { /* Read character from Serial Port */

while (!(U1LSR & 0x01));

return (U1RBR);

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分