⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 car_driver.c

📁 msp430+dypme007
💻 C
字号:
//By LINZH_BME_SEU

//头文件
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "sysctl.h"
#include "pwm.h"


//函数

void CarInit(void);
void CarModeSet(unsigned char);	//0++1+-2-+3--
void CarSpdSet(unsigned char);		//Speed 0~250

//初始化
void CarInit(void) 
 {
	//IO初始化
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_2);
	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_3);
	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_4);
	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_5);
	
	GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,0);
	GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,0);
	GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_4,0);
	GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_5,0);

	//PWM初始化
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);
   	SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

	GPIOPinTypePWM(GPIO_PORTB_BASE,GPIO_PIN_0);
	GPIOPinTypePWM(GPIO_PORTB_BASE,GPIO_PIN_1);

	PWMGenConfigure(PWM_BASE, PWM_GEN_1,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
	PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, 6000);  
   	PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, 3000);
    	PWMDeadBandEnable(PWM_BASE,PWM_GEN_1,50,50);
   	//PWMOutputState(PWM_BASE,PWM_OUT_2_BIT,true);
    	//PWMGenEnable(PWM_BASE, PWM_GEN_1);
}

void CarModeSet(unsigned char state)
{
	if(state>=4) return;
	//电机1
	if(state<2)
	{
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,0x04);
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,0);
	}else
	{
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,0);
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,0x08);
	}
	//电机2
	if(state%2==0)
	{
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_4,0x10);
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_5,0);
	}else
	{
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_4,0);
		GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_5,0x20);
	}
}

void CarSpdSet(unsigned char speed)
{	
	PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, speed*20);
   	PWMOutputState(PWM_BASE,PWM_OUT_2_BIT,true);
    	PWMGenEnable(PWM_BASE,PWM_GEN_1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -