/****************************************************************************
* $Id:: adc.c 4085 2010-07-30 22:31:04Z usb00423 $
* Project: NXP LPC11xx ADC example
*
* Description:
* This file contains ADC code example which include ADC
* initialization, ADC interrupt handler, and APIs for ADC
* reading.
*
****************************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
****************************************************************************/
#include "LPC11xx.h" /* LPC11xx Peripheral Registers */
#include "adc.h"
volatile uint32_t ADCValue[ADC_NUM];
volatile uint32_t ADCIntDone = 0;
volatile uint32_t OverRunCounter = 0;
#if BURST_MODE
volatile uint32_t channel_flag = 0;
#endif
#if ADC_INTERRUPT_FLAG
/******************************************************************************
** Function name: ADC_IRQHandler
**
** Descriptions: ADC interrupt handler
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void ADC_IRQHandler (void)
{
uint32_t regVal, i;
regVal = LPC_ADC->STAT; /* Read ADC will clear the interrupt */
if ( regVal & 0x0000FF00 ) /* check OVERRUN error first */
{
OverRunCounter++;
for ( i = 0; i < ADC_NUM; i++ )
{
regVal = (regVal & 0x0000FF00) >> 0x08;
/* if overrun, just read ADDR to clear */
/* regVal variable has been reused. */
if ( regVal & (0x1 << i) )
{
regVal = LPC_ADC->DR[i];
}
}
LPC_ADC->CR &= 0xF8FFFFFF; /* stop ADC now */
ADCIntDone = 1;
return;
}
if ( regVal & ADC_ADINT )
{
for ( i = 0; i < ADC_NUM; i++ )
{
if ( (regVal&0xFF) & (0x1 << i) )
{
ADCValue[i] = ( LPC_ADC->DR[i] >> 6 ) & 0x3FF;
}
}
#if BURST_MODE
channel_flag |= (regVal & 0xFF);
if ( (channel_flag & 0xFF) == 0xFF )
{
/* All the bits in have been set, it indicates all the ADC
channels have been converted. */
LPC_ADC->CR &= 0xF8FFFFFF; /* stop ADC now */
channel_flag = 0;
ADCIntDone = 1;
}
#else
LPC_ADC->CR &= 0xF8FFFFFF; /* stop ADC now */
ADCIntDone = 1;
#endif
}
return;
}
#endif
/*****************************************************************************
** Function name: ADCInit
**
** Descriptions: initialize ADC channel
**
** parameters: ADC clock rate
** Returned value: None
**
*****************************************************************************/
void ADCInit_NXP( uint32_t ADC_Clk )
{
uint32_t i;
/* Disable Power down bit to the ADC block. */
LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);
/* Enable AHB clock to the ADC. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
for ( i = 0; i < ADC_NUM; i++ )
{
ADCValue[i] = 0x0;
}
/* Unlike some other pings, for ADC test, all the pins need
to set to analog mode. Bit 7 needs to be cleared according
to design team. */
#ifdef __JTAG_DISABLED
LPC_IOCON->R_PIO0_11 &= ~0x8F; /* ADC I/O config */
LPC_IOCON->R_PIO0_11 |= 0x02; /* ADC IN0 */
LPC_IOCON->R_PIO1_0 &= ~0x8F;
LPC_IOCON->R_PIO1_0 |= 0x02; /* ADC IN1 */
LPC_IOCON->R_PIO1_1 &= ~0x8F;
LPC_IOCON->R_PIO1_1 |= 0x02; /* ADC IN2 */
LPC_IOCON->R_PIO1_2 &= ~0x8F;
LPC_IOCON->R_PIO1_2 |= 0x02; /* ADC IN3 */
LPC_IOCON->SWDIO_PIO1_3 &= ~0x8F;
LPC_IOCON->SWDIO_PIO1_3 |= 0x02; /* ADC IN4 */
#endif
LPC_IOCON->PIO1_4 &= ~0x8F; /* Clear bit7, change to analog mode. */
LPC_IOCON->PIO1_4 |= 0x01; /* ADC IN5 */
LPC_IOCON->PIO1_10 &= ~0x8F; /* Clear bit7, change to analog mode. */
LPC_IOCON->PIO1_10 |= 0x01; /* ADC IN6 */
LPC_IOCON->PIO1_11 &= ~0x8F; /* Clear bit7, change to analog mode. */
LPC_IOCON->PIO1_11 |= 0x01; /* ADC IN7 */
LPC_ADC->CR = ( 0x01 << 0 ) | /* SEL=1,select channel 0~7 on ADC0 */
( ( SystemAHBFrequency / ADC_Clk - 1 ) << 8 ) | /* CLKDIV = Fpclk / 1000000 - 1 */
( 0 << 16 ) | /* BURST = 0, no BURST, software controlled */
( 0 << 17 ) | /* CLKS = 0, 11 clocks/10 bits */
( 1 << 21 ) | /* PDN = 1, normal operation */
( 0 << 22 ) | /* TEST1:0 = 00 */
( 0 << 24 ) | /* START = 0 A/D conversion stops */
( 0 << 27 ); /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
/* If POLLING, no need to do the following */
#if ADC_INTERRUPT_FLAG
NVIC_EnableIRQ(ADC_IRQn);
LPC_ADC->INTEN = 0x1FF; /* Enable all interrupts */
#endif
return;
}
void ADCInit(uint32_t ADC_Clk)
{
uint32_t i;
/* Disable Power down bit to the ADC block. */
LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);
/* Enable AHB clock to the ADC. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
for ( i = 0; i < ADC_NUM; i++ )
{
ADCValue[i] = 0x0;
}
/* Unlike some other pings, for ADC test, all the pins need
to set to analog mode. Bit 7 needs to be cleared according
to design team. */
#ifdef __JTAG_DISABLED
LPC_IOCON->R_PIO0_11 &= ~0x8F; /* ADC I/O config */
LPC_IOCON->R_PIO0_11 |= 0x02; /* ADC IN0 */
LPC_IOCON->R_PIO1_0 &= ~0x8F;
LPC_IOCON->R_PIO1_0 |= 0x02; /* ADC IN1 */
LPC_IOCON->R_PIO1_1 &= ~0x8F;
LPC_IOCON->R_PIO1_1 |= 0x02; /* ADC IN2 */
LPC_IOCON->R_PIO1_2 &= ~0x8F;
LPC_IOCON->R_PIO1_2 |= 0x02; /* ADC IN3 */
LPC_IOCON->SWDIO_PIO1_3 &= ~0x8F;
LPC_IOCON->SWDIO_PIO1_3 |= 0x02; /* ADC IN4 */
#endif
//LPC_IOCON->PIO1_4 &= ~0x8F; /* Clear bit7, change to analog mode. */
//LPC_IOCON->PIO1_4 |= 0x01; /* ADC IN5 */
//LPC_IOCON->PIO1_10 &= ~0x8F; /* Clear bit7, change to analog mode. */
//LPC_IOCON->PIO1_10 |= 0x01; /* ADC IN6 */
//LPC_IOCON->PIO1_11 &= ~0x8F; /* Clear bit7, change to analog mode. */
//LPC_IOCON->PIO1_11 |= 0x01; /* ADC IN7 */
LPC_IOCON->R_PIO0_11 &= ~0x87; /* Clear bit7, change to analog mode. */
LPC_IOCON->R_PIO0_11 |= 0x02; /* ADC IN0 */
LPC_IOCON->R_PIO1_2 &= ~0x87; /* Clear bit7, change to analog mode. */
LPC_IOCON->R_PIO1_2 |= 0x02; /* ADC IN3 */
LPC_ADC->CR = ( 0x01 << 0 ) | /* SEL=1,select channel 0~7 on ADC0 */
( ( SystemAHBFrequency / ADC_Clk - 1 ) << 8 ) | /* CLKDIV = Fpclk / 1000000 - 1 */
( 0 << 16 ) | /* BURST = 0, no BURST, software controlled */
( 0 << 17 ) | /* CLKS = 0, 11 clocks/10 bits */
( 1 << 21 ) | /* PDN = 1, normal operation */
( 0 << 22 ) | /* TEST1:0 = 00 */
( 0 << 24 ) | /* START = 0 A/D conversion stops */
( 0 << 27 ); /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
/* If POLLING, no need to do the following */
#if ADC_INTERRUPT_FLAG
NVIC_EnableIRQ(ADC_IRQn);
LPC_ADC->INTEN = 0x1FF; /* Enable all interrupts */
#endif
return;
}
/*****************************************************************************
** Function name: ADCRead
**
** Descriptions: Read ADC channel
**
** parameters: Channel number
** Returned value: Value read, if interrupt driven, return channel #
**
*****************************************************************************/
uint32_t ADCRead( uint8_t channelNum )
{
#if !ADC_INTERRUPT_FLAG
uint32_t regVal, ADC_Data;
#endif
/* channel number is 0 through 7 */
if ( channelNum >= ADC_NUM )
{
channelNum = 0; /* reset channel numbe
adc.zip_LPC1114_M0 ADC
版权申诉
71 浏览量
2022-09-24
04:55:02
上传
评论
收藏 3KB ZIP 举报
局外狗
- 粉丝: 66
- 资源: 1万+
最新资源
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
- for daily read
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈