2016年10月9日 星期日

i2c - read / write LED - LP55281


The LP55281 device is a RGB LED driver for handheld devices. Follow the "Device Functional Modes" to bring up the LED.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "i2c-dev.h"
#include <sys/ioctl.h>

int main(void)
{
    int file,i;
    int addr = 0x4C;
    char *filename = "/dev/i2c-2";
    char wbuf[4];
    char read_start_buf[1];
    char rbuf[1024] = {0};
    if((file = open(filename, O_RDWR)) < 0)
    {
 perror("Failed to open i2c device.\n");
 return -1;
    }
    if(ioctl(file, I2C_SLAVE, addr) < 0)
    {
        printf("Failed to access bus.\n");
        return -1;
    }
    printf("1=0x%x\n", 0x01 << 7 | 0x01 << 6);
    //reset
    wbuf[0] = 0x60;
    wbuf[1] = 0x00;
    write(file, wbuf, 2);
    //standby
    wbuf[0] = 0x11;
    wbuf[1] = 0x01 << 7;
    write(file, wbuf, 2);
    //delay around 10ms and set EN_BOOST=H
    usleep(1000*12);
    wbuf[0] = 0x11;
    wbuf[1] = 0x01 << 6 | 0x01 << 7;
    write(file, wbuf, 2);
    //delay around 10ms
    usleep(1000*12);
    //Enable EN_RGB4
    wbuf[0] = 0x11;
    wbuf[1] = 0x01 << 6 | 0x01 << 7 | 0x0F;
    write(file, wbuf, 4);
    //usleep(1000*12);
    //Normal Mode
    wbuf[0] = 0x06;
    wbuf[1] = 0x3F;
    wbuf[2] = 0x7F;
    wbuf[3] = 0xFF;
    write(file, wbuf, 4);
    //for(i=0;i<5;i++)
    {
 read_start_buf[0] = 0x06;
 write(file, read_start_buf, 1);
 read(file, rbuf, 1);
 printf("%x", rbuf[0]);
 usleep(1000*10);
    }
    close(file);
}

沒有留言:

張貼留言