void TMR2_Initialize(void) { // Set TMR2 to the options selected in the User Interface // T2CKPS 1:4; T2OUTPS 1:8; TMR2ON on; // T2CON = 0x3D; T2CONbits.T2OUTPS = 15; T2CONbits.T2CKPS = 0; // PR2 255; PR2 = 0xFF; // TMR2 0; TMR2 = 0x00; // Clearing IF flag before enabling the interrupt. PIR1bits.TMR2IF = 0; // Enabling TMR2 interrupt. PIE1bits.TMR2IE = 1; // Set Default Interrupt Handler TMR2_SetInterruptHandler(TMR2_DefaultInterruptHandler); // Start TMR2 TMR2_StartTimer(); }
void StartAudio(void) { TMR2_SetInterruptHandler(TMR2_CallBack); // 1. init the timebase TMR2_StartTimer(); // 2. activate the Audio DAC // configured in the System init INTERRUPT_PeripheralInterruptEnable(); INTERRUPT_GlobalInterruptEnable(); } // start audio
void TMR2_Initialize(void) { // Set TMR2 to the options selected in the User Interface // TMR2ON off; T2CKPS 1:1; T2OUTPS 1:1; T2CON = 0x00; // PR2 179; PR2 = 0xB3; // TMR2 0x0; TMR2 = 0x00; // Clearing IF flag before enabling the interrupt. PIR1bits.TMR2IF = 0; // Enabling TMR2 interrupt. PIE1bits.TMR2IE = 1; // Start TMR2 TMR2_StartTimer(); }
/* Main application */ void main(void) { uint16_t u16VoltVar; uint8_t u8CtrlHighCnt; uint8_t u8CtrlLowCnt; uint8_t u8SignalCnt; uint8_t u8VoltCnt; bool bPowerIssue; bool bCheckPower; // initialize the device SYSTEM_Initialize(); InitVariable(); u16VoltVar = 0; u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; u8SignalCnt = 0; u8VoltCnt = 0; bPowerIssue = false; bCheckPower = false; // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); __delay_ms(100); while (1) { if( gu8Mode != MODE_CHECK_VOLT && bPowerIssue == false ) { if( bCheckPower == false ) { if( gu8TMR0State & TMR0_FLAG_1S ) { gu8TMR0State &= ~TMR0_FLAG_1S; u16VoltVar = 0; u16VoltVar = ADC_GetVolt(); // 8.5V = 2250, 12V = 2850, 14V = 3200 1V ~= 170 if( u16VoltVar > DEF_OVP || u16VoltVar < DEF_UVP ) { // Issue u8VoltCnt = 0; bCheckPower = true; } else { bPowerIssue = false; } } } else { if( gu8TMR0State & TMR0_FLAG_10MS ) { gu8TMR0State &= ~TMR0_FLAG_10MS; u16VoltVar = 0; u16VoltVar = ADC_GetVolt(); // To-Do: // 8.5V = 2250, 12V = 2850, 16V = 3530 1V ~= 170 if( u16VoltVar > DEF_OVP || u16VoltVar < DEF_UVP ) { u8VoltCnt++; if( u8VoltCnt > 5 ) { bPowerIssue = true; bCheckPower = false; } } else { u8VoltCnt = 0; bCheckPower = false; } } } } // Add your application code switch( gu8Mode ) { case MODE_CHECK_VOLT: if( gu8TMR0State & TMR0_FLAG_10MS ) { gu8TMR0State &= ~TMR0_FLAG_10MS; u16VoltVar = 0; u16VoltVar = ADC_GetVolt(); // To-Do: // 8.5V = 2250, 12V = 2850, 16V = 3530 1V ~= 170 if( u16VoltVar > DEF_OVP || u16VoltVar < DEF_UVP ) { u8VoltCnt++; if( u8VoltCnt > 5 ) { bPowerIssue = true; } } else { u8VoltCnt = 0; InitMotorPosition(); } } break; case MODE_IN_VOLT: break; case MODE_CHECK_CONTROL: if( gu8TMR0State & TMR0_FLAG_10MS ) { gu8TMR0State &= ~TMR0_FLAG_10MS; u16VoltVar = 0; u16VoltVar = ADC_GetVolt(); // To-Do: // 8.5V = 2250, 12V = 2850, 16V = 3530 1V ~= 170 if( u16VoltVar > DEF_OVP || u16VoltVar < DEF_UVP ) { u8VoltCnt++; if( u8VoltCnt > 5 ) { gu8Mode = MODE_CHECK_VOLT; } } else { if( IO_CONTROL_GetValue() == HIGH ) { u8CtrlHighCnt++; u8CtrlLowCnt = 0; if( u8CtrlHighCnt > 5 ) { u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; u8SignalCnt = 0; gu8Mode = MODE_CONTROL_HIGH; } } else { u8CtrlHighCnt = 0; u8CtrlLowCnt++; if( u8CtrlLowCnt > 5 ) { u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; u8SignalCnt = 0; gu8Mode = MODE_CONTROL_LOW; } } } } break; case MODE_CONTROL_HIGH: if( gu8TMR0State & TMR0_FLAG_10MS ) { gu8TMR0State &= ~TMR0_FLAG_10MS; IO_FEEDBACK_SetDigitalOutput(); if( IO_FEEDBACK_GetValue() == HIGH ) { // C1F1 if( u8SignalCnt < 5 ) { u8SignalCnt++; } else { gu8Mode = MODE_FORWARD; // CW TMR2_StartTimer(); TMR2_WriteTimer(0); IO_CW_GREEN_SetHigh(); } } else { // C1F0 u8SignalCnt = 0; u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; gu8Mode = MODE_CHECK_CONTROL; } } break; case MODE_CONTROL_LOW: if( gu8TMR0State & TMR0_FLAG_10MS ) { gu8TMR0State &= ~TMR0_FLAG_10MS; IO_FEEDBACK_SetDigitalOutput(); if( IO_FEEDBACK_GetValue() == LOW ) { // C0F0 if( u8SignalCnt < 5 ) { u8SignalCnt++; } else { gu8Mode = MODE_BACKWARD; // CCW TMR2_StartTimer(); TMR2_WriteTimer(0); IO_CCW_RED_SetHigh(); } } else { // C0F1 u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; u8SignalCnt = 0; gu8Mode = MODE_CHECK_CONTROL; } } break; case MODE_FORWARD: if( LOW == IO_CW_GREEN_GetValue() || true == bCheckInTheMiddle() ) { TMR2_StopTimer(); IO_CW_GREEN_SetLow(); IO_B_CTRL_SetHigh(); // Set B signal to low IO_Y_CTRL_SetLow(); // Set Y signal to high IO_W_CTRL_SetHigh(); // Set W signal to low IO_FEEDBACK_SetDigitalOutput(); IO_FEEDBACK_SetLow(); u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; if( bPowerIssue == true ) { bPowerIssue = false; gu8Mode = MODE_CHECK_VOLT; } else { gu8Mode = MODE_CHECK_CONTROL; } } break; case MODE_BACKWARD: if( LOW == IO_CCW_RED_GetValue() || true == bCheckBConnectW() ) { TMR2_StopTimer(); IO_CCW_RED_SetLow(); IO_B_CTRL_SetHigh(); // Set B signal to low IO_Y_CTRL_SetLow(); // Set Y signal to high IO_W_CTRL_SetHigh(); // Set W signal to low IO_FEEDBACK_SetDigitalOutput(); IO_FEEDBACK_SetHigh(); u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; if( bPowerIssue == true ) { bPowerIssue = false; gu8Mode = MODE_CHECK_VOLT; } else { gu8Mode = MODE_CHECK_CONTROL; } } break; case MODE_FINISH: IO_CW_GREEN_SetLow(); IO_CCW_RED_SetLow(); IO_B_CTRL_SetHigh(); // Set B signal to low IO_Y_CTRL_SetLow(); // Set Y signal to high IO_W_CTRL_SetHigh(); // Set W signal to low TMR2_StopTimer(); IO_FEEDBACK_SetDigitalOutput(); if( IO_CONTROL_GetValue() == LOW ) { IO_FEEDBACK_SetHigh(); } else { IO_FEEDBACK_SetLow(); } u8CtrlHighCnt = 0; u8CtrlLowCnt = 0; if( bPowerIssue == true ) { bPowerIssue = false; gu8Mode = MODE_CHECK_VOLT; } else { gu8Mode = MODE_CHECK_CONTROL; } break; default: break; } CLRWDT(); } }
/**************************************************************************** * * 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(); // // } } }