专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
最详细的SPWM产生方法
2019-07-20 22:38
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
1499
16
1222
淘来的绝对详细的SPWM产生方法,分享给大家。哪位大哥发出了波形上传看看,小弟对于这个文档的一些细节配置不太清楚。
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
正点原子
1楼-- · 2019-07-21 00:33
谢谢分享,不过建议发分享贴。
加载中...
tao96
2楼-- · 2019-07-21 05:44
Mark。。
加载中...
幻影觉123
3楼-- · 2019-07-21 08:45
原子哥,我按照文档里的内容在高级定时器内设置通道1,通道2。利用通道2每次向下计数溢出时将正弦表数据赋给通道1比较寄存器,可是波形出不来啊,感觉向下溢出这方面配置有点错误。
#include "pwm.h"
#include "stm32f10x_tim.h"
extern uint16_t NUM;
extern uint16_t SPWM[];
extern uint16_t i;
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//GPIOê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüGPIOíaéèê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μÄ
WMÂö3å2¨DÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //TIM1-CH1/CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μÄ
WMÂö3å2¨DÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //TIM1-CH1/CH2μÄêä3ö»¥21í¨μà¡£
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel =TIM1_CC_IRQn; //TIM1ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_DeInit(TIM1);
//TIMx¶¨ê±Æ÷3õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //èçéèÖÃarr=999£¬μ±¶¨ê±Æ÷′ó0¼Æêyμ½999£¬ò»121000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú¡£
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃÔ¤·ÖÆμÖμ£¬Ö¸μÄêÇ′óAPB2×üÏßÆμÂêμ½Ïàó|μĶ¨ê±Æ÷TIMxμÄ·ÖÆμÏμêy¡£
TIM_TimeBaseStructure.TIM_ClockDivision =0; //éèÖÃê±Öó·ÖÆμÏμêy¡£Ö¸μÄêÇTIMx¶¨ê±Æ÷ê±Öóμ½Ïàó|μÄTIMx_CNT¶¨ê±Æ÷¼ÆêyÆ÷μÄ·ÖÆμÏμêy¡£
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//¸üDÂÕ¼¿Õ±è
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_CenterAligned1; //TIMÖDÑë¶ÔÆëÄ£ê½1
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ClearFlag(TIM1,TIM_FLAG_CC2);
TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);
/*--------------------------------------------------------------------------------------------------------------------------------*/
//TIM1--CH1êä3öí¨μàPWMÄ£ê½ÅäÖã¨í¨óÃêä3öí¨μàPWMÄ£ê½ÅäÖã©
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse =0; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖ죬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷ÖμD¡óúéÏÃæËùéèÖÃμÄTIM_PulseÖμê±£¬
WMêä3ö¸ßμçƽ(衾öóúTIMμļÆêyÄ£ê½)
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
//ê1ÄüTIMxêä3öí¨μà1 PWMģ꽣¨±ØDëê1ÄüÏàó|μÄÖØ×°ÔؼÄ′æÆ÷TIMx_CCRx£©
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1ԤװÔØê1Äü
//TIM1->DIER=0X01<<1;
//TIM1--CH2êä3öí¨μàPWMÄ£ê½ÅäÖã¨í¨óÃêä3öí¨μàPWMÄ£ê½ÅäÖã©
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse =0; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖ죬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷ÖμD¡óúéÏÃæËùéèÖÃμÄTIM_PulseÖμê±£¬
WMêä3ö¸ßμçƽ(衾öóúTIMμļÆêyÄ£ê½)
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
//ê1ÄüTIMxêä3öí¨μà1 PWMģ꽣¨±ØDëê1ÄüÏàó|μÄÖØ×°ÔؼÄ′æÆ÷TIMx_CCRx£©
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2ԤװÔØê1Äü
//TIM1->DIER=0X01<<2;
/*---------------------------------------------------------------------------------------------------------------------------------*/
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
}
void TIM1_CC_IRQHandler(void) //TIM3ÖD¶Ï
{
if (TIM_GetITStatus(TIM1,TIM_IT_CC2) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
{
TIM1->CCR1=SPWM[i++];
if(i>=NUM)
{
i=0;
}
}
TIM_ClearITPendingBit(TIM1,TIM_IT_CC2); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
}
加载中...
幻影觉123
4楼-- · 2019-07-21 10:04
精彩回答 2 元偷偷看……
加载中...
张册
5楼-- · 2019-07-21 15:00
我最近也在写spwm有机会可以交流学习我微信ZC138285
加载中...
陈柒
6楼-- · 2019-07-21 16:47
谢谢楼主分享
加载中...
1
2
3
下一页
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
STM32开发板免费用活动
7 个回答
stm32 处理 DHT11占用太多时间,大家程序是怎么设计的
8 个回答
分享一个STM32单片机做的离线编程器代码
9 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
#include "pwm.h"
#include "stm32f10x_tim.h"
extern uint16_t NUM;
extern uint16_t SPWM[];
extern uint16_t i;
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//GPIOê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüGPIOíaéèê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μÄWMÂö3å2¨DÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //TIM1-CH1/CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μÄWMÂö3å2¨DÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //TIM1-CH1/CH2μÄêä3ö»¥21í¨μà¡£
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel =TIM1_CC_IRQn; //TIM1ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_DeInit(TIM1);
//TIMx¶¨ê±Æ÷3õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //èçéèÖÃarr=999£¬μ±¶¨ê±Æ÷′ó0¼Æêyμ½999£¬ò»121000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú¡£
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃÔ¤·ÖÆμÖμ£¬Ö¸μÄêÇ′óAPB2×üÏßÆμÂêμ½Ïàó|μĶ¨ê±Æ÷TIMxμÄ·ÖÆμÏμêy¡£
TIM_TimeBaseStructure.TIM_ClockDivision =0; //éèÖÃê±Öó·ÖÆμÏμêy¡£Ö¸μÄêÇTIMx¶¨ê±Æ÷ê±Öóμ½Ïàó|μÄTIMx_CNT¶¨ê±Æ÷¼ÆêyÆ÷μÄ·ÖÆμÏμêy¡£
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//¸üDÂÕ¼¿Õ±è
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_CenterAligned1; //TIMÖDÑë¶ÔÆëÄ£ê½1
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ClearFlag(TIM1,TIM_FLAG_CC2);
TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);
/*--------------------------------------------------------------------------------------------------------------------------------*/
//TIM1--CH1êä3öí¨μàPWMÄ£ê½ÅäÖã¨í¨óÃêä3öí¨μàPWMÄ£ê½ÅäÖã©
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse =0; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖ죬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷ÖμD¡óúéÏÃæËùéèÖÃμÄTIM_PulseÖμê±£¬WMêä3ö¸ßμçƽ(衾öóúTIMμļÆêyÄ£ê½)
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
//ê1ÄüTIMxêä3öí¨μà1 PWMģ꽣¨±ØDëê1ÄüÏàó|μÄÖØ×°ÔؼÄ′æÆ÷TIMx_CCRx£©
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1ԤװÔØê1Äü
//TIM1->DIER=0X01<<1;
//TIM1--CH2êä3öí¨μàPWMÄ£ê½ÅäÖã¨í¨óÃêä3öí¨μàPWMÄ£ê½ÅäÖã©
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse =0; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖ죬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷ÖμD¡óúéÏÃæËùéèÖÃμÄTIM_PulseÖμê±£¬WMêä3ö¸ßμçƽ(衾öóúTIMμļÆêyÄ£ê½)
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
//ê1ÄüTIMxêä3öí¨μà1 PWMģ꽣¨±ØDëê1ÄüÏàó|μÄÖØ×°ÔؼÄ′æÆ÷TIMx_CCRx£©
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2ԤװÔØê1Äü
//TIM1->DIER=0X01<<2;
/*---------------------------------------------------------------------------------------------------------------------------------*/
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
}
void TIM1_CC_IRQHandler(void) //TIM3ÖD¶Ï
{
if (TIM_GetITStatus(TIM1,TIM_IT_CC2) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
{
TIM1->CCR1=SPWM[i++];
if(i>=NUM)
{
i=0;
}
}
TIM_ClearITPendingBit(TIM1,TIM_IT_CC2); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
}
一周热门 更多>