4.PWM舵机

PWM舵机驱动

知识点补充,之前没有提到的计算定时器更新事件的频率的公式

CK_CNT_OV=CK_CNT/(ARR+1)=CK_PSC/(PSC+1)/(ARR+1)

PWM.c:

#include "stm32f10x.h"                  // Device header
//CK_CNT_OV=CK_CNT/(ARR+1)=CK_PSC/(PSC+1)/(ARR+1)
void PWM_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	TIM_InternalClockConfig(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1;		//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;		//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 0;		//CCR
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);
	
	TIM_Cmd(TIM3, ENABLE);
}

void PWM_SetCompare2(uint16_t Compare)
{
	TIM_SetCompare2(TIM3, Compare);
}

就改了ARR和PSC的值

servo.c:

#include "stm32f10x.h"                  // Device header
#include "PWM.h"

void Servo_Init(void)
{
	PWM_Init();
}

void Servo_SetAngle(float Angle)
{
	PWM_SetCompare2(Angle / 180 * 2000 + 500);
}

就是调用了PWM_SetCompare2

key.c:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#define LONG_PRESS_TIME 1000            // 长按时间阈值,单位ms
#define CONTINUOUS_PRESS_INTERVAL 100   // 长按后连续返回按键键码的时间间隔,单位ms

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_Scan(void)
{
    if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
    {
        return 1;
    }
    else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
    {
        return 2;
    }
    
    return 0;
}

uint8_t Key_GetNum(void)
{
    static uint32_t PressTime = 0;       // 静态变量保持其值
    static uint32_t LastReleaseTime = 0; // 上次按键松开时间
    static uint8_t LastKeyNum = 0;       // 上次按键的键码
    uint8_t KeyNum = 0;                  // 默认键码为0
    static uint32_t CurrentTime = 0;     // 当前时间,模拟一个系统计时器
    
    KeyNum = Key_Scan();
    if (KeyNum != 0)
    {
        while(Key_Scan() == KeyNum)
        {
            Delay_ms(10);
            PressTime += 10;
            CurrentTime += 10;
            
            if (PressTime >= LONG_PRESS_TIME)
            {
                // 长按1秒后,连续返回按键键码
                while (Key_Scan() == KeyNum)
                {
                    Delay_ms(CONTINUOUS_PRESS_INTERVAL);
                    return KeyNum;
                }
            }
        }
        
        if (PressTime >= 20 && PressTime < LONG_PRESS_TIME)
        {
                LastKeyNum = KeyNum;     // 记录本次按键的键码
                LastReleaseTime = CurrentTime; // 记录本次按键松开的时间
                PressTime = 0;           // 重置PressTime
                return KeyNum+10;           // 返回单击键码
        }
    }
    else
    {
        PressTime = 0;                   // 按键松开时重置PressTime
        CurrentTime += 10;               // 模拟系统时间的增加
    }
    
    return 0;
}

这里我用到了更好的按键里的长按和单次短按

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Servo.h"
#include "Key.h"

uint8_t KeyNum;
float Angle;

int main(void)
{
	OLED_Init();
	Servo_Init();
	Key_Init();
	
	OLED_ShowString(1, 1, "Angle:");
	
	while (1)
	{
		KeyNum = Key_GetNum();
		if (KeyNum == 11)
		{
			Angle += 30;
			if (Angle > 180)
			{
				Angle = 0;
			}
		}
		else if (KeyNum == 12)
		{
			Angle -= 30;
			if (Angle < 0)
			{
				Angle = 180;
			}
		}
		else if(KeyNum == 1)
		{
			Angle++;
			if (Angle > 180)
			{
				Angle = 0;
			}
		}
		else if(KeyNum == 2)
		{
			Angle--;
			if (Angle < 0)
			{
				Angle = 180;
			}
		}
		Servo_SetAngle(Angle);
		OLED_ShowNum(1, 7, Angle, 3);
	}
}

这边就给到了角度变化,因为我的舵机是最多转180度的,所以我设置了0和180两个界限,到达0或180后就会立刻跳到另一个界限点

接线图:

演示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值