摘要: 我用的核心板是RCT6的核心板,在程序中用到了通用定时器的PWM波的输出、还有按键调整PWM波的占空比,然后还有通过LED灯来判断按键是否成功按下。 一、PWM输出 我用的是通用定时器3的通道3(PB0)具体参考下图 定时器的通道 定时器参考图 二、定时器3的配置
void TIM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_Init_advancetim;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_Init_advancetim.GPIO_Pin = GPIO_Pin_0 ;
GPIO_Init_advancetim.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init_advancetim.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_Init_advancetim);
}
void TIM_MODE_Config(u16 arr,u16 psc)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
}
void NVIC_TIM3_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_PWMCH3_Init(u16 duty)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
#include "stm32f10x.h"
#include "TIM.h"
#include "led.h"
#include "key.h"
#include "delay.h"
u16 jishu;
int main(void)
{
delay_init();
TIM_GPIO_Config();
keyinit();
LED_GPIO_Config();
TIM_MODE_Config(999,(1440-1));
NVIC_TIM3_Config();
while(1)
{
if(keyscan()==1)
{
ledtoggle;
jishu =jishu +12;
}
if(jishu>=75&&jishu<=125)
TIM3_PWMCH3_Init(jishu);
else
jishu=75;
}
}```
还有一定要注意先用示波器先试一下看波形,还有如果按下按键舵机左转右转,可能电压不够, 我用的是6V左右的电压,如果和我一样外接电压的话一定要共地。
文件在下面
链接:https:
提取码:rzzg
|