《单片机 C 语言程序设计实训 100 例—基于 8051+Proteus 仿真》案例
第 01 篇 基础程序设计
01 闪烁的 LED
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int sbit LED=P1^0;
//延时
void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
while(1)
{
LED=~LED; DelayMS(150);
}
}
02 从左到右的流水灯
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
P0=0xfe;
while(1)
{
P0=_crol_(P0,1); //P0 的值向左循环移动
DelayMS(150);
}
}
03 8 只 LED 左右来回点亮
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
uchar i; P2=0x01; while(1)
{
for(i=0;i<7;i++)
{
P2=_crol_(P2,1); //P2 的值向左循环移动
DelayMS(150);
}
for(i=0;i<7;i++)
{
P2=_cror_(P2,1); //P2 的值向右循环移动
DelayMS(150);
}
}
}
04 花样流水灯
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int uchar code Pattern_P0[]=
{
0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
uchar code Pattern_P2[]=
{
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,
0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
//延时
void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
uchar i;
while(1)
{ //从数组中读取数据送至 P0 和 P2 口显示
for(i=0;i<136;i++)
{
P0=Pattern_P0[i]; P2=Pattern_P2[i]; DelayMS(100);
}
}
}
05 LED 模拟交通灯
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; //东西向灯
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3; //南北向灯
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
uchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量
//延时
void DelayMS(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
//交通灯切换
void Traffic_Light()
{
switch(Operation_Type)
{
case 1: // 东 西 向 绿 灯 与 南 北 向 红 灯 亮 RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1; DelayMS(2000);
Operation_Type=2;
break;
case 2: // 东 西 向 黄 灯 闪 烁 , 绿 灯 关 闭 DelayMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1; if(++Flash_Count!=10) return; // 闪 烁 5 次
Flash_Count=0;
Operation_Type=3;
break;
case 3: // 东 西 向 红 灯 , 南 北 向 绿 灯 亮 RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0; DelayMS(2000);
Operation_Type=4;
break;
case 4: //南北向黄灯闪烁 5 次 DelayMS(300); YELLOW_B=~YELLOW_B;GREEN_B=1;
if(++Flash_Count!=10) return; Flash_Count=0;
Operation_Type=1;
}
}
//主程序
void main()
{
while(1) Traffic_Light();