/* main.c*/
#include "drive.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
extern __IO uint16_t prescaler;
uint8_t dir=0; // 0 :顺时针 1:逆时针
int main(void)
{
vu8 key=0;
delay_init(); //延时函数初始化
LED_Init(); //LED端口初始化
KEY_GPIO_Init(); //初始化与按键连接的硬件接口
TIMx_PWM_Init();
LED0=0; //先点亮红灯
while(1)
{
if(KEY1_StateRead()==KEY_DOWN) // 增速
{
prescaler-=10;
if(prescaler<10) // 最快速度限制
prescaler=10;
}
if(KEY2_StateRead()==KEY_DOWN) // 减速
{
prescaler+=50;
if(prescaler>3000) // 最慢速度限制
prescaler=3000;
}
if(KEY3_StateRead()==KEY_DOWN)//只有这个控制正反转的能实现
{
if(dir==0)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_13); // 逆时针
dir=1;
}
else
{
GPIO_SetBits(GPIOB,GPIO_Pin_13); // 顺时针
dir=0;
}
}
}
}
/* drive.c*/
#include "drive.h"
#include "sys.h"
__IO uint16_t prescaler=1000;
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//GPIO口设置
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
}
void TIMx_Configuration()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
//中断优先级设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//时基结构体设置
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler =4-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//输出比较结构体设置
TIM_SelectOnePulseMode(TIM1,TIM_OPMode_Single);//设置为单脉冲模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =prescaler;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void TIMx_PWM_Init(void)
{
GPIO_Config();
TIMx_Configuration();
}
void TIM1_CC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
TIM_SetCounter(TIM1,0);
TIM_SetCompare1(TIM1,prescaler);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
prescaler这个变量自己调试一下看看是几
一周热门 更多>