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

📄 zhaohu.c

📁 控制电机的程序
💻 C
字号:
**************************************************************************************************
定义头文件
**************************************************************************************************
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "sysctl.h"
#include "timer.h"
#include "interrupt.h"
#include "systick.h"
**************************************************************************************************
定义宏
**************************************************************************************************
#define timer_a   65535                                                   // 设置计数器初值
#define perL    0.7                                                       // 设置左PWM占空比
#define perR    0.7                                                       // 设置右PWM占空比
#define P0 GPIO_PIN_0
#define P1 GPIO_PIN_1
#define P2 GPIO_PIN_2
#define P3 GPIO_PIN_3
#define P4 GPIO_PIN_4
#define P5 GPIO_PIN_5
#define P6 GPIO_PIN_6
#define P7 GPIO_PIN_7
**************************************************************************************************
延时函数
**************************************************************************************************
void timeDelay (unsigned long ulVal)
{unsigned long  i;
    i= ulVal*2000000;
	do 
        {
	} 
        while ( --i != 0 );
}
**************************************************************************************************
GPIO初始化函数
**************************************************************************************************
void initial_gpio ( )
{
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); /* 使能GPIOD模块 */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); /* 使能GPIOE模块 */
  GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, (P0 | P1)); /* 设置PD0和PD1为输出模式 */
  GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, (P0 | P1)); /* 设置PE0和PE1为输出模式 */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); /* 使能GPIOC模块 */
  GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7));/*设置PC4,PC5,PC6,PC7其为输出*/
  asm("nop");
  asm("nop");
  GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x00); 
  GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x00); 
}
**************************************************************************************************
前进函数
**************************************************************************************************
void forword ( )
{
  GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x01); 
  GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x01);
}
**************************************************************************************************
后退函数
**************************************************************************************************
void backward ( )
{
  GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x02);
  GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x02); 
}
**************************************************************************************************
停止函数
**************************************************************************************************
void stop ( )
{ 
  GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x00); 
  GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x00); 
}
**************************************************************************************************
左转函数
**************************************************************************************************		
void left()
{
 GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x01);
 GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x00);
}
**************************************************************************************************
右转函数
**************************************************************************************************
void right()
{
 GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x00);
 GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x01);
}
**************************************************************************************************
画圈函数
**************************************************************************************************
void circle( )
{
    TimerDisable(TIMER0_BASE , TIMER_A);				                   
    TimerDisable(TIMER1_BASE , TIMER_A);	
    TimerMatchSet(TIMER0_BASE,TIMER_A,(int)(timer_a*(1-0.4)));	// 设置左PWMA的匹配值
    TimerMatchSet(TIMER1_BASE,TIMER_A,(int)(timer_a*(1-0.9)));	// 设置右PWMA的匹配值
    TimerEnable(TIMER0_BASE , TIMER_A);				//  PWMA模式使能
    TimerEnable(TIMER1_BASE , TIMER_A);	
    GPIOPinWrite(GPIO_PORTD_BASE, (P0 | P1), 0x01); /* 置位PD0,清零PD1 */
    GPIOPinWrite(GPIO_PORTE_BASE, (P0 | P1), 0x01); /* 置位PD0,清零PD1 */
}
**************************************************************************************************
PWM波初始化函数
**************************************************************************************************
void initial_pwm ( )
{
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |SYSCTL_XTAL_6MHZ);  // 设定晶振为时钟源,分频为1
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0 ); // 使能定时器0和1外设。
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1 );
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);       // 使能GPIO B口外设。
    IntMasterEnable();                              // 处理器使能。
    GPIODirModeSet(GPIO_PORTB_BASE, P0 , GPIO_DIR_MODE_HW);   // 设置PB0,PB1输出PWMA波形
    GPIODirModeSet(GPIO_PORTB_BASE, P1 , GPIO_DIR_MODE_HW); 
    TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PWM); // 设置16-PWMA模式
    TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PWM); 
    TimerLoadSet(TIMER0_BASE ,TIMER_A,timer_a);			        // 设置PWMA的装载值
    TimerLoadSet(TIMER1_BASE ,TIMER_A,timer_a);		
    TimerMatchSet(TIMER0_BASE,TIMER_A,(int)(timer_a*(1-perL)));	// 设置左PWMA的匹配值
    TimerMatchSet(TIMER1_BASE,TIMER_A,(int)(timer_a*(1-perR)));	// 设置右PWMA的匹配值
    TimerEnable(TIMER0_BASE , TIMER_A);				//  PWMA模式使能
    TimerEnable(TIMER1_BASE , TIMER_A);	
}
**************************************************************************************************
扫描函数
**************************************************************************************************
void scan(void)
{
  Forward();  
  int Pin_B7_Val=GPIOPinRead(GPIO_PORTb_BASE, GPIO_PIN_7);
  while(Pin_B7_Val)
   {
    Pin_B7_Val=GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_7);
   }
  bizhang();
}
**************************************************************************************************
壁障函数
**************************************************************************************************
void bizhang()
{
left();
timeDelay(1L);
stop();
timeDelay(1L);

forword();
timeDelay(2L);
stop();
timeDelay(1L);

right();
timeDelay(1L);
stop();
timeDelay(1L);


forword();
timeDelay(5L);
stop();
timeDelay(1L);
}
**************************************************************************************************
主函数
**************************************************************************************************
int main(void)
 {
   timeDelay(2L); /* 开机延迟 */
   initial_gpio ( );
   initial_pwm ( );
   
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xE0); /* PC4=0*/
   timeDelay(4L);
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xF0);
   forword ( );
   timeDelay(5L);
   
   stop ( );
   timeDelay(2L);
   
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xD0); /* PC5=0*/
   timeDelay(3L);
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xF0);
   backward ( );
   timeDelay(5L);
   
   stop ( );
   timeDelay(2L);
   
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xB0); /* PC6=0*/
   timeDelay(3L);
   GPIOPinWrite(GPIO_PORTC_BASE, (P4 | P5 | P6 | P7), 0xF0);
   circle ( );
   timeDelay(12L);
   
   stop ( );
   timeDelay(2L);
   
   forword();
   timeDelay(2L);
   stop ( );
   timeDelay(2L);
 
   scan();
   GPIOPinWrite(GPIO_PORTA_BASE, (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3), 0x07); // PA3=0,语音选段
   timeDelay(2L);
   GPIOPinWrite(GPIO_PORTA_BASE, (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3), 0x0F);
   stop();
   

}              
**************************************************************************************************
The end
**************************************************************************************************

⌨️ 快捷键说明

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