定时器4通道3PB8
PWM周期为20ms
0.5ms---0度
-
-
-
2.5ms--180度
单片机源程序如下:
- #include "stm32f10x.h"
- #include "bsp_led.h"
- #include "bsp_GeneralTim.h"
- #include "key.h"
- #include "systick.h"
- #include <math.h>
- #include <stdlib.h>
- int main(void)
- {
-
- /* led 端口配置 */
- LED_GPIO_Config();
- Key_GPIO_Config();
- /* 定时器初始化 */
- GENERAL_TIM_Init();
- //LED1_TOGGLE;
-
- uint16_t jiaodu=0,pulse=0;
- while(1)
- {
- if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )
- {
- LED1_TOGGLE;
- jiaodu=jiaodu+10;
- }
- if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )
- {
- LED2_TOGGLE;
- jiaodu=jiaodu-10;
- }
- if((jiaodu>0) && (jiaodu<=180))
- {
- pulse=500+(2500-500)/180*jiaodu;
- TIM_SetCompare3(GENERAL_TIM ,pulse);
- }
- else
- {
- jiaodu=0;
- pulse=500+(2500-500)/180*jiaodu;
- TIM_SetCompare3(GENERAL_TIM ,pulse);
- }
- }
- }
- /*********************************************END OF FILE**********************/
复制代码
所有代码51hei提供下载:
按键控制舵机.7z
(182.96 KB, 下载次数: 44)
|