51单片机简单功能的实现(2)
延时函数外加闪烁
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit D1=P1^0;
//void delay();
void delay(uint);
void main()
{
while(1)
{
D1=0;
delay(600);
D1=1;
delay(600);
}
}
/*void delay()
{
uint x,y;
for(x=100;x>0;x--)
for(y=600;y>0;y--);
}*/
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
流水灯
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
void delay(uint);
void main()
{
temp=0xfe;
P1=temp;
while(1)
{
temp=_crol_(temp,1);
delay(600);
P1=temp;
}
}
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
流水灯结合蜂鸣器
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
sbit beep=P2^3;
void delay(uint);
void main()
{
temp=0xfe;//11111110
P1=temp;
beep=0;
while(1)
{
temp=_crol_(temp,1); //_cror_循环右移
delay(600);
P1=temp;
}
}
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
第一个数码管的显示
#include<reg52.h>
sbit dula=P2^6;
sbit wela=P2^7;
void main()
{
wela=1;
P0=0xfe; //P0=0xfc; //P0=0xc0;
wela=0;
dula=1;
P0=0x06; //P0=0x3f;
dula=0;
while(1);
}
数码管的有规律显示
#include<reg52.h>
#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 delay(uint z);
void main()
{
wela=1; //11101010
P0=0xc0;//P0=0xea;
wela=0;
while(1)
{
for(num=0;num<16;num++)
{
dula=1;
P0=table[num];
dula=0;
delay(1000);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
外部中断程序(1)
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit d1=P1^0;
uchar num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delay(uint z);
void main()
{
EA=1; //开总中断
EX0=1;//开外部中断0
//IT0=1
//TCON=0x11;灯闪一下就没
wela=1; //11101010
P0=0xc0;//P0=0xea;
wela=0;
while(1)
{
for(num=0;num<16;num++)
{
dula=1;
P0=table[num];
dula=0;
delay(1000);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
void exter0() interrupt 0
{
d1=0;
}
外部中断程序(2)
#include<reg52.h>//外部中断程序
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit d1=P1^0;
uchar num,tt;
uchar code table[]=