MCP3021
MCP3021.cpp@0:423652b49d07, 2014-09-02 (annotated)
- Committer:
- Reiko
- Date:
- Tue Sep 02 15:34:15 2014 +0000
- Revision:
- 0:423652b49d07
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reiko | 0:423652b49d07 | 1 | #include "MCP3021.h" |
Reiko | 0:423652b49d07 | 2 | |
Reiko | 0:423652b49d07 | 3 | //Create instance |
Reiko | 0:423652b49d07 | 4 | MCP3021::MCP3021(PinName sda, PinName scl, float supplyVoltage) : i2c(sda, scl), _supplyVoltage(supplyVoltage) { |
Reiko | 0:423652b49d07 | 5 | } |
Reiko | 0:423652b49d07 | 6 | |
Reiko | 0:423652b49d07 | 7 | //destroy instance |
Reiko | 0:423652b49d07 | 8 | MCP3021::~MCP3021() { |
Reiko | 0:423652b49d07 | 9 | } |
Reiko | 0:423652b49d07 | 10 | |
Reiko | 0:423652b49d07 | 11 | float MCP3021::read() { |
Reiko | 0:423652b49d07 | 12 | |
Reiko | 0:423652b49d07 | 13 | //You cannot write to an MCP3021, it has no writable registers. |
Reiko | 0:423652b49d07 | 14 | //MCP3021 also requires an ACKnowledge between each byte sent, before it will send the next byte. So we need to be a bit manual with how we talk to it. |
Reiko | 0:423652b49d07 | 15 | //It also needs an (NOT) ACKnowledge after the second byte or it will keep sending bytes (continuous sampling) |
Reiko | 0:423652b49d07 | 16 | // |
Reiko | 0:423652b49d07 | 17 | //From the datasheet. |
Reiko | 0:423652b49d07 | 18 | // |
Reiko | 0:423652b49d07 | 19 | //I2C.START |
Reiko | 0:423652b49d07 | 20 | //Send 8 bit device/ part address to open conversation. (See .h file for part explanation) |
Reiko | 0:423652b49d07 | 21 | //read a byte (with ACK) |
Reiko | 0:423652b49d07 | 22 | //read a byte (with NAK) |
Reiko | 0:423652b49d07 | 23 | //I2C.STOP |
Reiko | 0:423652b49d07 | 24 | |
Reiko | 0:423652b49d07 | 25 | |
Reiko | 0:423652b49d07 | 26 | // char data[2]; |
Reiko | 0:423652b49d07 | 27 | |
Reiko | 0:423652b49d07 | 28 | i2c.start(); |
Reiko | 0:423652b49d07 | 29 | int acknowledged = i2c.write(MCP3021_CONVERSE); //send a byte to start the conversation. It should be acknowledged. |
Reiko | 0:423652b49d07 | 30 | _data[0] = i2c.read(1); //read a byte. acknowledge when we have it. |
Reiko | 0:423652b49d07 | 31 | _data[1] = i2c.read(0); //read the second byte. (n)acknowledge when we have it to stop the flow. |
Reiko | 0:423652b49d07 | 32 | i2c.stop(); |
Reiko | 0:423652b49d07 | 33 | |
Reiko | 0:423652b49d07 | 34 | //convert to 12 bit. |
Reiko | 0:423652b49d07 | 35 | short res; |
Reiko | 0:423652b49d07 | 36 | int _10_bit_var; // 2 bytes |
Reiko | 0:423652b49d07 | 37 | char _4_bit_MSnibble = _data[0]; // 1 byte, example 0000 1000 |
Reiko | 0:423652b49d07 | 38 | char _6_bit_LSByte = _data[1]; // 1 byte, example 1111 0000 |
Reiko | 0:423652b49d07 | 39 | |
Reiko | 0:423652b49d07 | 40 | _10_bit_var = ((0x0F & _4_bit_MSnibble) << 6) | _6_bit_LSByte >> 2; //example 100011110000 |
Reiko | 0:423652b49d07 | 41 | res = _10_bit_var; |
Reiko | 0:423652b49d07 | 42 | |
Reiko | 0:423652b49d07 | 43 | return (_supplyVoltage / 1024) * res; |
Reiko | 0:423652b49d07 | 44 | |
Reiko | 0:423652b49d07 | 45 | } |