技术热线: 4007-888-234
设计开发

专注差异化嵌入式产品解决方案 给智能产品定制注入灵魂给予生命

开发工具

提供开发工具、应用测试 完善的开发代码案例库分享

技术支持

从全面的产品导入到强大技术支援服务 全程贴心伴随服务,创造无限潜能!

新品推广

提供新的芯片及解决方案,提升客户产品竞争力

新闻中心

提供最新的单片机资讯,行业消息以及公司新闻动态

渐明渐暗动态流水灯效果(C源程序)

更新时间: 2019-03-25
阅读量:228

//--------------------------------------------------------
//项目: 灯闪控制
//设计: 程序匠人
//--------------------------------------------------------
/*
匠人按:一般的流水灯,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口灯闪_V05: 2008-5-22 23:19
 渐明渐暗动态流水灯效果

*/
//--------------------------------------------------------
//插入文件包
//--------------------------------------------------------
#include   //加入头文件;

//--------------------------------------------------------
//重新命名数据类型
//--------------------------------------------------------
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}
*/

//--------------------------------------------------------
//主函数
//--------------------------------------------------------
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 ;
   duty_led4 = duty_led5 ;

 duty_led5 = duty_led6 ;
   duty_led6 = duty_led7 ;
   duty_led7 = duty_led_buf0 ;
   duty_led_buf0 = duty_led_buf1 ;
   duty_led_buf1 = duty_led_buf2 ;
   duty_led_buf2 = duty_led_buf3 ;
   duty_led_buf3 = duty_led_buf4 ;
   duty_led_buf4 = duty_led_buf5 ;
   duty_led_buf5 = duty_led_buf6 ;
   duty_led_buf6 = duty_led_buf7 ;
   duty_led_buf7 = 0 ;
  }

  //delay(65535);   //延时

//--------------------------------------------------------
//
// THE END