8-bit A/D and D/A converter

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
wataaki
Date:
Wed Feb 04 06:12:52 2015 +0000
Commit message:
Initial version

Changed in this revision

PCF8591.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8591.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8591.cpp	Wed Feb 04 06:12:52 2015 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+#include "PCF8591.h"
+ 
+I2C i2c(p28,p27);        // sda, scl
+Serial pc(USBTX, USBRX); // tx, rx
+char cmd[32];
+char i;
+ 
+void set_ch(char sel)
+{    // PCA9541のサンプル
+        // MST_0側の自分にスレーブ側の制御権を得る場合
+    cmd[0] = 1;                     // PCA9541 コマンドコード Cont Reg
+    i2c.write( 0xe2, cmd, 1);       // Cont Regを指定
+    i2c.read( 0xe2, cmd, 1);        // Cont Regを読込み
+    wait(0.1);                      // 0.1s待つ
+    switch(cmd[0] & 0xf)
+    {
+    case 0:                         // bus off, has control
+    case 1:                         // bus off, no control
+    case 5:                         // bus on, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 4;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 2:                         // bus off, no control
+    case 3:                         // bus off, has control
+    case 6:                         // bus on, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 5;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 9:                         // bus on, no control
+    case 0xc:                       // bus on, no control
+    case 0xd:                       // bus off, no control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 0;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    case 0xa:                       // bus on, no control
+    case 0xe:                       // bus off, no control
+    case 0xf:                       // bus on, has control
+        cmd[0] = 1;                 // PCA9541 コマンドコード Cont Reg
+        cmd[1] = 1;                 // bus on, has control
+        i2c.write( 0xe2, cmd, 2);   // Cont Regにcmd[1]を書込み
+        i2c.read( 0xe2, cmd, 1);    // Cont Regを読込み
+        break;
+    default:
+        break;
+    }
+
+    cmd[0] = sel;                   // PCA9546 Cont Reg sel channel enabled
+    i2c.write( 0xe8, cmd, 1);       // Send command string
+}
+
+int main ()
+{
+   i2c.frequency(100000);
+    pc.printf("PCF8591T Sample Program\r\n");
+     
+    set_ch(2);              // PCF8591Tはch1に接続
+
+  // PCF8591T
+
+    cmd[0] = 0x44;
+    cmd[1] = 0x80;           // SLEEP = 0
+    i2c.write(PCF8591_ADDR, cmd, 2);   // cmd[0]Regにcmd[1]を書込み
+    pc.printf("AOUT=%2x  AIN0=%2x AIN1=%2x AIN2=%2x AIN3=%2x\r\n", i++, cmd[1], cmd[2], cmd[3], cmd[4]); // Cont Regを表示
+
+  while(1)
+  {
+    cmd[0] = 0x44 + 0x0;          // DAC有効、オートインクリメント
+    cmd[1] = i;             // DAC値設定 0~0xFF
+    i2c.write(PCF8591_ADDR, cmd, 2);    // DAC値更新
+
+    i2c.read(PCF8591_ADDR, cmd, 5);     // AD変換値を5バイト分取得
+    // AD変換値はCH0の前回データ、CH0~CH3の変換データ
+    pc.printf("AOUT=%2x  PREV=%2x AIN0=%2x AIN1=%2x AIN2=%2x AIN3=%2x\r\n", i++, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); // AD変換値を表示
+    wait(1.0);
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8591.h	Wed Feb 04 06:12:52 2015 +0000
@@ -0,0 +1,37 @@
+#define PCF8591_ADDR 0x90
+//#define PCF8591_ADDR 0x92
+//#define PCF8591_ADDR 0x94
+//#define PCF8591_ADDR 0x96
+//#define PCF8591_ADDR 0x98
+//#define PCF8591_ADDR 0x9A
+//#define PCF8591_ADDR 0x9C
+//#define PCF8591_ADDR 0x9E
+
+#define MODE1            0x00
+#define MODE2            0x01
+#define PWM0             0x02
+#define PWM1             0x03
+#define PWM2             0x04
+#define PWM3             0x05
+#define PWM4             0x06
+#define PWM5             0x07
+#define PWM6             0x08
+#define PWM7             0x09
+#define PWM8             0x0a
+#define PWM9             0x0b
+#define PWM10            0x0c
+#define PWM11            0x0d
+#define PWM12            0x0e
+#define PWM13            0x0f
+#define PWM14            0x10
+#define PWM15            0x11
+#define GRPPWM           0x12
+#define GRPFREQ          0x13
+#define LEDOUT0          0x14
+#define LEDOUT1          0x15
+#define LEDOUT2          0x16
+#define LEDOUT3          0x17
+#define SUBADR1          0x18
+#define SUBADR2          0x19
+#define SUBADR3          0x1a
+#define ALLCALLADR       0x1b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Feb 04 06:12:52 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1
\ No newline at end of file