/* * PWM.c * * Created on: 14 janv. 2019 * Author: e_rcluze */ #include "PWM.h" #include "Tab.h" #include "tim.h" #include "gpio.h" extern Sortie tabSortie[10]; extern uint8_t gl_Stop; extern uint8_t gl_Essai; void PWMSetDutyCycle(uint8_t sortie, uint8_t pourcentage) { TIM_OC_InitTypeDef sConfigOC; sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.Pulse = PourcentageToValue(pourcentage); //HAL_Delay(1); switch (sortie) { case 0: HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3); HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); break; case 1: HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4); HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); break; case 2: HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); break; case 3: HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); break; case 4: HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); break; case 5: HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); break; case 6: HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); break; case 7: HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2); HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2); break; case 8: HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); break; case 9: HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4); HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4); break; } } uint16_t PourcentageToValue(uint8_t val) { float t = ((100-val) * PWM_MAX) / 100; //Logic inverse (si 0 alors allumé si 1 alors eteint) return (uint16_t) t; } void GerePWM(uint16_t heure) { for(int i = 0; i<10;i++){ if ((heure >= tabSortie[i].Demmarage) & (heure < tabSortie[i].Extinxtion)) { if (heure < tabSortie[i].Demmarage + tabSortie[i].Pente) PWMSetDutyCycle(i,PWMPourcentageAll(i,heure)); else if(heure > tabSortie[i].Extinxtion - tabSortie[i].Pente) PWMSetDutyCycle(i,PWMPourcentageExt(i,heure)); else PWMSetDutyCycle(i,tabSortie[i].PuissanceMax); } else PWMSetDutyCycle(i,0); } } void Stop(){ for(int i = 0; i<10;i++){ PWMSetDutyCycle(i,0); } } void Essai(){ for(int i = 0; i<10;i++){ PWMSetDutyCycle(i,tabSortie[i].PuissanceEssai); } } uint8_t PWMPourcentageAll(uint8_t sortie, uint16_t heure) { return (float)tabSortie[sortie].PuissanceMax / ((float)tabSortie[sortie].Pente / (float)(heure - tabSortie[sortie].Demmarage)); } uint8_t PWMPourcentageExt(uint8_t sortie, uint16_t heure) { return tabSortie[sortie].PuissanceMax / (tabSortie[sortie].Pente / (tabSortie[sortie].Extinxtion - heure)); }