当前位置:首页 > 舵机控制程序要点 - 图文
TH0=-(2700-PWM_Value[3])/256; TL0=-(2700-PWM_Value[3])%6; break; case 9: PWM_OUT4=1;
TH0=-PWM_Value[4]/256; TL0=-PWM_Value[4]%6; break; case 10: PWM_OUT4=0;
TH0=-(2700-PWM_Value[4])/256; TL0=-(2700-PWM_Value[4])%6; break; case 11: PWM_OUT5=1;
TH0=-PWM_Value[5]/256; TL0=-PWM_Value[5]%6; break; case 12: PWM_OUT5=0;
TH0=-(2700-PWM_Value[5])/256; TL0=-(2700-PWM_Value[5])%6; break; case 13: PWM_OUT6=1;
TH0=-PWM_Value[6]/256; TL0=-PWM_Value[6]%6; break; case 14: PWM_OUT6=0;
TH0=-(2700-PWM_Value[6])/256; TL0=-(2700-PWM_Value[6])%6; break; case 15: PWM_OUT7=1;
TH0=-PWM_Value[7]/256; TL0=-PWM_Value[7]%6; break; case 16: PWM_OUT7=0; order1=0;
TH0=-(2700-PWM_Value[7])/256; TL0=-(2700-PWM_Value[7])%6; order1=0; break;
default : order1=0; }
order1++; }
/*=================================================================================== 初始化中断
=====================================================================================*/ void InitPWM(void) {
order1=1; TMOD |=0x11; TH0=-1500/256; TL0=-1500%6; EA=1; EX0=0;
ET0=1; TR0=1;PT0=1;PX0=0; }
void delay(void) {
uint16 i=100; while(i--); }
void main(void) {
InitPWM(); while(1) {
if(key1==0) {
if(PWM_Value[0]<2500) PWM_Value[0]++; }
if(key2==0) {
if(PWM_Value[0]>500) PWM_Value[0]--; }
delay(); } }
单片机舵机控制程序
# include
# define uchar unsigned char # define uint unsigned int uint a,b,c,d,n; sbit p12=P1^2; sbit p13=P1^3; sbit p37=P3^7;
void timer0(void) interrupt 1 using 1 {p12=!p12; c=20000-c; TH0=-(c/256); TL0=-(c%6); if(c>=500&&c<=2500) c=a; else c=20000-a; }
void delay() { uint i;
for(i=0;i<200;i++) { } }
void init_serialcomm(void) {
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr TMOD |= 0x21; //TMOD: timer 1, mode 2, 8-bit reload PCON |= 0x80; //SMOD=1;
TH1 = 0xF4; //Baud:4800 fosc=11.0592MHz IE |= 0x93; //Enable Serial Interrupt TR1 = 1; // timer 1 run // TI=1; }
void serial () interrupt 4 using 3 { if(RI) {
RI = 0; b=SBUF;
SBUF=0xff; while(TI==0); TI=0; } }
void main(void) {//TMOD=0x21; init_serialcomm(); p12=1; a=1500; c=a;
TH0=-(a/256); TL0=-(a%6); PX0=0;PT0=1; TR0=1;
while(1) { a=b*10; } }
舵机控制程序(改变a值可控制任意角度)
#include
#define uchar unsigned char #define uint unsigned int uint a,c; sbit p10=P1^0; sbit p11=P1^1;
void timer0(void) interrupt 1 {p10=!p10; p11=!p11; c=20000-c; TH0=-(c/256); TL0=-(c%6); if(c>=500&&c<=2500) c=a; else
c=20000-a; }
共分享92篇相关文档