uint32_t ADC_getVal(void) { uint32_t avg_Value = 0, tempC = 0; ADCIntClear(ADC0_BASE, 1); ADCProcessorTrigger(ADC0_BASE, 1); while (!ROM_ADCIntStatus(ADC0_BASE, 1, false)) { } ADCSequenceDataGet(ADC0_BASE, 1, ADC_value); avg_Value = (ADC_value[0] + ADC_value[1] + ADC_value[2] + ADC_value[3] + 2) / 4; tempC = (1475 - ((2475 * avg_Value)) / 4096) / 10; return tempC; }
//***************************************************************************** // // The interrupt handler for the second timer interrupt. (temperature) // //***************************************************************************** void Timer1IntHandler(void) { // // Clear the timer interrupt. // ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT); // // Clear ADC interrupt // ROM_ADCIntClear(ADC0_BASE, 3); //Increment Timer A Count TimerBCount++; /*// // Use the flags to Toggle the LED for this timer // GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, g_ui32Flags);*/ // // Toggle the flag for the temperature timer. // HWREGBITW(&g_ui32InterruptFlags, 0) = 1; // // Trigger ADC Conversion // ROM_ADCProcessorTrigger(ADC0_BASE, 3); // //Wait for ADC conversion to complete // while(!ROM_ADCIntStatus(ADC0_BASE, 3, false)) { //Wait for ADC to finish sampling } ROM_ADCSequenceDataGet(ADC0_BASE, 3, adc_value); //Get data from Sequencer 3 // // Update the interrupt status. // //ROM_IntMasterDisable(); //UARTprintf("ADC Value = %d\n", adc_value[0]); //Print out the first (and only) value //ROM_IntMasterEnable(); }
int main(void) { uint32_t ui32ADC0Value[4]; volatile uint32_t ui32TempAvg; volatile uint32_t ui32TempValueC; volatile uint32_t ui32TempValueF; ROM_SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); ROM_ADCHardwareOversampleConfigure(ADC0_BASE, 64); ROM_ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0); ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS); ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS); ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS); ROM_ADCSequenceStepConfigure(ADC0_BASE,1,3,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END); ROM_ADCSequenceEnable(ADC0_BASE, 1); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_ADCHardwareOversampleConfigure(ADC0_BASE, 64); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); IntMasterEnable(); IntEnable(INT_UART0); UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); while(1) { if(mode == 0) { ROM_ADCIntClear(ADC0_BASE, 1); ROM_ADCProcessorTrigger(ADC0_BASE, 1); while(!ROM_ADCIntStatus(ADC0_BASE, 1, false)) { } ROM_ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value); ui32TempAvg = (ui32ADC0Value[0] + ui32ADC0Value[1] + ui32ADC0Value[2] + ui32ADC0Value[3] + 2)/4; ui32TempValueC = (1475 - ((2475 * ui32TempAvg)) / 4096)/10; ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5; UARTStrPut("Current Temperature "); UARTIntPut(ui32TempValueC); UARTCharPut(UART0_BASE, 176); UARTStrPut("C, Set Temperature "); UARTIntPut(setTemp); UARTCharPut(UART0_BASE, 176); UARTStrPut("C\r\n"); int temp; if (setTemp < ui32TempValueC) temp = 2; // red else if (setTemp > ui32TempValueC) temp = 8; // green else temp = 4; // blue if equal GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, temp); SysCtlDelay(SysCtlClockGet()/3); } } }
void vTempDriverTask() //Code Credit to Tivaware Example temperature_sensor.c { uint32_t measurement[1]; uint32_t ui32TempValueC; ROM_GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_1); ROM_ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0); //D0 ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH15 | ADC_CTL_IE | ADC_CTL_END); ROM_ADCSequenceEnable(ADC0_BASE, 3); ROM_ADCIntClear(ADC0_BASE, 3); for(;;) { xSemaphoreTake(pollTempSem, portMAX_DELAY); ROM_ADCProcessorTrigger(ADC0_BASE, 3); // Wait for conversion to be completed. while(!ROM_ADCIntStatus(ADC0_BASE, 3, false)) {} // Clear the ADC interrupt flag. ROM_ADCIntClear(ADC0_BASE, 3); // Read ADC Value. ROM_ADCSequenceDataGet(ADC0_BASE, 3, measurement); ui32TempValueC = (uint32_t)(log(40960/(29.0*measurement[0]) - 10.0/29) * -1/0.041); outputBuffer.tempR = ui32TempValueC; //Switch to other Thermistor ROM_ADCSequenceDisable(ADC0_BASE, 3); ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH14 | ADC_CTL_IE | //D1 ADC_CTL_END); ROM_ADCSequenceEnable(ADC0_BASE, 3); //Begin ADC poll ROM_ADCProcessorTrigger(ADC0_BASE, 3); // Wait for conversion to be completed. while(!ROM_ADCIntStatus(ADC0_BASE, 3, false)) {} // Clear the ADC interrupt flag. ROM_ADCIntClear(ADC0_BASE, 3); // Read ADC Value. ROM_ADCSequenceDataGet(ADC0_BASE, 3, measurement); ui32TempValueC = (uint32_t)(log(40960/(29.0*measurement[0]) - 10.0/29) * -1/0.041); outputBuffer.tempL = ui32TempValueC; ROM_ADCSequenceDisable(ADC0_BASE, 3); ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH15 | ADC_CTL_IE | ADC_CTL_END); ROM_ADCSequenceEnable(ADC0_BASE, 3); } }