forked from stancecoke/BMSBattery_S_controllers_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadc.c
More file actions
executable file
·70 lines (57 loc) · 1.62 KB
/
adc.c
File metadata and controls
executable file
·70 lines (57 loc) · 1.62 KB
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
/*
* EGG OpenSource EBike firmware
*
* Copyright (C) Casainho, 2015, 2106, 2017.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include "stm8s.h"
#include "gpio.h"
#include "stm8s_adc1.h"
uint8_t adc_throttle_busy_flag = 0;
void adc_init (void)
{
//init GPIO for the used ADC pins
GPIO_Init(GPIOB,
(THROTTLE__PIN || CURRENT_PHASE_B__PIN || CURRENT_TOTAL__PIN),
GPIO_MODE_IN_FL_NO_IT);
//de-Init ADC peripheral
ADC1_DeInit();
//init ADC1 peripheral
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_5,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_LEFT,
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
DISABLE);
}
uint8_t adc_read_throttle (void)
{
uint8_t ui8_temp;
adc_throttle_busy_flag = 1;
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,
ADC1_CHANNEL_4,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_LEFT,
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
DISABLE);
ADC1->CR1 |= ADC1_CR1_ADON;
while (!(ADC1->CSR & ADC1_FLAG_EOC)) ;
ui8_temp = ADC1->DRH;
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_5,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_RIGHT,
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
DISABLE);
ADC1->CR1 |= ADC1_CR1_ADON;
adc_throttle_busy_flag = 0;
return ui8_temp;
}