|
/************************************************************************
STM32 pwm调速小车
PB5,PB6 接直流电机驱动器IN1 IN2,用于小车前进后退及调速
PB7,PB8 接直流电机驱动器IN3 IN4,用于小车转向,为加入转向速度调节
PA0(高电平有效)接无线输入D0引脚 小车前进
PA1(高电平有效)接无线输入D1引脚 小车后进
PA2(高电平有效)接无线输入D2引脚 小车左转进
PA3(高电平有效)接无线输入D3引脚 小车右转进
PA4(高电平有效)接按钮开关 用于小车调速
************************************************************************/
#include "stm32f10x.h"
#include "smartcar.h"
#include "usart_2.h"
#include "pwm.h"
#include "delay.h"
#include "KEY.h"
int main(void)
{
u16 numval= 1;
//u16 numval3=0;
u16 numval4=1;
u16 pwmval= 0;
u16 pwmval4=0;
vu8 key=0;
smartcar_Init();//智能小车GPIO初始化
TIM3_PWM_Init(899,0); //不分频。PWM频率=72000000/900=80Khz
TIM4_PWM_Init(899,0); //不分频。PWM频率=72000000/900=80Khz
delay_init(); //延时函数初始化
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
key=KEY_Scan(0); //得到键值
if(key)
{
switch(key)
{
case KEYA_PRES://前进
switch(numval)
{
case 1: pwmval=350; pwmval4=0;break;
case 2: pwmval=450; pwmval4=0;break;
case 3: pwmval=550; pwmval4=0;break;
case 4: pwmval=650; pwmval4=0;break;
case 5: pwmval=750; pwmval4=0;break;
case 6: pwmval=899; pwmval4=0;break;
}
INA3_0; INA4_0;
TIM_SetCompare2(TIM3,pwmval); //pwm输出函数
TIM_SetCompare1(TIM4,pwmval4);
break;
case KEYB_PRES://后退
switch(numval4)
{
case 1: pwmval4=350;pwmval=0;break;
case 2: pwmval4=450;pwmval=0;break;
case 3: pwmval4=550;pwmval=0;break;
case 4: pwmval4=650;pwmval=0;break;
case 5: pwmval4=750;pwmval=0;break;
case 6: pwmval4=899;pwmval=0;break;
}
INA3_0; INA4_0;
TIM_SetCompare2(TIM3,pwmval); //pwm输出函数
TIM_SetCompare1(TIM4,pwmval4);
break;
case KEYC_PRES: //左转
INA3_1
INA4_0
TIM_SetCompare2(TIM3,pwmval); //pwm输出函数
TIM_SetCompare1(TIM4,pwmval4);
break;
case KEYD_PRES: //右转
INA4_1
INA3_0
TIM_SetCompare2(TIM3,pwmval); //pwm输出函数
TIM_SetCompare1(TIM4,pwmval4);
break;
case KEYE_PRES://调速
numval++;
numval4++;
if(numval>6) numval=1; //值不能设为0,否则高速无法换向
if(numval4>6) numval4=1;
break;
case KEYF_PRES://停止
pwmval =0;
pwmval4=0;
INA4_0
INA3_0
break;
}
}
if(KEYA==0&&KEYB==0&&KEYC==0&&KEYD==0&&KEYE==0&&KEYF==0)
{
TIM_SetCompare2(TIM3,0); //pwm输出函数
TIM_SetCompare1(TIM4,0);
INA4_0; INA3_0;
}
}
}
|
评分
-
查看全部评分
|