add measurement, stolen from tea thermometer

This commit is contained in:
Wolfgang Hottgenroth 2019-02-04 10:03:10 +01:00
parent a31c929053
commit d3e3967bee
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4
5 changed files with 118 additions and 1 deletions

View File

@ -2,7 +2,7 @@ CC=msp430-gcc
CFLAGS=-O3 -g0 -Wall -mmcu=msp430g2553 -std=gnu99 -I hottislib
LDFLAGS=-mmcu=msp430g2553
blinky1.elf: main.o led.o time.o pattern.o PontCoopScheduler.o myrand.o
blinky1.elf: main.o led.o time.o pattern.o PontCoopScheduler.o myrand.o measure.o
$(CC) -o $@ $(LDFLAGS) $^
PontCoopScheduler.o: hottislib/PontCoopScheduler.c hottislib/PontCoopScheduler.h

84
measure.c Normal file
View File

@ -0,0 +1,84 @@
/*
* adc.cpp
*
* Created on: 03.10.2014
* Author: wn
*/
#include <msp430g2553.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <float.h>
#include "measure.h"
#include "PontCoopScheduler.h"
#include "pattern.h"
const float R_REF = 3000.0;
const uint16_t N_MAX = 1023;
const float PT1000_R0 = 1000.0;
const float PT1000_Coeff = 3.85e-3;
const uint32_t MEASURE_CYCLE = 20;
const uint32_t MEASURE_FETCH_RESULT_DELAY = 5;
const uint8_t AVERAGING_CYCLES = 50;
void measureInit() {
ADC10CTL0 = SREF1 | ADC10SHT_3 | ADC10SR | REFOUT | REFON | REF2_5V | ADC10ON;
ADC10CTL1 = INCH_3;
ADC10AE0 = BIT3;
}
void measureCollectAndProcessConversion();
void measureStartConversion(void *handleArg) {
ADC10CTL0 |= ENC | ADC10SC;
schAdd(measureCollectAndProcessConversion, NULL, MEASURE_FETCH_RESULT_DELAY, 0);
}
void measureCollectAndProcessConversion() {
static uint32_t averagingSum = 0;
static uint8_t averagingCnt = 0;
uint16_t n = 0xffff;
if ((ADC10CTL0 & ADC10IFG) != 0) {
n = ADC10MEM;
ADC10CTL0 &= ~(ADC10IFG | ENC);
}
if (n != 0xffff) {
averagingSum += n;
averagingCnt++;
}
if (averagingCnt == AVERAGING_CYCLES) {
uint32_t nAvg = averagingSum / averagingCnt;
// process adcValue
// store result in variable temperature
float r = 0.0;
if (nAvg == 0) {
r = 0.0;
} else if (nAvg == N_MAX) {
r = FLT_MAX;
} else {
r = R_REF / ((((float)N_MAX) / ((float)nAvg)) - 1.0);
}
float t = (r / PT1000_R0 - 1) / PT1000_Coeff;
uint8_t temperature = (t < 0) ? 0 : ((uint8_t)t);
// do something with the temperature
patternSetTemperature(temperature);
averagingCnt = 0;
averagingSum = 0;
}
}

20
measure.h Normal file
View File

@ -0,0 +1,20 @@
/*
* adc.h
*
* Created on: 03.10.2014
* Author: wn
*/
#ifndef MEASURE_H_
#define MEASURE_H_
#include <stdint.h>
void measureInit();
void measureStartConversion();
#endif /* MEASURE_H_ */

View File

@ -4,6 +4,14 @@
#include <stdlib.h>
#include "myrand.h"
uint8_t temperature = 20;
void patternSetTemperature(uint8_t t) {
temperature = t;
}
/*
* traversing the whole matrix
void patternExec() {

View File

@ -1,8 +1,13 @@
#ifndef PATTERN_H_
#define PATTERN_H_
#include <stdlib.h>
void patternExec();
void patternInit();
void patternSetTemperature(uint8_t t);
#endif /* PATTERN_H_ */