当前位置:文档之家› 单片机实验proteus 仿真C语言

单片机实验proteus 仿真C语言

P0=table[light];
P2=table[high];
delay1s();
}
}
}
void delay1s()
{unsigned char i,j,k;
for(i=82;i>0;i--)
for(j=20;j>0;j--)
for(k=248;k>0;k--);
}
实验十:可预置数加减计数器
程序:
//K1(P3_7)加计数.K2(P3_6)头减计数.
delay10ms();
}
实验九:00~99计数
程序:
//00~99计数器
#include "AT89X51.H"
void delay1s();
void main()
{unsigned char code table[]={0xf9,0x24,0x30,0x19,0x12,0x03,0xf8,0x00,0x18,0x40};
{P0=table1[i];
P2=(P2<<1)+1;
}
}
else if(P1_0==1)
{delay01ms();
if(P1_0==1)
for(i=0;i<8;i++)
{P0=table2[i];
P2=(P2<<1)+1;
}
}
}
}
void delay01ms()
{unsigned char a,b;
unsigned int high=9;
unsigned int light=9;
P0=table[light];
P2=table[high];
delay1s();
while(1)
{for(light=0;light<10;light++)
{if(light==9) high++;
if(high==10) high=0;
#include "AT89X51.H"
void delay02s();
void delay10ms();
void main()
{P0=P2;
while(1)
{if(P3_6==0)
{delay10ms();
if(P3_6==0)
{P0++;
delay02s();
}
}
if(P3_7==0)
{delay10ms();
{switch(key)
{case 0x0e: key=0;break;
case 0x0d: key=1;break;
case 0x0b: key=2;break;
case 0x07: key=3;break;
}
}
P0=table[key];
key=key&0x0f;
while(key!=0x0f)
单片机实验:
说明,以下是我整理了一些在学习单片机的过程中所做的一些典型的实验,所有程序均用C语言编写,为了方便,在仿真时电路能省的都被省掉了(比如省去晶振用了外部时钟)。程序不一定很优化,也不一定完全没有错误,仅供大家参考。
希望大家一块学习!!!!
第一:I/O操作篇
实验一.闪烁的灯
图:
程序:
#include "AT89X51.H"
}
实验四:跑马灯(一)
图:
程序:
//单一灯的左移右移
//八个发光二极管L1-L8分别接在单片机的P1.0-P1.7接口上
//输出“0”时,发光二极管亮,开始时P1.0→P1.1→P1.2→P1.3→┅→P1.7→P1.6→┅→P1.0亮,重复循环。
#include "AT89X51.H"
void delay02s();
}
}
}
void delay02s()
{unsigned int i,j,k;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
for(k=0;k<248;k++);
}
实验五:跑马灯(二、查表方式)
图:
程序:
#include "AT89X51.H"
void delay02s();
void main()
if(P3_7==0)
{P0--;
delay02s();
}
}
}
}
void delay02s()
{unsigned char j;
for(j=20;j>0;j--)
delay10ms();
}
void delay10ms()
{unsigned char i,k;
for(i=20;i>0;i--)
for(k=248;k>0;k--);
unsigned char disp;
while(1)
{for(disp=0;disp<10;disp++)
{P0=table[disp];
delay02s();
}
}
}
void delay02s()
{ unsigned char i,j,k;
for(i=20;i>0;i--)
for(j=20;j>0;j--)
for(a=4;a>0;a--)
for(b=248;b>0;b--);
}
实验十二、4*4键盘
图:
程序:
#include "AT89X51.H"
void delay10ms();
void main()
{unsigned char key;
unsignedchar table[]={0x40,0xf9,0x24,0x30,
}
实验十一:
图:共阴LED
程序:
//开关按下时显示"12345",开关弹起时显示"HELLO".
#include <AT89X51.H>
void delay01ms();
void main(void)
{unsigned char code table1[]={0xf9,0x24,0x30,0x19,0x12,0xff,0xff,0xff};
{delay10ms();
key=P3&0x0f;
if(key!=0x0f)
{switch(key)
{case 0x0e: key=8;break;
case 0x0d: key=9;break;
case 0x0b: key=10;break;
case 0x07: key=11;break;
else if ( P1_7==1) P1_0 = 1;//key弹起P1_7=1,led熄灭
}
}
实验三:多路开关
程序:
#include "AT89X51.H"
void main()
{while(1)
{P1_0 = P1_7;
P1_1 = P1_6;
P1_2 = P1_5;
P1_3 = P1_4;
}
void main()
{unsigned int a;
while (1)
{P1=0xfe;
delay02s();
for(a=7;a>0;a--)//右移
{P1=(P1<<1)+1;
delay02s();
}
for(a=6;a>0;a--)//左移
{P1=(P1>>1)+0x80;
delay02s();
unsigned char code table2[]={0x09,0x06,0xc7,0xc7,0x40,0xff,0xff,0xff};
unsigned char i;
while(1)
{P2=0xfe;
if(P1_0==0)
{delay01ms();
if(P1_0==0)
for(i=0;i<8;i++)
if(P1_7==0)
{id++;
if(id==4) id=0;
while(P1_7==0);
}
}
switch(id)
{ case 0:
P0_0=~P0_0;
delay02s();
break;
case 1:
P0_1=~P0_1;
delay02s();
break;
case 2:
P0_2=~P0_2;
delay02s();
for(k=248;k>0;k--);
}
实验七:二进制计数
程序:
//用四个发光二极管模拟计数点击开关次数.
#include "AT89X51.H"
void delay10ms();
void main()
{P0=0xff;
while(1)
{
/*
//////////////////////////////////////////////
if(P0_7==1) //按键按下
{delay10ms();
if(P0_7==0) //按键弹起,此时灯亮.
P0=P0-1;
}
//////////////////////////////////////////////
*/
//////////////////////////////////////////////
相关主题