* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*/
+#if defined(__SDCC)
#include <sdcc-lib.h>
#include <pic18fregs.h> /* ONLY FOR PIC18x */
+#else
+#include <htc.h>
+#endif
#include <signal.h>
#include "iodef.h"
void idle_init(void)
{
- INTCON = INTCON & ~(_TMR0IF | _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
- INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
+ RCONbits.IPEN = 1;
+ INTCONbits.GIE = 1;
+ INTCONbits.TMR0IF = 0;
+ INTCONbits.TMR0IE = 1;
+// INTCONbits.RBIF = 0;
+ INTCON2bits.TMR0IP = 0;
+// INTCON = INTCON & ~(_TMR0IF | _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
+// INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
WDTCON = 1; // OK? WDT=Disabled.
}
void idle(unsigned int initial)
{
- unsigned char osccon;
- unsigned char contword;
- unsigned int i;
+ unsigned int i;
WDTCONbits.SWDTEN = 0; // Lame WDT OFF.
/* Enable IDLE */
- osccon = OSCCON;
- osccon = osccon | _IDLEN;
- OSCCON = osccon;
- INTCON &= ~_TMR0IF;
- INTCON |= _TMR0IE; // Enable tmr0 as interrupt and clear interrupt flags.
-
/* Set TMR0 for interrupt*/
- /* Pre-scaler: 1/16, PSA=0(ON), TOSE=0, T0CS=0(INTERNAL), T08BIT=0(16bit), TMR0ON=1(START) */
- contword = _T0PS0 | _T0PS1 | _T0PS2 | _TMR0ON; // Prescaler = 1:256.
- //contword = _T0PS2 | _TMR0ON; // Pre-scakler is 1:32.
- //TMR0H = initial >> 8;
- i = initial;
+ /* Pre-scaler: 1/2, PSA=1(ON), TOSE=0, T0CS=0(INTERNAL), T08BIT=0(16bit), TMR0ON=1(START) */
+ /* 1Tick = 1/1000 ms*/
+ T0CON = 0b00001000; // 1/2
+// T0CON = 0b00001010; // 1/8
+ OSCCONbits.IDLEN = 1;
+ INTCONbits.TMR0IF = 0;
+ INTCONbits.TMR0IE = 1;
+// TMR0L = initial & 0xff;
+// TMR0H = initial >> 8; // Write order : L->H
+ TMR0H = initial >> 8; // Write order : H->L
TMR0L = initial & 0xff;
- TMR0H = initial >> 8; // Write order : L->H
- T0CON = contword;
- do {
+ T0CONbits.TMR0ON = 1; // Start
+ do {
Sleep();
- i = TMR0H << 8 + TMR0L; // Check if IDLE-Timer was elapsed.
- } while(i < 3); // Dead area : 0-2.
+// i = TMR0H << 8 + TMR0L; // Check if IDLE-Timer was elapsed.
+ } while(INTCONbits.TMR0IF == 0); // Dead area : 0-2.
+ INTCONbits.TMR0IF = 0;
WDTCONbits.SWDTEN = 1; // WDT ON.
}
void idle_time_ms(unsigned int ms)
{
unsigned int tim;
- unsigned char upper;
- unsigned char i;
+ unsigned int upper;
if(ms == 0) return;
- upper = (ms & 0xe000) >> 13;
-// unsigned int upper;
- tim = ms << 3 - ms >> 4 - ms >>3;
- tim = 65535 - tim + 1; // tim = 65536 - tim;
+ upper = (ms & 0xffc0) >> 6;
+ tim = (ms & 0x3f) * 1000; //
+ tim = (65535 - tim) + 1; // tim = 65536 - tim;
while(upper > 0) {
- idle(0x0000); // Upper is 65536 tick.
+ idle(65535 - 64000 + 1); // Upper is 512ms
+ upper--;
}
idle(tim);
}
// Tim = 1ms * 64 - 1ms - 0.5ms
// Tim = 0.128ms * (488 + 2.2)
// =
- idle(65535 - 488 + 4);
+ idle_time_ms(62); // 62ms
+ idle(500); // 500us
}
void idle_time_35ms(void)
{
// Tim = 35 / 0.128 = 273.44
- idle(65535 - 274 + 1);
-}
\ No newline at end of file
+ idle_time_ms(35);
+}