无线电爱好网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2446|回复: 0

[分享]渐明渐暗动态流水灯效果

[复制链接]
发表于 2009-7-18 11:42:58 | 显示全部楼层

[分享]渐明渐暗动态流水灯效果

//--------------------------------------------------------
//项目: 灯闪控制
//设计: 程序匠人
//--------------------------------------------------------
/*
匠人按:一般的流水灯,LED都是固定亮度的。本程序在此基础上,实现渐明渐暗的效果。原理就是采用软件实现PWM,通过调节每个LED的亮灭时间比(占空比)来调节LED的亮度。本程序在EDN提供的51助学板上测试通过。
*/
//--------------------------------------------------------
/*
版本说明:
程序01_P0口灯闪_V01: 2008-5-21 22:53
8个LED一起闪烁
   
程序01_P0口灯闪_V02: 2008-5-21 22:53
8个LED流水灯

程序01_P0口灯闪_V03: 2008-5-22 21:36
仍旧是8个LED流水灯,规范程序

程序01_P0口灯闪_V04: 2008-5-22 21:52
优化程序

程序01_P0口灯闪_V05: 2008-5-22 23:19
渐明渐暗动态流水灯效果

*/
//--------------------------------------------------------
//插入文件包
//--------------------------------------------------------
#i nclude<at89x52.h>   //加入头文件;

//--------------------------------------------------------
//重新命名数据类型
//--------------------------------------------------------
typedef unsigned char tU08;   //unsigned 8 bit definition
typedef unsigned char tU8;   //unsigned 8 bit definition
typedef unsigned int tU16;   //unsigned 16 bit definition
typedef unsigned long tU32;   //unsigned 32 bit definition

typedef signed char tS08;   //signed 8 bit definition
typedef signed char tS8;   //signed 8 bit definition
typedef signed int tS16;   //signed 16 bit definition
typedef signed long tS32;   //signed 32 bit definition

typedef float tF32;

//--------------------------------------------------------
//--------------------------------------------------------

#define LED_ON 0  //LED点亮
#define LED_OFF 1  //LED熄灭

/*
//--------------------------------------------------------
//延时子程序
//--------------------------------------------------------
void delay(tU16 a)   
{
tU16 b;   
for(b=0;b<a;b++);    //根据输入A 的值达到某一定时间的延时;
}
*/

//--------------------------------------------------------
//主函数
//--------------------------------------------------------
void main()     
{
tU08 i,j ;

tU08 t_jsq ;  //周期计数器(范围0~199)

tU08 duty_led0 ; //LED0的占空(范围0~200;数值越大,LED越亮;下同)
tU08 duty_led1 ; //LED1的占空
tU08 duty_led2 ; //LED2的占空
tU08 duty_led3 ; //LED3的占空
tU08 duty_led4 ; //LED4的占空
tU08 duty_led5 ; //LED5的占空
tU08 duty_led6 ; //LED6的占空
tU08 duty_led7 ; //LED7的占空

tU08 duty_led_buf0 ; //缓冲
tU08 duty_led_buf1 ; //缓冲
tU08 duty_led_buf2 ; //缓冲
tU08 duty_led_buf3 ; //缓冲
tU08 duty_led_buf4 ; //缓冲
tU08 duty_led_buf5 ; //缓冲
tU08 duty_led_buf6 ; //缓冲
tU08 duty_led_buf7 ; //缓冲


while(1)     //无限循环
{
  duty_led0 = 0 ;
  duty_led1 = 0 ;
  duty_led2 = 0 ;
  duty_led3 = 0 ;
  duty_led4 = 0 ;
  duty_led5 = 0 ;
  duty_led6 = 0 ;
  duty_led7 = 0 ;

  duty_led_buf0 = 1 ;
  duty_led_buf1 = 2 ;
  duty_led_buf2 = 4 ;
  duty_led_buf3 = 200 ;
  duty_led_buf4 = 4 ;
  duty_led_buf5 = 2 ;
  duty_led_buf6 = 1 ;
  duty_led_buf7 = 0 ;


  //P1_0 = LED_ON ;
  //P1_1 = LED_ON ;
  //P1_2 = LED_ON ;
  //P1_3 = LED_ON ;

  for ( j = 0 ; j<50 ; j++ )  //调节流水灯间隔时间
  {


   for ( i = 0 ; i<5 ; i++ )  //调节流水灯速度
   {
   
    //根据占空比,控制每个LED的亮度
    for ( t_jsq = 0 ; t_jsq <200  ; t_jsq ++ )
    {
     if ( t_jsq < duty_led0 ) P0_0 = LED_ON ; else P0_0 = LED_OFF ;
     if ( t_jsq < duty_led1 ) P0_1 = LED_ON ; else P0_1 = LED_OFF ;
     if ( t_jsq < duty_led2 ) P0_2 = LED_ON ; else P0_2 = LED_OFF ;
     if ( t_jsq < duty_led3 ) P0_3 = LED_ON ; else P0_3 = LED_OFF ;
     if ( t_jsq < duty_led4 ) P0_4 = LED_ON ; else P0_4 = LED_OFF ;
     if ( t_jsq < duty_led5 ) P0_5 = LED_ON ; else P0_5 = LED_OFF ;
     if ( t_jsq < duty_led6 ) P0_6 = LED_ON ; else P0_6 = LED_OFF ;
     if ( t_jsq < duty_led7 ) P0_7 = LED_ON ; else P0_7 = LED_OFF ;
  
    }
   }
   
   //占空队列移动
   duty_led0 = duty_led1 ;
   duty_led1 = duty_led2 ;
   duty_led2 = duty_led3 ;
   duty_led3 = duty_led4 ;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|关于本站|小黑屋|Archiver|手机版|无线电爱好网 ( 粤ICP备15040352号 ) 无线电爱好技术交流5 无线电爱好技术交流1无线电爱好技术交流9开关电源讨论群LED照明应用、电源无线电爱好技术交流4无线电爱好技术交流8无线电爱好技术交流10无线电爱好技术交流11

粤公网安备 44030702001224号

GMT+8, 2024-4-25 21:07 , Processed in 0.140400 second(s), 14 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表