查看: 655|回复: 0

SIN210学习笔记__Twinkle LED

[复制链接]
  • TA的每日心情
    慵懒
    2016-10-17 12:07
  • 签到天数: 306 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-2-26 09:00:07 | 显示全部楼层 |阅读模式
    分享到:
    目的 ,让LED 闪烁起来
    第一个,是让LED1-LED4,按着不同的规律闪烁,LED1每秒闪烁一次,LED2 每两秒闪烁一次 ,LED3没四秒闪烁一次.。。。。。 不说了直接上代码
    /*led_twinkle.c*/  #include <stdio.h>  #include <string.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>  #include <linux/input.h>  #define LED1         0  #define LED2         1  #define LED3         2  #define LED4         3      int main(int argc, char **argv)  {      unsigned int count = 0;      int fdled = -1;      usleep(500*1000);      fdled = open("/dev/led",O_RDWR);      if(fdled<0)       {           printf("Error:Can't open /dev/leds\n");         return -1;       }       printf("\nThe LEDs start Twinkle\n");      while(1)      {        count++;        ioctl(fdled, count%2,      LED1);        ioctl(fdled, (count%4)/2,   LED2);        ioctl(fdled, (count%8)/4,  LED3);        ioctl(fdled, (count)/8, LED4);        usleep(500*1000);       }     return 0;  }      Makefile如下;  Exec := led_twinkle  Obj := led_twinkle.c  CC := arm-linux-gcc    $(Exec) : $(Obj)          $(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))    clean:          rm -vf $(Exec) *.elf *.o   第二个就是呼吸灯了,,嘿嘿。。。用普通的GPIO口来模拟PWM。
    /*respiration_lamp.c*/  #include <stdio.h>  #include <string.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>  #include <linux/input.h>    #define LED_ON  0  #define LED_OFF 1    #define LED1    0  #define LED2    1  #define LED3    2  #define LED4    3    #define MAX 100  int main(int argc, char **argv)  {      unsigned int count = 0;      int fdled = -1;      usleep(500*1000);      fdled = open("/dev/led",O_RDWR);      if(fdled<0)       {         printf("Error:Can't open /dev/leds\n");         return -1;       }      printf("\nRespiration Lamp\n");      while(1)      {        count = 0;        while(++count < MAX)     {          ioctl(fdled, LED_ON, LED1);          usleep((MAX-count)*100);          ioctl(fdled,LED_OFF, LED1);          usleep(count*100);        }        usleep(1000*1000);          count = 0;        while(++count < MAX)        {         ioctl(fdled,LED_OFF,LED1);         usleep((MAX-count)*100);         ioctl(fdled,LED_ON, LED1);         usleep(count*100);        }        usleep(1000*1000);     }     return 0;    /***********************/    Makefile如下:  Exec := respiration_lamp  Obj := respiration_lamp.c  CC := arm-linux-gcc    $(Exec) : $(Obj)          $(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))    clean:          rm -vf $(Exec) *.elf *.o     编译生存可执行文件之后,运行就可以看到效果了。。这个程序一直是在循环中如果想推出,请 ctrl + C
    LED,真是个神奇的东西,下次继续闪灯,,Twinkle,Twinkle,Twinkle....
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 04:52 , Processed in 0.107318 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.