PCF8591.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <fcntl.h>
00003 #include <unistd.h>
00004 #include <sys/ioctl.h>
00005 #include <linux/i2c-dev.h>
00006 #include <ncurses.h> /* libncurses5-dev */
00007 
00008 /*
00009 2014-08-26 PCF8591.c
00010 
00011 sudo apt-get install libncurses5-dev
00012 
00013 gcc -o PCF8591 PCF8591.c -lcurses -lpigpio -lpthread
00014 
00015 sudo ./PCF8591
00016 */
00017 
00018 /*
00019 Connect Pi 3V3 - VCC, Ground - Ground, SDA - SDA, SCL - SCL.
00020 */
00021 
00022 #define PCF8591_I2C_ADDR 0x48
00023 
00024 /*
00025     P4 The thermister voltage is provided at AIN 1.
00026     P5 The photocell voltage is provided at AIN 0.
00027     P6 The single turn 10K ohm trim pot voltage is provided at AIN 3.
00028 */
00029 
00030 /*
00031 7 6 5 4 3 2 1 0
00032 0 X X X 0 X X X
00033   | | |   | | |
00034   A B B   C D D
00035 
00036 0 1 0 0 0 1 0 0
00037 
00038 A 0 D/A inactive
00039   1 D/A active
00040 
00041 B 00 single ended inputs
00042   01 differential inputs
00043   10 single ended and differential
00044   11 two differential inputs
00045 
00046 C 0 no auto inc
00047   1 auto inc
00048 
00049 D 00 select channel 0
00050   01 select channel 1
00051   10 select channel 2
00052   11 select channel 3
00053 
00054 */
00055 
00056 int main(int argc, char *argv[])
00057 {
00058    int i;
00059    int r;
00060    int handle;
00061    char aout;
00062    unsigned char command[2];
00063    unsigned char value[4];
00064    unsigned char str[8];
00065 
00066    int j;
00067    int key;
00068 
00069    if (gpioInitialise() < 0) return 1;
00070 
00071    initscr();
00072    noecho();
00073    cbreak();
00074    nodelay(stdscr, true);
00075    curs_set(0);
00076 
00077    printw("PCF8591 + or - to change aout, any other key to quit.");
00078 
00079    mvaddstr(10, 0, "Brightness");
00080    mvaddstr(12, 0, "Temperature");
00081    mvaddstr(14, 0, "?");
00082    mvaddstr(16, 0, "Resistor");
00083 
00084    refresh();
00085 
00086    handle = i2cOpen(1, PCF8591_I2C_ADDR, 0);
00087 
00088    command[1] = 0;
00089    aout = 128;
00090 
00091    while (1)
00092    {
00093       for (i=0; i<4; i++)
00094       {
00095          command[1] = aout;
00096          command[0] = 0x40 | ((i + 1) & 0x03); // output enable | read input i
00097 
00098          i2cWriteDevice(handle, &command, 2);
00099 
00100          usleep(20000);
00101 
00102          // the read is always one step behind the selected input
00103          value[i] = i2cReadByte(handle);
00104 
00105          sprintf(str, "%3d", value[i]);
00106          mvaddstr(10+i+i, 12, str);
00107          value[i] = value[i] / 4;
00108          move(10 + i + i, 16);
00109 
00110          for(j = 0; j < 64; j++)
00111             if(j < value[i]) addch('*'); else addch(' ');
00112       }
00113 
00114       refresh();
00115 
00116       key = getch();
00117 
00118       if      ((key == '+') || (key == '=')) aout++;
00119       else if ((key == '-') || (key == '_')) aout--;
00120       else if  (key != -1)                   break;
00121    }
00122 
00123    endwin();
00124  
00125    i2cClose(handle);
00126 
00127    gpioTerminate();
00128 
00129    return (0);
00130 }
00131 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57