专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
新人求助stm32在FreeRtos下的PWM读取
2019-03-23 19:41
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
10667
2
1249
各位前辈,我现在在用一个传感器,其中两个引脚分别输出两个PWM信号,我准备用STM32F4discover的板子在Freertos下面进行数据读取,我看了官方的例程,我用的笨办法就是用两个Timer分别捕获一路的PWM信号。但是我现在的问题是代码在网站上我找到了,也明白具体是怎么工作的了,但是现在就是不懂要是在Freertos下面怎么进行修改,比如怎么设置task。这里我想请有经验的前辈给我指点一下,看了几天实在是不知道怎么下手了,现在很急了,非常感谢各位。 此帖出自
小平头技术问答
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
haohongyu
1楼-- · 2019-03-23 21:41
/ /* TIM2 channel 2 pin (PA.01) configuration */
//PWM capture PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM3 channel 2 pin (PA.07) configuration */
//PWM capture PA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM2, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM3, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQ(void)
{
PlusCounter ++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value = TIM_GetCapture2(TIM2); /* Get the Input Capture value */
if (IC2Value != 0)
{
DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value; /* Duty cycle computation */
Frequency = 72000000 / IC2Value; /* Frequency computation */
}
else
{
DutyCycle = 0;
Frequency = 0;
}
}
void TIM3_IRQ(void)
{
PlusCounter2 ++;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value2 = TIM_GetCapture2(TIM3); /* Get the Input Capture value */
if (IC2Value2 != 0)
{
DutyCycle2 = (TIM_GetCapture1(TIM3) * 100) / IC2Value2; /* Duty cycle computation */
Frequency2 = 72000000 / IC2Value2; /* Frequency computation */
}
else
{
DutyCycle2 = 0;
Frequency2 = 0;
}
}
加载中...
haohongyu
2楼-- · 2019-03-23 23:14
精彩回答 2 元偷偷看……
加载中...
一周热门
更多
>
相关问题
相关文章
嵌入式编译生成的HEX文件和BIN文件内容详解
0个评论
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
单片机中把部分Flash虚拟成Eeprom使用时,如何延长使用寿命
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
敢问路在何方,STM32迈出的第一步,却注定了它非凡的一生
0个评论
年工作时间真的就等于3年工作经验?也许你就不配
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
//PWM capture PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM3 channel 2 pin (PA.07) configuration */
//PWM capture PA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM2, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
TIM_Cmd(TIM3, ENABLE); /* TIM enable counter */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQ(void)
{
PlusCounter ++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value = TIM_GetCapture2(TIM2); /* Get the Input Capture value */
if (IC2Value != 0)
{
DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value; /* Duty cycle computation */
Frequency = 72000000 / IC2Value; /* Frequency computation */
}
else
{
DutyCycle = 0;
Frequency = 0;
}
}
void TIM3_IRQ(void)
{
PlusCounter2 ++;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
IC2Value2 = TIM_GetCapture2(TIM3); /* Get the Input Capture value */
if (IC2Value2 != 0)
{
DutyCycle2 = (TIM_GetCapture1(TIM3) * 100) / IC2Value2; /* Duty cycle computation */
Frequency2 = 72000000 / IC2Value2; /* Frequency computation */
}
else
{
DutyCycle2 = 0;
Frequency2 = 0;
}
}
一周热门 更多>