- /*
- @hehung
- 2023-6-17
- email: 1398660197@qq.com
- wechat: hehung95
- reproduced and please indicate the source @hehung
- */
- #include <stdbool.h>
- #include "ac780x_gpio.h"
- #include "ac780x_pwm.h"
- #include <string.h>
- #include "app_pwm.h"
- void Pwm_Init(void)
- {
- PWM_ConfigType config;
- PWM_ModulationConfigType pwmConfig;
- PWM_IndependentChConfig independentChConfig[2];
- memset(&config, 0U, sizeof(config));
- memset(&pwmConfig, 0U, sizeof(pwmConfig));
- memset(&independentChConfig, 0U, sizeof(independentChConfig));
- GPIO_SetFunc(GPIOA, GPIO_PIN2, GPIO_FUN1); /* PA2--PWM2_CH1.*/
- GPIO_SetFunc(GPIOA, GPIO_PIN3, GPIO_FUN1); /* PA3--PWM2_CH0.*/
- /*
- 边沿对齐PWM模式:
- 周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
- 脉宽=(CHnV-CNTIN+1)*PWM计数器时钟周期
- 中心对齐PWM模式:
- 周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
- 脉宽=2*(CHnV-CNTIN)*PWM计数器时钟周期
- */
- independentChConfig[0].channel = PWM_CH_0; /*独立输出通道选择 */
- independentChConfig[0].chValue = 0; /*通道channel值,决定占空比 */
- independentChConfig[0].levelMode = PWM_HIGH_TRUE; /*输出PWM高有效,可配置高有效或低有效 */
- independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; /*通道输出极性配置为高有效,PWM Mask后输出为低电平 */
- independentChConfig[0].interruptEn = ENABLE; /*通道匹配中断使能位 */
- independentChConfig[0].initLevel = PWM_LOW_LEVEL; /*通道初始电平输出配置,该配置受initChOutputEn控制,决定PMM计数器未工作前PWM口的输出电平 */
- independentChConfig[0].triggerEn = DISABLE; /*通道匹配触发使能位,在通道值匹配时可产生触发信号用于其他模块的触发信号 */
- independentChConfig[1].channel = PWM_CH_1; /*独立输出通道选择 */
- independentChConfig[1].chValue = 0; /*通道channel值,决定占空比 */
- independentChConfig[1].levelMode = PWM_HIGH_TRUE; /*输出PWM高有效,可配置高有效或低有效 */
- independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; /*通道输出极性配置为高有效,PWM Mask后输出为低电平 */
- independentChConfig[1].interruptEn = ENABLE; /*通道匹配中断使能位 */
- independentChConfig[1].initLevel = PWM_LOW_LEVEL; /*通道初始电平输出配置,该配置受initChOutputEn控制,决定PMM计数器未工作前PWM口的输出电平 */
- independentChConfig[1].triggerEn = DISABLE; /*通道匹配触发使能位,在通道值匹配时可产生触发信号用于其他模块的触发信号 */
- pwmConfig.countMode = PWM_UP_DOWN_COUNT; /*PWM计数器模式,PWM_UP_COUNT--向上计数模式;PWM_UP_DOWN_COUNT-- 向上/向下计数模式 */
- pwmConfig.independentChannelNum = 2; /*独立通道数目*/
- pwmConfig.combineChannelNum = 0; /*组合通道对数 */
- pwmConfig.independentChConfig = independentChConfig; /*独立通道配置赋值 */
- pwmConfig.combineChConfig = NULL; /*组合通道配置赋值 */
- pwmConfig.deadtime = 0; /*死区时间值,死区功能仅在组合模式下有效 */
- pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1; /*死区时间时钟分频系数,死区功能仅在组合模式下有效 */
- pwmConfig.initChOutputEn = ENABLE; /*初始化通道输出使能位,使能后独立PWM模式的 initLevel 和组合模式的ch1stInitLevel和ch2ndInitLevel配置才会生效*/
- pwmConfig.initTriggerEn = DISABLE; /*PWM初始值触发使能位 */
- /*
- 向上-向下技术模式下,频率计算为:
- freq = pwm_clk/(2*(maxValue - initValue))
- 16000/(2*8000) = 1K
- */
- config.mode = PWM_MODE_MODULATION; /*PWM模式配置,PWM_MODE_MODULATION--PWM调制模式 */
- config.initModeStruct = &pwmConfig; /*不同模式相应初始化配置结构体赋值 */
- config.clkSource = PWM_CLK_SOURCE_APB; /*PWM时钟源选择 */
- config.clkPsc = 0; /*时钟分频系数 */
- config.initValue = 0; /*初始计数值 */
- config.maxValue = APP_PWM_MAX_DUTY_COUNT; /*最大计数值,决定输出波形频率 */
- config.overflowInterrupEn = DISABLE; /*溢出中断使能位 */
- config.cntOverflowFreq = 0; /*溢出中断产生的频率与计数器频率的关系,0表示每次计数器溢出都产生中断,1表示间隔一次,以此类推 */
- config.interruptEn = DISABLE; /*PWM总中断使能位 */
- config.callBack = NULL; /*中断回调函数 */
- PWM_Init(PWM2, &config);
- }
app_pwm.h中实现了如下功能,定义了最大占空比宏:10000。
- /*
- @hehung
- 2023-6-17
- email: 1398660197@qq.com
- wechat: hehung95
- reproduced and please indicate the source @hehung
- */
- #ifndef APP_PWM_H__
- #define APP_PWM_H__
- #include "ac780x_pwm.h"
- #include "ac780x_pwm_reg.h"
- #define APP_PWM_MAX_DUTY_COUNT (10000U)
- extern void Pwm_Init(void);
- #endif /* APP_PWM_H__ */
2.2 主函数实现
主函数中实现了对LED1和LED2的驱动,主要是占空比的控制,LED1和LED2的占空比值正好相反,即LED1最亮的时候,LED2最暗,反之亦然。
设置了最大占空比为APP_PWM_MAX_DUTY_COUNT/2是因为一半占空比和满占空比的时候亮度变化不大,不便于观察,所以直接设置半占空比为最亮。
- Pwm_Init();
- while(1)
- {
- PWM_SetChannelCountValue(PWM2, 0, pwm_duty);
- PWM_SetChannelCountValue(PWM2, 1, APP_PWM_MAX_DUTY_COUNT/2 - pwm_duty);
- if ((pwm_duty >= APP_PWM_MAX_DUTY_COUNT/2) || (pwm_duty == 0))
- pwm_flg ^= 1;
- if (pwm_flg == 1)
- pwm_duty += 50;
- else
- pwm_duty -= 50;
- mdelay(10);
- }
3 试验效果
见下面的动图,可见试验成功。