-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
executable file
·137 lines (112 loc) · 3.88 KB
/
adc.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* TongSheng TSDZ2 motor controller firmware/
*
* Copyright (C) Casainho, 2018.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include <stdio.h>
#include "stm8s.h"
#include "pins.h"
#include "stm8s_adc1.h"
#include "adc.h"
#include "ebike_app.h"
#include "motor.h"
static void adc_trigger (void);
void adc_init (void)
{
uint16_t ui16_counter;
uint16_t ui16_adc_battery_current_offset;
uint16_t ui16_adc_torque_sensor_offset;
uint8_t ui8_i;
//init GPIO for the used ADC pins
GPIO_Init(GPIOB,
(GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_3),
GPIO_MODE_IN_FL_NO_IT);
//init ADC1 peripheral
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,
ADC1_CHANNEL_7,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_LEFT,
(ADC1_SCHMITTTRIG_CHANNEL3 | ADC1_SCHMITTTRIG_CHANNEL5 | ADC1_SCHMITTTRIG_CHANNEL6 | ADC1_SCHMITTTRIG_CHANNEL7),
DISABLE);
ADC1_ScanModeCmd (ENABLE);
ADC1_Cmd (ENABLE);
//********************************************************************************
// next code is for "calibrating" the offset value of some ADC channels
// read and discard few samples of ADC, to make sure the next samples are ok
for (ui8_i = 0; ui8_i <= 64; ui8_i++)
{
ui16_counter = TIM3_GetCounter () + 10; // delay ~10ms
while (TIM3_GetCounter () < ui16_counter) ; // delay ~10ms
adc_trigger ();
while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion
}
// read and average a few values of ADC battery current
ui16_adc_battery_current_offset = 0;
for (ui8_i = 0; ui8_i <= 16; ui8_i++)
{
ui16_counter = TIM3_GetCounter () + 10; // delay ~10ms
while (TIM3_GetCounter () < ui16_counter) ; // delay ~10ms
adc_trigger ();
while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion
ui16_adc_battery_current_offset += UI8_ADC_BATTERY_CURRENT;
}
ui16_adc_battery_current_offset >>= 4;
ui8_adc_battery_current_offset = ui16_adc_battery_current_offset >> 2;
ui8_adc_motor_phase_current_offset = ui8_adc_battery_current_offset;
// read and average a few values of ADC torque sensor
ui16_adc_torque_sensor_offset = 0;
for (ui8_i = 0; ui8_i <= 16; ui8_i++)
{
ui16_counter = TIM3_GetCounter () + 10; // delay ~10ms
while (TIM3_GetCounter () < ui16_counter) ; // delay ~10ms
adc_trigger ();
while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion
ui16_adc_torque_sensor_offset += UI8_ADC_TORQUE_SENSOR;
}
ui16_adc_torque_sensor_offset >>= 4;
ui8_adc_torque_sensor_min_value = ((uint8_t) ui16_adc_torque_sensor_offset) + ADC_TORQUE_SENSOR_THRESHOLD;
ui8_adc_torque_sensor_max_value = ui8_adc_torque_sensor_min_value + 32;
}
static void adc_trigger (void)
{
// trigger ADC conversion of all channels (scan conversion, buffered)
ADC1->CSR &= 0x07; // clear EOC flag first (selected also channel 7)
ADC1->CR1 |= ADC1_CR1_ADON; // Start ADC1 conversion
}
uint16_t ui16_adc_read_battery_current_10b (void)
{
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*)(0x53EB);
temph = *(uint8_t*)(0x53EA);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint16_t ui16_adc_read_torque_sensor_10b (void)
{
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*)(0x53E9);
temph = *(uint8_t*)(0x53E8);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint16_t ui16_adc_read_throttle_10b (void)
{
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*)(0x53EF);
temph = *(uint8_t*)(0x53EE);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint16_t ui16_adc_read_battery_voltage_10b (void)
{
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*)(0x53ED);
temph = *(uint8_t*)(0x53EC);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}