呼吸灯_流水灯

呼吸灯流水灯切换——改变占空比

#include "common.h"
#include "include.h"


/*!
 *  @brief      main函数
 *  @since      v5.0
 *  @note       测试 KEY 循环扫描
 */
void main()
{
    key_init(KEY_A);
    led_init(LED0);                         //初始化LED0
    led_init(LED1);                         //初始化LED1
    led_init(LED2);
    led_init(LED3);
    unsigned int flag=1;
    unsigned int PWM_LOW=0;//定义周期并赋值

    while(1)
     {
            //if(key_check(KEY_A) == KEY_DOWN)
              while(1)
             { //检测key状态(带延时消抖)
              DELAY_MS(500);
              while (flag%2) 
              {//呼吸灯
              led(LED0,LED_OFF);
              led(LED1,LED_OFF);
              led(LED2,LED_OFF);
              led(LED3,LED_OFF);
              DELAY_MS(1500); //特意加延时,可以看到熄灭的过程
              for(PWM_LOW=1;PWM_LOW<25;PWM_LOW++)//PWM_LOW表示低电平时间
              { //,这个循环中低电平时长从1累加到CYCLE(周期)的值,即600次

                led(LED0,LED_ON);
                led(LED1,LED_ON);
                led(LED2,LED_ON);
                led(LED3,LED_ON);
                DELAY_MS(PWM_LOW);//延时长度,600次循环中从1加至599
                led(LED0,LED_OFF);
                led(LED1,LED_OFF);
                led(LED2,LED_OFF);
                led(LED3,LED_OFF);
                DELAY_MS(25-PWM_LOW);//延时长度,600次循环中从599减至1
              }
              //已经变亮
              for(PWM_LOW=25;PWM_LOW>0;PWM_LOW--)
                {//与逐渐变亮相反的过程

                led(LED0,LED_ON);
                led(LED1,LED_ON);
                led(LED2,LED_ON);
                led(LED3,LED_ON);
                DELAY_MS(PWM_LOW);
                led(LED0,LED_OFF);
                led(LED1,LED_OFF);
                led(LED2,LED_OFF);
                led(LED3,LED_OFF);
                DELAY_MS(25-PWM_LOW);
                }
              if(key_check(KEY_A) == KEY_DOWN)flag++;
              }

              DELAY_MS(500);
              while((flag+1)%2)
              {//流水灯       
                led(LED0, LED_ON);                  //LED0 亮
         //       led_turn(LED1);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED0, LED_OFF);                 //LED0 灭
          //      led_turn(LED1);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED1, LED_ON);                  //LED0 亮
           //     led_turn(LED2);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED1, LED_OFF);                 //LED0 灭
            //    led_turn(LED2);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

               led(LED2, LED_ON);                  //LED0 亮
          //      led_turn(LED3);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED2, LED_OFF);                 //LED0 灭
          //      led_turn(LED3);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED3, LED_ON);                  //LED0 亮
            //    led_turn(LED0);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms

                led(LED3, LED_OFF);                 //LED0 灭
             //   led_turn(LED0);                     //LED1翻转

                DELAY_MS(500);                      //延时500ms
                if(key_check(KEY_A) == KEY_DOWN)flag++;
              }
             }
     }

}

你可能感兴趣的:(K60例程for智能车)