|
存在问题:预期的Speed是在-100~100,但实际proteus仿真,反转时Speed为何存在超出范围的数值
单片机源程序如下:
main.c代码:#include "stm32f10x.h"
#include "OLED.h"
#include "delay.h"
#include "Dc_Motor.h"
#include "key.h"
u8 Key;
int8_t Speed;
int main(void)
{
delay_init();
OLED_Init();
Key_Init();
Dc_Motor_Init();
OLED_Clear();
OLED_ShowString(1,1,"Key:");
OLED_ShowString(2,1,"Speed:");
Dc_Motor_SetSpeed(0);
while(1)
{
Key=Key_GetNum();
if (Key == 1)
{
Speed += 30;
if (Speed > 100)
{
Speed = -100;
}
OLED_ShowString(1,5,"+");
OLED_ShowNum(2,7,Speed,6);
}
if (Key == 2)
{
Speed -= 40;
if (Speed < -100)
{
Speed = 100;
}
OLED_ShowString(1,5,"-");
OLED_ShowNum(2,7,Speed,6);
}
Dc_Motor_SetSpeed(Speed);
}
}
PWM.c代码:
#include "PWM.h"
void PWM_Init(void)
{
//开启定时器2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
//开启GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//GPIOA配置
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIOA初始化
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);//TIM2 选择定时器的内部时钟源
//时基单元配置
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
//时钟分频:1,2,4分频
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
//计数器计数方式:向上、向下、中央对齐计数
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
//设置ARR时钟周期
TIM_TimeBaseInitStructure.TIM_Period=100-1;//ARR:10000个数后重装
//设置PSC预分频的值
TIM_TimeBaseInitStructure.TIM_Prescaler=720-1;//PSC:720分频,计数频率100k
//重复计数器的值,高级定时器,这里用不到设置为0
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
//时基单元初始化
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
//初始化配置定时器输出比较单元:TIM_OC
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
//输出比较的模式:PWM1模式
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
//输出比较的极性:高极性:极性不翻转
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
//设置输出使能
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
//设置捕获比较寄存器CCR的值
TIM_OCInitStructure.TIM_Pulse=0;
//输出比较单元,通道3初始化
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
//使能TIM2
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare1(u16 Compare)
{
//修改CCR值
TIM_SetCompare1(TIM2,Compare);
}
void PWM_SetCompare2(u16 Compare)
{
//修改CCR值
TIM_SetCompare2(TIM2,Compare);
}
void PWM_SetCompare3(u16 Compare)
{
//修改CCR值
TIM_SetCompare3(TIM2,Compare);
}
Dc_Motor.c代码:
#include "Dc_Motor.h"
void Dc_Motor_Init(void)
{
//电机速度控制设置
PWM_Init();
//电机方向控制设置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Dc_Motor_SetSpeed(int8_t Speed)
{
//正转
if (Speed > 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);//AIN1:H
GPIO_ResetBits(GPIOA, GPIO_Pin_5);//AIN2:L
PWM_SetCompare3(Speed);
}
//反转
else if(Speed < 0)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);//AIN1:L
GPIO_SetBits(GPIOA, GPIO_Pin_5);//AIN2:H
PWM_SetCompare3(-Speed);
}
//停止
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);//AIN1:L
GPIO_ResetBits(GPIOA, GPIO_Pin_5);//AIN2:L
// PWM_SetCompare3(-Speed);
}
}
key.c代码:
#include "key.h"
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置上拉模式 GPIO_Mode_IPU
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_1|GPIO_Pin_11);
}
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0;
if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0)
{
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0);
delay_ms(20);
KeyNum=1;
}
else if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_11)==0)
{
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_11)==0);
delay_ms(20);
KeyNum=2;
}
return KeyNum;
}
|
|