Beispiel #1
0
/*****************************************************************************
* Function Name: BatteryLevel_Measure()
******************************************************************************
* Summary:
* Measures the current battery level.
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* The function checks if the battery measurement enable flag is set in the 
* ADC ISR, and then measures the current battery level.
*
* Side Effects:
* None
*
* Note:
*
*****************************************************************************/
void BatteryLevel_Measure(void)
{
    uint16 adcCountsVref;
    static uint32 vddaVoltageMv;
    
    /* Disconnect the VREF pin from the chip and lose its existing voltage */
    CY_SET_REG32(CYREG_SAR_CTRL, CY_GET_REG32(CYREG_SAR_CTRL) & ~(0x01 << 7));
    VrefInputPin_SetDriveMode(VrefInputPin_DM_STRONG);
    VrefInputPin_Write(0);
    CyDelayUs(10);
    
    /* Switch SAR reference to 1.024V to charge external cap */
    VrefInputPin_SetDriveMode(VrefInputPin_DM_ALG_HIZ);
    CY_SET_REG32(CYREG_SAR_CTRL, (CY_GET_REG32(CYREG_SAR_CTRL) & ~(0x000000F0Lu)) | (0x00000040Lu) | (0x01Lu << 7));
    CyDelayUs(100);
    
    /* Switch the reference back to VDDA/2 for measuring the REF voltage */
    CY_SET_REG32(CYREG_SAR_CTRL, CY_GET_REG32(CYREG_SAR_CTRL) & ~(0x01 << 7));
    CY_SET_REG32(CYREG_SAR_CTRL, (CY_GET_REG32(CYREG_SAR_CTRL) & ~(0x00000070Lu)) | (0x00000060Lu));
    
    /* Enable channel 1 of the ADC, disable channel 0 */
    ADC_SetChanMask(0x02);
    
    /* Clear ADC interrupt triggered flag and start a new conversion */
    canMeasureBattery = false;
    ADC_StartConvert();
    while(true != canMeasureBattery);

    /* Since our ADC reference is VDDA/2, we get full scale (11-bit) at VDDA/2.
     * We can calculate VDDA by the formula:
     * VDDA = (VREF * (Full scale ADC out) * 2) / (ADC out for VREF)
     */
    adcCountsVref = ADC_GetResult16(1);
    if(adcCountsVref != 0)
    {
        vddaVoltageMv = ((uint32)VREF_VOLTAGE_MV * ADC_FULL_SCALE_OUT * 2) / (uint32)adcCountsVref;
    }
    
    /* Battery level is implemented as a linear plot from 2.0V to 3.0V 
     * Battery % level = (0.1 x VDDA in mV) - 200
     */
    batteryLevel = ((uint32)(vddaVoltageMv / 10)) - 200;
    if((batteryLevel > 100) && (batteryLevel < 230))
    {
        batteryLevel = 100;
    }
    else if(batteryLevel >= 230)
    {
        batteryLevel = 0;
    }
    
    
    /* Enable channel 0 again, disable channel 1 */
    ADC_SetChanMask(0x01);
    
    /* Enable bypass cap for the VDDA/2 reference */
    CY_SET_REG32(CYREG_SAR_CTRL, CY_GET_REG32(CYREG_SAR_CTRL) | (0x01 << 7));
}
Beispiel #2
0
void PrintColumn(uint8 col)
{
    uint16 res[8];
    for(uint8 i=0; i<8; ++i)
    {
        res[i] = ADC_GetResult16(i);
    }

    if (status_register.matrix_output > 0)
    {
        outbox.response_type = C2RESPONSE_MATRIX_STATUS;
        outbox.payload[0] = col;
        outbox.payload[1] = status_register.matrix_output;
        memcpy(&outbox.payload[2], res, sizeof(res));
        usb_send();
    }
}
void main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    uint16 adc, compare;
    
    LCD_Start();
    ADC_Start();
    PWM_Start();
    
    

    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    for(;;)
    {
        /* Place your application code here. */
//        LCD_ClearDisplay();
        LCD_Start();
        
        adc = 0;
        ADC_StartConvert();
        ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
        ADC_StopConvert();
        adc = ADC_GetResult16();
        
        if(adc > 255)
        {
            if(adc == 0xFFFF) /* underflow correction */
            {
                adc = 0x00;
            }
            else
            adc = 0xFF; /* Overflow correction */
        }    
        
        LCD_Position(0,0);
        LCD_PrintHexUint8(adc);
                
        compare = (uint16)(1000 + ((float)((float)((float)adc / (float)255) * (float)1000)));
        LCD_Position(1,0);
        LCD_PrintDecUint16(compare);
        
        PWM_WriteCompare(compare);
        PWM_WritePeriod(compare + 39999);
    }
}
Beispiel #4
0
BTN_states getButton(void){
    BTN_states btn;
    uint16 temp = ADC_GetResult16(1); //A0
    
    if (temp == 0xFFFF || temp <= 5)
        btn = RIGHT;
    else if(temp <= 20)
        btn = UP;
    else if(temp <= 40)
        btn = DOWN;
    else if(temp <= 60)
        btn = LEFT;
    else if(temp <= 100)
        btn = SELECT;
    else
        btn = UNPRESSED;
    
    if (btn_old != btn)
        return btn_old = btn;
    else
        return UNPRESSED;
}
Beispiel #5
0
/*****************************************************************************
* Function Name: MeasureSensorVoltage()
******************************************************************************
* Summary:
* Measures the voltage connected at ADC input. 
*
* Parameters:
* None
*
* Return:
* uint16 - Measured voltage
*
* Theory:
* This functions sequences the AMux to next channel and connects reference 
* signal or thermistor or offset signal at ADC input. It then triggers the ADC
* and measures the signal.
*
* Side Effects:
* None
*
* Note:
*
*****************************************************************************/
static uint16 MeasureSensorVoltage ()
{
    /* Connect next channel available at AMux input to Amux output */
    /* Note: If no channels are connected, channel 0 gets connected by this 
    *  fucntion */
    AMuxSeq_Next();
    
    /* Start sample conversion */
    ADC_StartConvert();
    
    /* Wait till end of two conversions and drop one sample for signal to settle 
    *  down, it's not required if reference is continuously available.  
    *  To reduce current consumption, CPU can be put to sleep while ADC conversion
    *  is in process. */
    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
    
    /* Stop ADC coversion */ 
    ADC_StopConvert();
    
    /* Return 16 bit measured value */
    return (ADC_GetResult16(0));
}
Beispiel #6
0
int main()
{
    
    CyDelay(200);

    uint16 Counts=0;            // ADC value (0 to 4095) right shifted by 6 which gives
                                // us 0 to 63 to be used to simulate actual temperature
    uint16 TempSet = 2400;      // Temperature set default value (left justified) 24 deg
    uint16 DisplayTemp = 0;     // The combined sum of desired temp and actual temp
    
    uint16 bleTemp = 0;         // Temperature sent to BLE module
    uint16 bleTempSet = 0;      // Temperature set value sent to BLE module
    
    uint8 button0 = 0;      // Declare CapSense button name button0
    uint8 button1 = 0;      // Declare CapSense button name button1
    uint8 firstpress0 = 0;  // Detects a transition of button1 from 0 to 1
    uint8 firstpress1 = 0;  // Detects a transition of button1 from 0 to 1
    
    int buttonPrevious = 1;

    CyGlobalIntEnable;
    
    ADC_Start();        // Starts the ADC component
    ADC_StartConvert(); // The ADC conversion process begins
    LCD_Start();        // Start the LCD component 
    
    CapSense_Start();
    CapSense_ScanAllWidgets();
    
    LCD_WritePixel(LCD_COLON, TRUE);
       
    ResetTimer_Start();
    sendBootload_StartEx(StartBootload_ISR);
    
    BLEIOT_Start();
    
    /* Initialize temperuature values out of range so that main loop update is triggered */
    BLEIOT_updateTemperature(10000);
    BLEIOT_updatePot(100);    
       
    for(;;)
    {
        
        /* Turn BLE on/off with button press */
        if(buttonPrevious && (Button_Read() == 0))
        {          
            if(BLEIOT_remote.bleState == BLEIOT_BLEOFF)
            {
                BLEIOT_updateBleState(BLEIOT_BLEON);  
            }
            else
            {
                BLEIOT_updateBleState(BLEIOT_BLEOFF);
            }
            
        }
        buttonPrevious = Button_Read();
           
        /* Local Thermostat Operation */
        /* ADC */
        // Read the ADC, shift right by 6 (i.e. divide by 64)
        // and store result in Counts
        Counts = ADC_GetResult16(POT_CHAN);
        Counts = Counts >> 6;

        /* CapSense */
        if (!CapSense_IsBusy())
        {
            // Check Button states and store
            CapSense_ProcessAllWidgets();
            if(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))
            {
                button0 = 1;
            }
            else
            {
                button0 = 0;
            }
            if(CapSense_IsWidgetActive(CapSense_BUTTON1_WDGT_ID))
            {
                button1 = 1;
            }
            else
            {
                button1 = 0;
            }
            
            // Light LEDs Based on Capsense buttons
            LED_CS0_Write(~button0);
            LED_CS1_Write(~button1);
            
            // Check for button touchdown transitions
            if (button0 == 1)
            {
                if(firstpress0 == 0) // Touchdown event
                {
                    firstpress0 = 1; // Remember button0 was pressed
                    TempSet = TempSet + 100; // Increment Temp by 1 deg
                }
            }
            else
            {
                firstpress0 = 0; // Button released
            }
            if (button1 == 1)
            {
                if(firstpress1 == 0) // Touchdown event
                {
                    firstpress1 = 1; // Remember button0 was pressed                   
                    TempSet = TempSet - 100; // Decrement Temp by 1 deg
                }
            }
            else
            {
                firstpress1 = 0; // Button released
            }
                 
            CapSense_ScanAllWidgets();  // Start Next Scan
        }
     
        /* Warning LEDs and Buzzer */
        if((Counts * 100) < TempSet)    // Temperature Cold
        {
            LED_Blue_Write(LED_ON);     // Blue On
            LED_Green_Write(LED_OFF);   // Green Off
            LED_Red_Write(LED_OFF);     // Red Off
            PWM_Stop(); // Buzzer Off
        }
        else if ((Counts * 100) <= (TempSet + 500))  // Temperature OK
        {
            LED_Blue_Write(LED_OFF);    // Blue Off
            LED_Green_Write(LED_ON);    // Green On
            LED_Red_Write(LED_OFF);     // Red Off
            PWM_Stop();// Buzzer Off
        }
        else // Tempearture too high
        {
            LED_Blue_Write(LED_OFF);    // Blue Off
            LED_Green_Write(LED_OFF);   // Green Off
            LED_Red_Write(LED_ON);      // Red On
            PWM_Start(); // Buzzer On
        }
    
        /* LCD Display */
        DisplayTemp = TempSet + Counts;
        LCD_Write7SegNumber_0(DisplayTemp, POS, MODE);        
    
        
        /* BLE operation - do only if BLE is not off */    
        if(BLEIOT_remote.bleState != BLEIOT_BLEOFF)
        {
            /* Send new temperature data to the BLE module */
            if(bleTemp != Counts)
            {
                bleTemp = Counts;
                BLEIOT_updatePot(bleTemp);
            }
            if(bleTempSet != TempSet)
            {
                bleTempSet = TempSet;
                /* Scale set temperature down to whole number of  degrees */
                BLEIOT_updateTemperature(TempSet / 100); 
            }     
                       
            /* Get new data from the BLE module */
            /* LED0 is used for temperature changes */
            if(BLEIOT_getDirtyFlags() & BLEIOT_FLAG_LED0)
            {
                /* Update local variable copy and clear dirty flag */
                BLEIOT_updateLed0(BLEIOT_remote.led0); 
                if(BLEIOT_local.led0 == UP)
                {
                    TempSet = TempSet + 100; // Increment Temp by 1 deg
                }
                else if (BLEIOT_local.led0 == DOWN)
                {
                    TempSet = TempSet - 100; // Decrement Temp by 1 deg
                }
            }
        } /* End of !BLEOFF state operations */
    } /* End of superloop */
} /* End of main */