专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
stm32 spwm控制步进电机的转速应该调整哪些参数呢
2019-03-23 18:00
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
4275
2
1260
stm32 spwm控制步进电机的转速应该调整哪些参数呢
此帖出自
小平头技术问答
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
mao20602130
1楼-- · 2019-03-23 22:01
/ 我的pwm配置程序如下
u16 time_cnt = 3600;
/*
* º¯ÊýÃû£ºTIM2_GPIO_Config
* ÃèÊö £ºÅäÖÃTIM2¸´ÓÃÊä³öPWMʱÓõ½µÄI/O
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
//PCLK1¾­¹ý2±¶Æµºó×÷ΪTIM3µÄʱÖÓÔ´µÈÓÚ72MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
// NVIC_InitTypeDef NVIC_InitStructure;
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏÈÕ¼ÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure); //³õʼ»¯NVIC¼Ä´æÆ÷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
/*GPIOA Configuration: TIM2 channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*
* º¯ÊýÃû£ºTIM2_Mode_Config
* ÃèÊö £ºÅäÖÃTIM2Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMÐźŵçƽÌø±äÖµ */
u16 CCR_Val = 899;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =time_cnt-1; //Êý72´ÎΪ1us£¬ÖÜÆÚΪ25us£¬¾ÍÊÇ40£Ë£È£ú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖÃÔ¤·ÖƵ£º²»·ÖƵ£¬¼´Îª72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1
TIM_OCInitStructure.TIM_Pulse = CCR_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪµÍµçƽ
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;// ¿ÕÏÐ״̬ÏµĹ¤×÷״̬
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ2
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ1
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ3
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ4
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷2
}
void TIM2_IRQHandler(void) //TIM2ÖжÏ
{
static s16 Ta,Tb,Tc,Td;
float angle = 0;
// Ta = 0;
// Tb = 0;
// Tc = 0;
// Td = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éTIM3¸üÐÂÖжϷ¢ÉúÓë·ñ
{
// TIM_SetCompare1(TIM2,SinDataA[Counter_sine]/pa);
// TIM_SetCompare2(TIM2,SinDataB[Counter_sine]/pa);
// TIM_SetCompare3(TIM2,SinDataC[Counter_sine]/pa);
// TIM_SetCompare4(TIM2,SinDataD[Counter_sine]/pa);
// Counter_sine-=1;
// Counter_sine = (Counter_sine+1024) % 1024;
stepCounter++;
if((stepCounter%(8 * 2300))==0)
{
stepCounter++;
LED_FLASH(1);
}
TIM_SetCompare1(TIM2,Ta);
TIM_SetCompare2(TIM2,Tb);
TIM_SetCompare3(TIM2,Tc);
TIM_SetCompare4(TIM2,Td);
angle = 2.0 * PI * Counter_sine/ 1024;
t1 = time_cnt * sin(angle)+0.5;
t2 = time_cnt * sin(angle + PI * 0.5)+0.5;
if(t1>=0)
{
Ta = t1;
Tb = 0;
}
else
{
Tb = abs(t1);
Ta = 0;
}
if(t2>=0)
{
Tc = t2;
Td = 0;
}
else
{
Td = abs(t2);
Tc = 0;
}
Counter_sine+=step;
Counter_sine = Counter_sine % 1024;
TIM2->SR = (uint16_t)~TIM_IT_Update; //Çå³ýTIM2¸üÐÂÖжϱêÖ¾
}
/*ΪÁ˲úÉúÒ»¸öÂö³å,¼ìÑéÊÇ·ñ½øÈëÖжÏ*/
}
加载中...
mao20602130
2楼-- · 2019-03-24 00:37
精彩回答 2 元偷偷看……
加载中...
一周热门
更多
>
相关问题
相关文章
嵌入式编译生成的HEX文件和BIN文件内容详解
0个评论
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
单片机中把部分Flash虚拟成Eeprom使用时,如何延长使用寿命
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
敢问路在何方,STM32迈出的第一步,却注定了它非凡的一生
0个评论
年工作时间真的就等于3年工作经验?也许你就不配
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
u16 time_cnt = 3600;
/*
* º¯ÊýÃû£ºTIM2_GPIO_Config
* ÃèÊö £ºÅäÖÃTIM2¸´ÓÃÊä³öPWMʱÓõ½µÄI/O
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
//PCLK1¾­¹ý2±¶Æµºó×÷ΪTIM3µÄʱÖÓÔ´µÈÓÚ72MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
// NVIC_InitTypeDef NVIC_InitStructure;
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏÈÕ¼ÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure); //³õʼ»¯NVIC¼Ä´æÆ÷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
/*GPIOA Configuration: TIM2 channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*
* º¯ÊýÃû£ºTIM2_Mode_Config
* ÃèÊö £ºÅäÖÃTIM2Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMÐźŵçƽÌø±äÖµ */
u16 CCR_Val = 899;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =time_cnt-1; //Êý72´ÎΪ1us£¬ÖÜÆÚΪ25us£¬¾ÍÊÇ40£Ë£È£ú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖÃÔ¤·ÖƵ£º²»·ÖƵ£¬¼´Îª72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1
TIM_OCInitStructure.TIM_Pulse = CCR_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪµÍµçƽ
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;// ¿ÕÏÐ״̬ÏµĹ¤×÷״̬
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ2
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ1
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ3
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ4
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷2
}
void TIM2_IRQHandler(void) //TIM2ÖжÏ
{
static s16 Ta,Tb,Tc,Td;
float angle = 0;
// Ta = 0;
// Tb = 0;
// Tc = 0;
// Td = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éTIM3¸üÐÂÖжϷ¢ÉúÓë·ñ
{
// TIM_SetCompare1(TIM2,SinDataA[Counter_sine]/pa);
// TIM_SetCompare2(TIM2,SinDataB[Counter_sine]/pa);
// TIM_SetCompare3(TIM2,SinDataC[Counter_sine]/pa);
// TIM_SetCompare4(TIM2,SinDataD[Counter_sine]/pa);
// Counter_sine-=1;
// Counter_sine = (Counter_sine+1024) % 1024;
stepCounter++;
if((stepCounter%(8 * 2300))==0)
{
stepCounter++;
LED_FLASH(1);
}
TIM_SetCompare1(TIM2,Ta);
TIM_SetCompare2(TIM2,Tb);
TIM_SetCompare3(TIM2,Tc);
TIM_SetCompare4(TIM2,Td);
angle = 2.0 * PI * Counter_sine/ 1024;
t1 = time_cnt * sin(angle)+0.5;
t2 = time_cnt * sin(angle + PI * 0.5)+0.5;
if(t1>=0)
{
Ta = t1;
Tb = 0;
}
else
{
Tb = abs(t1);
Ta = 0;
}
if(t2>=0)
{
Tc = t2;
Td = 0;
}
else
{
Td = abs(t2);
Tc = 0;
}
Counter_sine+=step;
Counter_sine = Counter_sine % 1024;
TIM2->SR = (uint16_t)~TIM_IT_Update; //Çå³ýTIM2¸üÐÂÖжϱêÖ¾
}
/*ΪÁ˲úÉúÒ»¸öÂö³å,¼ìÑéÊÇ·ñ½øÈëÖжÏ*/
}
一周热门 更多>