stm32按鍵控制舵機

在这里插入图片描述
在这里插入图片描述

pwm.h:

#ifndef __PWM_H
#define __PWM_H

#include "stm32f10x.h"

void TIM3_PWM_Init(u16 arr , u16 psc);

#endif

pwm.c:

#include "pwm.h"

//TIM4 PWM初始化
void TIM3_PWM_Init(u16 arr , u16 psc)

{
  
  
  
  GPIO_InitTypeDef  GPIO_InitTypestrue;
  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitTypestrue;
  
  TIM_OCInitTypeDef  TIM_OCInitTypesture;
  
  
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);//使能GPIO时钟,以及TIM3时钟使能
  
  GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//复用
  
  
  
  GPIO_InitTypestrue.GPIO_Mode=GPIO_Mode_AF_PP;
  
  GPIO_InitTypestrue.GPIO_Pin=GPIO_Pin_5;
  
  GPIO_InitTypestrue.GPIO_Speed=GPIO_Speed_50MHz;
  
  GPIO_Init(GPIOB,&GPIO_InitTypestrue);    //GPIO初始化
  
  
  
  TIM_TimeBaseInitTypestrue.TIM_Period=arr;
  
  TIM_TimeBaseInitTypestrue.TIM_Prescaler=psc;
  
  TIM_TimeBaseInitTypestrue.TIM_CounterMode=TIM_CounterMode_Up;
  
  TIM_TimeBaseInitTypestrue.TIM_ClockDivision=TIM_CKD_DIV1;
  
  TIM_TimeBaseInit(TIM3 ,&TIM_TimeBaseInitTypestrue);   //定时器初始化
  
  
  
  TIM_OCInitTypesture.TIM_OutputState=TIM_OutputState_Enable;
  
  TIM_OCInitTypesture.TIM_OCMode=TIM_OCMode_PWM2;
  
  TIM_OCInitTypesture.TIM_OCPolarity=TIM_OCPolarity_High;
  
  TIM_OC2Init(TIM3 ,&TIM_OCInitTypesture );//CH2通道初始化
  
  
  
  TIM_OC2PreloadConfig( TIM3 ,TIM_OCPreload_Enable);//使能预装载寄存器
  
  TIM_Cmd(TIM3,ENABLE);
  
}

main.c:

#include "stm32f10x.h" // Device header
#include "pwm.h"
#include "Delay.h"
#include "Key.h"
uint8_t KeyNum;
int main(void){
  
  u16 led0pwmval=185;
  TIM3_PWM_Init(199, 7199);//50hz
  Key_Init();
  
  while(1){
    KeyNum=Key_GetNum();
    if(KeyNum==1){
      led0pwmval=195;//順180
      TIM_SetCompare2(TIM3,led0pwmval);
      
    }
    if(KeyNum==2){
	  led0pwmval=175;//逆180
      TIM_SetCompare2(TIM3,led0pwmval);
      
		
		
    }
  }
}


Key.h:

#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);




#endif

Key.c:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
void Key_Init(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
}

uint8_t Key_GetNum(void){
	uint8_t KeyNum=0;
	if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1))==0){
		Delay_ms(20);
		while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1))==0);
		Delay_ms(20);
		KeyNum=1;
	}
	if((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11))==0){
		Delay_ms(20);
		while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11))==0);
		Delay_ms(20);
		KeyNum=2;
	}
	return KeyNum;
}

Delay.c:

#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
void Delay_us(uint32_t xus)
{
	SysTick->LOAD = 72 * xus;				//设置定时器重装值
	SysTick->VAL = 0x00;					//清空当前计数值
	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
	SysTick->CTRL = 0x00000004;				//关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
	while(xs--)
	{
		Delay_ms(1000);
	}
} 

Delay.h:

#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值