Esempio n. 1
0
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
Esempio n. 2
0
void main(void)
{
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();

    TMR0_Initialize();
    TMR0_SetInterruptHandler(tmr0_handler);

    EPWM1_Initialize();
    
    EUSART_Initialize();
    
    PROTOCOL_Initialize(DEVICE_ID, start_handler, stop_handler, set_handler);
    PROTOCOL_Loop();
}
Esempio n. 3
0
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    EUSART_Initialize();
    EPWM1_Initialize();

    uint16_t dutyValue;
    uint16_t angle;
    uint8_t c;
    uint8_t buf[32] = {'\0'};
    uint8_t cnt = 0;
    
    // read angle from EEPROM
    angle = (uint16_t)DATAEE_ReadByte(0);
    // printf("read angle from EEPROM: %d\n", angle);
    dutyValue = calc_duty(angle);
    EPWM1_LoadDutyValue(dutyValue);
            
    while (1)
    {
        __delay_ms(500);
        LATCbits.LATC4 ^= 1;

        do {
            c = EUSART_Read();
            if (c == '\n') {
                buf[cnt] = '\0';
                cnt = 0;
                if (strcmp(buf, "w") == 0) {
                    DATAEE_WriteByte(0, (uint8_t)angle);
                    // printf("write the last angle onto EEPROM: %d\n", angle);
                } else {
                    angle = atoi(buf);
                    dutyValue = calc_duty(angle);
                    EPWM1_LoadDutyValue(dutyValue);
                }
            } else {
                buf[cnt++] = c;
            }
        } while (EUSART_DataReady);

        CLRWDT();
    } 
}
/**
 * @brief Main application
 */
void main()
{
    // initialize the device
    SYSTEM_Initialize();

    // 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();

    // Initialize Remote's program to start state
    
    // Connect to all peripherals
    connect_to_peripherals();
    
    // Flash indicator LEDs
//    indicatorLEDsPassed = btn_test_LEDs();
    
//    if (! (spellButtonsPassed & indicatorLEDsPassed) ) {
//         We have failed so bad if we get here...
//         Show failure
//    }
    
    // Indicate success or failure
    
    while (1)
    {

    }
}
Esempio n. 5
0
/*
                         Main application
 */
void main(void) {
    // initialize the device
    SYSTEM_Initialize();

    // 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();

    while (1) {
        // Add your application code
    }
}
Esempio n. 6
0
void initVGA( void)
{
    // 1. init Timer 2
    TMR2_Period8BitSet( 253);
    
    // 2. UART baudrate
    SPBRGL = 0;

    // 3. PWM intialize
    PWM4_LoadDutyValue( 60);

    // 5. init the vertical sync state machine
    VState = SV_PREEQ; 
    VCount = 1;                 

    TMR2_Start();
    
    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

} // initVGA
Esempio n. 7
0
/*
                         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();
    }
    
}
Esempio n. 8
0
void main(void) 
{
    double angle;
    int minX=0,maxX=0,minY=0,maxY=0; // pour la calib
    float dX,dY; // pareil
    
    SYSTEM_Initialize();
    LIS3MDL_init();
    
    zeroNeedle(); // fait revenir l'aiguille en butee a gauche
    
    
    INTERRUPT_GlobalInterruptEnable(); 
    INTERRUPT_PeripheralInterruptEnable(); // autorise l'UART a recevoir des messages
    RC2=1; // active le puce bluetooth
    __delay_ms(100);
    printf("str\r"); // met la puce en mode stream, cf doc sur le site truconnect
    
    for(int i=1000;i>0;i--) // calib sur 1000 points (il faut physiquement faire tourner le magneto sur 360deg ou plus sans changer l'assiete (a vZ doit pas trop changer)))
    {
        LIS3MDL_Wait();
        LIS3MDL_Read_Raw_XY();
        if(vX<minX)
            minX=vX;
        if(vX>maxX)
            maxX=vX;
        if(vY<minY)
            minY=vY;
        if(vY>maxY)
            maxY=vY;
        display(i/10);
    }
    
    dX = maxX-minX;
    dY = maxY-minY;
    Xsf=dY/dX;
    if(Xsf<1)
        Xsf=1;
    Ysf=dX/dY;
    if(Ysf<1)
        Ysf=1;
    
    Xoff= ((dX)/2 - maxX) * Xsf;
    Yoff = ((dY)/2 - maxY) * Ysf;
    
    
    while (1) 
    {
        //LIS3MDL_Read_XYZ();
        //norme = sqrt(((long)vX*(long)vX)+((long)vY*(long)vY)+((long)vZ*(long)vZ));
        //printf("X: %d  Y: %d \r\n",vX,vY);
        LIS3MDL_Wait();
        LIS3MDL_Read_XY();
        
        //LIS3MDL_Wait();
        angle = ((argXY()/M_PI)*180)+180;
        //display((int)angle);
        //goToPos((int)angle); // Fait bouger l'aiguille
        //printf("Angle: %f \r\n",angle); //Envoi l'angle par bluetooth
       //printf("%d\r\n",angle);
        //stepClock();
        __delay_ms(200);
        
        

    }
}
Esempio n. 9
0
/****************************************************************************
 *
 *                        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();
//
//        }
 
    }
}