Difference between revisions of "MCP3021"

From Steak Wiki
Jump to navigationJump to search
Line 60: Line 60:
 
* Art of Electronics 3rd edition, ADC chapter.
 
* Art of Electronics 3rd edition, ADC chapter.
 
* https://tronixstuff.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/ - This tutorial shows a variety of i2c applications w/wire, and a cursory glance through the code explains how you can use both beginTransmission/endTransmission, as well as omitting those entirely and only using requestFrom
 
* https://tronixstuff.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/ - This tutorial shows a variety of i2c applications w/wire, and a cursory glance through the code explains how you can use both beginTransmission/endTransmission, as well as omitting those entirely and only using requestFrom
 +
 +
{{Electronics}}

Revision as of 21:41, 30 June 2020

This chip uses I2C. It is not the most common I2C you will find online. Instead of sending a requested register or command to execute, 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. It turns out that using requestFrom instead of beginTransmission / endTransmission will allow the 8th bit to be set high, and start a conversion.

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