焊接资讯

您现在的位置是:首页 > led灯 > 正文

led灯

定时器控制8个led灯闪烁程序汇编(用定时器实现单个led灯闪烁)

工品易达2022-11-16led灯18

51单片机定时器控制8个LED灯亮3秒灭8秒再亮12秒的程序

一、利用定时器实现:

/*****************************预处理命令*****************************/

#includereg52.h

#define OUT P0

//晶振:12MHZ

/********************重新定义数据类型*******************************/

typedef unsigned char unit8;

typedef unsigned short unit_16;

/*

函数名:main()

函数功能: 主函数

输入:无

输出:无

*/

void main()

{

TMOD = 0x01;

TR0 = 1;

ET0 = 1;

EA = 1;

while(1);

}

/*

函数名:Timer0_Rountine() interrupt 1

函数功能:内部中断T0 服务函数

输入: 无

输出:无

*/

void Timer0_Rountine() interrupt 1

{

static unit_16 Count=0;

static unit8 Count_Sign=0;

static bit Sign=0;

TH0 = 0xfc;

TL0 = 0x18;

Count++;

if(!Sign)

OUT = 0xaa; //奇数 (1秒)

if(Count=1000)

{

Sign=1;

Count=0;

if(Count_Sign3) //2秒

OUT = 0xaa; //奇数

else if(Count_Sign=3Count_Sign=10) //8秒

OUT = 0x55; //偶数

else if(Count_Sign10Count_Sign=22) //12秒

OUT = 0x00; //全部

else if(Count_Sign22)Count_Sign=Sign=0;

Count_Sign++;

}

}

二、利用软件延时实现:

#includereg52.h

#define OUT P0

typedef unsigned char unit8;

typedef unsigned int unit16;

/*

函数名:delay_ms(unit16 x)

函数功能: 延时函数1MS

输入:x

输出:无

*/

void delay_ms(unit16 x)

{

unit8 a,b;

while(x--)

{

for(b=142;b0;b--)

for(a=2;a0;a--);

}

}

/*

函数名:main()

函数功能: 主函数

输入:无

输出:无

*/

void main()

{

while(1)

{

OUT = 0xaa; //奇数

delay_ms(3000); //3秒

OUT = 0x55; //偶数

delay_ms(8000); //8秒

OUT = 0x00; //全部

delay_ms(12000);//12秒

}

}

汇编语言编一个八个灯,由左至右循环闪烁的程序~~~谢谢

首先,是单灯如何点亮的问题,无非就是某个gpio口输出高电平或者低电平(由外部电路决定)使灯亮灭,假设输出高电平为亮,输出低电平为灭。

其次,1hz闪烁,即代表定时1s开、关灯。所以要用到定时器。

代码如下:

#include

"stdio.h"

#define

led_on

p1.1

=

1;

#define

led_off

p1.1

=

0;

bit

flag_1s;

bit

flag_cnt;

void

main(void)

{

sys_init();

while(1)

{

if(flag_1s

==

1)

{

flag_1s

=

0;

if(flag_cnt

^=

1)

led_on;

else

led_off;

}

}

}

sys_init()

{

//

初始化mcu的io口

//

初始化定时器

}

void

timer_isr

(void)

interrupt

1

{

//

定时器中断处理函数

1s

进入一次

flag_1s

=

1;

}

单片机实验利用定时器控制八个LED灯每隔1S每四个为一组轮流点亮

03  8只LED左右来回点亮

/*    名称:8只LED左右来回点亮

说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果

*/

#includereg51.h

#includeintrins.h

#define uchar unsigned char

#define uint unsigned int

//延时

void DelayMS(uint x)

{

uchari;

while(x--)

{

for(i=0;i120;i++);

}

}

//主程序

void main()

{

uchari;

P2=0x01;

while(1)

{

for(i=0;i7;i++)

{

P2=_crol_(P2,1);//P2的值向左循环移动

DelayMS(150);

}

for(i=0;i7;i++)

{

P2=_cror_(P2,1);//P2的值向右循环移动

DelayMS(150);

}

}

}

04  花样流水灯

/*    名称:花样流水灯

说明:16只LED分两组按预设的多种花样变换显示

*/

#includereg51.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=0;i120;i++);

}

}

//主程序

void main()

{

uchari;

while(1)

{     //从数组中读取数据送至P0和P2口显示

for(i=0;i136;i++)

{

P0=Pattern_P0[i];

P2=Pattern_P2[i];

DelayMS(100);

}

}

}

05  LED模拟交通灯

/*    名称:LED模拟交通灯

说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。

*/

#includereg51.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=0;i120;i++);

}

//交通灯切换

void Traffic_Light()

{

switch(Operation_Type)

{

case1:     //东西向绿灯与南北向红灯亮

RED_A=1;YELLOW_A=1;GREEN_A=0;

RED_B=0;YELLOW_B=1;GREEN_B=1;

DelayMS(2000);

Operation_Type=2;

break;

case2:     //东西向黄灯闪烁,绿灯关闭

DelayMS(300);

YELLOW_A=~YELLOW_A;GREEN_A=1;

if(++Flash_Count!=10)return; //闪烁5次

Flash_Count=0;

Operation_Type=3;

break;

case3:     //东西向红灯,南北向绿灯亮

RED_A=0;YELLOW_A=1;GREEN_A=1;

RED_B=1;YELLOW_B=1;GREEN_B=0;

DelayMS(2000);

Operation_Type=4;

break;

case4:     //南北向黄灯闪烁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();

}

06  单只数码管循环显示0~9

/*    名称:单只数码管循环显示0~9

说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示

*/

#includereg51.h

#includeintrins.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=0;t120;t++);

}

//主程序

void main()

{

uchari=0;

P0=0x00;

while(1)

{/* for(;i11;i++){ P0=~DSY_CODE[i]; DelayMS(300);}  //注:另一方案 */

P0=~DSY_CODE[i];

i=(i+1)%10;

DelayMS(300);

}

}

定时器控制8个led灯闪烁程序汇编的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用定时器实现单个led灯闪烁、定时器控制8个led灯闪烁程序汇编的信息别忘了在本站进行查找喔。微信号:ymsc_2016

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~