EVM LED Blinking.c

From eLinux.org
Revision as of 22:34, 21 May 2007 by SpaceMonkey (Talk | contribs)

Jump to: navigation, search

/**********************************************************************

  • EVM_LED_BLINKING.C
  • Simple program to blink the LEDs on the DaVinci
  • EVM using the the LED port expander PCF8574A at I2C Slave Address 0x38.
  • I2C Support and I2C device interface must be turned on in the kernel.
                                                                                                                                                    • /
  1. include <stdio.h>
  2. include <sys/types.h>
  3. include <sys/stat.h>
  4. include <fcntl.h>
  5. include <stdlib.h>
  6. include <unistd.h>
  7. include <string.h>
  8. include <sys/ioctl.h>
  9. include <errno.h>
  10. include <signal.h>


  1. define I2C_DEVICE "/dev/i2c/0"
  2. define I2C_DEV_ADDR 0x38
  3. define I2C_SLAVE 0x0703

int main() {

       int i2c_fd;
       int state = 0;
       unsigned char val = 0x00;
       int ret;
       struct timespec req =
       {
               .tv_sec = 0,
               .tv_nsec = 100000000,
       };


       if((i2c_fd = open(I2C_DEVICE,O_RDWR)) < 0)
       {
               printf("Unable to open the i2c port!\n");
               exit(1);
       }


       if(ioctl(i2c_fd, I2C_SLAVE, I2C_DEV_ADDR)== -1)
       {
               close( i2c_fd );
               printf("Unable to setup the i2c port!\n");
               exit(1);
       }
       do
       {
               val <<= 1;
               val |= 0x01;
               if(val == 0xFF)
                       val = 0xFE;
               write( i2c_fd, &val, 1);


        } while (!nanosleep (&req, NULL));


       close ( i2c_fd );
       printf ("End.\n");
       return 0;

}