51c单片机开发
扫描二维码
随时随地手机看文章
//TX-1C实验板
//芯片 STC89C52RC
//晶振频率 11.0592MHz
//=====一个LED灯闪
#include
#define uint unsigned int
sbit led1=P1^0;
void delay1s();
void main()
{
while(1)
{
led1=0;
delay1s();
led1=1;
delay1s();
}
}
void delay1s()
{
uint i,j;
for(i=500;i>0;i--)
for(j=110;j>0;j--);
}
//======流水灯程序
#include
#include
#define uint unsigned int
#define uchar unsigned char
void delayms(uint xms);
uchar aa;
void main()
{
aa=0xfe;
while(1)
{
P1=aa;;
delayms(500);
aa=_crol_(aa,1);
}
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
//=====启动蜂鸣
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit beep=P2^3;
void delay(uint z);
void main()
{
while(1)
{
beep=0;
delay(100);
beep=1;
delay(10000000);
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//======数码管的显示;数字6
#include
sbit dula=P2^6;
sbit wela=P2^7;
void main()
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=0x7d;
dula=0;
while(1);
}
//======数码管的静态显示
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delayms(uint);
void main()
{
wela=1;
P0=0xc0;
wela=0;
while(1)
{
for(num=0;num<16;num++)
{
dula=1;
P0=table[num];
dula=0;
delayms(1000);
}
}
}
void delayms(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
//======数码管的动态显示
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delayms(uint);
void main()
{
while(1)
{
dula=1;
P0=table[1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(500);
dula=1;
P0=table[2];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(500);
dula=1;
P0=table[3];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delayms(500);
dula=1;
P0=table[4];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delayms(500);
dula=1;
P0=table[5];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delayms(500);
dula=1;
P0=table[6];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delayms(500);
}
}
void delayms(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
//======定时器0工作方式1=====
#include
#define uchar unsigned char
#define uint unsigned int
uchar num;
void main()
{
P1=0x55;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if(num==20)
{
num=0;
P1=~P1;
}
}
}
void t0_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
//=======用定时器0的方式1实现第一个发光管以ms间隔闪烁,用定时器1的方式1实现数码管前两位59s循环计时。
#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint);
void display(uchar,uchar);
uchar num,num1,num2,shi,ge;
void main()
{
led1=0;
TMOD=0x11;//00010001
TH0=(65536-50000)/256;
TL0=(65539-50000)%256;
TH1=(65536-50000)/256;
TL1=(65539-50000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
display(shi,ge);
}
}
void display(uchar s,uchar g)
{
dula=1;
P0=table[s];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[g];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void t0_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65539-50000)%256;
num1++;
if(num1==20)
{
num1=0;
led1=~led1;
}
}
void t1_time() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65539-50000)%256;
num2++;
if(num2==20)
{
num2=0;
num++;
if(num==60)
nu