void InitADC() { Chip_SCU_ADC_Channel_Config(ADC_ID,ADC_CH1); Chip_ADC_Init(LPC_ADC0, & ADCSetup);/* aca debo pasarle un puntero a LPC_DAC_T que es un tipo de dato definido con typedef*/ Chip_ADC_EnableChannel(LPC_ADC0,ADC_CH1,ENABLE);/*ADC_CH1 Y ENABLE son parte de un enum y corresponden a numeros que definen el canal que estoy usando "el 1" y habilitando ese canal*/ Chip_ADC_SetSampleRate(LPC_ADC0, &ADCSetup,ADC_MAX_SAMPLE_RATE); }
/* P0.23 -> AD0 */ void adcInit(void) { ADC_CLOCK_SETUP_T adc; Chip_ADC_Init(LPC_ADC, &adc); Chip_ADC_SetSampleRate(LPC_ADC, &adc, 22000); Chip_ADC_EnableChannel(LPC_ADC, ADC_CH0, ENABLE); Chip_ADC_Int_SetChannelCmd(LPC_ADC, ADC_CH0, ENABLE); Chip_ADC_SetBurstCmd(LPC_ADC, ENABLE); NVIC_EnableIRQ(ADC_IRQn); }
/** \brief ADC Initialization method */ uint8_t init_ADC_EDUCIAA(void) { /** \details * This function initialize the ADC peripheral in the EDU-CIAA board, * with the correct parameters with LPCOpen library. It uses CH1 * * \param none * * \return uint8_t: TBD (to support errors in the init function) * */ static ADC_CLOCK_SETUP_T configADC; configADC.adcRate=1000; /** max 409 KHz*/ configADC.burstMode=DISABLE; configADC.bitsAccuracy=ADC_10BITS; Chip_ADC_Init(LPC_ADC0,&configADC); Chip_ADC_EnableChannel(LPC_ADC0,ADC_CH1,ENABLE); Chip_ADC_SetSampleRate(LPC_ADC0, &configADC,ADC_MAX_SAMPLE_RATE); return TRUE; }
/* * @brief: enable/disable the ADC and DAC peripheral * @param: ENEABLE_AI, DISABLE_AI, ENEABLE_AO, DISABLE_AO * @return: none */ void analogConfig( uint8_t config ){ switch(config){ case ENABLE_ANALOG_INPUTS: { /* Config ADC0 sample mode */ /* ADC_CLOCK_SETUP_T ADCSetup = { 400000, // ADC rate 10, // ADC bit accuracy 0 // ADC Burt Mode (true or false) }; */ ADC_CLOCK_SETUP_T ADCSetup; /* Initialized to default values: * - Sample rate:ADC_MAX_SAMPLE_RATE=400KHz * - resolution: ADC_10BITS * - burst mode: DISABLE */ Chip_ADC_Init( LPC_ADC0, &ADCSetup ); /* Disable burst mode */ Chip_ADC_SetBurstCmd( LPC_ADC0, DISABLE ); /* Set sample rate to 200KHz */ Chip_ADC_SetSampleRate( LPC_ADC0, &ADCSetup, ADC_MAX_SAMPLE_RATE/2 ); /* Disable all channels */ Chip_ADC_EnableChannel( LPC_ADC0,ADC_CH1, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH1, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH2, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH2, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH3, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH3, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH4, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH4, DISABLE ); } break; case DISABLE_ANALOG_INPUTS: /* Disable ADC peripheral */ Chip_ADC_DeInit( LPC_ADC0 ); break; case ENABLE_ANALOG_OUTPUTS: /* Initialize the DAC peripheral */ Chip_DAC_Init(LPC_DAC); /* Enables the DMA operation and controls DMA timer */ Chip_DAC_ConfigDAConverterControl(LPC_DAC, DAC_DMA_ENA); /* DCAR DMA access */ /* Update value to DAC buffer*/ Chip_DAC_UpdateValue(LPC_DAC, 0); break; case DISABLE_ANALOG_OUTPUTS: /* Disable DAC peripheral */ Chip_DAC_DeInit( LPC_DAC ); break; } }
/** * @brief Main routine for ADC example * @return Nothing */ int main(void) { bool end_Flag = false; uint32_t _bitRate = ADC_MAX_SAMPLE_RATE; uint8_t bufferUART; SystemCoreClockUpdate(); Board_Init(); /* Chip_IOCON_PinMux(0, 25, IOCON_ADMODE_EN, IOCON_FUNC1); */ /*ADC Init */ Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup); Chip_ADC_EnableChannel(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE); while (!end_Flag) { DEBUGOUT(WelcomeMenu); while (!end_Flag) { bufferUART = 0xFF; bufferUART = DEBUGIN(); if (bufferUART == 'c') { DEBUGOUT(SelectMenu); bufferUART = 0xFF; while (bufferUART == 0xFF) { bufferUART = DEBUGIN(); if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) { bufferUART = 0xFF; } } switch (bufferUART) { case '1': /* Polling Mode */ App_Polling_Test(); break; case '2': /* Interrupt Mode */ App_Interrupt_Test(); break; case '3': /* DMA mode */ App_DMA_Test(); break; } break; } else if (bufferUART == 'x') { end_Flag = true; DEBUGOUT("\r\nADC demo terminated!"); } else if (bufferUART == 'o') { _bitRate -= _bitRate > 0 ? 1000 : 0; Chip_ADC_SetSampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate); DEBUGOUT("Rate : %ld Sample/s\r\n", _bitRate); } else if (bufferUART == 'p') { _bitRate += _bitRate < 400000 ? 1000 : 0; Chip_ADC_SetSampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate); DEBUGOUT("Rate : %ld Sample/s\r\n", _bitRate); } else if (bufferUART == 'b') { Burst_Mode_Flag = !Burst_Mode_Flag; ADCSetup.burstMode = Burst_Mode_Flag; Chip_ADC_SetSampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate); if (Burst_Mode_Flag) { DEBUGOUT("Burst Mode ENABLED\r\n"); } else { DEBUGOUT("Burst Mode DISABLED\r\n"); } } } } return 0; }
/* Set the ADC accuracy bits */ void Chip_ADC_SetResolution(LPC_ADC_T *pADC, ADC_CLOCK_SETUP_T *ADCSetup, ADC_RESOLUTION_T resolution) { ADCSetup->bitsAccuracy = resolution; Chip_ADC_SetSampleRate(pADC, ADCSetup, ADCSetup->adcRate); }