舵机,模拟舵机,数字舵机(arduino,stm32f103代码开源)

舵机组成

 舵机是一种常见的伺服电机,主要用来控制角度的变化。常见的sg90舵机组成结构如下:

舵机,模拟舵机,数字舵机(arduino,stm32f103代码开源)_第1张图片 舵机组成图

模拟舵机与数字舵机的区别

sg90是模拟舵机,除了模拟舵机外,还有数字舵机,他们的控制区别如下

舵机,模拟舵机,数字舵机(arduino,stm32f103代码开源)_第2张图片 模拟舵机和数字舵机的控制区别

 模拟舵机和数字舵机的机械结构一样;硬件方面数字舵机多了一块芯片,充当一个管家的作用;控制方面只需要接受一个50HZ的PWM波即可稳定角度(多发几次会稳妥一点),而模拟舵机要不断接收50HZ的PWM波。

控制代码(模拟和数字可通用)

arduino控制代码(需要自己修改引脚号)

#include 

Servo myservo1;  // create servo object to control a servo
Servo myservo2;

// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  int outPin1 = D6;
  int outPin2 = D7;

  myservo1.attach(D6,500,2500);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(D7,500,2500);
}

void loop() {
  for (pos = 500; pos <= 2500; pos += 20) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo1.writeMicroseconds(pos);  // set servo to mid-point
    myservo2.writeMicroseconds(pos);
    delayMicroseconds(100);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 2500; pos >= 500; pos -= 20) { // goes from 180 degrees to 0 degrees
    myservo1.writeMicroseconds(pos);              // tell servo to go to position in variable 'pos'
    myservo2.writeMicroseconds(pos);
    delayMicroseconds(100);                       // waits 15 ms for the servo to reach the position
  }
}

stm32f103工程请下载查看,里面有注释,工程可直接运行。

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
 

 int main(void)
 {		

	delay_init();	    	 //ÑÓʱº¯Êý³õʼ»¯	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	 //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
	uart_init(115200);	 //´®¿Ú³õʼ»¯Îª115200
// 	LED_Init();			     //LED¶Ë¿Ú³õʼ»¯

	 TIM3_PWM_Init(19999,71);//GPIOB_5
	 
   	while(1)
	{
	
	delay_ms(1000);	
	TIM_SetCompare2(TIM3,500);	//0
    delay_ms(1000);		
    TIM_SetCompare2(TIM3,1000);
    delay_ms(1000);		
    TIM_SetCompare2(TIM3,1500); //90
    delay_ms(1000);		
    TIM_SetCompare2(TIM3,2000);
    delay_ms(1000);				
    TIM_SetCompare2(TIM3,2500); //180
    delay_ms(1000);		
	}	 
	
 }

//TIM3 PWM
void TIM3_PWM_Init(u16 arr,u16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	//ʹÄܶ¨Ê±Æ÷3ʱÖÓ
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
	
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä  TIM3_CH2->PB5    
 
   //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐÎ	GPIOB.5
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
 
   //³õʼ»¯TIM3
	TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ 
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
	
	//³õʼ»¯TIM3 Channel2 PWMģʽ	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ1, when TIMX_CNT

关于舵机原理与控制,舵机死区,我做了一个视频讲解,希望能对你有所帮助。

视频连接:《舵机从入门到放弃》模拟舵机,数字舵机,死区_哔哩哔哩_bilibili

arduino,stm32f103控制代码:motorcountry/SERVO: arduino,stm32f103控制舵机工程 (github.com)

你可能感兴趣的:(嵌入式,stm32,单片机,arm)