stm32三自由度机器人c语言程序代码.docx
- 文档编号:10612272
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:101
- 大小:32.52KB
stm32三自由度机器人c语言程序代码.docx
《stm32三自由度机器人c语言程序代码.docx》由会员分享,可在线阅读,更多相关《stm32三自由度机器人c语言程序代码.docx(101页珍藏版)》请在冰豆网上搜索。
stm32三自由度机器人c语言程序代码
stm32三自由度机器人c语言程序代码
main.c
/**
******************************************************************************
*@fileGPIO/IOToggle/main.c
*@authorMCDApplicationTeam
*@versionV3.5.0
*@date08-April-2011
*@briefMainprogrambody.
******************************************************************************
*@attention
*
******************************************************************************
*/
/*Includes------------------------------------------------------------------*/
#include"stm32f10x.h"
#include
#include"main.h"
#include"delay.h"
#include"DMA.h"
#include"TIMx.h"
#include"GPIOx.h"
#include"NVIC.h"
#include"USARTx.h"
/*宏定义------------------------------------------------------------------*/
#defineucharunsignedchar
#defineuintunsignedint
#defineMotor1_SUB16
#defineMotor2_SUB16
#defineMotor3_SUB16
//电机
//电机
//电机
116细分
216细分
316细分
#defineMotor1_DIRPAout(4)
//电机1
方向
#defineMotor2_DIRPAout(5)
//电机
2方向
#defineMotor3_DIRPAout(7)
//电机
3方向
#defineMotor1_UPMotor1_DIR=1#defineMotor2_UPMotor2_DIR=1#defineMotor3_UPMotor3_DIR=1
//电机//电机//电机
1上升
2上升
3上升
#defineMotor1_DOWNMotor1_DIR=0
//
电机1下降
#defineMotor2_DOWNMotor2_DIR=0
//
电机2下降
#defineMotor3_DOWNMotor3_DIR=0
//
电机3下降
/*
变量定义------------------------------------------------------------------
*/
u8
Delay_1s_Finishflag=0;
u8
KeyTrg=0;
//按键触发
u8
KeyCont=0;//按键长按
uinti;
u16EXIT0_CNT=0;
u8Motor1_OriginFlag=0;//电机1原点标识
u8Motor2_OriginFlag=0;//电机1原点标识
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);
//开启所有外部中断
EXTI->IMR=0x07;
while(!
Motor1_OriginFlag|!
Motor2_OriginFlag|!
Motor3_OriginFlag)
{
TIM1_TIM2_PWM(10003200);//让没有回到原位的导轨滑块回到原位用于初始状态的
调平基准
}
//屏蔽所有外部中断
EXTI->IMR=0;
TIM_Cmd(TIM1DISABLE);//关闭TIM1
TIM1->CNT=0;//定时器1当前计数值清零
TIM2->CNT=0;//定时器2当前计数值清零
Delay_Ms(1000);
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);//
使能
TIM2->CH2
通道
PWM
输
出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Enable);//
使能
TIM2->CH3
通道
PWM
输
出
TIM_ITConfig(TIM1TIM_IT_UpdateENABLE);//
开启
TIM1
中断
Motor_Arrived=0;
TIM1_TIM2_PWM(10001600);
while(!
Motor_Arrived);
Motor_Arrived=0;
TIM1_TIM2_PWM(7003200);
while(!
Motor_Arrived);
Motor1_DOWN;//电机1下降
Motor2_DOWN;//电机2下降
Motor3_DOWN;//电机3下降
Motor_Arrived=0;
TIM1_TIM2_PWM(5001600);
while(!
Motor_Arrived);
//====================================F1
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);
//
关闭
TIM2->CH1
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Enable);
//
使能
TIM2->CH2
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);
//
关闭
TIM2->CH3
通道
PWM
输出
Motor_Arrived=0;
Motor2_UP;//电机2上升
TIM1_TIM2_PWM(1500500);//2#
while(!
Motor_Arrived);
delay_ms(2000);
电机上升
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Enable);
//
使能
TIM2->CH1
通道
PWM
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);
//
关闭
TIM2->CH2
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);
//
关闭
TIM2->CH3
通道
PWM
输出
Motor_Arrived=0;
Motor1_UP;//电机1上升
TIM1_TIM2_PWM(15001500);//1#电机上升
while(!
Motor_Arrived);
delay_ms(3000);
//====================================F2
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);//关闭TIM2->CH1通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Enable);//使能TIM2->CH2通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);//关闭TIM2->CH3通道PWM
输出
Motor_Arrived=0;
Motor2_DOWN;//电机2下降
TIM1_TIM2_PWM(15002000);//2#电机下降
while(!
Motor_Arrived);
delay_ms(3000);
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);//关闭TIM2->CH1通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);//关闭TIM2->CH2通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Enable);//使能TIM2->CH3通道PWM
输出
Motor_Arrived=0;
Motor3_UP;//电机3上升
TIM1_TIM2_PWM(15001500);//3#电机上升
while(!
Motor_Arrived);
delay_ms(3000);
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Enable);//使能TIM2->CH1通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);//关闭TIM2->CH2通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);//关闭TIM2->CH3通道PWM
输出
Motor_Arrived=0;
Motor1_DOWN;//电机1下降
TIM1_TIM2_PWM(15002500);//1#电机下降
while(!
Motor_Arrived);
delay_ms(3000);
//===================================F3
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);
//
关闭
TIM2->CH1
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Enable);
//
使能
TIM2->CH2
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);
//
关闭
TIM2->CH3
通道
PWM
输出
Motor_Arrived=0;
Motor2_UP;//电机2上升
TIM1_TIM2_PWM(15003000);//2#
while(!
Motor_Arrived);
delay_ms(2000);
电机上升
//====================================F4
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Enable);
//
使能
TIM2->CH1
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);
//
关闭
TIM2->CH2
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);
//
关闭
TIM2->CH3
通道
PWM
输出
Motor_Arrived=0;
Motor1_UP;//电机1上升
TIM1_TIM2_PWM(15002500);//1#
while(!
Motor_Arrived);
delay_ms(3000);
电机上升
//====================================F5
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);
//
关闭
TIM2->CH1
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);
//
关闭
TIM2->CH2
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Enable);
//
使能
TIM2->CH3
通道
PWM
输出
Motor_Arrived=0;
Motor3_UP;//电机3上升
TIM1_TIM2_PWM(15002500);//3#
while(!
Motor_Arrived);
delay_ms(3000);
电机上升
//====================================F6
=========================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);
//
关闭
TIM2->CH1
通道
PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Enable);//使能TIM2->CH2通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);//关闭TIM2->CH3通道PWM输出
Motor_Arrived=0;
Motor2_UP;//电机2上升
TIM1_TIM2_PWM(15003000);//2#电机上升
while(!
Motor_Arrived);
delay_ms(2000);
//================================F7回原位
======================================
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Enable);//使能TIM2->CH1通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);//关闭TIM2->CH2通道PWM
输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Disable);//关闭TIM2->CH3通道PWM输出
Motor_Arrived=0;
TIM1_TIM2_PWM(15003000);//1#电机上升
while(!
Motor_Arrived);
TIM_CCxCmd(TIM2TIM_Channel_1TIM_CCx_Disable);//关闭TIM2->CH1通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_2TIM_CCx_Disable);//关闭TIM2->CH2通道PWM输出
TIM_CCxCmd(TIM2TIM_Channel_3TIM_CCx_Enable);//使能TIM2->CH3通道PWM
输出
Motor_Arrived=0;
Motor3_UP;//电机3上升
TIM1_TIM2_PWM(1500500);//3#电机上升
while(!
Motor_Arrived);
delay_ms(2000);
for(i=0;i<5;++i)
{
BUZZER=!
BUZZER;
delay_ms(200);
}
BUZZER=0;//蜂鸣器复位
while
(1)
{
LED2=1;
LED3=1;
LED1=1;
delay_ms(1000);
LED1=0;
delay_ms(1000);
}
}
//==========================子程序入口=================================/**
*@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);//
RCC_PCLK2Config(RCC_HCLK_Div1);//
RCC_PCLK1Config(RCC_HCLK_Div2);//
设置AHB不分频HCLK=SYSCLK
设置APB2不分频P2CLK=HCLK
设置APB1为2分频P1CLK=HCLK/2
FLASH_SetLatency(FLASH_Latency_2);//设置FLASH代码延时
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//设置预取指缓存
RCC_PLLConfig(RCC_PLLSource_HSE_Div1RCC_PLLMul_9);//
钟不分频为HSE的9倍频8MhZ*9=72MHZ
RCC_PLLCmd(ENABLE);//使能PL
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- stm32 自由度 机器人 语言 程序代码