LIGHT2

Dependencies:   FastPWM mbed

Committer:
Lightvalve
Date:
Fri Jun 24 00:18:20 2022 +0000
Revision:
248:bfdf0f479a38
Parent:
245:e9c5ec04e378
Child:
249:544011ad9c85
220624 LVDT good

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GiJeongKim 0:51c43836c1d7 1 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 2 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 3 #include "setting.h"
GiJeongKim 0:51c43836c1d7 4
Lightvalve 244:30896263bd8b 5 extern ADC_HandleTypeDef hadc1;
Lightvalve 244:30896263bd8b 6 extern ADC_HandleTypeDef hadc2;
Lightvalve 245:e9c5ec04e378 7 extern ADC_HandleTypeDef hadc3;
Lightvalve 244:30896263bd8b 8
Lightvalve 245:e9c5ec04e378 9 void Init_ADC1(void)
Lightvalve 244:30896263bd8b 10 {
Lightvalve 248:bfdf0f479a38 11 /*
Lightvalve 248:bfdf0f479a38 12 // ADC Setup
Lightvalve 248:bfdf0f479a38 13 // RCC->APB2ENR |= RCC_APB2ENR_ADC3EN; // clock for ADC3
Lightvalve 248:bfdf0f479a38 14 // RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
Lightvalve 248:bfdf0f479a38 15 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
Lightvalve 248:bfdf0f479a38 16 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable clock for GPIOC
Lightvalve 248:bfdf0f479a38 17 // RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // Enable clock for GPIOB
Lightvalve 248:bfdf0f479a38 18 ADC1->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC1 ON
Lightvalve 248:bfdf0f479a38 19 // ADC->CCR = 0x00000016; // Regular simultaneous mode only
Lightvalve 248:bfdf0f479a38 20 ADC1->CR1 |= 0x800; // Discontinuous mode
Lightvalve 248:bfdf0f479a38 21 ADC1->CR1 |= 0x100; // Scan mode
Lightvalve 248:bfdf0f479a38 22 ADC1->CR2 |= 0x400; // EOC
Lightvalve 248:bfdf0f479a38 23 ADC1->SQR1 |= 0x100000; // 2 conversions
Lightvalve 248:bfdf0f479a38 24 // ADC1->SQR3 = 0x00000008; // use PB_0 as input - ADC1_IN8
Lightvalve 248:bfdf0f479a38 25 ADC1->SQR3 |= 0x0000000E; //channel // use PC_4 as input - ADC1_IN14
Lightvalve 248:bfdf0f479a38 26 ADC1->SQR3 |= 0x000001E0; // use PC_5 as input - ADC1_IN15 0b0000 0000 0000 0000 0000 0001 1110 0000
Lightvalve 248:bfdf0f479a38 27 // ADC2->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC2 ON
Lightvalve 248:bfdf0f479a38 28 // ADC2->SQR3 = 0x00000008; // use PB_0 as input - ADC2_IN8
Lightvalve 248:bfdf0f479a38 29 // ADC3->CR2 |= ADC_CR2_ADON; // ADC3 ON00000000000
Lightvalve 248:bfdf0f479a38 30 // ADC3->SQR3 = 0x0000000B; // use PC_1, - ADC3_IN11
Lightvalve 248:bfdf0f479a38 31 GPIOC->MODER |= 0b111100000000; //each channel // PC_4, PC_5 are analog inputs
Lightvalve 248:bfdf0f479a38 32 // GPIOB->MODER |= 0x3; // PB_0 as analog input
Lightvalve 248:bfdf0f479a38 33 // ADC1->SMPR2 |= 0x01000000; // 15 cycles on CH_8, 0b0000000100000000<<16
Lightvalve 248:bfdf0f479a38 34 // ADC1->SMPR1 |= 0x00001000; // 15 cycles on CH_14
Lightvalve 248:bfdf0f479a38 35 // ADC1->SMPR1 |= 0x00008000; // 15 cycles on CH_15
Lightvalve 248:bfdf0f479a38 36 // ADC2->SMPR2 |= 0x01000000; // 15 cycles on CH_8, 0b0000000100000000<<16
Lightvalve 248:bfdf0f479a38 37 // ADC3->SMPR1 |= 0x00000008; // 15 cycles on CH_11, 0b0000000000001000
Lightvalve 248:bfdf0f479a38 38
Lightvalve 248:bfdf0f479a38 39 */
Lightvalve 245:e9c5ec04e378 40
Lightvalve 248:bfdf0f479a38 41 //////////////////////////////////////////////////ADC1////////////////////////////////////////////////
Lightvalve 248:bfdf0f479a38 42 ADC_ChannelConfTypeDef sConfig = {0};
Lightvalve 245:e9c5ec04e378 43
Lightvalve 248:bfdf0f479a38 44 hadc1.Instance = ADC1;
Lightvalve 248:bfdf0f479a38 45 hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
Lightvalve 248:bfdf0f479a38 46 hadc1.Init.Resolution = ADC_RESOLUTION_12B;
Lightvalve 248:bfdf0f479a38 47 hadc1.Init.ScanConvMode = ENABLE;
Lightvalve 248:bfdf0f479a38 48 hadc1.Init.ContinuousConvMode = DISABLE;
Lightvalve 248:bfdf0f479a38 49 hadc1.Init.DiscontinuousConvMode = ENABLE;
Lightvalve 248:bfdf0f479a38 50 hadc1.Init.NbrOfDiscConversion = 1;
Lightvalve 248:bfdf0f479a38 51 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
Lightvalve 248:bfdf0f479a38 52 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
Lightvalve 248:bfdf0f479a38 53 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
Lightvalve 248:bfdf0f479a38 54 hadc1.Init.NbrOfConversion = 2;
Lightvalve 248:bfdf0f479a38 55 hadc1.Init.DMAContinuousRequests = DISABLE;
Lightvalve 248:bfdf0f479a38 56 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
Lightvalve 248:bfdf0f479a38 57 if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 58 // Error_Handler();
Lightvalve 248:bfdf0f479a38 59 }
Lightvalve 248:bfdf0f479a38 60 sConfig.Channel = ADC_CHANNEL_14;
Lightvalve 248:bfdf0f479a38 61 sConfig.Rank = 1;
Lightvalve 248:bfdf0f479a38 62 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
Lightvalve 248:bfdf0f479a38 63 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 64 // Error_Handler();
Lightvalve 248:bfdf0f479a38 65 }
Lightvalve 248:bfdf0f479a38 66 sConfig.Channel = ADC_CHANNEL_15;
Lightvalve 248:bfdf0f479a38 67 sConfig.Rank = 2;
Lightvalve 248:bfdf0f479a38 68 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 69 // Error_Handler();
Lightvalve 248:bfdf0f479a38 70 }
Lightvalve 248:bfdf0f479a38 71 hadc1.Instance->CR2 |= ADC_CR2_ADON;
Lightvalve 244:30896263bd8b 72 }
Lightvalve 244:30896263bd8b 73
Lightvalve 245:e9c5ec04e378 74 void Init_ADC2(void)
Lightvalve 245:e9c5ec04e378 75 {
Lightvalve 248:bfdf0f479a38 76
Lightvalve 248:bfdf0f479a38 77 //////////////////////////////////////////////////ADC2////////////////////////////////////////////////
Lightvalve 248:bfdf0f479a38 78 ADC_ChannelConfTypeDef sConfig = {0};
Lightvalve 248:bfdf0f479a38 79
Lightvalve 248:bfdf0f479a38 80 hadc2.Instance = ADC2;
Lightvalve 248:bfdf0f479a38 81 hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
Lightvalve 248:bfdf0f479a38 82 hadc2.Init.Resolution = ADC_RESOLUTION_12B;
Lightvalve 248:bfdf0f479a38 83 hadc2.Init.ScanConvMode = ENABLE;
Lightvalve 248:bfdf0f479a38 84 hadc2.Init.ContinuousConvMode = DISABLE;
Lightvalve 248:bfdf0f479a38 85 hadc2.Init.DiscontinuousConvMode = ENABLE;
Lightvalve 248:bfdf0f479a38 86 hadc2.Init.NbrOfDiscConversion = 1;
Lightvalve 248:bfdf0f479a38 87 hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
Lightvalve 248:bfdf0f479a38 88 hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
Lightvalve 248:bfdf0f479a38 89 hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
Lightvalve 248:bfdf0f479a38 90 hadc2.Init.NbrOfConversion = 2;
Lightvalve 248:bfdf0f479a38 91 hadc2.Init.DMAContinuousRequests = DISABLE;
Lightvalve 248:bfdf0f479a38 92 hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
Lightvalve 248:bfdf0f479a38 93 if (HAL_ADC_Init(&hadc2) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 94 // Error_Handler();
Lightvalve 248:bfdf0f479a38 95 }
Lightvalve 248:bfdf0f479a38 96 sConfig.Channel = ADC_CHANNEL_11;
Lightvalve 248:bfdf0f479a38 97 sConfig.Rank = 1;
Lightvalve 248:bfdf0f479a38 98 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
Lightvalve 248:bfdf0f479a38 99 if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 100 // Error_Handler();
Lightvalve 248:bfdf0f479a38 101 }
Lightvalve 248:bfdf0f479a38 102 sConfig.Channel = ADC_CHANNEL_9;
Lightvalve 248:bfdf0f479a38 103 sConfig.Rank = 2;
Lightvalve 248:bfdf0f479a38 104 if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 105 // Error_Handler();
Lightvalve 248:bfdf0f479a38 106 }
Lightvalve 248:bfdf0f479a38 107 hadc2.Instance->CR2 |= ADC_CR2_ADON;
Lightvalve 245:e9c5ec04e378 108 }
Lightvalve 244:30896263bd8b 109
Lightvalve 245:e9c5ec04e378 110 void Init_ADC3(void)
Lightvalve 244:30896263bd8b 111 {
Lightvalve 248:bfdf0f479a38 112 //////////////////////////////////////////////////ADC3////////////////////////////////////////////////
Lightvalve 248:bfdf0f479a38 113 ADC_ChannelConfTypeDef sConfig = {0};
Lightvalve 248:bfdf0f479a38 114
Lightvalve 248:bfdf0f479a38 115 hadc3.Instance = ADC3;
Lightvalve 248:bfdf0f479a38 116 hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
Lightvalve 248:bfdf0f479a38 117 hadc3.Init.Resolution = ADC_RESOLUTION_12B;
Lightvalve 248:bfdf0f479a38 118 hadc3.Init.ScanConvMode = DISABLE;
Lightvalve 248:bfdf0f479a38 119 hadc3.Init.ContinuousConvMode = DISABLE;
Lightvalve 248:bfdf0f479a38 120 hadc3.Init.DiscontinuousConvMode = DISABLE;
Lightvalve 248:bfdf0f479a38 121 hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
Lightvalve 248:bfdf0f479a38 122 hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
Lightvalve 248:bfdf0f479a38 123 hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
Lightvalve 248:bfdf0f479a38 124 hadc3.Init.NbrOfConversion = 1;
Lightvalve 248:bfdf0f479a38 125 hadc3.Init.DMAContinuousRequests = DISABLE;
Lightvalve 248:bfdf0f479a38 126 hadc3.Init.EOCSelection = ADC_EOC_SEQ_CONV;
Lightvalve 248:bfdf0f479a38 127 if (HAL_ADC_Init(&hadc3) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 128 // Error_Handler();
Lightvalve 248:bfdf0f479a38 129 }
Lightvalve 248:bfdf0f479a38 130 sConfig.Channel = ADC_CHANNEL_1;
Lightvalve 248:bfdf0f479a38 131 sConfig.Rank = 1;
Lightvalve 248:bfdf0f479a38 132 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
Lightvalve 248:bfdf0f479a38 133 if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) {
Lightvalve 245:e9c5ec04e378 134 // Error_Handler();
Lightvalve 248:bfdf0f479a38 135 }
Lightvalve 248:bfdf0f479a38 136 hadc3.Instance->CR2 |= ADC_CR2_ADON;
Lightvalve 245:e9c5ec04e378 137 }
GiJeongKim 0:51c43836c1d7 138
Lightvalve 11:82d8768d7351 139
Lightvalve 244:30896263bd8b 140 void Init_TMR3()
Lightvalve 244:30896263bd8b 141 {
Lightvalve 11:82d8768d7351 142 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // enable TIM3 clock
Lightvalve 241:fb5b57e3f157 143 NVIC_EnableIRQ(TIM3_IRQn); //Enable TIM3 IRQ
Lightvalve 11:82d8768d7351 144
Lightvalve 11:82d8768d7351 145 TIM3->DIER |= TIM_DIER_UIE; // enable update interrupt
Lightvalve 244:30896263bd8b 146 TIM3->CR1 = 0x40; // CMS = 10, interrupt only when counting up // Center-aligned mode
Lightvalve 11:82d8768d7351 147 TIM3->CR1 |= TIM_CR1_UDIS;
Lightvalve 244:30896263bd8b 148 TIM3->RCR |= 0x001; // update event once per up/down count of TIM3
Lightvalve 11:82d8768d7351 149 TIM3->EGR |= TIM_EGR_UG;
Lightvalve 11:82d8768d7351 150
Lightvalve 244:30896263bd8b 151 TIM3->PSC = 0x00; // no prescaler, timer counts up in sync with the peripheral clock
Lightvalve 241:fb5b57e3f157 152 TIM3->ARR = TMR3_COUNT-1; // set auto reload, 5 khz
Lightvalve 248:bfdf0f479a38 153 TIM3->CNT = 0;
Lightvalve 248:bfdf0f479a38 154 TIM3->SR = 0;
Lightvalve 11:82d8768d7351 155 TIM3->CCER |= ~(TIM_CCER_CC1NP); // Interupt when low side is on.
Lightvalve 11:82d8768d7351 156 TIM3->CR1 |= TIM_CR1_CEN; // enable TIM4
Lightvalve 20:806196fda269 157 }
Lightvalve 20:806196fda269 158
Lightvalve 244:30896263bd8b 159 void Init_TMR2()
Lightvalve 244:30896263bd8b 160 {
Lightvalve 241:fb5b57e3f157 161 RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // enable TIM2 clock
Lightvalve 244:30896263bd8b 162
Lightvalve 244:30896263bd8b 163 //ISR Setup
Lightvalve 241:fb5b57e3f157 164 NVIC_EnableIRQ(TIM2_IRQn); //Enable TIM2 IRQ
Lightvalve 20:806196fda269 165
Lightvalve 23:59218d4a256d 166 TIM2->DIER |= TIM_DIER_UIE; // enable update interrupt
Lightvalve 23:59218d4a256d 167 TIM2->CR1 = 0x40; // CMS = 10, interrupt only when counting up // Center-aligned mode
Lightvalve 23:59218d4a256d 168 TIM2->CR1 |= TIM_CR1_UDIS;
Lightvalve 244:30896263bd8b 169 TIM2->RCR |= 0x001; // update event once per up/down count of TIM2
Lightvalve 23:59218d4a256d 170 TIM2->EGR |= TIM_EGR_UG;
Lightvalve 20:806196fda269 171
Lightvalve 241:fb5b57e3f157 172 TIM2->PSC = 0x00; // no prescaler, timer counts up in sync with the peripheral clock
Lightvalve 241:fb5b57e3f157 173 TIM2->ARR = TMR2_COUNT-1; // set auto reload, 5 khz
Lightvalve 248:bfdf0f479a38 174 TIM2->CNT = 0;
Lightvalve 248:bfdf0f479a38 175 TIM2->SR = 0;
Lightvalve 241:fb5b57e3f157 176 TIM2->CR1 |= TIM_CR1_CEN; // enable TIM2
Lightvalve 241:fb5b57e3f157 177 }
Lightvalve 241:fb5b57e3f157 178
Lightvalve 248:bfdf0f479a38 179
Lightvalve 244:30896263bd8b 180 void Init_TMR1()
Lightvalve 244:30896263bd8b 181 {
Lightvalve 244:30896263bd8b 182 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clock
Lightvalve 248:bfdf0f479a38 183
Lightvalve 245:e9c5ec04e378 184 FastPWM pwm_H1(PIN_H1);
Lightvalve 245:e9c5ec04e378 185 FastPWM pwm_L1(PIN_L1);
Lightvalve 245:e9c5ec04e378 186 FastPWM pwm_H2(PIN_H2);
Lightvalve 245:e9c5ec04e378 187 FastPWM pwm_L2(PIN_L2);
Lightvalve 244:30896263bd8b 188
Lightvalve 248:bfdf0f479a38 189
Lightvalve 248:bfdf0f479a38 190 TIM1->CR2 |= 0x20; //MMS = 010
Lightvalve 248:bfdf0f479a38 191
Lightvalve 248:bfdf0f479a38 192
Lightvalve 244:30896263bd8b 193 TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt
Lightvalve 244:30896263bd8b 194 TIM1->CR1 = 0x40; // CMS = 10, interrupt only when counting up // Center-aligned mode
Lightvalve 248:bfdf0f479a38 195 // TIM1->CR1 = 0x10; // CMS = 10, interrupt only when counting up // Center-aligned mode
Lightvalve 244:30896263bd8b 196 TIM1->CR1 |= TIM_CR1_UDIS;
Lightvalve 244:30896263bd8b 197 TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on,
Lightvalve 248:bfdf0f479a38 198 TIM1->RCR |= 0x001; // update event once per up/down count of TIM1
Lightvalve 244:30896263bd8b 199 TIM1->EGR |= TIM_EGR_UG;
Lightvalve 244:30896263bd8b 200
Lightvalve 245:e9c5ec04e378 201 TIM1->CCMR1 |= 0x60; //CH1 - PWM mode 1
Lightvalve 245:e9c5ec04e378 202 TIM1->CCMR1 |= 0x6000; //CH2 - PWM mode 1
Lightvalve 244:30896263bd8b 203
Lightvalve 248:bfdf0f479a38 204 TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
Lightvalve 245:e9c5ec04e378 205 TIM1->ARR = TMR1_COUNT-1; // set auto reload, 20 khz
Lightvalve 248:bfdf0f479a38 206 TIM1->CNT = 0;
Lightvalve 248:bfdf0f479a38 207 TIM1->SR = 0;
Lightvalve 244:30896263bd8b 208 TIM1->CCER |= 0x05; // CC1E = 1, CC1P = 0, CC1NE = 1, CC1NP = 0
Lightvalve 245:e9c5ec04e378 209 TIM1->CCER |= 0x50; // CC2E = 1, CC2P = 0, CC2NE = 1, CC2NP = 0
Lightvalve 248:bfdf0f479a38 210
Lightvalve 244:30896263bd8b 211 TIM1->BDTR |= 0x8000; // MOE = 1;
Lightvalve 244:30896263bd8b 212 TIM1->BDTR |= 0x7F; // Dead-time 7Ftick
Lightvalve 248:bfdf0f479a38 213
Lightvalve 245:e9c5ec04e378 214 TIM1->CR1 |= TIM_CR1_CEN; // enable TIM1
Lightvalve 248:bfdf0f479a38 215 }
Lightvalve 248:bfdf0f479a38 216
Lightvalve 248:bfdf0f479a38 217 void Init_TIM4()
Lightvalve 248:bfdf0f479a38 218 {
Lightvalve 248:bfdf0f479a38 219 RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // enable TIM4 clock
Lightvalve 248:bfdf0f479a38 220
Lightvalve 248:bfdf0f479a38 221 NVIC_EnableIRQ(TIM4_IRQn); //Enable TIM4 IRQ
Lightvalve 248:bfdf0f479a38 222
Lightvalve 248:bfdf0f479a38 223 TIM4->DIER |= TIM_DIER_UIE; // enable update interrupt
Lightvalve 248:bfdf0f479a38 224 TIM4->CR1 = 0x10;
Lightvalve 248:bfdf0f479a38 225 TIM4->CR1 |= TIM_CR1_UDIS;
Lightvalve 248:bfdf0f479a38 226 TIM4->EGR |= TIM_EGR_UG;
Lightvalve 248:bfdf0f479a38 227
Lightvalve 248:bfdf0f479a38 228 TIM4->PSC = 10-1; // 10 prescaler, timer counts up in sync with the peripheral clock
Lightvalve 248:bfdf0f479a38 229 TIM4->ARR = 1;
Lightvalve 248:bfdf0f479a38 230 TIM4->CNT = 0;
Lightvalve 248:bfdf0f479a38 231 TIM4->SR = 0;
Lightvalve 248:bfdf0f479a38 232 TIM4->SMCR = 0x07; //External clock mode
Lightvalve 248:bfdf0f479a38 233 TIM4->CR1 |= TIM_CR1_CEN; // enable TIM4
Lightvalve 244:30896263bd8b 234 }