Created
December 16, 2019 11:34
-
-
Save Electronza/1ff40a0d211858ba483ad80821c240de to your computer and use it in GitHub Desktop.
4-20mA R click calibration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
____ __ ____ ___ ____ ____ __ __ _ ____ __ | |
( __)( ) ( __)/ __)(_ _)( _ \ / \ ( ( \(__ ) / _\ | |
) _) / (_/\ ) _)( (__ )( ) /( O )/ / / _/ / \ | |
(____)\____/(____)\___) (__) (__\_) \__/ \_)__)(____)\_/\_/ | |
Project name: | |
Project page: https://electronza.com/4-20ma-current-loop-revisited-a-simpler-calibration-procedure | |
Description: | |
********************************************************************/ | |
/* | |
4-20mA R click calibration | |
Reads result on 4-20mA bus and outputs the | |
result of the conversion on serial monitor | |
*/ | |
#include <SPI.h> | |
// Arduino UNO with Mikroe Arduino Uno Click shield | |
// 4-20mA R click is placed in socket #2 | |
// CS is pin 9 | |
// SCK is pin 13 | |
// MISO is pin 12 | |
// MOSI is pin 11 | |
#define ADC_CS 9 | |
unsigned int ADC_result; | |
float ADC_avrg; | |
void setup() { | |
/* Resetting MCP3201 | |
* From MCP3201 datasheet: If the device was powered up | |
* with the CS pin low, it must be brought high and back low | |
* to initiate communication. | |
* The device will begin to sample the analog | |
* input on the first rising edge after CS goes low. */ | |
pinMode (ADC_CS, OUTPUT); | |
digitalWrite(ADC_CS, 0); | |
delay(100); | |
digitalWrite(ADC_CS, 1); | |
// initialize serial | |
Serial.begin(9600); | |
// initialize SPI | |
SPI.begin(); | |
} | |
void loop() { | |
ADC_avrg = 0; | |
// Average of 100 measurements | |
for (int i=0; i<100; i++){ | |
// put your main code here, to run repeatedly: | |
ADC_result = get_ADC(); | |
//Serial.print("Conversion result: "); | |
//Serial.println(ADC_result); | |
// Dont print too often | |
delay(10); | |
ADC_avrg = ADC_avrg + ADC_result; | |
} | |
Serial.print("Conversion average: "); | |
Serial.println(ADC_avrg / 100); | |
delay(500); | |
} | |
unsigned int get_ADC(void){ | |
/* | |
DAC works on SPI | |
We receive 16 bits | |
Of which we extract only 12 bits | |
MCP3201 has a strange way of formatting data | |
with 5 bits in the first byte and | |
the rest of 7 bits in the second byte | |
*/ | |
unsigned int result; | |
unsigned int first_byte; | |
unsigned int second_byte; | |
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1)); | |
digitalWrite(ADC_CS, 0); | |
first_byte = SPI.transfer(0); | |
second_byte = SPI.transfer(0); | |
digitalWrite(ADC_CS, 1); | |
SPI.endTransaction(); | |
/* After the second eight clocks have been | |
sent to the device, the MCU receive register | |
will contain the lowest order seven bits and | |
the B1 bit repeated as the A/D Converter has begun | |
to shift out LSB first data with the extra clock. | |
Typical procedure would then call for the lower order | |
byte of data to be shifted right by one bit | |
to remove the extra B1 bit. | |
See MCP3201 datasheet, page 15 | |
*/ | |
result = ((first_byte & 0x1F) << 8) | second_byte; | |
result = result >> 1; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment