/*********************************************************************************** * @prototype void Timer1Init(void) * @description Initializes Timer 1 * @param None * @return None ***********************************************************************************/ void Timer1Init(void) { TMR1_SetPrescaler(TMR1_PRESCALER_1_256); TCNT1 = TIM1_INIT; // Set timer 1 counter (1/(16L/256))*(2^16-TIM1_INIT) TMR1_StartTimer(); }
void delay(int count, int factor) { int i; TMR1_StopTimer(); TMR1_WriteTimer(factor); for (i = 0; i < count; i++) { TMR1_StartTimer(); while (!TMR1_HasOverflowOccured()); PIR1bits.TMR1IF = 0; TMR1_StopTimer(); TMR1_WriteTimer(factor); } }
/**************************************************************************** * * Main application * ****************************************************************************/ void main(void) { // adc_result_t vbatt; // raw ADC of battery voltage // adc_result_t vbus; // raw ADC of charger input voltage uint8_t soc_leds; // result of raw ADC to 5 SOC LED conversion uint8_t c; // dbg0, dbg1, // initialize the device SYSTEM_Initializer(); CE_N_SetLow(); // enable the input charger USBA_EN_SetHigh(); // enable usb porta M1_A_SetLow(); // don't care since using pin_ignore/I2C only mode M2_A_SetHigh(); // don't care since using pin_ignore/I2C only mode EM_EN_A_SetHigh(); // don't care since using pin_ignore/I2C only mode USBB_EN_SetHigh(); // enable usb portb M1_B_SetLow(); // don't care since using pin_ignore/I2C only mode M2_B_SetHigh(); // don't care since using pin_ignore/I2C only mode EM_EN_B_SetHigh(); // don't care since using pin_ignore/I2C only mode otg_mode_flag = 0; // initialise variables BTN0_dbstate = 0; // initial pushbutton state = released BTN0_change = 0; // clear pushbutton change flag (no change) BTN1_dbstate = 0; // initial pushbutton state = released BTN1_change = 0; // clear pushbutton change flag (no change) ADC_read_flag = 0; // //ADC_channel = 0; print_start_msg(); __delay_ms(10); // DEBUG charger_init(); __delay_ms(10); //DS: Upon power-up, the UCS1002 will not respond to any SMBus communications for 5.5 ms usb_port_init(USBA_ADDR); // setup the USB smart output chips __delay_ms(10); // DEBUG usb_port_init(USBB_ADDR); // setup the USB smart output chips //enable interrupts - TODO should this wait unitl after i2c init routines? INTCONbits.IOCIF = 0; IOCBF1 = 0; IOCBF2 = 0; IOCBF3 = 0; IOCBF4 = 0; INTCONbits.IOCIE = 1; TMR0_StartTimer(); TMR1_StartTimer(); TMR2_StartTimer(); INTERRUPT_PeripheralInterruptEnable(); INTERRUPT_GlobalInterruptEnable(); // enable global interrupts /** * CORE APPLICATION */ while (1) { // Add your application code //***DEBUG***// if (EUSART_IsDataReady() == 1) // check for input { c = EUSART_GetByte(); // reading RCREG clear RCIF select_status_report(c); } if (A_DET_A_N_GetValue() == 0) FLASHLIGHT_SetHigh(); //***DEBUG***// // check for self-attached cable if (SELF_DETECT_GetValue() == 1) { USBA_EN_SetLow(); putstring0("Self Detect, USBA disabled"); // DEBUG } else USBA_EN_SetHigh(); //grab battery level, input voltage, update SOC byte if (ADC_read_flag == 1) { ADC_read_flag = 0; vbatt = (ADC_GetConversion(channel_AN1) << 1); // input voltage divided by 2 - multiply it back __delay_ms(1); // provide switching time // vbus = (ADC_GetConversion(channel_AN2) << 1); // TODO figure out what to do with this } soc_leds = calc_soc(vbatt); // check for debounced button press // if (BTN0_change && !BTN0_dbstate) // if button state changed and pressed (low) if (DBG_SPARE_change && DBG_SPARE_dbstate) // if button state changed and pressed (high) { FLASHLIGHT_Toggle(); // turn flashlight off and on // BTN0_change = 0; // clear button change flag DBG_SPARE_change = 0; // clear button change flag } // if (BTN1_change && !BTN1_dbstate) // if button state changed and pressed (low) // { // soc_cntr_start_flag = 1; soc_update(soc_leds); // display SOC animation // BTN1_change = 0; // clear button change flag // } // if (soc_cntr_done_flag == 1) // let the SOC display stand for 5s // { // dbg1 += 1; // soc_cntr_start_flag = 0; // soc_cntr_done_flag = 0; // soc_update(dbg1); //// soc_update(CLR_SOC); // } // TODO update more charger regs, react slave_check_fault(CHRGR_ADDR); if (otg_mode_flag == 1) i2c_slave_command(CHRGR_ADDR, 0x01, 0x6B); //REG01 reset watchdog, enable OTG only else i2c_slave_command(CHRGR_ADDR, 0x01, 0x5B); //REG01 reset watchdog, enable charger only // TODO update more usba regs, react slave_check_fault(USBA_ADDR); usb_porta_regs.REG00 = i2c_slave_read(USBA_ADDR, 0x00); //update the current reading // TODO update more usbb regs, react slave_check_fault(USBB_ADDR); usb_portb_regs.REG00 = i2c_slave_read(USBB_ADDR, 0x00); //update the current reading //debug // for (dbg0 = 0; dbg0 < 255; dbg0++); // { // __delay_ms(100); // } // soc_update(dbg1); // dbg1 += 1; // if (dbg1 == 0x1F) // dbg1 = 0; // if (debug_rprt_flag == 1) // { // debug_rprt_flag = 0; // FLASHLIGHT_Toggle(); // // } } }