PCF8591.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <sys/ioctl.h>
5 #include <linux/i2c-dev.h>
6 #include <ncurses.h> /* libncurses5-dev */
7 
8 /*
9 2014-08-26 PCF8591.c
10 
11 sudo apt-get install libncurses5-dev
12 
13 gcc -o PCF8591 PCF8591.c -lcurses -lpigpio -lpthread
14 
15 sudo ./PCF8591
16 */
17 
18 /*
19 Connect Pi 3V3 - VCC, Ground - Ground, SDA - SDA, SCL - SCL.
20 */
21 
22 #define PCF8591_I2C_ADDR 0x48
23 
24 /*
25  P4 The thermister voltage is provided at AIN 1.
26  P5 The photocell voltage is provided at AIN 0.
27  P6 The single turn 10K ohm trim pot voltage is provided at AIN 3.
28 */
29 
30 /*
31 7 6 5 4 3 2 1 0
32 0 X X X 0 X X X
33  | | | | | |
34  A B B C D D
35 
36 0 1 0 0 0 1 0 0
37 
38 A 0 D/A inactive
39  1 D/A active
40 
41 B 00 single ended inputs
42  01 differential inputs
43  10 single ended and differential
44  11 two differential inputs
45 
46 C 0 no auto inc
47  1 auto inc
48 
49 D 00 select channel 0
50  01 select channel 1
51  10 select channel 2
52  11 select channel 3
53 
54 */
55 
56 int main(int argc, char *argv[])
57 {
58  int i;
59  int r;
60  int handle;
61  char aout;
62  unsigned char command[2];
63  unsigned char value[4];
64  unsigned char str[8];
65 
66  int j;
67  int key;
68 
69  if (gpioInitialise() < 0) return 1;
70 
71  initscr();
72  noecho();
73  cbreak();
74  nodelay(stdscr, true);
75  curs_set(0);
76 
77  printw("PCF8591 + or - to change aout, any other key to quit.");
78 
79  mvaddstr(10, 0, "Brightness");
80  mvaddstr(12, 0, "Temperature");
81  mvaddstr(14, 0, "?");
82  mvaddstr(16, 0, "Resistor");
83 
84  refresh();
85 
86  handle = i2cOpen(1, PCF8591_I2C_ADDR, 0);
87 
88  command[1] = 0;
89  aout = 128;
90 
91  while (1)
92  {
93  for (i=0; i<4; i++)
94  {
95  command[1] = aout;
96  command[0] = 0x40 | ((i + 1) & 0x03); // output enable | read input i
97 
98  i2cWriteDevice(handle, &command, 2);
99 
100  usleep(20000);
101 
102  // the read is always one step behind the selected input
103  value[i] = i2cReadByte(handle);
104 
105  sprintf(str, "%3d", value[i]);
106  mvaddstr(10+i+i, 12, str);
107  value[i] = value[i] / 4;
108  move(10 + i + i, 16);
109 
110  for(j = 0; j < 64; j++)
111  if(j < value[i]) addch('*'); else addch(' ');
112  }
113 
114  refresh();
115 
116  key = getch();
117 
118  if ((key == '+') || (key == '=')) aout++;
119  else if ((key == '-') || (key == '_')) aout--;
120  else if (key != -1) break;
121  }
122 
123  endwin();
124 
125  i2cClose(handle);
126 
127  gpioTerminate();
128 
129  return (0);
130 }
131 
#define PCF8591_I2C_ADDR
Definition: PCF8591.c:22
int gpioInitialise(void)
Definition: pigpio.c:8459
handle
Definition: PCF8591.py:19
int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:3884
ROSLIB_DECL std::string command(const std::string &cmd)
int r
Definition: DHT22.py:259
int i2cClose(unsigned handle)
Definition: pigpio.c:4017
void gpioTerminate(void)
Definition: pigpio.c:8495
int aout
Definition: PCF8591.py:26
int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
Definition: pigpio.c:3942
int main(int argc, char *argv[])
Definition: PCF8591.c:56
int i2cReadByte(unsigned handle)
Definition: pigpio.c:3368


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57