Skip to content

Commit 36853f5

Browse files
committed
ADC and hall sensors now works
1 parent c39caee commit 36853f5

File tree

5 files changed

+124
-124
lines changed

5 files changed

+124
-124
lines changed

adc.c

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
1-
///*
2-
// * EGG OpenSource EBike firmware
3-
// *
4-
// * Copyright (C) Casainho, 2015, 2106, 2017.
5-
// *
6-
// * Released under the GPL License, Version 3
7-
// */
8-
//
9-
//#include <stdint.h>
10-
//#include "stm8s.h"
11-
//#include "gpio.h"
12-
//#include "stm8s_adc1.h"
13-
//
14-
//void adc_init (void)
15-
//{
16-
// //init GPIO for the used ADC pins
17-
// GPIO_Init(GPIOB,
18-
// (THROTTLE__PIN || CURRENT_PHASE_B__PIN || CURRENT_TOTAL__PIN),
19-
// GPIO_MODE_IN_FL_NO_IT);
20-
//
21-
// //de-Init ADC peripheral
22-
// ADC1_DeInit();
23-
//
24-
// //init ADC1 peripheral
25-
// ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
26-
// ADC1_CHANNEL_5,
27-
// ADC1_PRESSEL_FCPU_D2,
28-
// ADC1_EXTTRIG_TIM,
29-
// DISABLE,
30-
// ADC1_ALIGN_LEFT,
31-
// (ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
32-
// DISABLE);
33-
//}
34-
//
35-
//uint8_t adc_read_throttle (void)
36-
//{
37-
// uint8_t ui8_temp;
38-
//
39-
// adc_throttle_busy_flag = 1;
40-
//
41-
// ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,
42-
// ADC1_CHANNEL_4,
43-
// ADC1_PRESSEL_FCPU_D2,
44-
// ADC1_EXTTRIG_TIM,
45-
// DISABLE,
46-
// ADC1_ALIGN_LEFT,
47-
// (ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
48-
// DISABLE);
49-
//
50-
// ADC1->CR1 |= ADC1_CR1_ADON;
51-
// while (!(ADC1->CSR & ADC1_FLAG_EOC)) ;
52-
// ui8_temp = ADC1->DRH;
53-
//
54-
// ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
55-
// ADC1_CHANNEL_5,
56-
// ADC1_PRESSEL_FCPU_D2,
57-
// ADC1_EXTTRIG_TIM,
58-
// DISABLE,
59-
// ADC1_ALIGN_RIGHT,
60-
// (ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
61-
// DISABLE);
62-
//
63-
// ADC1->CR1 |= ADC1_CR1_ADON;
64-
//
65-
// adc_throttle_busy_flag = 0;
66-
//
67-
// return ui8_temp;
68-
//}
1+
/*
2+
* EGG OpenSource EBike firmware
3+
*
4+
* Copyright (C) Casainho, 2015, 2106, 2017.
5+
*
6+
* Released under the GPL License, Version 3
7+
*/
8+
9+
#include <stdint.h>
10+
#include "stm8s.h"
11+
#include "gpio.h"
12+
#include "stm8s_adc1.h"
13+
14+
uint8_t adc_throttle_busy_flag = 0;
15+
16+
void adc_init (void)
17+
{
18+
//init GPIO for the used ADC pins
19+
GPIO_Init(GPIOB,
20+
(THROTTLE__PIN || CURRENT_PHASE_B__PIN || CURRENT_TOTAL__PIN),
21+
GPIO_MODE_IN_FL_NO_IT);
22+
23+
//de-Init ADC peripheral
24+
ADC1_DeInit();
25+
26+
//init ADC1 peripheral
27+
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
28+
ADC1_CHANNEL_5,
29+
ADC1_PRESSEL_FCPU_D2,
30+
ADC1_EXTTRIG_TIM,
31+
DISABLE,
32+
ADC1_ALIGN_LEFT,
33+
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
34+
DISABLE);
35+
}
36+
37+
uint8_t adc_read_throttle (void)
38+
{
39+
uint8_t ui8_temp;
40+
41+
adc_throttle_busy_flag = 1;
42+
43+
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,
44+
ADC1_CHANNEL_4,
45+
ADC1_PRESSEL_FCPU_D2,
46+
ADC1_EXTTRIG_TIM,
47+
DISABLE,
48+
ADC1_ALIGN_LEFT,
49+
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
50+
DISABLE);
51+
52+
ADC1->CR1 |= ADC1_CR1_ADON;
53+
while (!(ADC1->CSR & ADC1_FLAG_EOC)) ;
54+
ui8_temp = ADC1->DRH;
55+
56+
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
57+
ADC1_CHANNEL_5,
58+
ADC1_PRESSEL_FCPU_D2,
59+
ADC1_EXTTRIG_TIM,
60+
DISABLE,
61+
ADC1_ALIGN_RIGHT,
62+
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6),
63+
DISABLE);
64+
65+
ADC1->CR1 |= ADC1_CR1_ADON;
66+
67+
adc_throttle_busy_flag = 0;
68+
69+
return ui8_temp;
70+
}

adc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "main.h"
1313

14+
extern uint8_t adc_throttle_busy_flag;
15+
1416
void adc_init (void);
1517
uint8_t adc_read_throttle (void);
1618

main.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ int main (void)
6464
{
6565
// static uint32_t ui32_cruise_counter = 0;
6666
// static uint8_t ui8_cruise_duty_cycle = 0;
67-
// static uint16_t ui16_adc_value;
68-
// static uint8_t ui8_temp = 0;
67+
static uint16_t ui16_adc_value;
68+
static uint8_t ui8_temp = 0;
6969

7070
//set clock at the max 16MHz
7171
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
@@ -78,33 +78,25 @@ int main (void)
7878
uart_init ();
7979
pwm_init ();
8080
hall_sensor_init ();
81-
// adc_init ();
81+
adc_init ();
8282

83-
// ITC_SetSoftwarePriority (ITC_IRQ_PORTE, ITC_PRIORITYLEVEL_1); // hall sensors interrupt have the most priority
8483
// ITC_SetSoftwarePriority (ITC_IRQ_TIM1_OVF, ITC_PRIORITYLEVEL_2);
85-
//
84+
8685
enableInterrupts();
87-
//#if (SVM_TABLE == SVM)
88-
// TIM1_SetCompare1(126 << 1);
89-
// TIM1_SetCompare2(126 << 1);
90-
// TIM1_SetCompare3(126 << 1);
91-
//#elif (SVM_TABLE == SINE) || (SVM_TABLE == SINE_SVM)
92-
// TIM1_SetCompare1(126 << 2);
93-
// TIM1_SetCompare2(126 << 2);
94-
// TIM1_SetCompare3(126 << 2);
95-
//#endif
96-
//
97-
// hall_sensors_read_and_action (); // needed to start the motor
86+
#if (SVM_TABLE == SVM)
87+
TIM1_SetCompare1(126 << 1);
88+
TIM1_SetCompare2(126 << 1);
89+
TIM1_SetCompare3(126 << 1);
90+
#elif (SVM_TABLE == SINE) || (SVM_TABLE == SINE_SVM)
91+
TIM1_SetCompare1(126 << 2);
92+
TIM1_SetCompare2(126 << 2);
93+
TIM1_SetCompare3(126 << 2);
94+
#endif
95+
96+
hall_sensors_read_and_action (); // needed to start the motor
9897

9998
while (1)
10099
{
101-
while (1)
102-
{
103-
debug_pin_set ();
104-
printf("testing UART");
105-
106-
debug_pin_reset ();
107-
}
108100

109101
// // static uint16_t c;
110102
// static uint32_t ui32_counter = 0;
@@ -125,11 +117,16 @@ int main (void)
125117
// ui32_counter = 0;
126118
// while (ui8_adc_total_current_busy_flag) ;
127119
//
128-
// /****************************************************************************/
129-
// // read throttle and execute cruise control
130-
// //
131-
// ui16_adc_value = (uint16_t) adc_read_throttle ();
132-
// ui8_temp = (uint8_t) map (ui16_adc_value, ADC_THROTTLE_MIN_VALUE, ADC_THROTTLE_MAX_VALUE, 0, 237);
120+
/****************************************************************************/
121+
// read throttle and execute cruise control
122+
//
123+
ui16_adc_value = (uint16_t) adc_read_throttle ();
124+
ui8_temp = (uint8_t) map (ui16_adc_value, ADC_THROTTLE_MIN_VALUE, ADC_THROTTLE_MAX_VALUE, 0, 237);
125+
126+
debug_pin_set ();
127+
printf("%d\n", ui8_temp);
128+
129+
debug_pin_reset ();
133130
//
134131
// // Cruise control
135132
// if (ui8_cruise_state == 0)

motor.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,6 @@ uint16_t ui16_adc_current_phase_B_temp = 0;
825825
uint8_t adc_total_current = 0;
826826
uint8_t ui8_adc_total_current_busy_flag = 0;
827827
uint8_t adc_throttle = 0;
828-
uint8_t adc_throttle_busy_flag = 0;
829828

830829
void set_duty_cycle (uint8_t value)
831830
{

utils.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
///*
2-
// * EGG OpenSource EBike firmware
3-
// *
4-
// * Copyright (C) Casainho, 2015, 2106, 2017.
5-
// *
6-
// * Released under the GPL License, Version 3
7-
// */
8-
//
9-
//#include <stdint.h>
10-
//#include "stm8s.h"
11-
//
12-
//int32_t map (int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max)
13-
//{
14-
// // if input is smaller/bigger than expected return the min/max out ranges value
15-
// if (x < in_min)
16-
// return out_min;
17-
// else if (x > in_max)
18-
// return out_max;
19-
//
20-
// // map the input to the output range.
21-
// // round up if mapping bigger ranges to smaller ranges
22-
// else if ((in_max - in_min) > (out_max - out_min))
23-
// return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min + 1) + out_min;
24-
// // round down if mapping smaller ranges to bigger ranges
25-
// else
26-
// return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
27-
//}
1+
/*
2+
* EGG OpenSource EBike firmware
3+
*
4+
* Copyright (C) Casainho, 2015, 2106, 2017.
5+
*
6+
* Released under the GPL License, Version 3
7+
*/
8+
9+
#include <stdint.h>
10+
#include "stm8s.h"
11+
12+
int32_t map (int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max)
13+
{
14+
// if input is smaller/bigger than expected return the min/max out ranges value
15+
if (x < in_min)
16+
return out_min;
17+
else if (x > in_max)
18+
return out_max;
19+
20+
// map the input to the output range.
21+
// round up if mapping bigger ranges to smaller ranges
22+
else if ((in_max - in_min) > (out_max - out_min))
23+
return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min + 1) + out_min;
24+
// round down if mapping smaller ranges to bigger ranges
25+
else
26+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
27+
}

0 commit comments

Comments
 (0)