-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo.c
51 lines (50 loc) · 1.58 KB
/
servo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* SG90 Servo driver cycle -90 degree to +90 degree */
void main(void)
{
unsigned int *tcnt,*toc2,delay1, delay2,dwait;
unsigned char *tflg1,*tctl1,*pgddr,*pgdr;
tctl1=(unsigned char*)0x20;
tflg1=(unsigned char*)0x23;
toc2=(unsigned int*)0x18;
tcnt=(unsigned int*)0x0e;
pgddr=(unsigned char*)0x3;
pgdr=(unsigned char*)0x02;
/* Define Port G as all outputs */
*pgddr =0xff;
/* Allow Port A not be used by timer */
*tctl1=0x00;
/* Repeat forever */
for(;;)
{
/* Need to physical send a PWM for a fixed time */
for (dwait = 0;dwait <= 0xf;dwait++)
{
/* Move forward + 90 degrees*/
delay1 = 0x8ac0; /* off (Space) time */
delay2 = 0x0fa0; /* on (Mark) time */
*pgdr = 0x00; /* Turn PWM signal off */
*tflg1=0x40; /*Clear TOC2 Flag*/
*toc2=*tcnt+delay1; /*Read timer and add offset period*/
while(((*tflg1)&0x40)==0); /*Wait for TOC2 FLAG */
*pgdr = 0x1; /* Turn PWM signal on */
*tflg1=0x40; /*Clear TOC2 Flag*/
*toc2=*tcnt+delay2; /*Read timer and add offset period */
while(((*tflg1)&0x40)==0); /*Wait for TOC2 FLAG */
}
/* Need to physical send a PWM for a fixed time */
for (dwait = 0;dwait <= 0xf;dwait++)
{
/* Move back -90 degrees*/
delay1 = 0x9470; /* off (Space) time */
delay2 = 0x07d0; /* on (Mark) time */
*pgdr = 0x00; /* Turn PWM signal off */
*tflg1=0x40; /*Clear TOC2 Flag*/
*toc2=*tcnt+delay1; /*Read timer and add offset period*/
while(((*tflg1)&0x40)==0); /*Wait for TOC2 FLAG */
*pgdr = 0x1; /* Turn PWM signal on */
*tflg1=0x40; /*Clear TOC2 Flag*/
*toc2=*tcnt+delay2; /*Read timer and add offset period */
while(((*tflg1)&0x40)==0); /*Wait for TOC2 FLAG */
}
}
}