OSDN Git Service

[BUILD] Zap compiler warinigs.
[openi2cradio/OpenI2CRadio.git] / idle.c
diff --git a/idle.c b/idle.c
index 4959b88..ae8db1d 100644 (file)
--- a/idle.c
+++ b/idle.c
 
 void idle_init(void)
 {
-#if defined(__SDCC)
-   INTCON = INTCON & ~(_TMR0IF |  _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
-   INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
-#else
-   INTCON = INTCON & ~(_INTCON_TMR0IF_MASK |  _INTCON_RBIF_MASK); // Enable tmr0 as interrupt and clear interrupt flags.
-   INTCON2bits.TMR0IP = 0; // Interrupt is lower.
-#endif
+    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.
 }
 
@@ -57,32 +59,28 @@ void stop_idle(void)
 
 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 */
+   /* Set TMR0 for interrupt*/
+   /* 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; // 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) */
-#if defined(__SDCC)
-   contword = _T0PS0 | _T0PS1 | _T0PS2 | _TMR0ON; // Prescaler = 1:256.
-#else
-   contword = _T0CON_T0PS0_MASK | _T0CON_T0PS1_MASK | _T0CON_T0PS2_MASK | _T0CON_TMR0ON_MASK; // Prescaler = 1:256.
-#endif   //contword =  _T0PS2 | _TMR0ON; // Pre-scakler is 1:32.
-   //TMR0H = initial >> 8;
-   i = initial;
+   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.
+   } while(INTCONbits.TMR0IF == 0); // Dead area : 0-2.
+   INTCONbits.TMR0IF = 0;
    WDTCONbits.SWDTEN = 1; // WDT ON.
 }
 
@@ -90,15 +88,15 @@ void idle(unsigned int initial)
 void idle_time_ms(unsigned int ms)
 {
     unsigned int tim;
-    unsigned char upper;
+    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);
 }
@@ -108,11 +106,12 @@ void idle_time_62_5ms(void)
     // 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);
+    idle_time_ms(35);
 }