打印
[活动专区]

【AutoChips 7801x MCU评测报告】+PWM

[复制链接]
853|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
今天搞了一下PWM。
PB5,PB4,PC9,PB3
原理图:




代码:
#include "ac780x.h"
#include "ac780x_debugout.h"
#include "pwm_sample.h"

/**********<Macro>**********/

/**********<Union>**********/

/**********<Struct>*********/

/*********<Variable>********/

/********<Prototype>********/

/**
*
* @param[in] none
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*
* [url=home.php?mod=space&uid=247401]@brief[/url]
*/
int main(void)
{
    InitDebug();
    InitDelay();
    printf("\nRunning the PWM module sample code.\r\n");
    PWM_GpioInit();
    PWM1_GenerateFrequency();
    while (1)
    {

   
    }
}

/**********<End>*********/
void PWM_GpioInit(void)
{

    //配置GPIO为PWM
    //config PWM1 pinmux
    GPIO_SetFunc(GPIOB, GPIO_PIN5, GPIO_FUN1);//PWM1_CH0
    GPIO_SetFunc(GPIOB, GPIO_PIN4, GPIO_FUN1);//PWM1_CH1
    GPIO_SetFunc(GPIOC, GPIO_PIN9, GPIO_FUN1);//PWM1_CH6
    GPIO_SetFunc(GPIOB, GPIO_PIN3, GPIO_FUN2);//PWM1_CH7
}
/**
* PWM1_GenerateFrequency
*
* @param[in] none
* @return none
*
* @brief
* 1.PWM1_CH0,PWM1_CH1组合模式,PWM1_CH0输出频率为20K,占空比为25%的波形,PWM1_CH1与PWM1_CH0波形互补,带1us死区插入。
* 2.PWM1_CH6,PWM1_CH7独立PWM模式(边沿对齐),生成频率为20K,PWM1_CH6占空比50%,PWM1_CH7占空比25%的波形。
* 3.PWM计数器溢出中断,对输出PWM波进行计数
*
*/
void PWM1_GenerateFrequency(void)
{
    PWM_CombineChConfig combineChConfig[1]; //组合模式相关结构体
    PWM_IndependentChConfig independentChConfig[2];//独立模式相关结构体
    PWM_ModulationConfigType pwmConfig; //PWM模式相关结构体
    PWM_ConfigType config; //PWM模块结构体
    //结构体数据清零
    memset(&combineChConfig, 0, sizeof(combineChConfig));
    memset(&independentChConfig, 0, sizeof(independentChConfig));
    memset(&pwmConfig, 0, sizeof(pwmConfig));
    memset(&config, 0, sizeof(config));
   
    /*通道0/1配成组合模式PWM输出*/
    /*
    向上计数组合模式:
    周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
    占空比=|CH(n+1)V-CH(n)V|*PWM计数器时钟周期
    向上-向下计数组合模式:
    周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
    占空比=2*(|CH(n+1)V-CH(n)V|)*PWM计数器时钟周期
    */
    combineChConfig[0].pairChannel = PWM_CH_0; //PWM通道对数,PWM_CH_0/2/4/6对应PAIR0/1/2/3
    /*
    组合模式占空比由ch1stValue,ch2ndValue共同决定,pwmConfig.countMode配为PWM_UP_COUNT(计数器向上计数)时,占空比为:(ch1stValue-ch2ndValue)/(config.maxValue + 1)
    */
    combineChConfig[0].ch1stValue = MOD_PWM >> 2; //通道2n channel值,n为PWM对数编号
    combineChConfig[0].ch2ndValue = MOD_PWM >> 1; //通道2n+1 channel值,n为PWM对数编号
    combineChConfig[0].levelMode = PWM_HIGH_TRUE; //输出PWM高有效,如果占空比设为25%,是指的高有效电平占比25%
    combineChConfig[0].deadtimeEn = ENABLE;//死区插入使能,组合模式才支持死区插入
    combineChConfig[0].complementEn = ENABLE;//互补模式使能,使能后,PWM通道波形互补,DISABLE波形输出同向
    combineChConfig[0].ch1stMatchDir = PWM_MATCH_DIR_DOWN;//仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效,用于选择匹配生效点方向
    combineChConfig[0].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;//仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效,用于选择匹配生效点方向
    combineChConfig[0].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;//输出极性高有效,PWM mask后PWM输出低电平
    combineChConfig[0].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;//输出极性高有效,PWM mask后PWM输出低电平
    combineChConfig[0].ch1stInterruptEn = DISABLE;//PWM通道匹配中断使能位
    combineChConfig[0].ch2ndInterruptEn = DISABLE;//PWM通道匹配中断使能位
    combineChConfig[0].ch1stInitLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
    combineChConfig[0].ch2ndInitLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
    combineChConfig[0].ch1stTriggerEn = DISABLE;//通道2n外部触发使能,n为PWM对数编号
    combineChConfig[0].ch2ndTriggerEn = DISABLE;//通道2n+1外部触发使能,n为PWM对数编号
    /*
    边沿对齐PWM模式:
    周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
    占空比=(CHnV-CNTIN+1)*PWM计数器时钟周期
    中心对齐PWM模式:
    周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
    占空比=2*(CH(n)V-CNTIN)*PWM计数器时钟周期
    */
    /*channel 6*/
    independentChConfig[0].channel = PWM_CH_6; //通道6
    independentChConfig[0].chValue = MOD_PWM >> 1;//通道6 channel值,输出占空比 = chValue / (config.maxValue + 1) = 50%
    independentChConfig[0].levelMode = PWM_HIGH_TRUE; //输出PWM高有效
    independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; //输出极性高有效,PWM mask后PWM输出低电平
    independentChConfig[0].interruptEn = DISABLE;//PWM通道匹配中断使能位
    independentChConfig[0].initLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
    independentChConfig[0].triggerEn = DISABLE;//通道外部触发使能
    /*channel 7*/
    independentChConfig[1].channel = PWM_CH_7; //通道7
    independentChConfig[1].chValue = MOD_PWM >> 2;//通道7 channel值,输出占空比 = chValue / (config.maxValue + 1) = 25%
    independentChConfig[1].levelMode = PWM_HIGH_TRUE; //输出PWM高有效
    independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; //输出极性高有效,PWM mask后PWM输出低电平
    independentChConfig[1].interruptEn = DISABLE;//PWM通道匹配中断使能位
    independentChConfig[1].initLevel = PWM_LOW_LEVEL;//PWM初始电平输出为低,该配置受initChOutputEn控制,决定PWM计数器未工作前PWM口的输出电平值。
    independentChConfig[1].triggerEn = DISABLE;//通道外部触发使能

    /*modulation mode config*/
    pwmConfig.countMode = PWM_UP_COUNT; //PWM计数器模式 (不同的计数模式频率及占空比计算方式不同)
    pwmConfig.independentChannelNum = 2; //独立通道数
    pwmConfig.combineChannelNum = 1; //组合对数
    pwmConfig.independentChConfig = independentChConfig; //独立通道配置变量地址赋值
    pwmConfig.combineChConfig = combineChConfig; //组合通道配置变量地址赋值
    pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1;//死区插入分频值,与deadtime一起决定插入死区的时间。
    pwmConfig.deadtime = 24;  //死区时间 = (DTPSC * DTVAL)/PWM计数器时钟周期 = 1*24/24000000 = 1us
    pwmConfig.initChOutputEn = ENABLE; //使能初始化通道输出,使能后独立PWM模式的initLevel和组合PWM模式的ch1stInitLevel和ch2ndPolarity配置才会生效
    pwmConfig.initTriggerEn = DISABLE; //通道外部触发使能
   
    /*pwm config*/
    config.mode = PWM_MODE_MODULATION;//PWM模块配置为PWM模式
    config.initModeStruct = &pwmConfig;//PWM配置结构体地址赋值
    config.clkSource = PWM_CLK_SOURCE_APB; //PWM时钟源配置
    config.clkPsc = PWM_PRES;//PWM时钟源分频
    config.initValue = 0;//计数器初始寄存器值
    config.maxValue = MOD_PWM - 1; //PWM计数器最大值
    config.overflowInterrupEn = ENABLE;//计数器溢出中断使能
    config.cntOverflowFreq = 0;//CNTOF中断产生的频率与计数器频率的关系(0-127), 0表示每次计数器溢出都产生溢出中断,1表示间隔1次,2表示间隔2次,以此内推。
    config.interruptEn = ENABLE; //PWM中断使能
    config.callBack = PWM1_CallBack; //PWM中断回调
   
    PWM_Init(PWM1, &config); //配置初始化生效

    NVIC_SetPriority(PWM1_IRQn, 0); //设置PWM模块中断的优先级
}
/**********<End>*********/
效果图:
PB3:

PC9:






使用特权

评论回复

相关下载

相关帖子

沙发
marginer| | 2020-6-10 18:59 | 只看该作者
不上班天天搞机

使用特权

评论回复
板凳
Cjy_JDxy|  楼主 | 2020-6-10 19:15 | 只看该作者

我上班啊

使用特权

评论回复
地板
叶春勇| | 2020-6-10 20:57 | 只看该作者
不错楼主,我的板子也到了,先看看

使用特权

评论回复
5
marginer| | 2020-6-12 14:54 | 只看该作者

上班用这MCU上几个项目卖一下

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:绿水本无忧因风皱面,青山原不老为雪白头。

553

主题

3522

帖子

19

粉丝