您的位置:首页 > 其它

89s52单片机的硬件资源基本使用

2010-04-17 16:12 447 查看
1.最典型、基本的定时器T0的初始化方法

#include<reg51.h>
sbit b=P1^1;
void T0isr(void) interrupt 1
{
TL0=0X0C;
TH0=0X0F0;
b=~b;

}
void main()
{
b=0;
TMOD=0X00;
TL0=0X0C;
TH0=0X0F0;
TR0=1;
ET0=1;
EA=1;
while(1)
{}
}

2.最基本初始化串行口模式1

#include<reg52.h>
void main()
{
int ch;
SCON=0X50;
TMOD=0X20;
PCON=0X80;
TL1=0XF4;
TH1=0XF4;
ES=0;
TR1=1;
SBUF=0X79;
while(TI)
{
TI=0;
}
while(RI)
{
ch=SBUF;
RI=0;
}
}

3.字符串输入输出的仿真

#include<stdio.h>
#include<reg52.h>
void main()
{
char ch;
SCON=0x50;
TMOD|=0x20;
PCON|=0x80;
TL1=0xf4;
TH1=0xf4;
IE|=0X90;
TR1=1;
printf("please input a char!/n");

while((ch=_getkey())!=0x0d)
{
printf("the in char=%c,the hex number=%bx/n",ch,ch);
}

}
4.中断基本设置

#include<reg52.h>

void ISR0(void) interrupt 0
{
P1=P1+1;

}
void ISR1(void) interrupt 2
{
P2=~P2;

}
void main()
{
IP=0X05;
IT0=1;
IT1=1;
EX0=1;
EX1=1;
EA=1;
P1=0;

while(1){}
}
5.看门狗的设置

#include<reg52.h>

sfr WDTRST=0XA6;
void main()
{
int i;
WDTRST=0X1E;
WDTRST=0XE1;
while(1)
{
if(P1==0x01)
{
for(i=0;i<100;i++);
}
else if(P1==0x02)
{
for(i=0;i<10000;i++);
}
else
{

}
WDTRST=0X1E;
WDTRST=0XE1;
}

}
6.寄存器仿真

ORG 0000H
JMP START
ORG 1000H
START: MOV R0,#00H
MOV R1,#11H
MOV R2,#22H
MOV R3,#33H
MOV P0,R0
MOV P1,R1
MOV P2,R2
MOV P3,R3
JMP START
END
8.低功耗仿真

#include<reg52.h>
#include<stdio.h>
void main(void)
{
while(1)
{
if(P1==0x01)

{
PCON=0X01;
}
else if(P1==0X02)
{
PCON=0X02;
}
else{}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐