#include <intrins.h>
void delayms(unsigned char ms) // 延时子程序
{
unsigned char i
while(ms--)
{
for(i = 0i <120i++)
}
}
main()
{
unsigned char LED
LED = 0xff
P0 = LED
while(1)
{
delayms(1000)
LED = LED<<1 //循环右移1位,点亮下一个LED
if(LED==0x00)
{
LED=0xff
}
P0 = LED
}
}
《基于8051+Proteus仿真》案例
第01 篇基础程序设计
01 闪烁的LED
/* 名称:闪烁的LED
说明:LED按设定的时间间隔闪烁
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED=P1^0
//延时
void DelayMS(uint x)
{
uchari
while(x--)
{
for(i=0i<120i++)
}
}
//主程序
void main()
{
while(1)
{
LED=~LED
DelayMS(150)
}
}
02 从左到右的流水灯
/* 名称:从左到右的流水灯
说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x)
{
uchari
while(x--)
{
for(i=0i<120i++)
}
}
//主程序
void main()
{
P0=0xfe
while(1)
{
P0=_crol_(P0,1)//P0的值向左循环移动
DelayMS(150)
}
}
03 8只LED左右来回点亮
/* 名称:8只LED左右来回点亮
说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x)
{
uchari
while(x--)
{
for(i=0i<120i++)
}
}
//主程序
void main()
{
uchari
P2=0x01
while(1)
{
for(i=0i<7i++)
{
P2=_crol_(P2,1)//P2的值向左循环移动
DelayMS(150)
}
for(i=0i<7i++)
{
P2=_cror_(P2,1)//P2的值向右循环移动
DelayMS(150)
}
}
}
04 花样流水灯
/* 名称:花样流水灯
说明:16只LED分两组按预设的多种花样变换显示
*/
#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)
{
uchari
while(x--)
{
for(i=0i<120i++)
}
}
//主程序
void main()
{
uchari
while(1)
{ //从数组中读取数据送至P0和P2口显示
for(i=0i<136i++)
{
P0=Pattern_P0[i]
P2=Pattern_P2[i]
DelayMS(100)
}
}
}
05 LED模拟交通灯
/* 名称:LED模拟交通灯
说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
*/
#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)
{
uchari
while(x--)for(i=0i<120i++)
}
//交通灯切换
void Traffic_Light()
{
switch(Operation_Type)
{
case1: //东西向绿灯与南北向红灯亮
RED_A=1YELLOW_A=1GREEN_A=0
RED_B=0YELLOW_B=1GREEN_B=1
DelayMS(2000)
Operation_Type=2
break
case2: //东西向黄灯闪烁,绿灯关闭
DelayMS(300)
YELLOW_A=~YELLOW_AGREEN_A=1
if(++Flash_Count!=10)return//闪烁5次
Flash_Count=0
Operation_Type=3
break
case3: //东西向红灯,南北向绿灯亮
RED_A=0YELLOW_A=1GREEN_A=1
RED_B=1YELLOW_B=1GREEN_B=0
DelayMS(2000)
Operation_Type=4
break
case4: //南北向黄灯闪烁5次
DelayMS(300)
YELLOW_B=~YELLOW_BGREEN_B=1
if(++Flash_Count!=10)return
Flash_Count=0
Operation_Type=1
}
}
//主程序
void main()
{
while(1)Traffic_Light()
}
06 单只数码管循环显示0~9
/* 名称:单只数码管循环显示0~9
说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}
//延时
void DelayMS(uint x)
{
uchart
while(x--)for(t=0t<120t++)
}
//主程序
void main()
{
uchari=0
P0=0x00
while(1)
{/* for(i<11i++){ P0=~DSY_CODE[i]DelayMS(300)} //注:另一方案 */
P0=~DSY_CODE[i]
i=(i+1)%10
DelayMS(300)
}
}
07 8只数码管滚动显示单个数字
/* 名称:8只数码管滚动显示单个数字
说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
//延时
void DelayMS(uint x)
{
uchart
while(x--)for(t=0t<120t++)
}
//主程序
void main()
{
uchari,wei=0x80
while(1)
{
for(i=0i<8i++)
{
P2=0xff //关闭显示
wei=_crol_(wei,1)
P0=DSY_CODE[i] //发送数字段码
P2=wei //发送位码
DelayMS(300)
}
}
}
08 8只数码管动态显示多个不同字符
电路如上图
/* 名称:8只数码管动态显示多个不同字符
说明:数码管动态扫描显示0~7。
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
//延时
void DelayMS(uint x)
{
uchart
while(x--)for(t=0t<120t++)
}
//主程序
void main()
{
uchari,wei=0x80
while(1)
{
for(i=0i<8i++)
{
P2=0xff
P0=DSY_CODE[i] //发送段码
wei=_crol_(wei,1)
P2=wei //发送位码
DelayMS(2)
}
}
}
09 8只数码管闪烁显示数字串
电路如上图
/* 名称:8只数码管闪烁显示数字串
说明:数码管闪烁显示由0~7构成的一串数字
本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//段码表
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
//位码表
uchar codeDSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}
//延时
void DelayMS(uint x)
{
uchart
while(x--)for(t=0t<120t++)
}
//主程序
void main()
{
uchari,j
while(1)
{
for(i=0i<30i++)
{
for(j=0j<8j++)
{
P0=0xff
P0=DSY_CODE[j] //发送段码
P2=DSY_IDX[j] //发送位码
DelayMS(2)
}
}
P2=0x00 //关闭所有数码管并延时
DelayMS(1000)
}
}
10 8只数码管滚动显示数字串
电路如上图
/* 名称:8只数码管滚动显示数字串
说明:数码管向左滚动显示3个字符构成的数字串
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//段码表
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}
//下面数组看作环形队列,显示从某个数开始的8个数(10表示黑屏)
uchar Num[]={10,10,10,10,10,10,10,10,2,9,8}
//延时
void DelayMS(uint x)
{
uchart
while(x--)for(t=0t<120t++)
}
//主程序
void main()
{
uchari,j,k=0,m=0x80
while(1)
{ //刷新若干次,保持一段时间的稳定显示
for(i=0i<15i++)
{
for(j=0j<8j++)
{ //发送段码,采用环形取法,从第k个开始取第j个
P0=0xff
P0=DSY_CODE[Num[(k+j)%11]]
m=_crol_(m,1)
P2=m //发送位码
DelayMS(2)
}
}
k=(k+1)%11 //环形队列首支针k递增,Num下标范围0~10,故对11取余
}
}
11 K1-K4 控制LED移位
/* 名称:K1-K4 控制LED移位
说明:按下K1时,P0口LED上移一位;
按下K2时,P0口LED下移一位;
按下K3时,P2口LED上移一位;
按下K4时,P2口LED下移一位;
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x)
{
uchari
while(x--)for(i=0i<120i++)
}
//根据P1口的按键移动LED
void Move_LED()
{
if ((P1&0x10)==0) P0=_cror_(P0,1) //K1
elseif((P1&0x20)==0) P0=_crol_(P0,1) //K2
elseif((P1&0x40)==0) P2=_cror_(P2,1) //K3
elseif((P1&0x80)==0) P2=_crol_(P2,1) //K4
}
//主程序
void main()
{
ucharRecent_Key//最近按键
P0=0xfe
P2=0xfe
P1=0xff
Recent_Key=0xff
while(1)
{
if(Recent_Key!=P1)
{
Recent_Key=P1 //保存最近按键
Move_LED()
DelayMS(10)
}
}
}
//用两个按键控制16个流水灯的流速 要求共8个流动速度
#include<reg52.h>
void delay(unsigned char k) //延时函数
{
unsigned char i,j //定义两个变量,用于空循环
k=k*8
for(i=0i<ki++)
for(j=0j<255j++)
}
unsigned char flag
void main()
{
unsigned char i
flag=1
EA=1 //开放总中断
EX0=1 //允许使用外中断
IT0=1 //选择负跳变来触发外中断
EX1=1 //允许使用外中断
IT1=1 //选择负跳变来触发外中断
while(1) //用于反复循环
{
for(i=0i<9i++)
{
P1=~(1<<i)
delay(flag) //调用延时函数
//while(flag!=1)
}
for(i=0i<9i++)
{
P2=~(1<<i)
delay(flag) //调用延时函数
//while(flag!=1)
}
i=0
//
}
}
/**************************************************************
函数功能:外中断T0的中断服务程序
**************************************************************/
void int0(void) interrupt 0 using 0 //用于减小流水灯速度
{
if(flag<9)
flag++
}
/**************************************************************
函数功能:外中断T0的中断服务程序
**************************************************************/
void int1(void) interrupt 2 using 0 //用于增加流水灯速度
{
if(flag>1)
flag--
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)