Beispiel #1
0
void APP_Initialize ( void )
{
    //stopEverything();
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;
    
    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
    //Create the queue
    appData.local_q = xQueueCreate(10, sizeof(unsigned int));
    //Ensure queue was created. If not, do not continue and turn on LED
    if(appData.local_q == 0)
    {
        stopEverything();
    }
    appData.sensor1_q = xQueueCreate(100, sizeof(unsigned char));
    if(appData.sensor1_q == 0)
    {
        stopEverything();
    }
    //stopEverything();
    //Create the timer
    appData.local_timer = xTimerCreate( "50msTimer",
                50 / portTICK_PERIOD_MS,
                pdTRUE,
                0,
                vTimerCallback );
    
    //Ensure timer was created. If not, do not continue and turn on LED
    if(appData.local_timer == 0)
    {
        stopEverything();
    }
    BaseType_t started = xTimerStart(appData.local_timer, 0);
    
    //Ensure the timer started successfully. If not, do not continue and turn
    // on LED
    if(started == pdFAIL)
    {
        stopEverything();
    }   
    
    //Setup AD Driver
    SYS_INT_SourceEnable(INT_SOURCE_ADC_1);
    DRV_ADC_Initialize();
    DRV_ADC_Open();
    DRV_ADC_ChannelScanInputsAdd(ADC_INPUT_SCAN_AN0 | ADC_INPUT_SCAN_AN1|ADC_INPUT_SCAN_AN2);
    PLIB_ADC_MuxAInputScanEnable(ADC_ID_1);
    DRV_ADC_Start();
    
    /* Initialization is done, allow the state machine to continue */
    appData.state = APP_STATE_OUTPUT;
}
uint32_t SRAND_GetSeed(void)
{
#define WIFIRE_POT_ADC_CH_INDEX (8)
    unsigned int result = 0;

#ifdef __32MZ2048ECG100__

    AD1CON3bits.RQCONVRT = 1;
    while (AD1DSTAT1bits.ARDY8 == 0);
    result = AD1DATA8;

#else	

    DRV_ADC_Start();
    while (!DRV_ADC_SamplesAvailable(WIFIRE_POT_ADC_CH_INDEX))
        ;
    result = DRV_ADC_SamplesRead(WIFIRE_POT_ADC_CH_INDEX);
    DRV_ADC_Stop();

#endif    

    return result;
}
void APP1_Tasks ( void )
{
    /* Check the application's current state. */
    switch ( app1Data.state )
    {
        /* Application's initial state. */
        case APP1_STATE_INIT:
        {
            bool appInitialized = true;
       
        
            if (appInitialized)
            {
                // Open the ADC drivers
                DRV_ADC0_Open();
               // DRV_ADC1_Open();
                DRV_ADC_DigitalFilter0_Open();
               // DRV_ADC_DigitalFilter1_Open();
                DRV_ADC_Start();
                app1Data.state = APP1_STATE_SERVICE_TASKS;
            }
            break;
        }

        case APP1_STATE_SERVICE_TASKS:
        {
            // BSP tasks that control switch and led functions
            BSP_SYS_Tasks();
            
            // Check if switches are pressed and send a message to the queue
            if(BSP_SWITCH_SwitchGetState(BSP_SWITCH_1_PORT) != bspData.previousStateS1){
                BSP_SWITCH_SwitchSetPreviousState(BSP_SWITCH_1_PORT, BSP_SWITCH_SwitchGetState(BSP_SWITCH_1_PORT));
                mySwitchMessage.switchNum = BSP_SWITCH_1;
                mySwitchMessage.switchVal = bspData.previousStateS1;
                xQueueSendToBack( app1Data.switchQueue, &mySwitchMessage, 1 );
            }
            if(BSP_SWITCH_SwitchGetState(BSP_SWITCH_2_PORT) != bspData.previousStateS2){
                BSP_SWITCH_SwitchSetPreviousState(BSP_SWITCH_2_PORT, BSP_SWITCH_SwitchGetState(BSP_SWITCH_2_PORT));
                mySwitchMessage.switchNum = BSP_SWITCH_2;
                mySwitchMessage.switchVal = bspData.previousStateS2;
                xQueueSendToBack( app1Data.switchQueue, &mySwitchMessage, 1 );
            }
            if(BSP_SWITCH_SwitchGetState(BSP_SWITCH_3_PORT) != bspData.previousStateS3){
                BSP_SWITCH_SwitchSetPreviousState(BSP_SWITCH_3_PORT, BSP_SWITCH_SwitchGetState(BSP_SWITCH_3_PORT));
                mySwitchMessage.switchNum = BSP_SWITCH_3;
                mySwitchMessage.switchVal = bspData.previousStateS3;
                xQueueSendToBack( app1Data.switchQueue, &mySwitchMessage, 1 );
            }
            if(BSP_SWITCH_SwitchGetState(BSP_SWITCH_4_PORT) != bspData.previousStateS4){
                BSP_SWITCH_SwitchSetPreviousState(BSP_SWITCH_4_PORT, BSP_SWITCH_SwitchGetState(BSP_SWITCH_4_PORT));
                mySwitchMessage.switchNum = BSP_SWITCH_4;
                mySwitchMessage.switchVal = bspData.previousStateS4;
                xQueueSendToBack( app1Data.switchQueue, &mySwitchMessage, 1 );
            }
            
            // Trigger an ADC reading every one second for the pot
            if((SYS_TMR_TickCountGet() - app1Data.potTimer) > (1000)){
                app1Data.potTimer = SYS_TMR_TickCountGet();
                DRV_ADC_Start();
            }
            
            // If the ADC reading is ready, see if value changed and send a message to queue
            if(DRV_ADC_DigitalFilter0_DataIsReady()) {
                app1Data.newPotSamp = (uint16_t)DRV_ADC_DigitalFilter0_DataRead();
                uint32_t adcVal;
                adcVal = app1Data.newPotSamp >> 6;
                if(adcVal != app1Data.potValue) {
                    app1Data.potValue = adcVal;
                    app1Data.potChanged = true;
                }
                
                if(app1Data.potChanged){
                    xQueueSendToBack( app1Data.potentiometerQueue, &app1Data.potValue, 1 );
                    app1Data.potChanged = false;
                }
            }
            
            // Check light show queue for a state, if exists, set state
            if( uxQueueMessagesWaiting( app1Data.lightShowQueue ) > 0 ){
                uint32_t lightShowVar;
                xQueueReceive( app1Data.lightShowQueue, &lightShowVar, 1 );
                BSP_LED_LightShowSet(lightShowVar);
            }
            
            break;
        }
        
        /* The default state should never be executed. */
        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}
void toggleLedForever()
{   
    volatile int num = 0;
    
    writeReg(IODIRA, 0x00);
   // writeReg(GPPUA, 0xff);
        
    writeReg(IODIRB, 0x00);
    //writeReg(GPPUB, 0xff);
    
    DRV_ADC_Open();
    DRV_ADC_ChannelScanInputsAdd(DRV_ADC_INPUT_POSITIVE_AN11);
    DRV_ADC_Start();
    
    /* Open the Device Layer */
    
    LATF = 0;
    volatile int i = 0;
    while(1)
    {

        if(num%256 ==0)
            LATFINV = 0x00000002;
    
        for(i = 0; i < 0x3fff; i++);
        
        num++;
        
        static int adcVal = 0x1234;
        
        if((num%256 == 0))
        {   
            adcVal++;
            if(DRV_ADC_SamplesAvailable())
            {
                adcVal = DRV_ADC_SamplesRead(0);
                DRV_ADC_Start();
            }
        }
        
        writeReg(GPIOA, 0x0F);
        
        char adcHexDigit = (adcVal >> (4*(num%4))) & 0xf;
        
        int voltage = (adcVal*3300) / 1024;
        
        int temp = (voltage - 500) / 10;
        
        int digit = 16;
        
        int nDigit = (num%4);
        
        if (nDigit == 1)
            digit = abs(temp) / 10;
        else if(nDigit == 0)
            digit = abs(temp) % 10;
        else if(nDigit == 2 && temp < 0)
            digit = 18;
        
        writeReg(GPIOB, ~(numberPattern[digit]));  
        
        writeReg(GPIOA, 0x0F & (~(1 << (num%4))));
      
    }
}