David Pilling |
Main »
Differential ADCStarted 25th July 2017 - a differential ADC library for Arduino Differential ADC is also commonly available on the Arduino Mega 2560 which uses the ATMega 2560 chip. There are 1280 versions of both. There is also an ATMega1284 which has Arduino support and differential ADC. Photo shows a Pro Mini and a Pro Micro. My first efforts to write code came to grief through setting out using a Pro Micro and failing to grasp that ADC0 and ADC1 in the Atmel documentation mapped to A4 and A5 in Arduino-land, which do not exist on a Pro Micro. So my program was never going to work. The only example code I found is listed below [1][2]. The blog library [2] won't compile on modern Arduino and the blogger notes as supplied it does not work entirely correctly. This is because it has had some delay() lines commented out. But by fixing this code and using an Arduino Mega 2560 I actually got good results for differential ADC. In Arduino-land, one uses identifiers like A0 to refer to an analog pin. Internally A0 is defined as:
There is then a mapping between A0 and analog channels
Which gets something like A0 down to the number after the A i.e. 0 - the Arduino channel. Finally there is a mapping between Arduino channels and AVR channels (analogPinToChannel(P)) taking 0 to ADC7 (this mapping only takes place for the 32u4). const uint8_t PROGMEM analog_pin_to_channel_PGM[12] = { 7, // A0 PF7 ADC7 6, // A1 PF6 ADC6 5, // A2 PF5 ADC5 4, // A3 PF4 ADC4 1, // A4 PF1 ADC1 0, // A5 PF0 ADC0 taken from pins_arduino.txt In the AVR data sheet there is a table "Table 24-4. Input Channel and Gain Selections" (ATMega 32u4) and "Table 26-4. Input Channel Selections" (ATMega 2560) which has 64 entries that map to various combinations of ADC channels and gains. The row index number of the entry provides the 6 bit MUX code; the low 5 bits go into the ADMUX register and the high bit goes into the MUX5 bit in the ADCSRB register. The idea of the blog code is to input a "channel" number to the functions. At best this is a row index number from the data sheet table (in other words it has no independent meaning); for the 32u4 the Arduino to AVR channel mapping in the code means some values can not be obtained. Below is a download of my differential ADC library and an example program. I have tested this on an Arduino Leonardo and an Arduino Mega 2560. I've supplied code for the ATMega 1284 but it is untested. The second download shows the ADC library being used with the ADC free running and returning results from interrupts. See also my design for a soldering iron controller which uses this library.
Bug fixed versions of the code above are pending - see comment below. Presumably the job of software is to remove hardware dependence, so one should have a function that takes two input channels and the gain value and returns the result. However most of those combinations will not be valid and different processors offer different gains - some things will only work on a given processor. The first function in the library is: Arduino provides DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56 and EXTERNAL. These are a number 0..3 which provides two bits to go into the REF0 and REF1 bits of the ADMUX register. Consult the processor data sheet.
Onaka - 27th January 2018 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #define TABLESIZE 64 static tablestr codetable[TABLESIZE]= { 0,UNUSEDCHANNEL,1, 1,UNUSEDCHANNEL,1, 2,UNUSEDCHANNEL,1, 3,UNUSEDCHANNEL,1, 4,UNUSEDCHANNEL,1, 5,UNUSEDCHANNEL,1, 6,UNUSEDCHANNEL,1, 7,UNUSEDCHANNEL,1, 0,0,10, 1,0,10, //This line was incorrectly listed as 1,0,1, causing the gain to be wrong 0,0,200,
|
David Pilling's Wiki Set view |
Page last modified on February 10, 2018, at 03:01 PM - Powered by PmWiki pmwiki-2.3.31 (pmwiki-2.3.31) |