| ??? 01/18/10 11:19 Read: times |
#172542 - PWM IRQ code in C, bitops, 45 byte Responding to: ???'s previous message |
Hi,
you can boil down the code that was cited in the original code from 101 bytes to 45 bytes with: #include <8052.h>
static unsigned char ms_tenth;
static unsigned char servo1_target;
static unsigned char servo2_target;
static unsigned char servo3_target;
void Timer0_ISR (void) interrupt 1 {
P1_2 = (ms_tenth < servo1_target);
P1_3 = (ms_tenth < servo2_target);
P1_4 = (ms_tenth < servo3_target);
if (++ms_tenth == 180)
ms_tenth = 0;
TF0 = 0;
}
void main(void){}
As a bonus the code looks more readable (at least to me:) This is the generated assembler code:
0000 374 _Timer0_ISR:
0000 C0 E0 383 push acc
0002 C0 D0 384 push psw
0004 75 D0 00 385 mov psw,#0x00
386 ; servo_bitops.c:12: P1_2 = (ms_tenth < servo1_target);
0007 C3 387 clr c
0008 E5*00 388 mov a,_ms_tenth
000A 95*01 389 subb a,_servo1_target
000C 92 92 390 mov _P1_2,c
391 ; servo_bitops.c:13: P1_3 = (ms_tenth < servo2_target);
000E C3 392 clr c
000F E5*00 393 mov a,_ms_tenth
0011 95*02 394 subb a,_servo2_target
0013 92 93 395 mov _P1_3,c
396 ; servo_bitops.c:14: P1_4 = (ms_tenth < servo3_target);
0015 C3 397 clr c
0016 E5*00 398 mov a,_ms_tenth
0018 95*03 399 subb a,_servo3_target
001A 92 94 400 mov _P1_4,c
401 ; servo_bitops.c:16: if (++ms_tenth == 180)
001C 05*00 402 inc _ms_tenth
001E 74 B4 403 mov a,#0xB4
0020 B5*00 03 404 cjne a,_ms_tenth,00102$
405 ; servo_bitops.c:17: ms_tenth = 0;
0023 75*00 00 406 mov _ms_tenth,#0x00
0026 407 00102$:
408 ; servo_bitops.c:19: TF0 = 0;
0026 C2 8D 409 clr _TF0
0028 D0 D0 410 pop psw
002A D0 E0 411 pop acc
002C 32 412 reti
Might be enough to allow adding another one or two servos... |



