You are on page 1of 3

/*

===============================================================================
Name : Sanchez_ADC.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#include "lpc17xx_timer.h"
#include "lpc17xx_adc.h"
#include "stdio.h"
#include "lpc17xx_pinsel.h"
#endif

#include <cr_section_macros.h>

//funciones de inicializacion
void timerInit(void);
void adcInit(void);
void initPins(void);

//funciones de LEDS
void prendeLeds(void);
void apagaLeds(void);
void comparar (int acdValue);

//variables
int ledStatus;
int adcStatus;
uint32_t timerValue;
uint32_t adcResReg;
uint16_t adcValue;

//estructuras de configuracion
TIM_TIMERCFG_Type timerConfig;
TIM_MATCHCFG_Type matchConfig;
PINSEL_CFG_Type adcPinConfig;
PINSEL_CFG_Type matPinConfig;

int main(void) {

SystemCoreClockUpdate(); //actualizo variable de frecuencia del reloj

initPins();
adcInit(); //arranca el ADC para que convierta por polling
timerInit(); //arranca el timer con periodo de 1s e interrupciones
habilitadas

while(1) {
}
return 0 ;
}

void timerInit(void) {
//FUNCIONA
//relleno la struct de configuracion de match
matchConfig.MatchChannel = 1;
matchConfig.MatchValue = 1000000; //configuro el match para 1 segundo (un
millon de cuentas del prescaler de 1 us)
matchConfig.ResetOnMatch = ENABLE;
matchConfig.IntOnMatch = ENABLE;

TIM_ConfigMatch(LPC_TIM0, &matchConfig); //configuro match

TIM_ConfigStructInit(TIM_TIMER_MODE, &timerConfig); //relleno struct de


config del timer, por defecto 1 us el prescaler

TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timerConfig); //configuro el timer con la


struct de config
TIM_Cmd(LPC_TIM0, ENABLE); //inicio el contador
NVIC_EnableIRQ(TIMER0_IRQn);
return;
}

void adcInit(void) {

ADC_Init(LPC_ADC, 1000); //habilito el adc


ADC_PowerdownCmd(LPC_ADC, 1); //quito el powerdown
ADC_ChannelCmd(LPC_ADC, 0, ENABLE); //habilito el canal 0
return;
}

void TIMER0_IRQHandler() {
LPC_TIM0->IR |= (1<<1); //baja bandera

ADC_StartCmd(LPC_ADC, ADC_START_NOW); //inicio la conversion

while(ADC_ChannelGetStatus(LPC_ADC, 0, ADC_DATA_DONE) == RESET) {} //espero a


que termine la conversion
ADC_StartCmd(LPC_ADC, ADC_START_CONTINUOUS);
adcValue = ADC_GetData(0);
comparar(adcValue);
return;
}

void initPins(void) {
LPC_GPIO0->FIODIR |= (1<<22); //pin led rojo como salida
// LPC_GPIO3->FIODIR |= (1<<26); //pin led azul como salida
// LPC_GPIO3->FIODIR |= (1<<25); //pin led verde como salida

//relleno estructura configuracion pin adc


adcPinConfig.Portnum = PINSEL_PORT_0;
adcPinConfig.Pinnum = PINSEL_PIN_23;
adcPinConfig.Funcnum = PINSEL_FUNC_1;
adcPinConfig.Pinmode = PINSEL_PINMODE_TRISTATE;
adcPinConfig.OpenDrain = PINSEL_PINMODE_NORMAL;
PINSEL_ConfigPin(&adcPinConfig); //envio configuracion al pin adc
return;
}

void comparar (int acdValue)


{

if (adcValue == 0)
{ LPC_GPIO0->FIOSET |= (1<<22);
}

else {LPC_GPIO0->FIOCLR|= (1<<22);}

return;

You might also like