1 | /* Includes ------------------------------------------------------------------*/
|
2 | #include "stm32f0xx_hal.h"
|
3 |
|
4 | /* USER CODE BEGIN Includes */
|
5 | #include "stdbool.h"
|
6 | #include "states.h"
|
7 | /* USER CODE END Includes */
|
8 |
|
9 | /* Private variables ---------------------------------------------------------*/
|
10 | ADC_HandleTypeDef hadc;
|
11 | DMA_HandleTypeDef hdma_adc;
|
12 | TIM_HandleTypeDef htim1;
|
13 | TIM_HandleTypeDef htim3;
|
14 |
|
15 | /* USER CODE BEGIN PV */
|
16 |
|
17 | // ADC Buffer which the DMA writes into
|
18 | volatile uint16_t ADCBuffer[]={0x0000, 0x0000, 0x0000, 0x0000, 0xAAAA};
|
19 |
|
20 | /* USER CODE END PV */
|
21 |
|
22 | /* Private function prototypes -----------------------------------------------*/
|
23 | void SystemClock_Config(void);
|
24 | static void MX_GPIO_Init(void);
|
25 | static void MX_DMA_Init(void);
|
26 | static void MX_ADC_Init(void);
|
27 | static void MX_TIM1_Init(void);
|
28 | static void MX_TIM3_Init(void);
|
29 |
|
30 | /* USER CODE BEGIN PFP */
|
31 |
|
32 | /* USER CODE END PFP */
|
33 |
|
34 | /* USER CODE BEGIN 0 */
|
35 |
|
36 | /* USER CODE END 0 */
|
37 |
|
38 | int main(void)
|
39 | {
|
40 |
|
41 | /* USER CODE BEGIN 1 */
|
42 |
|
43 | /* USER CODE END 1 */
|
44 |
|
45 | /* MCU Configuration----------------------------------------------------------*/
|
46 |
|
47 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
48 | HAL_Init();
|
49 |
|
50 | /* Configure the system clock */
|
51 | SystemClock_Config();
|
52 |
|
53 | /* Initialize all configured peripherals */
|
54 | MX_GPIO_Init();
|
55 | MX_DMA_Init();
|
56 | MX_ADC_Init();
|
57 | MX_TIM1_Init();
|
58 | MX_TIM3_Init();
|
59 |
|
60 | /* USER CODE BEGIN 2 */
|
61 |
|
62 | // switch on clock (pwm) for charge pump
|
63 | HAL_TIM_PWM_MspInit(&htim3);
|
64 | HAL_TIM_PWM_Init(&htim3);
|
65 | HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
|
66 |
|
67 | // switch on pwm for MOSFET
|
68 | HAL_TIM_PWM_MspInit(&htim1);
|
69 | HAL_TIM_PWM_Init(&htim1);
|
70 | HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
|
71 |
|
72 | // switch on LED
|
73 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
|
74 |
|
75 | // switch on DMA
|
76 | HAL_DMA_Init(&hdma_adc);
|
77 | HAL_ADC_Start_DMA(&hadc,(uint32_t*)ADCBuffer, 4);
|
78 |
|
79 |
|
80 | // switch on ADC
|
81 | HAL_ADC_MspInit(&hadc); // Init ADC and DMA
|
82 | HAL_ADC_Start(&hadc);
|
83 |
|
84 |
|
85 |
|
86 | /* USER CODE END 2 */
|
87 |
|
88 | /* Infinite loop */
|
89 | /* USER CODE BEGIN WHILE */
|
90 | while (1)
|
91 | {
|
92 | /* USER CODE END WHILE */
|
93 |
|
94 | /* USER CODE BEGIN 3 */
|
95 |
|
96 |
|
97 | if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)==GPIO_PIN_SET)){
|
98 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET); // channel LED green on
|
99 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET); // channel LED red off
|
100 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); // activates driver stage
|
101 | HAL_Delay(1000);
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 | if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)==GPIO_PIN_SET)){
|
107 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET); // channel LED green off
|
108 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET); // channel LED red on
|
109 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); // deactivates driver stage
|
110 | HAL_Delay(1000);
|
111 | }
|
112 |
|
113 | if( ADCBuffer[3] > 100){
|
114 | HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
|
115 | } else {
|
116 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
|
117 | }
|
118 |
|
119 | if(ADCBuffer[3] > 300){
|
120 | HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);
|
121 | } else {
|
122 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
|
123 | }
|
124 |
|
125 |
|
126 | }
|
127 | /* USER CODE END 3 */
|
128 |
|
129 | }
|
130 |
|
131 | /** System Clock Configuration
|
132 | */
|
133 |
|
134 |
|
135 | /* ADC init function */
|
136 | void MX_ADC_Init(void)
|
137 | {
|
138 |
|
139 | ADC_ChannelConfTypeDef sConfig;
|
140 |
|
141 | /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
142 | */
|
143 | hadc.Instance = ADC1;
|
144 | hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
|
145 | hadc.Init.Resolution = ADC_RESOLUTION12b;
|
146 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
147 | hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
148 | hadc.Init.EOCSelection = EOC_SEQ_CONV;
|
149 | hadc.Init.LowPowerAutoWait = DISABLE;
|
150 | hadc.Init.LowPowerAutoPowerOff = DISABLE;
|
151 | hadc.Init.ContinuousConvMode = ENABLE;
|
152 | hadc.Init.DiscontinuousConvMode = DISABLE;
|
153 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
154 | hadc.Init.DMAContinuousRequests = ENABLE;
|
155 | hadc.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
156 | HAL_ADC_Init(&hadc);
|
157 |
|
158 | /**Configure for the selected ADC regular channel to be converted.
|
159 | */
|
160 | sConfig.Channel = ADC_CHANNEL_0;
|
161 | sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
162 | sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
|
163 | HAL_ADC_ConfigChannel(&hadc, &sConfig);
|
164 |
|
165 | /**Configure for the selected ADC regular channel to be converted.
|
166 | */
|
167 | sConfig.Channel = ADC_CHANNEL_1;
|
168 | HAL_ADC_ConfigChannel(&hadc, &sConfig);
|
169 |
|
170 | /**Configure for the selected ADC regular channel to be converted.
|
171 | */
|
172 | sConfig.Channel = ADC_CHANNEL_2;
|
173 | HAL_ADC_ConfigChannel(&hadc, &sConfig);
|
174 |
|
175 | /**Configure for the selected ADC regular channel to be converted.
|
176 | */
|
177 | sConfig.Channel = ADC_CHANNEL_3;
|
178 | HAL_ADC_ConfigChannel(&hadc, &sConfig);
|
179 |
|
180 | }
|
181 |
|
182 | /**
|
183 | * Enable DMA controller clock
|
184 | */
|
185 | void MX_DMA_Init(void)
|
186 | {
|
187 | /* DMA controller clock enable */
|
188 | __DMA1_CLK_ENABLE();
|
189 |
|
190 | /* DMA interrupt init */
|
191 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 2, 0);
|
192 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
193 |
|
194 | }
|