Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 AnalogIn adc(A4); 00004 DigitalOut led1(LED1); 00005 00006 typedef uint32_t message_t; 00007 Queue <message_t, 4> queue; 00008 00009 void led1_thread(void const *args) 00010 { 00011 while (true) { 00012 led1 = !led1; 00013 Thread::wait(1000); 00014 } 00015 } 00016 00017 //--- ADC Interrupt handler ----------------- 00018 extern "C" void ADC1_IRQHandler() 00019 { 00020 NVIC_ClearPendingIRQ(ADC_IRQn); //Clear ADC Interrupt Request Flag 00021 uint16_t raw = ADC1->DR; 00022 queue.put((message_t*)raw); //Send result through a Queue 00023 } 00024 00025 //--- Start conversion, wait for result ----- 00026 uint16_t adc_read(uint32_t ch) 00027 { 00028 ADC1->SQR3 = ch; //set conversion channel 00029 ADC1->SMPR2 = 7; //Sample time =480 00030 ADC1->CR1 |= ADC_CR1_EOCIE; //enable interrupt 00031 ADC1->CR2 |= ADC_CR2_SWSTART; //Start conversion 00032 osEvent evt = queue.get(); //Wait for a message 00033 return (uint16_t)evt.value.v; //Return obtained value 00034 } 00035 00036 int main() 00037 { 00038 int32_t v25 = 760; //Voltage at 25C (in millivolts) 00039 float m = 2.5; //Slope mV per degree) 00040 uint16_t dummy = adc.read(); //needed for ADC configuration 00041 ADC123_COMMON->CCR |= ADC_CCR_TSVREFE; //Enable inner channels 00042 NVIC_SetVector(ADC_IRQn,(uint32_t)&ADC1_IRQHandler); //Attach ADC ISR 00043 NVIC_EnableIRQ(ADC_IRQn); //Enable ADC interrupts 00044 Thread thread1(led1_thread); 00045 printf("\r\n Lab09 ADC interrupt in RTOS environment\r\n"); 00046 while(true) { 00047 uint32_t a1 = 0; 00048 uint32_t a2 = 0; 00049 for(int i=0; i<3300; i++) { 00050 a1 += adc_read(11); //Measure voltage at A4 (PTC_1) 00051 a2 += adc_read(18); //Internal temperature sensor 00052 } 00053 float v1 = a1/4096.0f; //Convert v1 to millivolts 00054 float temp1 = (v1-500)/10.0; //MCP9700 temperature in Celsius 00055 float v2 = a2/4096.0f; //Convert v2 to millivolts 00056 float temp2 = 25.0f+(v2-v25)/m; //Calculate temp in Celsius 00057 printf("A4 = %.0f mV Temp = %.1f C Inner Ts: %.0f mV Temp = %.1f C\r\n",v1,temp1,v2,temp2); 00058 Thread::wait(2000); 00059 } 00060 00061 }
Generated on Wed Sep 21 2022 07:04:04 by
1.7.2