MCP3021

From Steak Wiki
Revision as of 21:38, 30 June 2020 by Adminguy (talk | contribs)
Jump to navigationJump to search

This chip uses I2C. It is not the most common I2C you will find online. Instead of sending a requested register, you simply send the address of the ADC on the bus. The datasheet explains the expected comms in more detail, but it boils down to:

  • Host sends start bit, 7 bit address, and 1 bit at the end (1 for conversion, a 0 for getting only an announcement from the MCP).
  • MCP responds back with two bytes

This is slightly confusing, because Wire only sends a 7 bit address (not 8 bits) and there is no way to manually send bits (only bytes), that I found.

Default address is A5, which is 101 (5 binary) added to a base address of 1001. So 0b1001101. Different addresses can be obtained. See data sheet for more details. 0b1001101 is 77 in decimal.

This can be handled with the following Arduino Wire code:

#include <Wire.h>
int addr = 0b1001101; //(77 in dec)(however, technically
// an 8 bit i2c address, as it adds one bit to the end to 
// designate read/write or in this case, 
// read == check adc is accessible
// write == do adc conversion and respond with 2 bytes immediately
// after)

int U8_data = 0;
int L8_data = 0;

void setup(){
Serial.begin(9600);

Wire.begin();
Wire.setClock(10); //slow down clock, for debugging ease
}

void loop(){
delay(1000);

//Wire.beginTransmission(0b10011011);  
//Wire.beginTransmission(0b1001101);   //arduino library cuts off this
//only takes 7 bits, even if you want 8
//https://forum.arduino.cc/index.php?topic=482619.0 EDIT: this url was not helpful.
//Wire.endTransmission();
//i2c usually
//can either do beginTransmission w/writes and reads, then endTransmission
//or just requestFrom, without begin/end 

//For this adc, you want to request two bits, not do begintransmission
//or endtransmission


Wire.requestFrom(0b1001101, 2); //this appears to include the 8th
U8_data = Wire.read();
L8_data = Wire.read();

Serial.print("U8_data:");
Serial.println(U8_data);

Serial.print("L8_data:");
Serial.println(L8_data);
}

References