使用pwm驱动舵机转动至不同角度
main.c:
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "pwm.h"
int main(void)
{
u16 out_led0pwmval=1950;
//u8 dir=1;
u16 led0pwmval=1850;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168); //³õʼ»¯ÑÓʱº¯Êý
uart_init(115200);//³õʼ»¯´®¿Ú²¨ÌØÂÊΪ115200
TIM14_PWM_Init(2000-1,840-1); //84M/84=1MhzµÄ¼ÆÊýƵÂÊ,ÖØ×°ÔØÖµ500£¬ËùÒÔPWMƵÂÊΪ 1M/500=2Khz. --90-90-=0=-0900-0==
while(1)
{
delay_ms(10);
if(led0pwmval1900){out_led0pwmval--;TIM_SetCompare1(TIM14,out_led0pwmval);}
if(1900>led0pwmval >0){out_led0pwmval--;TIM_SetCompare1(TIM14,out_led0pwmval);}
if(out_led0pwmval==0){led0pwmval=1900;out_led0pwmval=1950;} //Ð޸ıȽÏÖµ£¬ÐÞ¸ÄÕ¼¿Õ±È
}
}
}
转动角度可自行设置,知道原理便可举一反三!加油!
1