新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)分時(shí)控制8路舵機(jī)程序

51單片機(jī)分時(shí)控制8路舵機(jī)程序

作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
本程序中用到的頭文件 12c5a.H下載:http://www.51hei.com/f/12c5a.rar

//分時(shí)控制舵機(jī),采用的單片機(jī)是STC12c5a 51增強(qiáng)型單片機(jī),晶振11.0592MHZ
#include <12c5a.H>

本文引用地址:http://m.butianyuan.cn/article/201612/324193.htm

void delay(uint16 time);
void Timer_init();
void Timer0(uint32 us);

uint16 xdata pwm[8]={1500,1500,1500,1500,1500,1500,1500,1500};

sbit pwm1=P0^0;
sbit pwm2=P0^1;
sbit pwm3=P0^2;
sbit pwm4=P0^3;
sbit pwm5=P0^4;
sbit pwm6=P0^5;
sbit pwm7=P0^6;
sbit pwm8=P0^7;

void main()
{
P0M1=0;
P0M0=0XFF;

Timer_init();
Timer0(100);
delay(100);
while(1)
{
;
}
}
void delay(uint16 time)
{
uint16 i;
uint16 j;
for(i=0;i<1000;i++)
for(j=0;j}
void Timer_init()
{
EA=1; //開(kāi)總中斷
AUXR|=0xC0; //T0,T1工作在1T
TMOD|= 0x11; //T0工作在方式1,16位
ET0 = 1; //開(kāi)定時(shí)器0中斷
}
void Timer0(uint32 us)
{
uint32 valu;
valu=us*11;
valu=valu;
valu=0xffff-valu;

TH0=valu>>8;
TL0=valu;
TR0 = 1;
}
void T0zd(void) interrupt 1
{
static uint8 i=1;
switch(i)
{
case 1:
{
pwm1=1;
Timer0(pwm[0]);
} break;
case 2:
{
pwm1=0;
Timer0(2500-pwm[0]);
} break;
case 3:
{
pwm2=1;
Timer0(pwm[1]);
} break;
case 4:
{
pwm2=0;
Timer0(2500-pwm[1]);
} break;
case 5:
{
pwm3=1;
Timer0(pwm[2]);
} break;
case 6:
{
pwm3=0;
Timer0(2500-pwm[2]);
} break;
case 7:
{
pwm4=1;
Timer0(pwm[3]);
} break;
case 8:
{
pwm4=0;
Timer0(2500-pwm[3]);
} break;
case 9:
{
pwm5=1;
Timer0(pwm[4]);
} break;
case 10:
{
pwm5=0;
Timer0(2500-pwm[4]);
} break;
case 11:
{
pwm6=1;
Timer0(pwm[5]);
} break;
case 12:
{
pwm6=0;
Timer0(2500-pwm[5]);
} break;

case 13:
{
pwm7=1;
Timer0(pwm[6]);
} break;
case 14:
{
pwm7=0;
Timer0(2500-pwm[6]);
} break;

case 15:
{
pwm8=1;
Timer0(pwm[7]);
} break;
case 16:
{
pwm8=0;
Timer0(2500-pwm[7]);
i=0;
} break;

default:break;

}

i++;
}



評(píng)論


技術(shù)專區(qū)

關(guān)閉