我用PWM控制电机 之前试过用TIM1 ch1控制 可以实现 但是我用多个pwm控制多个电机的时候就不能用了 下面是初始化程序 请帮忙看看哪里错了 谢谢
#include "delay.h"
#include "sys.h"
#include "pwm.h"
#include "opration.h"
#include "usart.h"
int main(void)
{
//u8 dir=1;
SystemInit();
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
//3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
TIM_PWM_Init(2000,719);//2»·ÖÆμ¡£PWMÆμÂê=72000/(71+1)=1000Khz
usart3_init(9600);
while(1){
//oprations();
TIM_SetCompare1(TIM1,1200);
TIM_SetCompare2(TIM1,800);
TIM_SetCompare3(TIM1,1500);
TIM_SetCompare4(TIM1,500);
TIM_SetCompare1(TIM3,1500);
TIM_SetCompare2(TIM3,500);
TIM_SetCompare3(TIM3,1500);
TIM_SetCompare4(TIM3,500);
TIM_SetCompare1(TIM4,1500);
TIM_SetCompare2(TIM4,500);
TIM_SetCompare3(TIM4,1500);
TIM_SetCompare4(TIM4,500);
TIM_SetCompare1(TIM5,1500);
TIM_SetCompare2(TIM5,500);
}
}
#include "pwm.h"
#include "led.h"
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructureA;
GPIO_InitTypeDef GPIO_InitStructureB;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//¿aÆô¶¨ê±Æ÷ 1¡¢3¡¢4¡¢5ê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4|RCC_APB1Periph_TIM5, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_TIM1, ENABLE); //ê1ÄüGPIOA¡¢Bíaéèê±Öóê1Äü
//éèÖÃòy½ÅÎa¸′óÃêä3ö1|Äü
GPIO_InitStructureA.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructureA.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructureA.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructureA);
GPIO_InitStructureB.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructureB.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructureB.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructureB);
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 2»·ÖÆμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 0; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC1Init(TIM5, &TIM_OCInitStructure);
TIM_OC2Init(TIM5, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE); //¸ß¼¶¶¨ê±Æ÷TIM 1 MOE Ö÷êä3öê1Äü
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//ԤװÔØê1Äü
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_ARRPreloadConfig(TIM5, ENABLE);
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIMx
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_Cmd(TIM5, ENABLE);
}
一周热门 更多>