| //use AD0-3 and AD5-7, don't use AD4(SWDIO) LPC_IOCON->R_PIO0_11= (1<<6)|(2<<0); //select AD0 LPC_IOCON->R_PIO1_0 = (1<<6)|(2<<0); //select AD1 LPC_IOCON->R_PIO1_1 = (1<<6)|(2<<0); //select AD2 LPC_IOCON->R_PIO1_2 = (1<<6)|(2<<0); //select AD3 LPC_IOCON->PIO1_4 = (1<<6)|(1<<0); //select AD5 LPC_IOCON->PIO1_10 = (1<<6)|(1<<0); //select AD6 LPC_IOCON->PIO1_11 = (1<<6)|(1<<0); //select AD7 | 
| int main(void) {
    SysTick_Config( SystemCoreClock/1000 );
    SysTick->CTRL &= (0 << 1);
    ADCInit( ADC_CLK );
    GPIOSetDir( LED_PORT, LED_BIT, 1 );
    GPIOSetValue( LED_PORT, LED_BIT, LED_ON );
    while(1)
    {
        if(reflexRead())
        {
            GPIOSetValue( LED_PORT, LED_BIT, LED_OFF );
        }
        else
        {
            GPIOSetValue( LED_PORT, LED_BIT, LED_ON );
        }
    }
    return 0 ;
}
 | 
| uint16_t reflexRead()
{
    uint32_t i = 4;
    for ( i = 4; i <= 5; i++ )
    {
        reflexValue = ADCRead( i );
        if( reflexValue >= 500 )
        {
            return 1;
        }
    }
    return 0;
}
 | 
| 
#include "LPC13xx.h"            /* LPC13xx Peripheral Registers */
#include "adc.h"
void ADCInit( uint32_t ADC_Clk )
{
  /* Disable Power down bit to the ADC block. */
  LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);
  /* Enable AHB clock to the ADC. */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
  GPIOSetDir( 1, 3, 0 ); //AD4 input
  GPIOSetDir( 1, 4, 0 ); //AD5 input
  GPIOSetDir( 1, 10, 0 );//AD6 input
  GPIOSetDir( 1, 11, 0 );//AD7 input
#ifdef __JTAG_DISABLED
#ifdef __SWD_DISABLED
  LPC_IOCON->ARM_SWDIO_PIO1_3   &= ~0x8F;
  LPC_IOCON->ARM_SWDIO_PIO1_3   |= 0x02;  /* ADC IN4 */
#endif
#endif
  LPC_IOCON->PIO1_4 &= ~0x8F; 
  LPC_IOCON->PIO1_4 |= 0x01; /* ADC IN5 */
  LPC_IOCON->PIO1_10 &= ~0x8F; 
  LPC_IOCON->PIO1_10 |= 0x01; /* ADC IN6 */
  LPC_IOCON->PIO1_11 &= ~0x8F; 
  LPC_IOCON->PIO1_11 |= 0x01; /* ADC IN7 */
  LPC_ADC->CR = ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/ADC_Clk-1)<<8;
  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 number to 0 */
  }
  LPC_ADC->CR &= 0xFFFFFF00; // clear channel selection
  LPC_ADC->CR |= (1 << 24) | (1 << channelNum);
                /* switch channel,start A/D convert */
#if !ADC_INTERRUPT_FLAG
  while ( 1 )            /* wait until end of A/D convert */
  {
    regVal = *(volatile unsigned long *)(LPC_ADC_BASE
            + ADC_OFFSET + ADC_INDEX * channelNum);
    /* read result of A/D conversion */
    if ( regVal & ADC_DONE )
    {
      break;
    }
  }
  LPC_ADC->CR &= 0xF8FFFFFF;    /* stop ADC now */
  if ( regVal & ADC_OVERRUN )    /* save data when it's not overrun, otherwise, return zero */
  {
    return ( 0 );
  }
  ADC_Data = ( regVal >> 6 ) & 0x3FF;
  return ( ADC_Data );    /* return A/D conversion value */
#else
  return ( channelNum );    /* if it's interrupt driven, the ADC reading is
                            done inside the handler. so, return channel number */
#endif
}
 | 
| #ifndef ADC_H_ #define ADC_H_/***************************************************************************** * adc.h: Header file for NXP LPC134x Family Microprocessors * * Copyright(C) 2008, NXP Semiconductor * All rights reserved. * * History * 2008.07.19 ver 1.00 Preliminary version, first Release * ******************************************************************************/ #ifndef __ADC_H #define __ADC_H #include "gpio.h" #define ADC_INTERRUPT_FLAG 0 /* 1 is interrupt driven, 0 is polling */ #define BURST_MODE 0 /* Burst mode works in interrupt driven mode only. */ #define ADC_DEBUG 1 #define ADC_OFFSET 0x10 #define ADC_INDEX 4 #define ADC_DONE 0x80000000 #define ADC_OVERRUN 0x40000000 #define ADC_ADINT 0x00010000 #define ADC_NUM 8 /* for LPC13xx */ #define ADC_CLK 4500000 /* set to 4.5Mhz */ extern void ADC_IRQHandler( void ); extern void ADCInit( uint32_t ADC_Clk ); extern uint32_t ADCRead( uint8_t channelNum ); extern void ADCBurstRead( void ); #endif /* end __ADC_H */ /***************************************************************************** ** End Of File ******************************************************************************/ #endif /* ADC_H_ */ | 
