-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRSensor.c
78 lines (56 loc) · 1.79 KB
/
IRSensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* IRSensor.c
*
* Created: 2021-04-26 18:37:52
* Author: Edvin
*/
#include "i2cmaster.h"
#include "IRSensor.h"
#include "GlobalDefinitions.h"
#include <util/delay.h>
static float averageValue(uint8_t adress);
/*******************************/
/* Initializes IR-sensor. */
/*******************************/
void IRinit() {
// Enable pull up
PORTC |= (1 << PINC4) | (1 << PINC5);
i2c_init();
}
/************************************************/
/* Reads current ambient temperature. */
/************************************************/
float IRreadAmbientTemp(){
float ambient = averageValue(TEMPAMBIENT);
return ((ambient / 50) - 273);
}
/************************************************/
/* Reads temperature of object. */
/************************************************/
float IRreadObjectTemp(){
float object = averageValue(TEMPOBJECT);
return ((object / 50) - 273);
}
/********************************************************************************/
/* Averages the reading of the IR-sensor by adding the readings and dividing */
/* by the sample size. */
/********************************************************************************/
static float averageValue(uint8_t adress){
float input = 0;
for(int i = 0; i < SAMPLESIZE; i++) {
// Write to slave
i2c_start(SLAVE_ADRESS + I2C_WRITE);
i2c_write(adress);
i2c_rep_start(SLAVE_ADRESS + I2C_READ);
// Read temperature.
uint8_t lowByte = i2c_readAck();
uint8_t highByte = i2c_readAck();
uint8_t pec = i2c_readNak();
i2c_stop();
// Add numbers.
input += ((uint16_t)highByte << 8) | lowByte;
// Wait a bit.
_delay_ms(1);
}
return input/SAMPLESIZE;
}