|
我想寫一個8051的c program,透過vb和電腦的rs232,
來同時控制6-9個servo,但8051的兩個timer,一個給rs232用了,而另一個則用來做約20ms一次的interrupt
那servo那兒的pwm應該怎麼寫?
在沒有外接pwm晶片的情況下怎樣可以在20ms的週期中輸出0.5ms-2.5ms的pwm給不同的servo?
我是一名新手,
這點一直都想不通......希望各位大大幫忙解困........
/* ser.c */
#include <reg51.h>
sbit wled = P2^3;
sbit spu0 = P2^4;
sbit spu1 = P2^7;
sbit wled2 = P2^6; //S
/* S3003 pulse time */
#define DEA 3 /* servo backward TC */
#define DEB 19 /* servo forkward TC */
#define DEC 12 /* 10 11 12 13 14 15 servo center TC */
#define SERVOS 2
//int timercounter;
//int servocounts[SERVOS];
//int tmp;
int servo0_time;
int servo1_time;
int time,a,b,temp0,temp1,temp2;
unsigned char high,low;
mov_servo(void);
compare_SBUF(void);
/*------------------------------*/
delay(int d)
{
int i, j;
for(i=0; i<d; i++)
for(j=0; j<50; j++) ;
}
/*----------------------------*/
led_bl()
{
int i;
for(i=0; i<2; i++)
{
wled=0; delay(50);
wled=1; delay(50);
}
}
/*------------------------------------------------*/
del() /* 100 uS 0.1 mS delay */
{
char i;
for(i=0; i<50; i++) ;
}
/*---------------------------(s)*/
void t0isr(void) interrupt 1
{
compare_SBUF();
mov_servo();
TH0=high;
TL0=low;
}
void UART_int(void) interrupt 4
{
if (RI==1)
{
RI=0;
temp0=SBUF;
switch(SBUF)
{
case 0: servo0_time=5;
servo1_time=15;
break;
case 1: servo0_time=15;
servo1_time=5;
break;
//case 2:
//case 3:
//case 4:
//case 5:
//case 6:
//case 7:
//case 8:
//case 9:
default: break;
}
temp1=servo0_time;
temp2=servo1_time;
}
}
/*-----------------------*/
mov_servo()
{
//timercounter++;
// for (tmp=0; tmp < SERVOS; tmp++)
// {
// if (timercounter < servocounts[tmp])
// servocounts[tmp]=1;
// else
// servocounts[tmp]=0;
// }
if (servo0_time > 0)
{spu0=1;
servo0_time--;}
else
spu0=0;
if (servo1_time > 0)
{spu1=1;
servo1_time--;}
else
spu1=0;
del();
}
/*------------------(S)*/
compare_SBUF()
{
if (SBUF==temp0)
{
servo0_time=temp1;
servo1_time=temp2;
}
}
/*-----------------(RS232)*/
init_UART()
{
SCON=0x50;
TMOD=0x21;
TH1 =0xFD;
TH0=high=(65536-20000)/256;
TL0=low=(65536-20000)%256;
}
/*----------------(RS232)*/
main()
{
spu0=0;
spu1=0;
led_bl();
init_UART();
ET0=1;
ES=1;
EA=1;
TR1=1;
PS=1;
while(1);
}
/*----------------------*/ |
|