Created
October 17, 2012 05:24
-
-
Save 2N2222A/3903835 to your computer and use it in GitHub Desktop.
Arduino voltmeter using an MCP3421 18 bit ∆-∑ ADC
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
/* | |
This revision includes the modified display output to the LCD | |
mcp adress = 0x68 | |
sr= Sample Rate Selection | |
sr=0 ; 00 = 240 SPS (12 bits), | |
sr=1 ; 01 = 60 SPS (14 bits), | |
sr=2 ; 10 = 15 SPS (16 bits), | |
sr=3 ; 11 = 3.75 SPS (18 bits) | |
pga= PGA Programmable Gain Amplifier Selector | |
0 = 1 V/V, | |
1 = 2 V/V, | |
2 = 4 V/V, | |
******--MCP.init(address,sr,pga);--****** | |
*/ | |
#include <Wire.h> | |
#include "MCP3421.h" | |
MCP3421 MCP = MCP3421(); | |
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); | |
//int pinVCC=17; // pin A3 used as + Powersupply for ADC | |
//int pinGND=16; // pin A2 used as GND Powersupply for ADC | |
int pinLed=13; | |
char st1[20]; | |
long l1; | |
double vin; | |
float f1; | |
void setup(void) | |
{ | |
lcd.begin(16,2); | |
//pinMode(pinVCC,OUTPUT); //Not Necessary unless you want to use the analog | |
//pinMode(pinGND,OUTPUT); //pins as a power supply output for the ADC | |
pinMode(pinLed,OUTPUT); | |
//digitalWrite(pinVCC,1); // switch on pin for MCP3421 5 volt powersupply | |
lcd.print("ADC MCP 3421"); // just to be sure things are working | |
Wire.begin(); | |
delay(1000); | |
lcd.setCursor(0,1); | |
lcd.print("begin"); | |
delay(2000); | |
lcd.clear(); | |
MCP.init(0x68,3,0); | |
} | |
void loop(void) | |
{ | |
while(MCP.ready()==0); | |
digitalWrite(pinLed,1); | |
vin=MCP.getDouble(); | |
f1=vin; | |
analogRead(0); | |
delay(10); | |
int v1=analogRead(0); | |
int calibration=map(v1,0,1023,0,255); | |
float prescale=(calibration*0.01); | |
lcd.setCursor(1,0); | |
float voltage=(f1*(11+prescale)); | |
float q=abs(voltage); | |
if (q>=10.0){ | |
lcd.setCursor(2,0); | |
lcd.print(q,2); | |
}else{ | |
lcd.setCursor(2,0); | |
lcd.print(q,3); | |
digitalWrite(pinLed,0); | |
delay(200); | |
} | |
//****************************************************************************************** | |
void sprintDouble( char *str, double val, byte precision){ | |
// prints val with number of decimal places determine by precision | |
// precision is a number from 0 to 6 indicating the desired decimial places | |
// example: lcdPrintDouble( 3.1415, 2); // prints 3.14 (two decimal places) | |
char st2[16]; | |
unsigned long frac; | |
unsigned long mult = 1; | |
int mant= int(val); | |
byte padding = precision -1; | |
byte sgn=0; | |
sprintf(str,""); | |
if (val < 0) sprintf(str,"-"); | |
mant=abs(mant); | |
sprintf(st2,"%d",mant); //prints the int part | |
strcat(str,st2); | |
if( precision > 0) { | |
strcat(str,"."); | |
while(precision--) | |
mult *=10; | |
if(val >= 0) | |
frac = (val - int(val)) * mult; | |
else | |
frac = (int(val)- val ) * mult; | |
unsigned long frac1 = frac; | |
int cnt=precision; | |
// while( frac1 /= 10 ) | |
while( frac1 = frac1 / 10 & cnt-- ) | |
padding--; | |
while( padding--) strcat(str,"0"); | |
sprintf(st2,"%ld",frac); | |
strcat(str,st2); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is the "MCP3421.h" file ?