使用TIM1的CH1输出PWM信号,使用定时器的PWM输出功能,通过按键调整电机转向及转速
单片机源程序如下:
- #include "bsp_led.h"
- #include "systick.h"
- #include "bsp_usart.h"
- #include "bsp_motor.h"
- #include "bsp_AdvanceTim.h"
- #include "bsp_key.h"
- #include "stdio.h"
- u16 temp = 200;
- int main(void)
- {
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
- KEY_GPIO_Config();
- Motor_GPIO_Init();
- ADVANCE_TIM_GPIO_Config();
- ADVANCE_TIM_Mode_Config();
- while(1)
- {
-
- if(Key_Scan(KEY_GPIO_PORT,KEY1_GPIO_PIN)==KEY_ON)
- {
- Motor_Backward();
- }
- if(Key_Scan(KEY_GPIO_PORT,KEY2_GPIO_PIN)==KEY_ON)
- {
- Motor_Forward();
- }
- if(Key_Scan(KEY_GPIO_PORT,KEY3_GPIO_PIN)==KEY_ON)
- {
- if(temp>0)
- temp-=100;
- else
- temp = 0;
- }
- if(Key_Scan(KEY_GPIO_PORT,KEY4_GPIO_PIN)==KEY_ON)
- {
- if(temp<1000)
- temp+=100;
- else
- temp = 999;
- }
- TIM_SetCompare1(TIM1,temp);
- }
- }
-
复制代码
Keil代码与Proteus仿真下载: 求指导
工程1.7z
(247.5 KB, 下载次数: 11)
|