Penn Electric / MAX518
Committer:
PennElectric
Date:
Sat Dec 22 02:08:11 2012 +0000
Revision:
4:83bdb4efea8b
Parent:
3:96d5489ff1ab
Child:
6:8857ccc9323d
Updated auto doc messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PennElectric 4:83bdb4efea8b 1 /* Copyright (c) <2012> <P. Patel>, MIT License
PennElectric 4:83bdb4efea8b 2 *
PennElectric 4:83bdb4efea8b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
PennElectric 4:83bdb4efea8b 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
PennElectric 4:83bdb4efea8b 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
PennElectric 4:83bdb4efea8b 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
PennElectric 4:83bdb4efea8b 7 * furnished to do so, subject to the following conditions:
PennElectric 4:83bdb4efea8b 8 *
PennElectric 4:83bdb4efea8b 9 * The above copyright notice and this permission notice shall be included in all copies or
PennElectric 4:83bdb4efea8b 10 * substantial portions of the Software.
PennElectric 4:83bdb4efea8b 11 *
PennElectric 4:83bdb4efea8b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
PennElectric 4:83bdb4efea8b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
PennElectric 4:83bdb4efea8b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
PennElectric 4:83bdb4efea8b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
PennElectric 4:83bdb4efea8b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PennElectric 4:83bdb4efea8b 17 */
PennElectric 4:83bdb4efea8b 18
PennElectric 4:83bdb4efea8b 19 // ------------------------------ MAX518 Interfacing Library ------------------------------------
PennElectric 4:83bdb4efea8b 20
PennElectric 4:83bdb4efea8b 21
PennElectric 4:83bdb4efea8b 22 #include "MAX518.h"
PennElectric 4:83bdb4efea8b 23 #include "mbed.h"
PennElectric 4:83bdb4efea8b 24
PennElectric 4:83bdb4efea8b 25 // Class constructor 1, create i2c connection, update instance variables
PennElectric 4:83bdb4efea8b 26 DAC::DAC(PinName sda, PinName scl, int address, int freq, bool ack) : i2c(sda, scl) {
PennElectric 4:83bdb4efea8b 27 _address = address;
PennElectric 4:83bdb4efea8b 28 _ack = ack;
PennElectric 4:83bdb4efea8b 29 i2c.frequency(freq);
PennElectric 4:83bdb4efea8b 30 }
PennElectric 4:83bdb4efea8b 31 // Class constructor 2, create i2c connection, update instance variables
PennElectric 4:83bdb4efea8b 32 DAC::DAC(PinName sda, PinName scl, int address, bool ack) : i2c(sda, scl) {
PennElectric 4:83bdb4efea8b 33 _address = address;
PennElectric 4:83bdb4efea8b 34 _ack = ack;
PennElectric 4:83bdb4efea8b 35 }
PennElectric 4:83bdb4efea8b 36 // Class constructor 3, create i2c connection, update instance variables
PennElectric 4:83bdb4efea8b 37 DAC::DAC(PinName sda, PinName scl, int address, int freq) : i2c(sda, scl) {
PennElectric 4:83bdb4efea8b 38 _address = address;
PennElectric 4:83bdb4efea8b 39 _ack = true;
PennElectric 4:83bdb4efea8b 40 i2c.frequency(freq);
PennElectric 4:83bdb4efea8b 41 }
PennElectric 4:83bdb4efea8b 42 // Class constructor 4, create i2c connection, update instance variables
PennElectric 4:83bdb4efea8b 43 DAC::DAC(PinName sda, PinName scl, int address) : i2c(sda, scl) {
PennElectric 4:83bdb4efea8b 44 _address = address;
PennElectric 4:83bdb4efea8b 45 _ack = true;
PennElectric 4:83bdb4efea8b 46 }
PennElectric 4:83bdb4efea8b 47 // Set output of both DAC channels with ints 0 - 255
PennElectric 4:83bdb4efea8b 48 bool DAC::setDAC(int a, int b) {
PennElectric 4:83bdb4efea8b 49 if (a > 255 || a < 0 || b > 255 || b < 0)
PennElectric 4:83bdb4efea8b 50 return false; // Return if int a or int b is out of range
PennElectric 4:83bdb4efea8b 51 int check = 0; // Counter for ACK signals
PennElectric 4:83bdb4efea8b 52
PennElectric 4:83bdb4efea8b 53 i2c.start();
PennElectric 4:83bdb4efea8b 54 check += i2c.write(_address); // Write address
PennElectric 4:83bdb4efea8b 55 check += i2c.write(0); // Select Channel 1
PennElectric 4:83bdb4efea8b 56 check += i2c.write(a); // Write output value
PennElectric 4:83bdb4efea8b 57 check += i2c.write(1); // Select Channel 2
PennElectric 4:83bdb4efea8b 58 check += i2c.write(b); // Write output value
PennElectric 4:83bdb4efea8b 59 i2c.stop(); // End communication (trigger DAC to update outputs)
PennElectric 4:83bdb4efea8b 60
PennElectric 4:83bdb4efea8b 61 if (_ack && check != 5) return false; // Return false if device did not acknowledge when expected
PennElectric 4:83bdb4efea8b 62 else return true;
PennElectric 4:83bdb4efea8b 63 }
PennElectric 4:83bdb4efea8b 64
PennElectric 4:83bdb4efea8b 65 // Set output of DAC Channel 1 with int 0 - 255
PennElectric 4:83bdb4efea8b 66 bool DAC::setCh1(int n) {
PennElectric 4:83bdb4efea8b 67 if (n > 255 || n < 0) return false; // Return if n is out of range
PennElectric 4:83bdb4efea8b 68 int check = 0; // Counter for ACK signals
PennElectric 4:83bdb4efea8b 69
PennElectric 4:83bdb4efea8b 70 i2c.start();
PennElectric 4:83bdb4efea8b 71 check += i2c.write(_address); // Write address
PennElectric 4:83bdb4efea8b 72 check += i2c.write(0); // Select Channel 1
PennElectric 4:83bdb4efea8b 73 check += i2c.write(n); // Write output value
PennElectric 4:83bdb4efea8b 74 i2c.stop(); // End communication (trigger DAC to update outputs)
PennElectric 4:83bdb4efea8b 75
PennElectric 4:83bdb4efea8b 76 if (_ack && check != 3) return false; // Return false if device did not acknowledge when expected
PennElectric 4:83bdb4efea8b 77 else return true;
PennElectric 4:83bdb4efea8b 78 }
PennElectric 4:83bdb4efea8b 79
PennElectric 4:83bdb4efea8b 80 // Set output of DAC Channel 2 with int 0 - 255
PennElectric 4:83bdb4efea8b 81 bool DAC::setCh2(int n) {
PennElectric 4:83bdb4efea8b 82 if (n > 255 || n < 0) return false; // Return if n is out of range
PennElectric 4:83bdb4efea8b 83 int check = 0; // Counter for ACK signals
PennElectric 4:83bdb4efea8b 84
PennElectric 4:83bdb4efea8b 85 i2c.start();
PennElectric 4:83bdb4efea8b 86 check += i2c.write(_address); // Write address
PennElectric 4:83bdb4efea8b 87 check += i2c.write(1); // Select Channel 1
PennElectric 4:83bdb4efea8b 88 check += i2c.write(n); // Write output value
PennElectric 4:83bdb4efea8b 89 i2c.stop(); // End communication (trigger DAC to update outputs)
PennElectric 4:83bdb4efea8b 90
PennElectric 4:83bdb4efea8b 91 if (_ack && check != 3) return false; // Return false if device did not acknowledge when expected
PennElectric 4:83bdb4efea8b 92 else return true;
PennElectric 4:83bdb4efea8b 93 }
PennElectric 4:83bdb4efea8b 94
PennElectric 4:83bdb4efea8b 95 // Reset DAC (all outputs to 0)
PennElectric 4:83bdb4efea8b 96 bool DAC::reset() {
PennElectric 4:83bdb4efea8b 97 int check = 0; // Counter for ACK signals
PennElectric 4:83bdb4efea8b 98
PennElectric 4:83bdb4efea8b 99 i2c.start();
PennElectric 4:83bdb4efea8b 100 check += i2c.write(_address); // Write address
PennElectric 4:83bdb4efea8b 101 check += i2c.write(16); // Reset command (all outputs to 0)
PennElectric 4:83bdb4efea8b 102 i2c.stop(); // End communication (trigger DAC to update)
PennElectric 4:83bdb4efea8b 103
PennElectric 4:83bdb4efea8b 104 if (_ack && check != 2) return false; // Return false if device did not acknowledge when expected
PennElectric 4:83bdb4efea8b 105 else return true;
PennElectric 4:83bdb4efea8b 106 }
PennElectric 4:83bdb4efea8b 107
PennElectric 4:83bdb4efea8b 108 // Power down DAC into silent monitoring mode (outputs also go to 0)
PennElectric 4:83bdb4efea8b 109 bool DAC::powerDown() {
PennElectric 4:83bdb4efea8b 110 int check = 0; // Counter for ACK signals
PennElectric 4:83bdb4efea8b 111
PennElectric 4:83bdb4efea8b 112 i2c.start();
PennElectric 4:83bdb4efea8b 113 check += i2c.write(_address); // Write address
PennElectric 4:83bdb4efea8b 114 check += i2c.write(8); // Power down DAC into silent mode (outputs also go to 0)
PennElectric 4:83bdb4efea8b 115 i2c.stop(); // End communication (trigger DAC to update)
PennElectric 4:83bdb4efea8b 116
PennElectric 4:83bdb4efea8b 117 if (_ack && check != 2) return false; // Return false if device did not acknowledge when expected
PennElectric 4:83bdb4efea8b 118 else return true;
PennElectric 0:8ebf1c452028 119 }