-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
68 lines (56 loc) · 1.53 KB
/
main.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
//
// Created by malled on 3/19/17.
//
#include <avr/io.h>
#include <util/delay.h>
#include "main.h"
#include "adc.h"
#include "uart.h"
static void init(void);
#define AKKU_TYPE_NIMH
#define AKKU_TYPE_LEAD
#if defined(AKKU_TYPE_NIMH) && defined(AKKU_TYPE_LEAD)
#error nope
#endif
#if defined(AKKU_TYPE_LEAD)
#define MIN_VOLTAGE 10500 // min voltage for lead-gel batteries [mV]
#endif
#if defined(AKKU_TYPE_NIMH)
#define MIN_VOLTAGE 8000 // min voltage for 8-cell NIMH batteries [mV]
#endif
typedef struct {
uint16_t bat_voltage;
uint16_t crc;
union {
struct{
uint8_t ext_power : 1; // external power supply connected
uint8_t reserved : 7;
}flags;
uint8_t value;
} usv_state;
} USV_STATE;
static USV_STATE state;
int main(void){
init();
while(1) {
for (int i = 0; i < 100; i++) {
_delay_ms(10);
}
state.bat_voltage = adc_get_bat_voltage();
if (state.bat_voltage < MIN_VOLTAGE) {
//shutdown
PORTB |= (1 << PIN_REL);
}
state.usv_state.value = 0;
state.usv_state.flags.ext_power = (PORTB & (1 << PIN_EXT_POWER)) >> PIN_EXT_POWER;
state.crc = 0; // TODO
uart_send_array((uint8_t*)&state, sizeof(USV_STATE));
}
return 42;
}
static void init(void){
PORTB &= ~((1 << PIN_REL) | (1 << PIN_RPI));
DDRB = (0 << PIN_EXT_POWER) | (1 << PIN_REL) | (1 << PIN_RPI);
adc_init();
uart_init();
}