自学内容网 自学内容网

stm32如何接收舵机的控制信号(而不是控制舵机)

看到很多如何stm32用pwm信号控制舵机的文章,老生常谈了
我来写一个stm32接收pwm信号的例子 ,这个pwm信号是用来控制舵机的

背景:

我需要接收航模接收机的,用来控制舵机的pwm信号, 得到这个信号后,做其他事情.

初版代码

pwm.h

#ifndef _pwm_H
#define _pwm_H

#include "system.h"

extern uint16_t  IC2Value;
extern float  DutyCycle;
extern float  Frequency;

void pwmIC_Init();

#endif

pwm.c

#include "pwm.h"
#include "usart.h"
#include "stm32f10x_gpio.h"

/**
me totally new 
**/
void pwmIC_Init()
{

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;  

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

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);
 
 
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);
 
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
 
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
 
TIM_Cmd(TIM3, ENABLE);
 
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
 
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
 
}


// https://blog.csdn.net/qq_37389133/article/details/79772878
/*
这里最关键的就是TIM_ICPolarity_Rising 为上升沿开始捕获,TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
选择IC2为有效的输入端,就是IC2获得的就是周期了,而IC1就是在这里就是高电平的时间! 假设周期:600,高电平时间:300
可以计算的得到占空比  = 300/600, 可以得到频率 =72M/ 600
*/
void TIM3_IRQHandler(void)
{
 
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
 
//ic2是周期
  IC2Value = TIM_GetCapture2(TIM3);
 
  if (IC2Value != 0)
  {
//TIM_GetCapture1(TIM3)是高电平的时间,除以周期得到占空比
    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; 
    Frequency = SystemCoreClock / IC2Value;
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

代码参考了:

https://blog.csdn.net/qq_37389133/article/details/79772878

代码是可以用的,
只需要在main.c里引用pwm.h然后定义extern变量 然后在初始化的地方执行pwmIC_Init(); 之后就可以用这几个变量了

但问题是:

在这里插入图片描述
dutycycle到101左右之后就又从零开始了.
跟踪到TIM_GetCapture1(TIM3) ,发现他到65xxx就开始变成0 , 查询了下返回类型是uint16_t

问AI

在这里插入图片描述
ai说%d虽然也可以但最好用%u, 还说了最大6xxxx,所以是我们的上升沿的计数超过了这个库函数的最大值,所以从0开始了. 咋办呢咋办呢,哦对了

问AI

在这里插入图片描述
ai告诉我可以配置定时器TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure.
我配了, 效果
在这里插入图片描述
是满足了我的需要的

真棒
原来我痛苦于低级语言的低级,现在我在AI的帮助下得心应手.

init的完整代码:


void pwmIC_Init()
{

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;  
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 
//start
    // 启用定时器时钟
    //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    
    // 配置定时器基本参数
    //TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // 设置自动重装载寄存器为最大值
    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 设置预分频器为72-1
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 设置时钟分频因子为1
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//end

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);
 
 
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);
 
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
 
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
 
TIM_Cmd(TIM3, ENABLE);
 
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
 
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
 
}


原文地址:https://blog.csdn.net/wangduqiang747/article/details/143905043

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!