从PM1
、PM2
到切换到PM0
,可以使用睡眠定时器,因为睡眠定时器计时时间到后,MCU
会产生睡眠定时器中断,电源模式会自动从PM1/PM2
切换到PM0
。在PM3
下,睡眠定时器不起作用,因为32K
的晶振都关闭了,故从PM3
切换到PM0
用的是外部IO
中断。
设置睡眠定时器的定时间隔
这一步一定要在设置电源模式
之前,因为进入睡眠后系统就不会继续执行程序了。读取睡眠定时器的当前计数值,顺序必须遵循读ST0 -> 读ST1 -> 读ST2
的顺序;写入睡眠定时器的比较值,顺序必须遵循写ST2 -> 写ST1 -> 写ST0
的顺序。ST
的计数时钟是32.768K
,向上计数,当定时器的计数值等于比较值时,产生中断。
#include <iocc2530.h>
#define uint unsigned int
#define uchar unsigned char
#define uint8 unsigned char
#define uint32 unsigned long
#define LED_ON 0
#define LED_OFF 1
#define gled P1_0
#define rled P1_1
#define crystal 0 /* 石英晶振 */
#define rc 1 /* RC晶振 */
#define s1 P0_1
void delay ( uint n ) {
uint i, j;
for ( i = 0; i < 50; i++ )
for ( j