stm32三自由度机器人c语言程序代码Word格式文档下载.docx
《stm32三自由度机器人c语言程序代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《stm32三自由度机器人c语言程序代码Word格式文档下载.docx(56页珍藏版)》请在冰豆网上搜索。
![stm32三自由度机器人c语言程序代码Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-11/27/601fb18b-57d9-4ba9-a8d3-5bb597f97399/601fb18b-57d9-4ba9-a8d3-5bb597f973991.gif)
GPIOx.h"
NVIC.h"
USARTx.h"
/*宏定义------------------------------------------------------------------*/
#defineucharunsignedchar
#defineuintunsignedint
#defineMotor1_SUB16//电机116细分
#defineMotor2_SUB16//电机216细分
#defineMotor3_SUB16//电机316细分
#defineMotor1_DIRPAout(4)//电机1方向
#defineMotor2_DIRPAout(5)//电机2方向
#defineMotor3_DIRPAout(7)//电机3方向
#defineMotor1_UPMotor1_DIR=1//电机1上升
#defineMotor2_UPMotor2_DIR=1//电机2上升
#defineMotor3_UPMotor3_DIR=1//电机3上升
#defineMotor1_DOWNMotor1_DIR=0//电机1下降
#defineMotor2_DOWNMotor2_DIR=0//电机2下降
#defineMotor3_DOWNMotor3_DIR=0//电机3下降
/*变量定义------------------------------------------------------------------*/
u8Delay_1s_Finishflag=0;
u8KeyTrg=0;
//按键触发
u8KeyCont=0;
//按键长按
uinti;
u16EXIT0_CNT=0;
u8Motor1_OriginFlag=0;
//电机1原点标识
u8Motor2_OriginFlag=0;
u8Motor3_OriginFlag=0;
//电机1原点标识
u8Motor_Arrived=0;
//电机到达标识
/*函数声明------------------------------------------------------------------*/
voidRTC_configuration(void);
voidRCC_Configuration(void);
voidKeyRead(void);
voidDelay_Ms(u16time);
voiddelay_1s(uinttime);
//==================================MAIN=======================================
intmain(void)
{
u16i=0;
BUZZER=0;
//蜂鸣器复位
GPIOE->
ODR&
=0xffff0000;
//LED1~LED8=0;
RCC_Configuration();
//RTC_configuration();
GPIO_Configuration();
NVIC_Configuration();
EXTI_Configuration();
Motor_Init(000);
//TIM1关闭状态不会触发TIM2
//屏蔽所有外部中断
EXTI->
IMR=0;
if(LS1)
{
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);
Motor1_OriginFlag=1;
}
if(LS2)
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);
Motor2_OriginFlag=1;
if(LS3)
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);
Motor3_OriginFlag=1;
Delay_Ms(1000);
//开启所有外部中断
IMR=0x07;
while(!
Motor1_OriginFlag|!
Motor2_OriginFlag|!
Motor3_OriginFlag)
TIM1_TIM2_PWM(10003200);
//让没有回到原位的导轨滑块回到原位用于初始状态的调平基准
TIM_Cmd(TIM1DISABLE);
//关闭TIM1
TIM1->
CNT=0;
//定时器1当前计数值清零
TIM2->
//定时器2当前计数值清零
Motor1_UP;
//电机1上升
Motor2_UP;
//电机2上升
Motor3_UP;
//电机3上升
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Enable);
//使能TIM2->
CH1通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Enable);
CH2通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Enable);
CH3通道PWM输出
TIM_ITConfig(TIM1TIM_IT_UpdateENABLE);
//开启TIM1中断
Motor_Arrived=0;
TIM1_TIM2_PWM(10001600);
Motor_Arrived);
TIM1_TIM2_PWM(7003200);
Motor1_DOWN;
//电机1下降
Motor2_DOWN;
//电机2下降
Motor3_DOWN;
//电机3下降
TIM1_TIM2_PWM(5001600);
//====================================F1=========================================
//关闭TIM2->
//使能TIM2->
TIM1_TIM2_PWM(1500500);
//2#电机上升
delay_ms(2000);
TIM1_TIM2_PWM(15001500);
//1#电机上升
delay_ms(3000);
//====================================F2=========================================
TIM1_TIM2_PWM(15002000);
//2#电机下降
//3#电机上升
TIM1_TIM2_PWM(15002500);
//1#电机下降
//===================================F3=========================================
TIM1_TIM2_PWM(15003000);
//====================================F4=========================================
//====================================F5=========================================
//====================================F6=========================================
//================================F7回原位======================================
for(i=0;
i<
5;
++i)
BUZZER=!
BUZZER;
delay_ms(200);
while
(1)
{
LED2=1;
LED3=1;
LED1=1;
delay_ms(1000);
LED1=0;
}
//==========================子程序入口=================================
*@brief时钟配置
*@paramNone
*@retvalNone
*/
//外部晶振为8MPLLCLK=SYSCLK=72MHCLK=72MP2CLK=72MP1CLK=36MADCCLK=36MUSBCLK=48MTIMCLK=72M
voidRCC_Configuration(void)
ErrorStatusHSEStartUpStatus;
//定义错误状态枚举变量ERRORSUCCESS
RCC_DeInit();
//RCC寄存器重新设置为默认值
RCC_HSEConfig(RCC_HSE_ON);
//打开外部高速时钟晶振
HSEStartUpStatus=RCC_WaitForHSEStartUp();
//等待外部高速时钟工作
if(HSEStartUpStatus==SUCCESS)
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//设置AHB不分频HCLK=SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
//设置APB2不分频P2CLK=HCLK
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置APB1为2分频P1CLK=HCLK/2
FLASH_SetLatency(FLASH_Latency_2);
//设置FLASH代码延时
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//设置预取指缓存
RCC_PLLConfig(RCC_PLLSource_HSE_Div1RCC_PLLMul_9);
//设置PLL时钟外部时钟不分频为HSE的9倍频8MhZ*9=72MHZ
RCC_PLLCmd(ENABLE);
//使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
//等待PLL准备就绪
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//设置PLL为系统时钟源
while(RCC_GetSYSCLKSource()!
=0x08);
//判断PLL是否为系统时钟源
*@briefRTC实时时钟配置
voidRTC_configuration(void)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWRENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
PWR_BackupAccessCmd(DISABLE);
*@briefSTM32主板按键查询函数
voidKeyRe