完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
基于stm32循迹小车运动实例
#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); 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_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¿ØÖÆ PB8GPIO_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¿ØÖÆ PB9GPIO_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; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ 80KTIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ ²»·ÖƵTIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽTIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2TIM_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ÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMxTIM_OC4Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMxTIM_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); //ʱ¼äΪºÁÃë } |
|
|
|
只有小组成员才能发言,加入小组>>
3280 浏览 9 评论
2958 浏览 16 评论
3460 浏览 1 评论
9005 浏览 16 评论
4052 浏览 18 评论
1117浏览 3评论
574浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
571浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2304浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1860浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-26 11:18 , Processed in 1.344452 second(s), Total 78, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号