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;
}
Пример #2
0
void IntHandlerDrvAdc(void)
{
    DRV_ADC_Stop();
    //TODO: make sure this corresponds to the Harmony config!!!
    int numSamp = 6;
        
    //clear the interrupt flag
    PLIB_INT_SourceFlagClear(INT_ID_0, INT_SOURCE_ADC_1);
    //get the ADC value
    unsigned int potValue = 0;
    unsigned int potValue1 = 0;
    unsigned int potValue2 = 0;
    unsigned int potValue3 = 0;
    unsigned int potValue4 = 0;
    unsigned int potValue5 = 0;
    int i = 0;
    
        for(i=0;i<numSamp;i+=numSamp)
    {
        potValue4 += DRV_ADC_SamplesRead(i);
        potValue += DRV_ADC_SamplesRead(i+1);
        potValue1 += DRV_ADC_SamplesRead(i+2);
        potValue3 += DRV_ADC_SamplesRead(i+3);
        potValue2 += DRV_ADC_SamplesRead(i+4);
        potValue5 += DRV_ADC_SamplesRead(i+5);
    }
    /*
    for(i=0; i<numberSamplesPerInterrupt; i += 3) {
        //potValue += PLIB_ADC_ResultGetByIndex(ADC_ID_1, i);
        potValue += DRV_ADC_SamplesRead(i);
        potValue1 += DRV_ADC_SamplesRead(i+1);
        potValue2 += DRV_ADC_SamplesRead(i+2);
    }
     * */
    //potValue = potValue/numberSamplesPerInterrupt; //the output is a 16-bit int
    
    unsigned int distance;
    unsigned int distance2;
    
    //convert ADC steps to distance in cm
    // distance = (unsigned int) (63.404-((double)potValue*0.058)); //cm
    // distance = (unsigned int) (24.952-((double)potValue*0.0227)); //in
    
    //distance = (unsigned int) potValue;
    //distance = distance & 0xFF;
    
    // DIST 
    if (potValue < 89)
        distance = 40;
    else
        distance = (unsigned int) (3530.0 / ((double)potValue));
    
    if (potValue5 < 89)
        distance2 = 40;
    else
        distance2 = (unsigned int) (3530.0 / ((double)potValue5));
    
    if (potValue2 < 833) potValue2 = 0;
    else potValue2 = potValue2 - 832;

    char data[MSG_LENGTH];
    data[0] = MSG_START;          // Start byte
    data[1] = TYPE_ADC;               // Type byte
    data[2] = 0x20;              // Count byte
    data[3] = distance & 0xFF;           
    data[4] = (potValue1 & 0x03FC) >> 2;  //FRONT
    data[5] = (potValue2 & 0x03FC) >> 2;  //REAR
    data[6] = (potValue3 & 0x03FC) >> 2;  //RIGHT
    data[7] = (potValue4 & 0x03FC) >> 2;  //LEFT
    data[8] = distance2 & 0xFF;           
    data[9] = MSG_STOP;           // Stop byte
    
    if (SEND_ADC == 0x1)
       putMsgOnSendQueue(data);
 
    //send the value to the queue
    sendValToSensorTaskFromISR(data);
    
    //PLIB_ADC_SampleAutoStartEnable(ADC_ID_1);
    
}
Пример #3
0
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))));
      
    }
}