Simple test program to get familiar with functionality of MBED RTOS on ST Nucleo-F411RE. Tasks for LED blinking, user button, temperature measurement with DS1620, temperature measurement with internal temperature sensor of ST32F411RE, ultrasonic distance measurement and displaying result on 16x2 TextLCD.

Dependencies:   DS1620_improved TextLCD_improved mbed-rtos mbed

Committer:
dzoni
Date:
Fri Dec 18 15:23:21 2015 +0000
Revision:
17:94c385ff2641
Child:
18:be0130c42925
Bugfixes. Works but freezes after some time. Button task requires rewrite (InterruptIn).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dzoni 17:94c385ff2641 1 #include "mbed.h"
dzoni 17:94c385ff2641 2 #include "rtos.h"
dzoni 17:94c385ff2641 3 #include "stm32f411xe.h"
dzoni 17:94c385ff2641 4
dzoni 17:94c385ff2641 5 #include "tsk_main.h"
dzoni 17:94c385ff2641 6 #include "tsk_inttemp.h"
dzoni 17:94c385ff2641 7
dzoni 17:94c385ff2641 8
dzoni 17:94c385ff2641 9 struct int_temp_data_struct int_temp_data = { 0 };
dzoni 17:94c385ff2641 10
dzoni 17:94c385ff2641 11
dzoni 17:94c385ff2641 12 uint32_t initIntTemp(void const *args) {
dzoni 17:94c385ff2641 13
dzoni 17:94c385ff2641 14 int_temp_data.temperature = 0.0f;
dzoni 17:94c385ff2641 15
dzoni 17:94c385ff2641 16 //enable ADC1 clock
dzoni 17:94c385ff2641 17 SET_BIT(RCC->APB2ENR, RCC_APB2ENR_ADC1EN);
dzoni 17:94c385ff2641 18
dzoni 17:94c385ff2641 19 // ADC on
dzoni 17:94c385ff2641 20 SET_BIT(ADC1->CR2, ADC_CR2_ADON);
dzoni 17:94c385ff2641 21 CLEAR_BIT(ADC1->CR2, ADC_CR2_CONT);
dzoni 17:94c385ff2641 22
dzoni 17:94c385ff2641 23 // Temp sensor on
dzoni 17:94c385ff2641 24 CLEAR_BIT(ADC->CCR, ADC_CCR_VBATE);
dzoni 17:94c385ff2641 25 SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
dzoni 17:94c385ff2641 26
dzoni 17:94c385ff2641 27 CLEAR_BIT(ADC1->CR1, ADC_CR1_RES);
dzoni 17:94c385ff2641 28
dzoni 17:94c385ff2641 29 return 1;
dzoni 17:94c385ff2641 30 }
dzoni 17:94c385ff2641 31
dzoni 17:94c385ff2641 32 static uint16_t getADCTemp() {
dzoni 17:94c385ff2641 33 // Set channel
dzoni 17:94c385ff2641 34 ADC1->SQR1 &= ~ADC_SQR1_L; // Count = 1
dzoni 17:94c385ff2641 35 ADC1->SQR3 |= ADC_SQR3_SQ1 & 18;
dzoni 17:94c385ff2641 36
dzoni 17:94c385ff2641 37 // Start conversion
dzoni 17:94c385ff2641 38 SET_BIT(ADC1->CR2, ADC_CR2_SWSTART);
dzoni 17:94c385ff2641 39
dzoni 17:94c385ff2641 40 // Wait for completion
dzoni 17:94c385ff2641 41 while (!READ_BIT(ADC1->SR, ADC_SR_EOC))
dzoni 17:94c385ff2641 42 ;
dzoni 17:94c385ff2641 43
dzoni 17:94c385ff2641 44 // Return result
dzoni 17:94c385ff2641 45 return READ_REG(ADC1->DR);
dzoni 17:94c385ff2641 46 }
dzoni 17:94c385ff2641 47
dzoni 17:94c385ff2641 48
dzoni 17:94c385ff2641 49 void inttemp_thread(void const *args) {
dzoni 17:94c385ff2641 50
dzoni 17:94c385ff2641 51 const float V25 = 943.3212f;// when V25=1.41V at ref 3.3V (0,76V)
dzoni 17:94c385ff2641 52 const float Avg_Slope = 3.1030303f; //when avg_slope=4.3mV/C at ref 3.3V (2.5mV/C)
dzoni 17:94c385ff2641 53 const uint16_t bufferSize = 4;
dzoni 17:94c385ff2641 54
dzoni 17:94c385ff2641 55 float temp;
dzoni 17:94c385ff2641 56 uint16_t i;
dzoni 17:94c385ff2641 57 uint16_t uiCnt = 0;
dzoni 17:94c385ff2641 58
dzoni 17:94c385ff2641 59
dzoni 17:94c385ff2641 60 while (true) {
dzoni 17:94c385ff2641 61 for (i = 0, temp = 0.0f; i < bufferSize; i++) {
dzoni 17:94c385ff2641 62 temp += ((float)getADCTemp() - V25) / Avg_Slope + 48.2f;
dzoni 17:94c385ff2641 63 Thread::wait(100);
dzoni 17:94c385ff2641 64 }
dzoni 17:94c385ff2641 65 temp /= (float)bufferSize;
dzoni 17:94c385ff2641 66
dzoni 17:94c385ff2641 67 mutexIntTemp.lock();
dzoni 17:94c385ff2641 68 int_temp_data.temperature = temp;
dzoni 17:94c385ff2641 69 mutexIntTemp.unlock();
dzoni 17:94c385ff2641 70
dzoni 17:94c385ff2641 71 Thread::wait(5000);
dzoni 17:94c385ff2641 72 }
dzoni 17:94c385ff2641 73 }