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, see <http://www.gnu.org/licenses/>.
19 #include <fx2macros.h>
22 void delay(WORD millis) {
24 * It takes 12 crystal pulses to make 1 machine cycle (8051.com)
26 * 83.3 ns at 48mhz per instruction cycle
27 * (assume 166.6ns at 24mhz)
28 * (assume 333.3ns at 12mhz)
30 * Includes the cycles for each instruction
33 volatile WORD count; // NOTE perhaps use different solutions w/ out volatile
36 // set count to the number of times we need to
37 // go around a loop for 1 millisecond
39 // then do that loop millis times. (1000 us=1ms)
41 // 48mhz: 1000 us / (17 cycles * 83.3 ns / cycle / 1000 ns/us) = 706
44 // recalculate if the number of cycles changes
45 // like if you change the loop below
46 loop_count = CPUFREQ == CLK_12M ? 177 :
47 CPUFREQ == CLK_24M ? 353 : 706;
49 // sdcc generated assembly
53 ; delay.c:32: } while ( --count );
54 2 dec _delay_count_1_1
56 4 cjne a,_delay_count_1_1,00121$
57 2 dec (_delay_count_1_1 + 1)
59 2 mov a,_delay_count_1_1
60 2 orl a,(_delay_count_1_1 + 1)