Не как не могу заставить её повернуться на 45, 60, 90, 180 градусов
Везде по разному... А нужно как на картинке:
как на картинке:
- Код: Выделить всё • Развернуть
- #include <stdio.h>
 #include <stm32f4xx.h>
 #include <stm32f4xx_gpio.h>
 #include <stm32f4xx_rcc.h>
 #include <stm32f4xx_tim.h>
 #include <stm32f4xx_exti.h>
 #include <misc.h>
 #include <stm32f4xx_syscfg.h>
 volatile int servo_angle[4] = { 0, 0, 0, 0 }; // +/- 90 degrees, use floats for fractional
 int main(void)
 { SystemInit(); // 25 MHz
 // Servo min(1ms) = 1580
 // Servo max(2ms) = 3160
 //TIM_SetCompare1(TIM1, 1580); // 1580 = 1.0ms
 //TIM_SetCompare1(TIM1, 2370); // 2370 = 1.5ms
 //TIM_SetCompare1(TIM1, 3160); // 3160 = 2.0ms
 GPIO_InitTypeDef GPIO_InitStructure;
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 TIM_OCInitTypeDef TIM_OCInitStructure;
 // TIM1 clock enable
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 // GPIOE clock enable
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
 // TIM1 channel 2 pin (PE.9) configuration
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
 GPIO_Init(GPIOE, &GPIO_InitStructure);
 // Connect TIM pins to AF2
 GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1);
 TIM_TimeBaseStructInit (&TIM_TimeBaseStructure);
 //TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/5000000; // pers 33.6
 //TIM_TimeBaseStructure.TIM_Period = 31600; // 20ms for servo period
 //TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock / 100000 - 1;
 //TIM_TimeBaseStructure.TIM_Period = 2000;
 TIM_TimeBaseStructure.TIM_Prescaler = 24 - 1; // 24 MHz / 24 = 1 MHz
 TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20 ms)
 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseInit(TIM1 , &TIM_TimeBaseStructure);
 // PWM1 Mode configuration: Channel1
 // Edge -aligned; not single pulse mode
 TIM_OCStructInit (& TIM_OCInitStructure);
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_Pulse = 600 + 900;
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 TIM_OC1Init(TIM1 , &TIM_OCInitStructure);
 TIM_BDTRInitTypeDef bdtr;
 TIM_BDTRStructInit(&bdtr);
 bdtr.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
 TIM_BDTRConfig(TIM1, &bdtr);
 // Enable Timer Interrupt and Timer
 TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //
 TIM_Cmd(TIM1 , ENABLE);
 //TIM_SetCompare1(TIM1, 1500); // 1580 = 1.0ms = 0
 //delayMS(1*100);
 //TIM_SetCompare1(TIM1, 2160); // 2370 = 1.5ms = 90
 //delayMS(1*100);
 //TIM_SetCompare1(TIM1, 3460); // 3160 = 2.0ms = 180
 //delayMS(1*100);
 uint8_t i;
 for (i=0;i<=90;i++)
 {
 //delayMS(100);
 set_pos(i);
 }
 while (1)
 {
 }
 return 0;
 }
 #define SERVO_180 3160
 #define SERVO_0 1500
 // Функция устанавливает позицию вала (в градусах)
 void set_pos(uint8_t pos)
 {
 uint32_t tmp=(SERVO_180 - SERVO_0) / 90 ;
 uint32_t tmp2 = SERVO_0 + tmp * pos;
 TIM_SetCompare1(TIM1, tmp2);
 }
 void delayMS(uint32_t ms)
 {
 ms *= 3360;
 while(ms--)
 {
 __NOP();
 }
 }
 // Функция задержки
 void delay(void)
 {
 volatile uint32_t i;
 for (i=1; i != 0xFFFF; i++);
 }





