流水灯是学习单片机嵌入式编程的必修课,当我们能够在一个单片机上跑流水灯的程序时,说明基本入门了单机机编程。简简单单的一个流水灯,其背后是一系列专业的操作实现的结果。我拿到一块板子,一般都想着怎么烧录程序,怎么跑流水灯,能够做到这里,那么就能用这开发板做点事情了。O(∩_∩)O哈!瞎扯了一下;下面说正题。
HiKey板载了6个LED灯,其中有四盏灯是绿色、一盏黄色、一盏蓝色。
在默认的状态下,led1、2、3是关掉的,WiFi(yellow)该Led指示灯,在连接上WiFi并且通信过程时会闪。Blue指示灯是蓝牙连接传输数据时闪。USER_LED1用做系统运行过程指示灯。Lemaker WiKi(连接地址http://wiki.lemaker.org/HiKey(LeMaker_version):HiKey_User_LEDs)的使用说明上给出了控制LED的方式
注意:(使用下列指令需要在root 权限下)
echo heartbeat >/sys/class/leds/<led_dir>/trigger #make a LED flash using heartbeat trigger
cat /sys/class/leds/<led_dir>/trigger #show the currently defined trigger
echo none > /sys/class/leds/<led_dir>/trigger #remove triggers
echo 1 > /sys/class/leds/<led_dir>/brightness #turn LED on
echo 0 > /sys/class/leds/<led_dir>/brightness #turn LED off
根据这个说明,那就可以很容易的编写一个脚本程序来实现Hikey 开发板上Led的流水灯实验。
- #############################
- # Filename:blinkyLed.sh
- # Author:silverze
- # Date:2016/4/28
- ############################
- #step1:turn off all leds
- echo 0 > /sys/class/leds/user_led1/brightness
- echo 0 > /sys/class/leds/user_led2/brightness
- echo 0 > /sys/class/leds/user_led3/brightness
- echo 0 > /sys/class/leds/user_led4/brightness
- #step2: turn on led1,led3
- while((1));
- do
- echo 1 > /sys/class/leds/user_led1/brightness
- sleep 1
- echo 0 > /sys/class/leds/user_led1/brightness
- echo 1 > /sys/class/leds/user_led2/brightness
- sleep 1
- echo 0 > /sys/class/leds/user_led2/brightness
- echo 1 > /sys/class/leds/user_led3/brightness
- sleep 1
- echo 0 > /sys/class/leds/user_led3/brightness
- echo 1 > /sys/class/leds/user_led4/brightness
- sleep 1
- echo 0 > /sys/class/leds/user_led4/brightness
- done
- echo "Led Control Error!"
复制代码
下面是使用C语言来编写这个程序:
- /**************************************
- Filename:blinkyLed.c
- Author :silverze
- Date :2016/5/1
- *************************************/
- #include
- #include
- #include
- #include
- //define the path:
- #define LED1 "/sys/class/leds/user_led1/brightness"
- #define LED2 "/sys/class/leds/user_led2/brightness"
- #define LED3 "/sys/class/leds/user_led3/brightness"
- #define LED4 "/sys/class/leds/user_led4/brightness"
- int main( void )
- {
- printf("nThis is a C Program that to control LEDs of Hikey:n");
- int led1_fd = open( LED1, O_WRONLY);
- int led2_fd = open( LED2, O_WRONLY);
- int led3_fd = open( LED3, O_WRONLY);
- int led4_fd = open( LED4, O_WRONLY);
- if(led1_fd < 0)
- {
- printf("Could not open File:%sn",LED1);
- return 0;
- }
- else if(led2_fd < 0)
- {
- printf("Could not open File:%sn",LED2);
- return 0;
- }
- else if(led3_fd < 0)
- {
- printf("Could not open File:%sn",LED3);
- return 0;
- }
- else if(led4_fd < 0)
- {
- printf("Could not open File:%sn",LED4);
- return 0;
- }
- int i;
- for(i = 0; i < 10; i ++)
- {
- write( led1_fd, "1", 2);
- printf("Turn On LED1: LED904n");
- sleep(2);
- write( led1_fd, "0", 2);
- write( led2_fd, "1", 2);
- printf("Turn On LED2: LED903n");
- sleep(2);
- write( led2_fd, "0", 2);
- write( led3_fd, "1", 2);
- printf("Turn On LED3: LED902n");
- sleep(2);
- write( led3_fd, "0", 2);
- write( led4_fd, "1", 2);
- printf("Turn On LED4: LED901n");
- sleep(2);
- write( led4_fd, "0", 2);
- }
- close(led1_fd);
- close(led2_fd);
- close(led3_fd);
- close(led4_fd);
- }
复制代码
实验现象:
0