heat control  r71
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
adc.c
Go to the documentation of this file.
1 #include <math.h>
2 #include "adc.h"
3 #if INC_TSEND_INIT == 0
4  #include "setup.h"
5 #endif
6 
18 UCHAR volatile g_justMuxed = FALSE;
19 
23 void AdcInit(void) {
24  // ADC initialization
25  // ADC Voltage Reference: AVCC pin
27 
28 // ADCSRA - ADC Control and Status Register A
29 // | ADEN [1] | ADSC [1] | ADATE [0] | ADIF [0] | ADIE [1] | ADPS2 [0] | ADPS1 [1] | ADPS0 [1] |
30 // ADEN <- enable ADC
31 // ADSC <- start conversion immediately to consumpt the initial 25 ADC cycles
32 // ADIE <- enable ADC interrupt
33 // ADPSn <- prescaler source clock 8MHz / 64 = 125KHz
34  ADC_SR = (1<<ADEN) | (1<<ADSC) | (0<<AD_FR) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0);
35 }
36 
41 ISR(ADC_vect) {
42  static UCHAR id=0, meanCnt=0;
43 
44  //terminate if nothing to do
45  if(!g_adcConverting)
46  return;
47  //throw away the conversion result if MUX just switched
48  if(! g_justMuxed) {
49  //read AD conversion result
50  g_adcData[id] += ADCW;
51  // Select next ADC input
52  if(++meanCnt > ADC_MEAN_VALUES)
53  {
54  meanCnt = 0;
55  if(++id >= ADC_INPUTS)
56  {
57  id=0;
58  SetMux(id);
59  //converting should evaluate to FALSE @ this point
61  return;
62  }
63  SetMux(id);
64  }
65  }
66  else
69 }
70 
75  //all results processed?
77 #if INC_TSEND_INIT == 1
78  && g_adcData[ADC_SUPP_VOLT_ID] == INVALID_VAL_U16
79 #endif
80  )
82 }
83 
87 void StartAdcConversion(void) {
89  //effectively start conversion
90  ADC_SR |= (1<<ADSC);
91 }
92 
97 void SetMux(UCHAR ch) {
98  ADMUX = (FIRST_ADC_INPUT | ADC_VREF_SEL) + ch;
99  g_justMuxed = TRUE;
100 }
101 
108  UINT res = g_adcData[id];
109  float rohm = 0.0, div = 0.0;
110 
111  //do nothing if in conversion progress
112  if(g_adcConverting)
113  return INVALID_VAL_U16;
116 #if INC_TSEND_INIT == 0
117  rohm = (float)((((float)(((float)((float)res / (float)ADC_MEAN_VALUES)) * (float)gArv())) / (float)ADC_RES)) * (float)gRcv();
118  div = log(rohm/(float)gRri());
119 #elif INC_TSEND_INIT == 1
120  rohm = (float)((((float)(((float)((float)res / (float)ADC_MEAN_VALUES)) * (float)ADC_REF_VOLTAGE)) / (float)ADC_RES)) * (float)REV_CURRENT_VALUE;
121  div = log(rohm/(id == ADC_TMP_INNER_ID ? (float)RR_IN : (float)RR_OUT));
122 #endif
123  return (UINT)(((1/(A1 + B1*div + C1*pow(div,2) + D1*pow(div,3))) - 273.15) * 1000.0);
124 }
125 
127  UINT res = g_adcData[id];
128 
129  //do nothing if in conversion progress
130  if(g_adcConverting)
131  return INVALID_VAL_U16;
134 // return res;
135  return (UINT)(((float)(((float)res / (float)ADC_MEAN_VALUES) * (float)ADC_REF_VOLTAGE) / (float)ADC_RES) * ADC_SUP_DIV_FCT);
136 }
137 
138 
139 
140 
141 
142 
#define ADC_SR
Definition: types.h:43
#define RR_IN
Definition: init.h:34
#define ADC_RES
Definition: adc.h:17
void ProcessNextConversion(void)
checks whether all converted values are processed and starts a new ADC conversion if the latter evalu...
Definition: adc.c:74
#define ADC_REF_VOLTAGE
Definition: init.h:42
UCHAR volatile g_justMuxed
Definition: adc.c:18
UINT CalcSupplyVoltage(UCHAR id)
Definition: adc.c:126
#define INC_TSEND_INIT
Definition: types.h:13
#define ADC_SUP_DIV_FCT
Definition: init.h:23
#define ADC_MEAN_VALUES
Definition: adc.h:21
UINT g_adcData[ADC_INPUTS]
Definition: adc.c:10
#define FALSE
Definition: types.h:93
ISR(ADC_vect)
ADC interrupt service.
Definition: adc.c:41
#define FIRST_ADC_INPUT
Definition: init.h:7
void SetMux(UCHAR ch)
set multiplexer thus switching between channels
Definition: adc.c:97
#define UCHAR
Definition: types.h:73
#define ADC_TMP_OUTER_ID
Definition: init.h:19
#define C1
Definition: adc.h:33
void AdcInit(void)
initialize the adc resource
Definition: adc.c:23
#define ADC_VREF_SEL
Definition: adc.h:9
#define ADC_TMP_INNER_ID
Definition: init.h:15
#define REV_CURRENT_VALUE
Definition: init.h:27
UINT CalcAdcTemp(UCHAR id)
calculate the temperature from specific ADC value in data array, specified by id
Definition: adc.c:107
#define D1
Definition: adc.h:37
#define TRUE
Definition: types.h:97
#define INVALID_VAL_U16
Definition: types.h:105
UCHAR volatile g_adcConverting
Definition: adc.c:14
#define RR_OUT
Definition: init.h:38
#define A1
Definition: adc.h:25
#define B1
Definition: adc.h:29
#define UINT
Definition: types.h:81
#define ADC_INPUTS
Definition: adc.h:13
void StartAdcConversion(void)
start AD conversion, this will trigger the conversion of all selected channels via the interrupt ...
Definition: adc.c:87
#define AD_FR
Definition: types.h:49