LIGHT2

Dependencies:   FastPWM mbed

Committer:
Lightvalve
Date:
Fri Jun 17 06:06:53 2022 +0000
Revision:
245:e9c5ec04e378
Parent:
244:30896263bd8b
Child:
248:bfdf0f479a38
220617

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