Hi..how can i do a "sleep" mode for 8051?
O.K. Most examples of sleep code have you do something like set the pcon bit and then just hang. An interrupt will awaken you and you process it, then go back to sleep and just hang. This was totally unsuitable for my project, as I needed to simply put the machine to sleep after some number of minutes of inactivity. I do not have to put it to sleep to save batteries. Here is how I did it. This is for a Silabs 320 Several specific things here. 1. Silabs 320 is running at 24 mhz, and to drop current consumption, I wanted to slow the processor down This assumes that the wakeup signals (in this case a pair of pushbuttons) are connected to int0 and int 1, and I wanted no other way to wake up. // // now disable all interrupts except for int0 and int1 // saved_ie=IE; // grab a copy of the interrupt enable register EA=0; // globally kill all of them saved_eie1=EIE1; // saved_eie2=EIE2; EIE1=0; EIE2=0; IE=0x85; // reenable global interrupts, and enable int0 and int1 CR=0; // disable the PCA TMR3CN=TMR3CN & 0xFB; // disable timer 3 // // slow the part down to slowest possible clockspeed in this mode // CLKSEL = 0x00; // Oscillator Clock Select Register ls bits 00 = CLKMUL=0; OSCICN=0x80; // sets system to internal oscliator /8 // // ensure that all the i/o pins are off // P1MDIN=0x00; // running value 0xFF // // shut down other ports the same way // PCON=PCON | 0x01; // suspend the processor // // we come out of sleep here // CR=1; // enable the PCA again EA=0; // // we are going to put code here to switch this to 24 mhz mode // OSCICN = 0x83; // system clock /1 CLKMUL = 0x80; // this sets the clock multiplyer to enabled mode delay=100; while (delay--); // wait at least 5 microseconds CLKMUL |= 0xC0; // initialize multiplier while ((CLKMUL & 0x20) != 0x20); // wait for multiplyer to be ready CLKSEL = 0x02; // system clock is 48mhz/2 i.e. 12 mhz * 4 /2