#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "oled7pin.h"
#include "BaseTim6.h"
#include "key.h"
#include "MPU6050.h"
#include "IOI2C.h"
#include "inv_mpu_dmp_motion_driver.h"
#include "inv_mpu.h"
#include "TB6612.h"
#include "encoder.h"
#include "stepper.h"
#include "stepper_T_curve.h"
#include "at24iic.h"
#include "at24cxx.h"
#include "servo1servo2.h"
#include "time9pwm.h"
int main(void)
{
u8 keyv=0;
Nvic_Init(NVIC_PriorityGroup_4);
Set_Nvic_Irq(USART1_IRQn,3,3);
Set_Nvic_Irq(TIM6_DAC_IRQn,0,3);
Set_Nvic_Irq(TIM1_UP_TIM10_IRQn,0,3);
Set_Nvic_Irq(TIM1_TRG_COM_TIM11_IRQn,0,3);
Set_Nvic_Irq(TIM8_CC_IRQn,0,3);
Delay_Init();
LED_Init();
BEEP_Init();
uart1_init(115200);
Usart_SendByte(USART1,'a');
Usart_SendByte(USART1,'b');
Usart_SendString(USART1,"tab\r\n");
printf("hello world\r\n");
printf("LED=2222\r\n");
Printf(USART1,"LED=2222,\r\n");
OLED_Init();
OLED_Clear();
OLED_ShowChar816(0,0,'H');
KEY_Init();
Servo1Servo2Timer9_init();
BaseTim6_Init(1);
while(1)
{
keyv=KEY_Read_Short();
if(keyv == KEY8_Short)
{
Servo2_move(0,1);
LED2=0;
}
else if(keyv==KEY7_Short)
{
Servo2_move(90,1);
LED2=1;
}
else if(keyv == KEY6_Short)
{
Servo2_move(225,1);
LED3=0;
}
else if(keyv==KEY5_Short)
{
Servo2_move(270,1);
LED3=1;
}
}
}
#include "time9pwm.h"
void TIM9_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource5,GPIO_AF_TIM9);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource6,GPIO_AF_TIM9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM9, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable);
TIM_OC2Init(TIM9, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM9,ENABLE);
TIM_Cmd(TIM9, DISABLE);
}
#ifndef __time9PWM_H
#define __time9PWM_H
#include "sys.h"
#include "stm32f4xx.h"
#include "delay.h"
void TIM9_PWM_Init(u32 arr,u32 psc);
#endif
#ifndef __servo1servo2_H
#define __servo1servo2_H
#include "sys.h"
#include "time9pwm.h"
extern int Servo1_LastAngle;
extern int Servo2_LastAngle;
#define ServoTimer9Arr 19999
#define ServoTimer9Psc 167
#define Servo1kkk 0.0111f
#define Servo1class 1
#define Servo2kkk 0.0111f
#define Servo2class 1
#define Servo2kkk270 0.00925f
#define Servo2class270 1
void Servo1Servo2Timer9_init(void);
void Servo1_move(int anglepos,int ServoDalayms);
void Servo2_move(int anglepos,int ServoDalayms);
#endif
#include "servo1servo2.h"
int Servo1_LastAngle=0;
int Servo2_LastAngle=0;
void Servo1Servo2Timer9_init(void)
{
Servo1_LastAngle=0;
Servo2_LastAngle=0;
TIM9_PWM_Init(ServoTimer9Arr,ServoTimer9Psc);
TIM_Cmd(TIM9, ENABLE);
}
void Servo1_move(int anglepos,int ServoDalayms)
{
int dangle=0;
u8 i=0;
u8 angleclass=0;
u8 angleyushu=0;
u32 ttt=0;
dangle=anglepos-Servo1_LastAngle;
if(dangle<0)
dangle=(-1)*dangle;
angleclass=dangle/Servo1class;
angleyushu=dangle%Servo1class;
if(angleyushu==0)
{
if((anglepos-Servo1_LastAngle)>0)
{
for(i=0;i<angleclass;i++)
{
ttt=(0.5+Servo1kkk*(Servo1_LastAngle+i*Servo1class))*1000;
TIM_SetCompare1(TIM9,ttt);
delay_ms(ServoDalayms);
}
}
else if((anglepos-Servo1_LastAngle)<0)
{
for(i=0;i<angleclass;i++)
{
ttt=(0.5+Servo1kkk*(Servo1_LastAngle-i*Servo1class))*1000;
TIM_SetCompare1(TIM9,ttt);
delay_ms(ServoDalayms);
}
}
}
else if(angleyushu!=0)
{
if((anglepos-Servo1_LastAngle)>0)
{
for(i=0;i<(angleclass-1);i++)
{
ttt=(0.5+Servo1kkk*(Servo1_LastAngle+i*Servo1class))*1000;
TIM_SetCompare1(TIM9,ttt);
delay_ms(ServoDalayms);
}
TIM_SetCompare1(TIM9,(0.5+Servo1kkk*anglepos)*1000);
delay_ms(ServoDalayms);
}
else if((anglepos-Servo1_LastAngle)<0)
{
for(i=0;i<angleclass;i++)
{
ttt=(0.5+Servo1kkk*(Servo1_LastAngle-i*Servo1class))*1000;
TIM_SetCompare1(TIM9,ttt);
delay_ms(ServoDalayms);
}
TIM_SetCompare1(TIM9,(0.5+Servo1kkk*anglepos)*1000);
delay_ms(ServoDalayms);
}
}
Servo1_LastAngle=anglepos;
}
void Servo2_move(int anglepos,int ServoDalayms)
{
int dangle=0;
int i=0;
int angleclass=0;
int angleyushu=0;
float ttt=0;
dangle=anglepos-Servo2_LastAngle;
if(dangle<0)
dangle=(-1)*dangle;
angleclass=abs(dangle)/Servo2class;
if((anglepos-Servo2_LastAngle)>0)
{
for(i=0;i<angleclass;i++)
{
ttt = 500+7.4*(Servo2_LastAngle+i);
TIM_SetCompare2(TIM9,(int)ttt);
printf("i=%d\r\n",i);
delay_ms(ServoDalayms);
}
}
else if((anglepos-Servo2_LastAngle)<0)
{
for(i=0;i<angleclass;i++)
{
ttt=500+7.4*(Servo2_LastAngle-i);
TIM_SetCompare2(TIM9,(int)ttt);
delay_ms(ServoDalayms);
}
}
Servo2_LastAngle = anglepos;
}