stm32舵机不运作或者舵机执行延迟很高

在正确连接舵机并且在代码正确的情况下,舵机没有任何反应,而在些许时间后舵机突然动了几下而又继续停摆。

1.刚开始以为代码有问题,但代码编译成功了,于是重新单独写了一份舵机驱动代码

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);
}

Servo.h

#ifndef __SERVO_H
#define __SERVO_H

void Servo_Init(void);
void Servo_SetAngle(float Angle);

#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_5 | 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_5) == 0)
	{
		Delay_ms(20);
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 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;
}

Key.h

#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNum(void);

#endif

main

#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();
		while(1)
		{
			Angle += 30;
			if (Angle > 210)
 			{
				Angle = 0;
        Servo_SetAngle(Angle);
		    OLED_ShowNum(1, 7, Angle, 3);
        Delay_ms(300);
			}
      Delay_ms(300);
      Servo_SetAngle(Angle);
		OLED_ShowNum(1, 7, Angle, 3);
		}
		
	}
}

但是舵机依然无法正常运行。于是觉得舵机坏了,改换另一新舵机,依然如此。

2.而代码中可能影响此运作的主要怀疑对象转变成了晶振函数,于是我改变策略,换成了LED代码测试LED的闪烁频率。的确,经过测试发现LED与设置延迟1秒的配置不能匹配。

3.于是我打开晶振函数的底层代码,发现应该运行的晶振函数是灰色的(代表此函数根本没有进行调用并运行)查询资料显示,缺少晶振stm32频率会减少9倍(72hz变为8hz)。和LED闪烁放慢了9倍相符。

并由以下代码再次验证晶振是否正常工作。

#include "stm32f10x.h"     // STM32 标准库头文件
#include "OLED.h"          // OLED 驱动(需提前准备好)
#include "Delay.h"         // 简单延时函数

int main(void)
{
    // OLED 初始化
    OLED_Init();
    OLED_ShowString(1, 1, "HSE Check...");

    // 复位 RCC 时钟设置
    RCC_DeInit();

    // 打开 HSE(外部晶振)
    RCC_HSEConfig(RCC_HSE_ON);

    // 等待 HSE 起振完成
    ErrorStatus HSEStatus = RCC_WaitForHSEStartUp();

    // 根据起振结果显示
    if (HSEStatus == SUCCESS)
    {
        OLED_ShowString(2, 1, "HSE OK");     // 成功
    }
    else
    {
        OLED_ShowString(2, 1, "HSE FAIL");   // 失败
    }

    while (1)
    {
        // 循环等待
    }
}

4.于是得出结论,舵机无法正常工作的原因是晶振问题即STM32晶振坏了。

在我重新换了新的32后,舵机正常运行。

5注意事项,出现此原因的情况可能是在舵机接线时候接线出错导致晶振被烧坏。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值