/***********************************Main***********************************/ int main() { int16 mav_data; ADC_ISR_StartEx(ADC_interrupt); CyGlobalIntEnable; UART_Start(); /* Initialize ADC */ ADC_Start(); /* Initialize ADC */ ADC_StartConvert(); /* Start ADC conversions */ ADC_IRQ_Enable(); /* Enable ADC interrupts */ for (;;) { if(data_ready) { mav_data = mavg_filter(accgroen); gradergroen = grader(mav_data); //UART_UartPutChar(mav_data); //UART_UartPutChar(mav_data>>8); UART_UartPutChar(gradergroen); UART_UartPutChar(gradergroen>>8); data_ready = FALSE; } } }
/* This is used to start a new ADC conversion every 100ms and to turn off the CapSense interrupt line */ void SysTickISRCallback(void) { static uint8 ADCcounter = 0; static uint8 CScounter = 0; ADCcounter++; if(ADCcounter > 99) { ADCcounter = 0; if(adcState == DONE) { ADC_StartConvert(); adcState = RUNNING; } } /* Read CS Interrupt pin state and turn off after 2nd tick */ if(CSINTR_Read() == 1) { CScounter++; if(CScounter > 1) { CSINTR_Write(0); CScounter = 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)); }
void ScanColumn(uint8 col) { InputControl_Write(0u); // CyDelayUs(10); SetColumns(col); //CyDelayUs(10); ADC_StartConvert(); ADC_IsEndConversion(ADC_WAIT_FOR_RESULT); InputControl_Write(1u); ResetColumns(); }
/******************************************************************************* * Function Name: ADC_Wakeup ******************************************************************************** * * Summary: * Restores the user configuration and enables the power to the block. * * Parameters: * None * * Return: * None * * Global variables: * ADC_backup: The structure field 'enableState' is used to * restore the enable state of block after wakeup from sleep mode. * *******************************************************************************/ void ADC_Wakeup(void) { /* Restore the configuration */ ADC_RestoreConfig(); /* Enables the component operation */ if(ADC_backup.enableState != ADC_DISABLED) { ADC_Enable(); if((ADC_backup.enableState & ADC_STARTED) != 0u) { ADC_StartConvert(); } } /* Do nothing if component was disable before */ }
void main() { ISR_UpBuff_Start(); //Start the Interrupt Component. ISR_UpBuff_SetVector(BufferUpdate);//Set Vector to the above ISR. CyGlobalIntEnable;//Enable Global Interrupts,EzI2C ad ADC require them. EZI2C_Start();//Start the EzI2C component. EZI2C_SetBuffer1(BUFFER_SIZE,BUFFER_RW_AREA_SIZE,(void *) (&EZI2C_Buffer));//Configure the Buffer for the EzI2C Component. ADC_Start();//Start the ADC ADC_IRQ_Enable();//Enable the EOC Interrupt ADC_StartConvert();//Start the Conversion for(;;);//Everything is done in the ISR. }
/******************************************************************************* * Function Name: ADC_Wakeup ******************************************************************************** * * Summary: * Restores the component enable state and configuration registers. * This should be called just after awaking from sleep mode. * * Parameters: * None. * * Return: * None. * * Global Variables: * ADC_backup - used. * *******************************************************************************/ void ADC_Wakeup(void) { ADC_SAR_DFT_CTRL_REG &= (uint32)~ADC_ADFT_OVERRIDE; if(ADC_backup.enableState != ADC_DISABLED) { /* Enable the SAR internal pump */ if((ADC_backup.enableState & ADC_BOOSTPUMP_ENABLED) != 0u) { ADC_SAR_CTRL_REG |= ADC_BOOSTPUMP_EN; } ADC_Enable(); if((ADC_backup.enableState & ADC_STARTED) != 0u) { ADC_StartConvert(); } } }
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); } }
/***************************************************************************** * 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)); }
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 */
void main() { /* Place your initialization/startup code here (e.g. MyInst_Start()) */ CyGlobalIntEnable; /* Uncomment this line to enable global interrupts. */ init(); // Start usb operation with 3V //Wait for device to enumerate USB_Start(0u,USB_3V_OPERATION); while(!USB_bGetConfiguration()); //Enumeration i done, enable out endpoint for recieve data from host USB_EnableOutEP(2); ADC_StartConvert(); while(1) { //wait for data while(USB_bGetEPState(2) !=USB_OUT_BUFFER_FULL); //read received bytes count length = USB_wGetEPCount(2); //turn on LED LED_Write(~LED_Read()); //unload the out buffer USB_ReadOutEP(2, &buffer[0], length); // check for in buffer is empty while(USB_bGetEPState(1) != USB_IN_BUFFER_EMPTY); // Turn off LED // load the in buffer USB_LoadInEP(1, &buffer[0], length); while(USB_bGetEPState(1) != USB_IN_BUFFER_EMPTY); USB_LoadInEP(1, &buffer[1], length); } for(;;) { } /* while(!USB_bGetConfiguration()); //Enumeration i done, enable out endpoint for recieve data from host USB_EnableOutEP(2); while(1) { //wait for data while(USB_bGetEPState(2) !=USB_OUT_BUFFER_FULL); //read received bytes count length = USB_wGetEPCount(2); //turn on LED LED_Write(~LED_Read()); //unload the out buffer USB_ReadOutEP(2, &buffer[0], length); // check for in buffer is empty while(USB_bGetEPState(1) != USB_IN_BUFFER_EMPTY); // Turn off LED // load the in buffer USB_LoadInEP(1, &buffer[0], length); }*/ /* if( ADC_IsEndConversion(ADC_WAIT_FOR_RESULT)) { //LCD_ClearDisplay(); //LCD_PrintNumber(ADC_GetResult16()); //CyDelay(250); Pin_1_Write(~Pin_1_Read()); //buffer[count]=ADC_GetResult8(); buffer[count]=count; ++count; USB_LoadInEP(1,&buffer[0],1); if (count==64) { // load the in buffer *? //USB_LoadInEP(1, &buffer[0], 64); count=0; } } */ }
int main(){ CyGlobalIntEnable; isrSW_Start(); isr_Timer_Start(); UART_Start(); ADC_Start(); ADC_StartConvert(); CyDelay(50); UART_PutString("nRF Tx\r\n"); NRF_INIT_t tx; tx.channel = NRF_CHANNEL; tx.isTX = true; tx.RF_SETUP_DR = NRF_RF_SETUP_RF_DR_1000; tx.RF_SETUP_PWR = NRF_RF_SETUP_RF_PWR_18; tx.SETUP_RETR_ARC = NRF_SETUP_RETR_ARC_15; tx.SETUP_RETR_ARD = NRF_SETUP_RETR_ARD_1500; /* Start the component before anything else */ nRF_Tx_Start(&tx); /* Test Dynamic Payload */ nRF_Tx_EnableDynamicPayload(NRF_DYNPD_DPL_P0); nRF_Tx_SetRxPayloadSize(NRF_RX_PW_P0, PAYLOAD_SIZE); nRF_Tx_SetRxAddress(ADDR, sizeof(ADDR)); nRF_Tx_SetTxAddress(ADDR, sizeof(ADDR)); Timer_Start(); for(;;){ if(true == isrTimerFlag){ // Stop the Timer just for fun Timer_Stop(); test++; data[0] = pressCount; data[9] = test; ADCoutput = ADC_Read32(); UART_PutString("Resultado de la conversion del ADC: "); UART_PutHexInt((uint16_t)ADCoutput); UART_PutCRLF(); data[1] = (ADCoutput & 0xFF00) >> 8; data[2] = ADCoutput & 0xFF; nRF_Tx_TxTransmit(data, sizeof(data)); isrTimerFlag = false; // Start the Timer again Timer_Start(); } if(isrFlag){ if(nRF_Tx_GetStatus() & NRF_STATUS_RX_DR_MASK){ UART_PutString("Status: "); UART_PutHexByte(nRF_Tx_GetStatus()); UART_PutCRLF(); UART_PutString("RX\r\n"); do{ nRF_Tx_RxPayload(RXdata, sizeof(RXdata)); nRF_Tx_ResetStatusIRQ(NRF_STATUS_RX_DR); nRF_Tx_ReadSingleRegister(NRF_FIFO_STATUS, &status); }while(!(status & NRF_STATUS_TX_FIFO_FULL)); printFlag = true; }else if(nRF_Tx_GetStatus() & NRF_STATUS_TX_DS_MASK){ UART_PutString("Status: "); UART_PutHexByte(nRF_Tx_GetStatus()); UART_PutCRLF(); UART_PutString("TX\r\n"); LED_Write(~LED_Read()); nRF_Tx_ResetStatusIRQ(NRF_STATUS_TX_DS); }else if(nRF_Tx_GetStatus() & NRF_STATUS_MAX_RT_MASK){ UART_PutString("Status: "); UART_PutHexByte(nRF_Tx_GetStatus()); UART_PutCRLF(); UART_PutString("Max RT\r\n"); MAX_Write(~MAX_Read()); nRF_Tx_ResetStatusIRQ(NRF_STATUS_MAX_RT); } isrFlag = false; } if(printFlag){ UART_PutHexByte(RXdata[0]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[1]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[2]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[3]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[4]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[5]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[6]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[7]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[8]); UART_PutString("\r\n"); UART_PutHexByte(RXdata[9]); UART_PutString("\r\n"); printFlag = false; } }