unsigned int getCurrentuA(void) { unsigned int uA; float voltage; int adresult; VCHAN = CSENSE; VREF = VDDREF; adresult = getADCVal(); voltage = ((float)adresult)/1024*Vdd*1000; uA = (unsigned int)(1000*voltage / RESVAL + 0.5); return uA; }
unsigned int getVoutmV(void) { int adresult = 1024; //avoid div/0 float voltage; unsigned int mV; VCHAN = BATT; VREF = VDDREF; adresult = getADCVal(); voltage = ((float)adresult)/1024 * Vdd * UNDIVIDE * 1000; //voltage = ((float)adresult)/1024 * EXPVDD * UNDIVIDE * 1000; mV = (unsigned int)(voltage + 0.5); return mV; }
unsigned int getVbgmV(void) { int adresult; float voltage; unsigned int mV; VCHAN = BG; VREF = VDDREF; while(!HLVDCONbits.BGVST); //wait for stable BG voltage adresult = getADCVal(); VREF = INPUTREF; voltage = (float)adresult/1024 * EXPVDD; mV = (unsigned int)(1000*voltage + 0.5); return mV; }
int main(void) { LPC_GPIO2->DIR |= (1<<10); /* PLL is already setup */ SystemCoreClockUpdate(); /* Blue LED Set as output */ /* Relay EN Set as output */ LPC_GPIO1->DIR |= (1<<5); uartInit(115200); puts("Sys Initted\n"); puts("uart Initted\n"); initSwitch(); puts("Switches Initted\n"); SysTick_Config(SystemCoreClock/1000); puts("SysTick Initted\n"); setup_pwm(1000,4092); puts("PWM Initted\n"); setup_GPIO_INT(); puts("GPIO INT Initted\n"); filt_adc_init(450); delay_ms(500); puts("Filt ADC Initted\n"); while(1) { while(next_run > msTicks) __WFI(); next_run= msTicks + 10; if ((msTicks - last_motion ) > (MAX(W_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS, RGB_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS ) + 2000)) { shut_off_supply(); transfer_to_sleep(); turn_on_supply(); /* Wait for the power to actually come on before starting the ramp up */ } // WW_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(WW_SWITCH) == 0) { if(ww_sw_asserted == 5) { puts("WW SWITCH PRESSED\n"); white_on = !white_on; white_scale = 0; } ww_sw_asserted += 1; } else ww_sw_asserted = 0; // OFF_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(OFF_SWITCH) == 0) { if(off_sw_asserted == 5) { puts("OFF SWITCH PRESSED\n"); white_on = 0; white_scale = 0; rgb_on = 0; rgb_scale = 0; } off_sw_asserted += 1; } else off_sw_asserted = 0; // RGB_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(RGB_SWITCH) == 0) { if(rgb_sw_asserted == 5) { puts("RGB SWITCH PRESSED\n"); rgb_on = !rgb_on; rgb_scale = 0; } if(rgb_sw_asserted == 100) { puts("RGB SWITCH HELD\n"); rgb_on = 2; } if(rgb_sw_asserted == 500) { puts("RGB SWITCH LONG HELD\n"); rgb_on = 3; } rgb_sw_asserted += 1; } else rgb_sw_asserted = 0; /* This handles the WW LEDs */ if (((msTicks - last_motion ) > W_POWER_OFF_TIME_MS) && white_on) { white_scale = RAMP_DOWN_TIME_MS - ((msTicks - last_motion) - W_POWER_OFF_TIME_MS); if ((msTicks - last_motion) > (W_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS)) white_scale = 0; } if ( white_scale < RAMP_DOWN_TIME_MS && white_on && ((msTicks - last_motion ) < W_POWER_OFF_TIME_MS)) { white_scale = msTicks - last_motion; /* Check just in case we get held up and skip the 1000th ms after motion */ if ( white_scale > RAMP_DOWN_TIME_MS) white_scale = RAMP_DOWN_TIME_MS; } if (white_scale != RAMP_DOWN_TIME_MS) { setWW( (white_scale * getADCVal(WW_POT)) / RAMP_DOWN_TIME_MS ); setWW2( (white_scale * getADCVal(WW_POT)) / RAMP_DOWN_TIME_MS ); } else { setWW(getADCVal(WW_POT)); setWW2(getADCVal(WW_POT)); } /* This handles the RGB LEDs */ if (((msTicks - last_motion ) > RGB_POWER_OFF_TIME_MS) && rgb_on) { rgb_scale = RAMP_DOWN_TIME_MS - ((msTicks - last_motion) - RGB_POWER_OFF_TIME_MS); if ((msTicks - last_motion) > (RGB_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS)) rgb_scale = 0; } if ( rgb_scale < RAMP_DOWN_TIME_MS && rgb_on && ((msTicks - last_motion ) < RGB_POWER_OFF_TIME_MS)) { rgb_scale = msTicks - last_motion; /* Check just in case we get held up and skip the 1000th ms after motion */ if ( rgb_scale > RAMP_DOWN_TIME_MS) rgb_scale = RAMP_DOWN_TIME_MS; } if (rgb_on == 1 || rgb_on == 0) { if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * getADCVal(RED_POT)) / RAMP_DOWN_TIME_MS, (rgb_scale * getADCVal(GREEN_POT)) / RAMP_DOWN_TIME_MS, (rgb_scale * getADCVal(BLUE_POT)) / RAMP_DOWN_TIME_MS ); else setRGB(getADCVal(RED_POT), getADCVal(GREEN_POT), getADCVal(BLUE_POT)); } else if (rgb_on == 2) { uint32_t R,G,B; h2rgb((msTicks)%(H2RGB_OUT_RANGE*6), &R, &G, &B); if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * R) / RAMP_DOWN_TIME_MS, (rgb_scale * G) / RAMP_DOWN_TIME_MS, (rgb_scale * B) / RAMP_DOWN_TIME_MS ); else setRGB(R, G, B); } else if (rgb_on == 3) { uint32_t R,G,B; if (rgb_mode_3_next_change < msTicks) { rgb_mode_3_next_change = msTicks + (getADCVal(RED_POT)/4); rgb_mode_3_color += 2; if(rgb_mode_3_color >=6) rgb_mode_3_color = 0; } h2rgb((H2RGB_OUT_RANGE * rgb_mode_3_color), &R, &G, &B); if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * R) / RAMP_DOWN_TIME_MS, (rgb_scale * G) / RAMP_DOWN_TIME_MS, (rgb_scale * B) / RAMP_DOWN_TIME_MS ); else setRGB(R, G, B); } } }