实验二 报警与流水灯控制
#include"reg51.h"
#include"intrins.h"
sbit P3_7=P3^7;
sbit P3_2=P3^2;
void dely() //using 0 //延迟n秒
//void dely(unsigned int t) //using 0 //延迟n秒
{
unsigned int i,j;
for(i=0;i<4;i++)
for(j=0;j<50000;j++);//0.01s
}
void int1_warning() interrupt 0 //using 0
{
unsigned int i;
for(i=0;i<3;i++)
{
P3_7=1;
dely();
P3_7=0;
}
}
void main()
{
unsigned char a[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned int i,j,LED;
EA=1;//CPU中断开
EX0=1;//INT0中断允许
IT0=0;//中断触发方式 0:低电平触发; 1:下降沿触发
P3_7=0;
while(1)
{
P3_2=1;
LED=0x01;
for(i=0;i<8;i++)
{
P1=a[i];
dely();//延迟
}
}
}实验三 3-8译码器
#include
main()
{
void delay();
unsigned char a[ ]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned int c;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
c=(P2_2)*4+(P2_1)*2+(P2_0)*1;
while(1)
{
switch(c)
{
case 0: P1=a[0];break;
case 1: P1=a[1];break;
case 2: P1=a[2];break;
case 3: P1=a[3];break;
case 4: P1=a[4];break;
case 5: P1=a[5];break;
case 6: P1=a[6];break;
case 7: P1=a[7];break;
}
}
}
void delay()
{
unsigned int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<5000;j++);
}
}
实验四 交通灯控制
#include
int n,count,flag,P3_2;
void main()
{
P3_2=P3^2;
TMOD=0x01;
TH0=0xD8;
TL0=0xf0;
EA=1;ET0=1;
EX0=1;
if(P3_2==0)
{IT0=1;}
n=0;
TR0=1;
flag=0;
while(1)
{
if (flag==0)
{
switch(n)
{
case 0:{P1=0xf3;count=1000;break;}
case 1:{P1=0xdc;count=300;break;}
case 2:{P1=0xcd;count=1000;break;}
case 3:{P1=0xdc;count=300;break;}}
}
}
}
}
void int0_fun(void) interrupt 0
{
P1=0x76;
}
void time0_int(void) interrupt 1
{
{
flag=1;
TH0=0xD8;
TL0=0Xf0;
count--;
if(count==0)
{flag=0;
n++;
if(n>3)n=0;
}
}
}实验五 时钟秒表技术
#include
#define uchar unsigned char
unsigned int delay,i,j;
sbit com4=P3^5;
sbit com3=P3^4;
code uchar a[]={0x10,0xf9,0xc4,0xb0,0x99,0x82,0xf8,0x80,0x90};
code uchar b[]={0xf9,0xc4,0xb0,0x99,0x82,0xf8};
void main()
{
TMOD=0X01;
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
ET0=1;
EA=1;
delay=0;
i=0;j=0;
TR0=1;
while(1)
{
com4=1;
com3=0;
P0=a[i];
}
}
void timer0() interrupt 1
{
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
delay++;
if(delay==19){i++;delay=0;}
if(i==9)
{
P0=b[j];com4=0;com3=1;
i=0;j++;
}
if(j==6) j=0;
}
实验六 键盘接口技术
#include
#define uchar unsigned char
#define uint unsigned int
sbit P3_4=P3^4;
sbit P3_5=P3^5;
code uchar Table1[12]={0xb7,0xbb,0xbd,0xbe,0xd7,0xdb,0xdd,0xde,0xe7,0xeb,0xed,0xee};
code uchar Table2[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int fastfound( )
{
uchar keyin,n;
P2=0x0f;
keyin=P2;
keyin=keyin&0x0f;
if(keyin!=0x0f)
for(n=0;n<200;n++);
if(keyin!=0x0f)
return(1);
else
return(0);
}
int keyfound( )
{
uchar keyvalue,keyin_i,keyin_j,keyin;
uchar k;
P2=0x0f;
keyin_j=P2;
keyin_j=keyin_j&0x0f;
P2=0xf0;
keyin_i=P2;
keyin_i=keyin_i&0xf0;