/************************************************************************
*** Copyright(c) 2011, by XH Du. ******
*** Filename: LPC11xx_ADC_user.c
*** Current Version: 1.0
*** Complete Date: 2011-4-21
*** Fuction: Timer32_0 driver to ADC
************************************************************************/
#include "LPC11xx.h"
#include "LPC11xx_ADC_user.h"
#define ADCTIMER_MAT (SystemAHBFrequency/800 - 1) // Timer 10ms SystemAHBFrequency = 48MHZ
#define ADC_ADINT 0x00010000
#define ADC_CLK 1000000 /* set to 1Mhz */
void init()
{
ADCInit( ADC_CLK );
}
/******************************************************************************
** Function name: ADC_IRQHandler
**
** Descriptions: ADC interrupt handler
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void ADC_IRQHandler (void)
{
uint32_t regVal;
regVal = LPC_ADC->STAT; /* Read ADC will clear the interrupt */
if ( regVal & 0x0000FF00 ) /* check OVERRUN error first */
{
regVal = (regVal & 0x0000FF00) >> 0x08;
switch ( regVal )
{
case 0x01:
regVal = LPC_ADC->DR[0];
break;
case 0x02:
regVal = LPC_ADC->DR[1];
break;
case 0x04:
regVal = LPC_ADC->DR[2];
break;
case 0x08:
regVal = LPC_ADC->DR[3];
break;
case 0x10:
regVal = LPC_ADC->DR[4];
break;
case 0x20:
regVal = LPC_ADC->DR[5];
break;
case 0x40:
regVal = LPC_ADC->DR[6];
break;
case 0x80:
regVal = LPC_ADC->DR[7];
break;
default:
break;
}
}
if ( regVal & ADC_ADINT )
{
switch ( regVal & 0xFF ) /* check DONE bit */
{
case 0x01:
regVal = ( LPC_ADC->DR[0] >> 6 ) & 0x3FF; // read ADC value
break;
case 0x02:
regVal = ( LPC_ADC->DR[1] >> 6 ) & 0x3FF;
break;
case 0x04:
regVal = ( LPC_ADC->DR[2] >> 6 ) & 0x3FF;
break;
case 0x08:
regVal = ( LPC_ADC->DR[3] >> 6 ) & 0x3FF;
break;
default:
break;
}
}
}
/*****************************************************************************
** Function name: ADCInit
**
** Descriptions: initialize ADC channel
**
** parameters: ADC clock rate
** Returned value: None
*****************************************************************************/
void ADCInit( uint32_t ADC_Clk)
{
/* Disable Power down bit to the ADC block. ADC Power UP*/
LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);
/* Enable AHB clock to the ADC. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
LPC_IOCON->JTAG_TDI_PIO0_11 &= ~0x9F; /* Clear bit7, change to analog mode. Clear bit4-3,remove pull-up/pull down resistor*/
LPC_IOCON->JTAG_TDI_PIO0_11 |= 0x02; /* Select ADC IN0 */
LPC_IOCON->JTAG_TMS_PIO1_0 &= ~0x9F; /* Clear bit7, change to analog mode. */
LPC_IOCON->JTAG_TMS_PIO1_0 |= 0x02; /* ADC IN1 */
LPC_IOCON->JTAG_TDO_PIO1_1 &= ~0x9F; /* Clear bit7, change to analog mode. */
LPC_IOCON->JTAG_TDO_PIO1_1 |= 0x02; /* ADC IN2 */
LPC_IOCON->JTAG_nTRST_PIO1_2 &= ~0x9F; /* Clear bit7, change to analog mode. */
LPC_IOCON->JTAG_nTRST_PIO1_2 |= 0x02; /* ADC IN3 */
LPC_ADC->CR = ((SystemAHBFrequency / ADC_Clk - 1 ) << 8 ); /* CLKDIV = Fpclk / 1000000 - 1 */
NVIC_EnableIRQ(ADC_IRQn);
LPC_ADC->INTEN = 0x00F; /* Enable all interrupts */
LPC_ADC->CR &= ~(( 0x01 << 17 ) | ( 1 << 18 ) | ( 1 << 19 ) | /* CLKS = 0, 11 clocks/10 bits */
( 1 << 24 ) | ( 1 << 25 ) | ( 1 << 16 )); /* START = 0 A/D conversion stops BURST=0*/
/* Channel select CH-0,1,2,3,7, BURST = 1, hardware controlled 26-24: 100 Timer320-MAT0 , 27:1 MAT0 Falling edge*/
LPC_ADC->CR |= ((0x01<<0) | ( 0x1 << 1 ) | ( 1 << 2 ) | ( 1 << 3 ) | ( 1 << 26 ) | (1<<27)); /* Start conversion when the falling edge on CT32B0_MAT0*/
ADCTimerInit(ADCTIMER_MAT); // Timer
}
/******************************************************************************
** Function name: init_timer
**
** Descriptions: Initialize timer, set timer interval, reset timer,
** install timer interrupt handler
**
** parameters: timer interval
** Returned value: None
**
******************************************************************************/
void ADCTimerInit(uint32_t MATInterval)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); /* Enable AHB clock to the Timer32-0. */
LPC_TMR32B0->PR = 0x07; /* set prescaler to 8 SystemAHBFrequency/8 = 6MHZ*/
LPC_TMR32B0->MR0 = MATInterval; // Match value (SystemAHBFrequency/800)*(1/6M) = 10ms
LPC_TMR32B0->EMR &= ~(0xFF<<4);
LPC_TMR32B0->EMR |= (0x3<<4); /* Timer32_0_MAT0 Match 0 driver adc*/
LPC_TMR32B0->MCR |= (0x01<<1); /* Match Reset on MR0 */
EnableADCTimer();
}
/******************************************************************************
** Function name: EnableADCTimer
**
** Descriptions: Enable timer
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void EnableADCTimer(void)
{
LPC_TMR32B0->TCR = 1;
}
/******************************************************************************
** Function name: DisableADCTimer
**
** Descriptions: Disable timer
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void DisableADCTimer(void)
{
LPC_TMR32B0->TCR = 0;
}
阅读(3007) | 评论(0) | 转发(0) |