2 * Copyright (C) 2009 Ubixum, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <fx2macros.h>
23 void delay(WORD millis) {
25 * It takes 12 crystal pulses to make 1 machine cycle (8051.com)
27 * 83.3 ns at 48mhz per instruction cycle
28 * (assume 166.6ns at 24mhz)
29 * (assume 333.3ns at 12mhz)
31 * Includes the cycles for each instruction
34 volatile WORD count; // NOTE perhaps use different solutions w/ out volatile
37 // set count to the number of times we need to
38 // go around a loop for 1 millisecond
40 // then do that loop millis times. (1000 us=1ms)
42 // 48mhz: 1000 us / (17 cycles * 83.3 ns / cycle / 1000 ns/us) = 706
45 // recalculate if the number of cycles changes
46 // like if you change the loop below
47 loop_count = CPUFREQ == CLK_12M ? 177 :
48 CPUFREQ == CLK_24M ? 353 : 706;
50 // sdcc generated assembly
54 ; delay.c:32: } while ( --count );
55 2 dec _delay_count_1_1
57 4 cjne a,_delay_count_1_1,00121$
58 2 dec (_delay_count_1_1 + 1)
60 2 mov a,_delay_count_1_1
61 2 orl a,(_delay_count_1_1 + 1)