單片機源程序如下:
- #include "motor.h"
- #include "Math.h"
- #include "delay.h"
- #include "stm32f10x.h" // Device header
- signed short sPWMR,sPWML,dPWM;
- //GPIO配置函數
- /*void MotorGPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能PA,PB端口時鐘
-
- GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_GO; //左電機方向控制
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(LEFT_MOTOR_GO_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_PWM;
- GPIO_Init(LEFT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_PWM;
- GPIO_Init(RIGHT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_GO;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(RIGHT_MOTOR_GPIO, &GPIO_InitStructure);
-
- }
- void run() //前進
- {
- RIGHT_MOTOR_GO_SET;
- RIGHT_MOTOR_PWM_RESET;//PB9
-
- LEFT_MOTOR_GO_SET;
- LEFT_MOTOR_PWM_RESET;//PB8
-
-
- }
- */
- void TIM4_PWM_Init(unsigned short arr,unsigned short psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO外設時鐘使能
-
- GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_GO; //左電機方向控制 PB7
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(LEFT_MOTOR_GO_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_PWM; //左電機PWM控制 PB8
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_Init(LEFT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_GO; //右電機方向控制 PA4
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(RIGHT_MOTOR_GPIO, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_PWM; //右電機PWM控制 PB9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_Init(RIGHT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
-
- TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 80K
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數的預分頻值 不分頻
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設置時鐘分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的參數初始化TIMx的時間基數單位
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //設置待裝入捕獲比較寄存器的脈沖值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
- TIM_OC3Init(TIM4, &TIM_OCInitStructure); //根據TIM_OCInitStruct中指定的參數初始化外設TIMx
- TIM_OC4Init(TIM4, &TIM_OCInitStructure); //根據TIM_OCInitStruct中指定的參數初始化外設TIMx
-
- TIM_CtrlPWMOutputs(TIM4,ENABLE); //MOE 主輸出使能
-
- TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //CH1預裝載使能
- TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //CH1預裝載使能
-
- TIM_ARRPreloadConfig(TIM4, ENABLE); //使能TIMx在ARR上的預裝載寄存器
- TIM_Cmd(TIM4, ENABLE); //使能TIM1
- }
- void SetMotorSpeed(unsigned char ucChannel,signed char cSpeed)
- {
- // static short sMotorSpeed = 0;
- short sPWM;
- // float fDir = 1;
-
- if (cSpeed>=100) cSpeed = 100;
- if (cSpeed<=-100) cSpeed = -100;
-
- sPWM = 7201 - fabs(cSpeed)*72;
- switch(ucChannel)
- {
- case 0://右輪
- TIM_SetCompare3(TIM4,sPWM);
- if (cSpeed>0)
- RIGHT_MOTOR_GO_RESET;
- else if(cSpeed<0)
- RIGHT_MOTOR_GO_SET;
- break;
- case 1://左輪
- TIM_SetCompare4(TIM4,sPWM);
- if (cSpeed>0)
- LEFT_MOTOR_GO_SET;
- else if (cSpeed<0)
- LEFT_MOTOR_GO_RESET;
- break;
- }
- }
- //----------------------------------運動函數--------------------------------
- void ZYSTM32_run(signed char speed,int time) //前進函數
- {
- signed char f_speed = - speed;
- SetMotorSpeed(1,f_speed);//左輪 //為負數
- SetMotorSpeed(0,speed);//右輪 //為正數
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_brake(int time) //剎車函數
- {
- SetMotorSpeed(1,0);//左輪 //為0
- SetMotorSpeed(0,0);//右輪 //為0
- RIGHT_MOTOR_GO_RESET;
- LEFT_MOTOR_GO_RESET;
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_Left(signed char speed,int time) //左轉函數
- {
- SetMotorSpeed(1,0);//左輪 //左輪不動
- SetMotorSpeed(0,speed); //右輪為正
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_Spin_Left(signed char speed,int time) //左旋轉函數
- {
- SetMotorSpeed(1,speed);//左輪 //左輪為正
- SetMotorSpeed(0,speed); //右輪為正
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_Right(signed char speed,int time) //右轉函數
- {
- signed char f_speed = - speed;
- SetMotorSpeed(1,f_speed);//左輪 //左輪為負
- SetMotorSpeed(0,0); //右輪為0
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_Spin_Right(signed char speed,int time) //右旋轉函數
- {
- signed char f_speed = - speed;
- SetMotorSpeed(1,f_speed);//左輪 //左輪為負
- SetMotorSpeed(0,f_speed); //右輪為負
- delay_ms(time); //時間為毫秒
- }
- void ZYSTM32_back(signed char speed,int time) //后退函數
- {
- signed char f_speed = - speed;
- SetMotorSpeed(1,speed);//左輪 //為正數
- SetMotorSpeed(0,f_speed);//右輪 //為負數
- delay_ms(time); //時間為毫秒
- }
復制代碼
所有資料51hei提供下載:
4、ZYSTM32-A0 智能小車黑線循跡實驗.rar
(330.42 KB, 下載次數: 111)
2018-12-10 03:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|