inverter2/my_src/inverter.c

144 lines
4.4 KiB
C
Raw Normal View History

2016-10-25 17:14:58 +02:00
/*
* inverter.c
*
* Created on: 25.10.2016
* Author: dehottgw
*/
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "stm32f1xx_hal.h"
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim5;
2016-10-25 17:14:58 +02:00
#define NUM_OF_SINE_SLOT 30
uint16_t freqOut = 100;
const uint32_t FREQ_IN = 1E6;
2016-10-25 17:14:58 +02:00
const float PI = 3.14159;
float slotAngle = 180.0 / NUM_OF_SINE_SLOT;
float sineValues[NUM_OF_SINE_SLOT];
uint16_t IV[NUM_OF_SINE_SLOT];
volatile uint32_t timer1Cnt;
2016-10-25 17:14:58 +02:00
typedef struct {
uint8_t slotCnt;
bool running;
GPIO_TypeDef *bridgePolarityPort;
uint16_t bridgePolarityPin;
IRQn_Type irqType;
2016-10-25 17:14:58 +02:00
TIM_HandleTypeDef *handle;
uint32_t channel;
2016-10-25 17:14:58 +02:00
} timerSupport_t;
#define NUM_OF_TIMER 3
volatile timerSupport_t timerSupport[NUM_OF_TIMER];
2016-10-25 17:14:58 +02:00
void inverterBegin() {
for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) {
float angle = i * slotAngle;
sineValues[i] = sinf(angle / 180 * PI);
}
timerSupport[0].handle = &htim2;
timerSupport[0].running = false;
timerSupport[0].slotCnt = 0;
timerSupport[0].bridgePolarityPort = BridgePolarity0_GPIO_Port;
timerSupport[0].bridgePolarityPin = BridgePolarity0_Pin;
timerSupport[0].irqType = TIM2_IRQn;
timerSupport[0].channel = TIM_CHANNEL_1;
timerSupport[1].handle = &htim5;
2016-10-25 17:14:58 +02:00
timerSupport[1].running = false;
timerSupport[1].slotCnt = 0;
timerSupport[1].bridgePolarityPort = BridgePolarity1_GPIO_Port;
timerSupport[1].bridgePolarityPin = BridgePolarity1_Pin;
timerSupport[1].irqType = TIM5_IRQn;
timerSupport[1].channel = TIM_CHANNEL_2;
2016-10-25 17:14:58 +02:00
timerSupport[2].handle = &htim4;
timerSupport[2].running = false;
timerSupport[2].slotCnt = 0;
timerSupport[2].bridgePolarityPort = BridgePolarity2_GPIO_Port;
timerSupport[2].bridgePolarityPin = BridgePolarity2_Pin;
timerSupport[2].irqType = TIM4_IRQn;
timerSupport[2].channel = TIM_CHANNEL_1;
2016-10-25 17:14:58 +02:00
}
void inverterSetFrequency(uint8_t freqOut) {
uint16_t slotWidth = (FREQ_IN / (freqOut * NUM_OF_SINE_SLOT * 4));
for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) {
IV[i] = (uint16_t)(sineValues[i] * 0.9 * slotWidth);
}
HAL_TIM_Base_Stop(&htim1);
__HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE);
2016-10-25 17:14:58 +02:00
for (uint8_t i = 0; i < NUM_OF_TIMER; i++){
HAL_TIM_PWM_Stop_DMA(timerSupport[i].handle, timerSupport[i].channel);
__HAL_TIM_DISABLE_IT(timerSupport[i].handle, TIM_IT_UPDATE);
2016-10-25 17:14:58 +02:00
timerSupport[i].slotCnt = 0;
timerSupport[i].running = false;
HAL_GPIO_WritePin(timerSupport[i].bridgePolarityPort, timerSupport[i].bridgePolarityPin, GPIO_PIN_RESET);
__HAL_TIM_SET_AUTORELOAD(timerSupport[i].handle, slotWidth);
}
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
timer1Cnt = 0;
__HAL_TIM_SET_AUTORELOAD(&htim1, slotWidth);
HAL_TIM_Base_Start_IT(&htim1);
__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
2016-10-25 17:14:58 +02:00
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
for (uint8_t i = 0; i < NUM_OF_TIMER; i++) {
if (htim == timerSupport[i].handle) {
timerSupport[i].slotCnt++;
if (timerSupport[i].slotCnt == NUM_OF_SINE_SLOT + 2) {
timerSupport[i].slotCnt = 2;
HAL_GPIO_TogglePin(timerSupport[i].bridgePolarityPort, timerSupport[i].bridgePolarityPin);
}
}
}
if (htim == &htim1) {
HAL_GPIO_TogglePin(Sync_GPIO_Port, Sync_Pin);
if (timer1Cnt == 0) {
2016-10-25 17:14:58 +02:00
if (! timerSupport[0].running) {
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
2016-10-25 17:14:58 +02:00
timerSupport[0].running = true;
__HAL_TIM_ENABLE_IT(timerSupport[0].handle, TIM_IT_UPDATE);
HAL_TIM_PWM_Start_DMA(timerSupport[0].handle, timerSupport[0].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT);
2016-10-25 17:14:58 +02:00
}
} else if (timer1Cnt == ((NUM_OF_SINE_SLOT * 2) / 3)) {
2016-10-25 17:14:58 +02:00
if (! timerSupport[1].running) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
2016-10-25 17:14:58 +02:00
timerSupport[1].running = true;
HAL_TIM_PWM_Start_DMA(timerSupport[1].handle, timerSupport[1].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT);
__HAL_TIM_ENABLE_IT(timerSupport[1].handle, TIM_IT_UPDATE);
2016-10-25 17:14:58 +02:00
}
} else if (timer1Cnt == ((NUM_OF_SINE_SLOT * 2) * 2 / 3)) {
2016-10-25 17:14:58 +02:00
if (! timerSupport[2].running) {
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
2016-10-25 17:14:58 +02:00
timerSupport[2].running = true;
HAL_TIM_PWM_Start_DMA(timerSupport[2].handle, timerSupport[2].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT);
__HAL_TIM_ENABLE_IT(timerSupport[2].handle, TIM_IT_UPDATE);
2016-10-25 17:14:58 +02:00
}
}
timer1Cnt++;
2016-10-25 17:14:58 +02:00
}
}