/* --------------------------------------------------------------------------- * power.c - v0.1 (c) 2007 Micro-key bv * --------------------------------------------------------------------------- * Micro-key bv * Industrieweg 28, 9804 TG Noordhorn * Postbus 92, 9800 AB Zuidhorn * The Netherlands * Tel: +31 594 503020 * Fax: +31 594 505825 * Email: support@microkey.nl * Web: www.microkey.nl * --------------------------------------------------------------------------- * Description: Checks for power supply * --------------------------------------------------------------------------- * Version(s): 0.1, 28-11-2007, fvds. * Creation. * --------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------- * System include files * --------------------------------------------------------------------------- */ #include #include /* --------------------------------------------------------------------------- * Application include files * --------------------------------------------------------------------------- */ #include "LPC23xx.h" #include "types.h" #include "power.h" /* FreeRTOS includes */ #include "FreeRTOS.h" /* --------------------------------------------------------------------------- * Local constant and macro definitions * --------------------------------------------------------------------------- */ #define ADC_INTERRUPT_FLAG 1 /* 1 is interrupt driven, 0 is polling */ #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 LPC23xx */ #define ADC_CLK 1000000 /* set to 1Mhz */ #define VCC_DIVIDER_R1 22000 #define VCC_DIVIDER_R2 19100 #define VCC_DIVIDER_CORR ((VCC_DIVIDER_R2 * 1000) / (VCC_DIVIDER_R1 + VCC_DIVIDER_R2)) #define V24_DIVIDER_R1 33000 #define V24_DIVIDER_R2 3300 #define V24_DIVIDER_CORR ((V24_DIVIDER_R2 * 1000) / (V24_DIVIDER_R1 + V24_DIVIDER_R2)) /* --------------------------------------------------------------------------- * Global variable definitions * --------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------- * Local variable definitions * --------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------- * Local function definitions * --------------------------------------------------------------------------- */ static UINT16 ADC0Read( UINT8 channelNum ); static UINT16 Convert2mV( UINT32 dataRegistry ); void powerInit() { /* Enable CLOCK into ADC controller */ PCONP |= (1 << 12); /* all the related pins are set to ADC inputs, AD0.0, 4 and 5 */ PINSEL1 &= ~0x0000C000; /* P0.23, A0.0, function 01 */ PINSEL1 |= 0x00004000; PINSEL3 |= 0xC0000000; /* P1.31, A0.5, function 11 */ AD0CR = ( 0x21 << 0 ) | /* SEL=1,select channel 0,4 and 5 on ADC0 */ ( 0 << 8 ) | /* CLKDIV = Fpclk / 1000000 - 1 => 2.4 MHz */ ( 1 << 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) */ } UINT16 powerVccVoltage() { UINT32 adcDataRegister; UINT32 voltage; adcDataRegister = AD0DR5; voltage = Convert2mV( adcDataRegister ); // Correct voltage divider voltage = (voltage * 1000)/VCC_DIVIDER_CORR; return voltage; } UINT16 powerV24Voltage() { UINT32 adcDataRegister; UINT32 voltage; adcDataRegister = AD0DR0; voltage = Convert2mV( adcDataRegister ); // Correct voltage divider voltage = (voltage * 1000)/V24_DIVIDER_CORR; return voltage; } UINT16 Convert2mV( UINT32 dataRegister ) { UINT32 pwrDummy; dataRegister = dataRegister >> 6; dataRegister = dataRegister & 0x03FF; // Convert ADC value to voltage (0..1024 -> 0.. 3300 mV) dataRegister = (dataRegister * 3300) / (1024); pwrDummy = dataRegister; return (UINT16)dataRegister; } UINT16 ADC0Read( UINT8 channelNum ) { UINT16 regVal, ADC_Data; /* channel number is 0 through 7 */ if ( channelNum >= ADC_NUM ) { channelNum = 0; /* reset channel number to 0 */ } AD0CR &= 0xFFFFFF00; AD0CR |= (1 << 24) | (1 << channelNum); /* switch channel,start A/D convert */ while ( 1 ) /* wait until end of A/D convert */ { regVal = *(volatile unsigned long *)(AD0_BASE_ADDR + ADC_OFFSET + ADC_INDEX * channelNum); /* read result of A/D conversion */ if ( regVal & ADC_DONE ) { break; } } AD0CR &= 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 */ }