今天只说话,不发图
 
	
目的 ,让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%16)/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....
	
	
	
			
			
			
						
			
 我要赚赏金
