OSDN Git Service

[IDLE] Enable idle-mode before kicking tmr0.
[openi2cradio/OpenI2CRadio.git] / idle.c
1 /*
2  * OpenI2CRADIO
3  * Idle routine.
4  * Using timer0.
5  * (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
6  * License: GPL2
7  */
8 #include <sdcc-lib.h>
9 #include <pic18fregs.h> /* ONLY FOR PIC18x */
10 #include <signal.h>
11 #include "iodef.h"
12
13
14 void idle_init(void)
15 {
16    INTCON = _TMR0IE & ~(_TMR0IF | _INT0IF | _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
17    INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
18    WDTCON = 0; // OK? WDT=Disabled.
19 }
20
21
22 void stop_idle(void)
23 {
24     T0CONbits.TMR0ON = 0;
25 }
26
27
28 void idle(unsigned int initial)
29 {
30    unsigned char osccon;
31    unsigned char  contword;
32
33    /* Enable IDLE */
34    osccon = OSCCON;
35    osccon = osccon | _IDLEN |  _SCS1 | _SCS0;
36    OSCCON = osccon;
37    
38    /* Set TMR0 for interrupt*/
39    /* Pre-scaler: 1/16, PSA=0(ON), TOSE=0, T0CS=0(INTERNAL), T08BIT=1(8bit), TMR0ON=1(START) */
40    contword = _T0PS0 | _T0PS1 | _T08BIT | _TMR0ON; // _T0PS2
41    TMR0H = initial >> 8;
42    TMR0L = initial & 0xff;
43    T0CON = contword;
44    
45    Sleep();
46 }