| ??? 01/19/10 09:07 Read: times |
#172587 - PWM IRQ code in C, lookup table, 30 byte Responding to: ???'s previous message |
Having the lookup table in xdata memory turns out to be shorter than having it in pdata memory:)
#include <8052.h>
static unsigned char ms_tenth_countdown;
// dynamically setup table.
// Indexed top down.
// setup this table glitchlessly (IRQ save).
// (located absolutely at the start of a 256 byte block)
unsigned char __xdata __at(0x0000) table_P2_over_time[200 + 1];
void Timer0_ISR (void) interrupt 1 {
// assuming P2 is only used for servos.
P2 = table_P2_over_time [ ms_tenth_countdown ];
if (!--ms_tenth_countdown)
ms_tenth_countdown = 200;
TF0 = 0;
}
void main(void){}
gives:
; File Created by SDCC : free open source ANSI-C Compiler
; Version 2.9.5 #5614 (Jan 4 2010) (UNIX)
0000 370 _Timer0_ISR:
0000 C0 E0 379 push acc
0002 C0 82 380 push dpl
0004 C0 83 381 push dph
382 ; servo_lookup.c:14: P2 = table_P2_over_time [ ms_tenth_countdown ];
0006 85*00 82 383 mov dpl,_ms_tenth_countdown
0009 75 83 00 384 mov dph,#(_table_P2_over_time >> 8)
000C E0 385 movx a,@dptr
000D F5 A0 386 mov _P2,a
387 ; servo_lookup.c:16: if (!--ms_tenth_countdown)
000F D5*00 03 388 djnz _ms_tenth_countdown,00102$
389 ; servo_lookup.c:17: ms_tenth_countdown = 200;
0012 75*00 C8 390 mov _ms_tenth_countdown,#0xC8
0015 391 00102$:
392 ; servo_lookup.c:19: TF0 = 0;
0015 C2 8D 393 clr _TF0
0017 D0 83 394 pop dph
0019 D0 82 395 pop dpl
001B D0 E0 396 pop acc
001D 32 397 reti
guess that's hard to beat!) |



