5 * Copyright (C) 2013-06-11 K.Ohta <whatisthis.sowhat ai gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2,
11 * or (at your option) any later version.
12 * This library / program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this library; see the file COPYING. If not, write to the
19 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22 * As a special exception, if you link this(includeed from sdcc) library
23 * with other files, some of which are compiled with SDCC,
24 * to produce an executable, this library does not by itself cause
25 * the resulting executable to be covered by the GNU General Public License.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
31 #include <pic18fregs.h> /* ONLY FOR PIC18x */
44 INTCONbits.TMR0IF = 0;
45 INTCONbits.TMR0IE = 1;
46 // INTCONbits.RBIF = 0;
47 INTCON2bits.TMR0IP = 0;
48 // INTCON = INTCON & ~(_TMR0IF | _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
49 // INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
50 WDTCON = 1; // OK? WDT=Disabled.
60 void idle(unsigned int initial)
64 WDTCONbits.SWDTEN = 0; // Lame WDT OFF.
66 /* Set TMR0 for interrupt*/
67 /* Pre-scaler: 1/2, PSA=1(ON), TOSE=0, T0CS=0(INTERNAL), T08BIT=0(16bit), TMR0ON=1(START) */
68 /* 1Tick = 1/1000 ms*/
69 T0CON = 0b00001000; // 1/2
70 // T0CON = 0b00001010; // 1/8
72 INTCONbits.TMR0IF = 0;
73 INTCONbits.TMR0IE = 1;
74 // TMR0L = initial & 0xff;
75 // TMR0H = initial >> 8; // Write order : L->H
76 TMR0H = initial >> 8; // Write order : H->L
77 TMR0L = initial & 0xff;
78 T0CONbits.TMR0ON = 1; // Start
81 // i = TMR0H << 8 + TMR0L; // Check if IDLE-Timer was elapsed.
82 } while(INTCONbits.TMR0IF == 0); // Dead area : 0-2.
83 INTCONbits.TMR0IF = 0;
84 WDTCONbits.SWDTEN = 1; // WDT ON.
88 void idle_time_ms(unsigned int ms)
94 upper = (ms & 0xffc0) >> 6;
95 tim = (ms & 0x3f) * 1000; //
96 tim = (65535 - tim) + 1; // tim = 65536 - tim;
98 idle(65535 - 64000 + 1); // Upper is 512ms
104 void idle_time_62_5ms(void)
106 // Tim = 1ms * 64 - 1ms - 0.5ms
107 // Tim = 0.128ms * (488 + 2.2)
109 idle_time_ms(62); // 62ms
113 void idle_time_35ms(void)
115 // Tim = 35 / 0.128 = 273.44