1、GPIOx.hNVIC.hUSARTx.h/* 宏定义 -*/#define uchar unsigned char#define uint unsigned int#define Motor1_SUB 16 /电机1 16细分#define Motor2_SUB 16 /电机2 16细分#define Motor3_SUB 16 /电机3 16细分#define Motor1_DIR PAout( 4 ) /电机1 方向#define Motor2_DIR PAout( 5 ) /电机2 方向#define Motor3_DIR PAout( 7 ) /电机3 方向#define Motor
2、1_UP Motor1_DIR = 1 /电机1 上升#define Motor2_UP Motor2_DIR = 1 /电机2 上升#define Motor3_UP Motor3_DIR = 1 /电机3 上升#define Motor1_DOWN Motor1_DIR = 0 /电机1 下降#define Motor2_DOWN Motor2_DIR = 0 /电机2 下降#define Motor3_DOWN Motor3_DIR = 0 /电机3 下降/* 变量定义 -*/u8 Delay_1s_Finishflag = 0;u8 KeyTrg = 0; /按键触发u8 KeyCon
3、t = 0; /按键长按uint i; u16 EXIT0_CNT = 0;u8 Motor1_OriginFlag = 0; /电机1 原点标识u8 Motor2_OriginFlag = 0;u8 Motor3_OriginFlag = 0; /电机1 原点标识 u8 Motor_Arrived = 0; /电机 到达标识/* 函数声明 -*/void RTC_configuration( void );void RCC_Configuration( void );void KeyRead( void );void Delay_Ms( u16 time );void delay_1s( u
4、int time );/= MAIN =int main( void ) u16 i = 0; BUZZER = 0;/蜂鸣器复位 GPIOE - ODR &= 0xffff0000; /LED1 LED8 = 0; RCC_Configuration();/ RTC_configuration(); GPIO_Configuration(); NVIC_Configuration(); EXTI_Configuration(); Motor_Init( 0 0 0 );/TIM1关闭状态 不会触发TIM2 /屏蔽所有外部中断 EXTI - IMR = 0; if( LS1 ) TIM_CCx
5、Cmd( TIM2 TIM_Channel_1 TIM_CCx_Disable ); Motor1_OriginFlag = 1; if( LS2 ) TIM_CCxCmd( TIM2 TIM_Channel_2 TIM_CCx_Disable ); Motor2_OriginFlag = 1; if( LS3 ) TIM_CCxCmd( TIM2 TIM_Channel_3 TIM_CCx_Disable ); Motor3_OriginFlag = 1; Delay_Ms( 1000 ); /开启所有外部中断 IMR = 0x07; while( !Motor1_OriginFlag |
6、!Motor2_OriginFlag | !Motor3_OriginFlag ) TIM1_TIM2_PWM( 1000 3200 );/让没有回到原位的导轨滑块回到原位 用于初始状态的调平基准 TIM_Cmd( TIM1 DISABLE );/关闭TIM1 TIM1 - CNT = 0;/定时器1 当前计数值清零 TIM2 -/定时器2 当前计数值清零 Motor1_UP;/电机1 上升 Motor2_UP;/电机2 上升 Motor3_UP;/电机3 上升 TIM_CCxCmd( TIM2 TIM_Channel_1 TIM_CCx_Enable );/使能 TIM2 - CH1通道PW
7、M输出 TIM_CCxCmd( TIM2 TIM_Channel_2 TIM_CCx_Enable ); CH2通道PWM输出 TIM_CCxCmd( TIM2 TIM_Channel_3 TIM_CCx_Enable ); CH3通道PWM输出 TIM_ITConfig( TIM1 TIM_IT_Update ENABLE ); /开启TIM1中断 Motor_Arrived = 0; TIM1_TIM2_PWM( 1000 1600 );Motor_Arrived ); TIM1_TIM2_PWM( 700 3200 ); Motor1_DOWN;/电机1 下降 Motor2_DOWN;/
8、电机2 下降 Motor3_DOWN;/电机3 下降 TIM1_TIM2_PWM( 500 1600 );/= F1 = /关闭 TIM2 - /使能 TIM2 - TIM1_TIM2_PWM( 1500 500 );/2#电机上升 delay_ms( 2000 ); TIM1_TIM2_PWM( 1500 1500 );/1#电机上升 delay_ms( 3000 );/= F2 = TIM1_TIM2_PWM( 1500 2000 );/2#电机下降/3#电机上升 TIM1_TIM2_PWM( 1500 2500 );/1#电机下降 /= F3 = TIM1_TIM2_PWM( 1500
9、3000 );/= 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 时钟配置 * param None * retval None */外部晶振为8M PLLCLK=SYSCLK=72M HCLK=72M P2CLK=72M P1CLK=36M ADCCLK=36M USBCLK=48M TIMCLK
10、=72Mvoid RCC_Configuration( void ) ErrorStatus HSEStartUpStatus; /定义错误状态枚举变量ERROR SUCCESS RCC_DeInit(); /RCC寄存器重新设置为默认值 RCC_HSEConfig( RCC_HSE_ON ); /打开外部高速时钟晶振 HSEStartUpStatus = RCC_WaitForHSEStartUp();/等待外部高速时钟工作 if(HSEStartUpStatus = SUCCESS) RCC_HCLKConfig( RCC_SYSCLK_Div1 ); /设置AHB不分频 HCLK = S
11、YSCLK 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_Div1 RCC_PLLMul_9 ); /设置PL
12、L时钟外部时钟不分频为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是否为系统时钟源 * brief RTC实时时钟配置void RTC_configuration( void ) RCC_APB1Perip
13、hClockCmd(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); * brief STM32主板按键 查询函数void KeyRe
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1