留意:电源办理寄存器PCON,它的最低两位IDL和PD别离用来设定是否单片机进入闲暇形式和掉电形式
因而,单片机进入掉电形式只需要PCON= 0x01;进入闲暇形式只需要PCON = 0x02;
闲暇形式:
单片机进入闲暇形式的时分,除了cpu处于休眠状况外,其他的硬件悉数处于活动状况,芯片中未触及的数据存储器和特别功用寄存器中的数据在闲暇形式期间都将坚持原值
单片机在闲暇形式下可由任何一个中止或者是硬件复位唤醒,值得留意的是,运用中止唤醒单片机,程序将从本来的中止的当地持续运转,当运用硬件复位时,程序将从头开始履行
掉电形式:
当单片机进入掉电形式(也叫休眠形式)外部晶振中止轰动,cpu,定时器,串口悉数中止作业,只要外部中止持续作业,使单片机进入掉电形式的指令将成为休眠前单片机作业的最终一条指令,值得留意的是,运用中止唤醒单片机,程序将从本来的中止的当地持续运转,当运用硬件复位时,程序将从头开始履行
示例代码:(此代码完成,在单片机上敞开两个外部中止,设置为低电平触发,用定时器计数而且显现在数码管的前两位,当计数到5时,单片机进入闲暇或者是休眠形式,当单片机呼应外部中止时,从休眠形式(闲暇形式)回来 ,并一起发动定时器
错误代码:
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar num ,gw,sw,aa;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init()//初始化函数
{
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;
ET0 = 1;
EX0 = 1;//翻开外部中止0
// IT0 = 0;//电平触发方法
EX1 = 1;//翻开外部中止1
// IT1 = 0;
TR0 = 1;
}
void delayms(uint x)//延时函数
{
uint i,j;
for(i = x;i》0;i–)
for(j=110;j》0;j–);
}
void display(uchar gw,uchar sw)//显现函数
{
dula = 1;
P0 = table[sw];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
delayms(5);
P0 = 0x0;//消影
dula = 1;
P0 = table[gw];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
delayms(5);
}
void main()
{
init();
while(1)
{
display(gw,sw);
}
}
void TImer0()interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
aa ++;
if(aa ==20)
{
aa =0;
num++;
if(num == 99)
num =0;
gw = num%10;
sw = num/10;
if(num == 6)
{
ET0 = 0;//封闭定时器
PCON = 0x01;//单片机进入闲暇形式
}
}
}
void ex_int0()interrupt 0
{
// PCON = 0;//可要可不要
ET0 =1;
}
void ex_int1()interrupt 2
{
// PCON = 0;//可要可不要
ET0 = 1;
}
正确代码:
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
uchar num ,gw,sw,aa;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init()//初始化函数
{
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;
ET0 = 1;
EX0 = 1;//翻开外部中止0
// IT0 = 0;//电平触发方法
EX1 = 1;//翻开外部中止1
// IT1 = 0;
TR0 = 1;
}
void delayms(uint x)//延时函数
{
uint i,j;
for(i = x;i》0;i–)
for(j=110;j》0;j–);
}
void display(uchar gw,uchar sw)//显现函数
{
dula = 1;
P0 = table[sw];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
delayms(5);
P0 = 0x0;//消影
dula = 1;
P0 = table[gw];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
delayms(5);
}
void main()
{
init();
while(1)
{
if(aa ==20)
{
aa =0;
num++;
if(num == 99)
num =0;
gw = num%10;
sw = num/10;
if(num == 6)
{
ET0 = 0;//封闭定时器
PCON = 0x01;//单片机进入闲暇形式
}
}
display(gw,sw);
}
}
void TImer0()interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
aa ++;
}
void ex_int0()interrupt 0
{
// PCON = 0;//可要可不要
ET0 =1;
}
void ex_int1()interrupt 2
{
// PCON = 0;//可要可不要
ET0 = 1;
}
错误代码中,把
if(aa ==20)
{
aa =0;
num++;
if(num == 99)
num =0;
gw = num%10;
sw = num/10;
if(num == 6)
{
ET0 = 0;//封闭定时器
PCON = 0x01;//单片机进入闲暇形式
}
}
来历;21ic