初级会员

- 积分
- 72
- 金钱
- 72
- 注册时间
- 2015-8-4
- 在线时间
- 10 小时
|
5金钱
求助四路舵机的调试问题。
#include "timer.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////
//±????ò???©?§?°??????????×÷???í???????????????ü????????
//Mini STM32??·?°?
//?¨???¨?±?÷ ?????ú??
//??????×?@ALIENTEK
//????????:www.openedv.com
//????????:2010/12/03
//°?±???V1.0
//°??¨?ù??????°?±?????
//Copyright(C) ??????×? 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
unsigned char order=0; //???????¤ ????
unsigned int PWM_value[6]={1000,1500,1000,1000,1500,2000}; //???úPWM ?????? ??±??ü????±??????í??
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //?¨?±?????? ?á????
#define PWM0_output_high GPIO_SetBits(GPIOB,GPIO_Pin_6) //?ê?¨?? ·?±???×÷
#define PWM0_output_low GPIO_ResetBits(GPIOB,GPIO_Pin_6)
#define PWM1_output_high GPIO_SetBits(GPIOB,GPIO_Pin_7)
#define PWM1_output_low GPIO_ResetBits(GPIOB,GPIO_Pin_7)
#define PWM2_output_high GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define PWM2_output_low GPIO_ResetBits(GPIOB,GPIO_Pin_8)
#define PWM3_output_high GPIO_SetBits(GPIOB,GPIO_Pin_9)
#define PWM3_output_low GPIO_ResetBits(GPIOB,GPIO_Pin_9)
//#define PWM4_output_high GPIO_SetBits(GPIOC,GPIO_Pin_4)
//#define PWM4_output_low GPIO_ResetBits(GPIOC,GPIO_Pin_4)
//#define PWM5_output_high GPIO_SetBits(GPIOC,GPIO_Pin_5)
//#define PWM5_output_low GPIO_ResetBits(GPIOC,GPIO_Pin_5)
#define PWM_OUR_TIME 20000/6 //???? 20000US=20MS ???·PWM :6
//???????· ???????¨??·????? ?????????????? ?????????????±???¨PWM_value?© ?????????? ???·PWM ????????????
//?????????ü???·??
//?¨???¨?±?÷??????????
//?????±????????APB1??2±?????APB1??36M
//arr??×?????×°????
//psc???±???¤·?????
//?????????????¨?±?÷4!
void TIM4_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //?±??????
TIM_TimeBaseStructure.TIM_Period = arr; //?è???????????ü??????×°????????×?????×°???????÷???????? ??????5000??500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //?è??????×÷??TIMx?±?????????????¤·????? 10Khz??????????
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?ò??????????
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù??????
TIM_ITConfig( TIM4,TIM_IT_Update , ENABLE );//?????ò???§?????¨??TIM????
//TIM2
//????
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //??????????0??
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //????????3??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??±?????
NVIC_Init(&NVIC_InitStructure); //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷
TIM_Cmd(TIM4, ENABLE); //????TIMx???è
}
//???ú????????
void SERVO_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9);
}
void TIM4_IRQHandler(void) //TIM3????
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //?ì?é???¨??TIM????·??ú??·?:TIM ??????
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //????TIMx???????????í??:TIM ??????
switch(order)
{
case 1:
PWM0_output_high;
TIM_TimeBaseStructure.TIM_Period = PWM_value[0];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 2:
PWM0_output_low;
TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[0];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 3:
PWM1_output_high;
TIM_TimeBaseStructure.TIM_Period = PWM_value[1];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 4:
PWM1_output_low;
TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[1];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 5:
PWM2_output_high;
TIM_TimeBaseStructure.TIM_Period = PWM_value[2];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 6:
PWM2_output_low;
TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[2];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
break;
case 7:
PWM3_output_high;
TIM_TimeBaseStructure.TIM_Period = PWM_value[3];//?????????±??????
TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
// break;
// case 8:
//   WM3_output_low;
// TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[3];//?????????±??????
// TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
//
// break;
// case 9:
//   WM4_output_high;
// TIM_TimeBaseStructure.TIM_Period = PWM_value[4];//?????????±??????
// TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
// break;
// case 10:
//   WM4_output_low;
// TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[4];//?????????±??????
// TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
//
// break;
// case 11:
//   WM5_output_high;
// TIM_TimeBaseStructure.TIM_Period = PWM_value[5];//?????????±??????
// TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
// break;
// case 12:
//   WM5_output_low;
// TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value[5];//?????????±??????
// TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
order=0; //×??ó???·???? ???????¤
break;
default: order=0;
}
order++;
}
}
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
//ALIENTEK Mini STM32??·?°?·????ú??7
//?¨?±?÷???????é
//?????§????www.openedv.com
//?????????í??×?????????????
int main(void)
{
delay_init(); //???±??????????
NVIC_Configuration();//?è??NVIC????·?×é2:2??????????????2???ì????????
LED_Init(); //????????LED??????????????
TIM4_Int_Init(1000,71);//10Khz??????????????????5000??500ms
SERVO_GPIO_Config();
while(1);
}
|
|