5 changed files with 144 additions and 2 deletions
-
2chip/chip.hpp
-
56chip/zpwm_generator.cpp
-
41chip/zpwm_generator.hpp
-
36chip/ztim.cpp
-
11chip/ztim.hpp
@ -0,0 +1,56 @@ |
|||||
|
#include "zpwm_generator.hpp"
|
||||
|
|
||||
|
#include "zirq_dispatcher.hpp"
|
||||
|
using namespace iflytop; |
||||
|
|
||||
|
void ZPWMGenerator::initialize(zchip_tim_t *htim, uint32_t channel, float freq, bool polarity) { |
||||
|
m_htim = htim; |
||||
|
m_polarity = polarity; |
||||
|
m_channel = channel; |
||||
|
m_freq = freq; |
||||
|
|
||||
|
ZASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE); |
||||
|
ZASSERT(m_htim->Init.CounterMode == TIM_COUNTERMODE_UP); |
||||
|
} |
||||
|
|
||||
|
/******************************************************
|
||||
|
* kTimMode_timer * |
||||
|
******************************************************/ |
||||
|
void ZPWMGenerator::startPWM(float duty) { startPWM(m_freq, duty); } |
||||
|
|
||||
|
void ZPWMGenerator::startPWM(float freq, float duty) { |
||||
|
if (!m_polarity) { |
||||
|
duty = 100.0 - duty; |
||||
|
} |
||||
|
|
||||
|
m_freq = freq; |
||||
|
|
||||
|
uint32_t prescaler = 0; |
||||
|
uint32_t autoreload = 0; |
||||
|
|
||||
|
uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim); |
||||
|
ZASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, freq, &prescaler, &autoreload)); |
||||
|
__HAL_TIM_SET_AUTORELOAD(m_htim, autoreload); |
||||
|
__HAL_TIM_SET_PRESCALER(m_htim, prescaler); |
||||
|
|
||||
|
/**
|
||||
|
* @brief |
||||
|
*/ |
||||
|
TIM_OC_InitTypeDef sConfigOC = {0}; |
||||
|
sConfigOC.OCMode = TIM_OCMODE_PWM1; |
||||
|
sConfigOC.Pulse = duty / 100.0 * __HAL_TIM_GET_AUTORELOAD(m_htim); |
||||
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||||
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||||
|
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
||||
|
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
||||
|
|
||||
|
if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, m_channel) != HAL_OK) { |
||||
|
Error_Handler(); |
||||
|
} |
||||
|
// HAL_TIM_PWM_Start(m_htim, Channel);
|
||||
|
HAL_TIM_PWM_Start(m_htim, m_channel); |
||||
|
} |
||||
|
void ZPWMGenerator::stopPWM() { |
||||
|
// HAL_TIM_PWM_Stop(m_htim, m_channel);
|
||||
|
startPWM(0); |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
|
||||
|
#pragma once
|
||||
|
#include <functional>
|
||||
|
|
||||
|
#include "basic/base.hpp"
|
||||
|
|
||||
|
#ifdef HAL_TIM_MODULE_ENABLED
|
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace iflytop; |
||||
|
class ZPWMGenerator { |
||||
|
public: |
||||
|
typedef function<void()> ontimirq_t; |
||||
|
|
||||
|
private: |
||||
|
zchip_tim_t *m_htim; |
||||
|
bool m_polarity; |
||||
|
uint32_t m_channel; |
||||
|
float m_freq; |
||||
|
|
||||
|
public: |
||||
|
ZPWMGenerator() {} |
||||
|
|
||||
|
/**
|
||||
|
* @brief |
||||
|
* |
||||
|
* @param Channel PWM通道 TIM_CHANNEL_1 TIM_CHANNEL_2 TIM_CHANNEL_3 TIM_CHANNEL_4 |
||||
|
* @param Polarity 1:高为有效电平,0:低为有效电平 |
||||
|
*/ |
||||
|
void initialize(zchip_tim_t *htim, uint32_t channel, float freq, bool polarity); |
||||
|
/*******************************************************************************
|
||||
|
* PWM_MODE * |
||||
|
*******************************************************************************/ |
||||
|
void startPWM(float freq, float duty); |
||||
|
void startPWM(float duty); |
||||
|
void stopPWM(); |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
||||
|
|
||||
|
#endif
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue