Program for reading data from one (or two) ACS712 (current sense amplifier). Read the configuration before trying it.

Dependencies:   mbed

This program aims at implementing a simple current reader which, sensing from an ACS712, prints on the USB port the average of n samples.

The program can be configured at compile-time by setting few defines:

  • the number of channels: either only p20 or the sum of p19 and p20;
  • the number of samples to read before printing their average
  • the operating mode:
    • PIN_TRIGGERED: read a handful of samples when the pin p9 rises
    • USER_TRIGGERED: start reading when the user presses the character 's'
    • ENDLESS_READ: read continuosly

First of all, when the program starts, it read a bunch of samples and then sets their mean value as reference. This means that the software assumes that when such a reference value is read, there is no current absorption.

Committer:
mariob
Date:
Tue Mar 12 12:20:25 2013 +0000
Revision:
0:36d465acb1a2
MB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 0:36d465acb1a2 1 /**********************************************************
mariob 0:36d465acb1a2 2 * CONFIGURATION: before starting, configure the reader
mariob 0:36d465acb1a2 3 **********************************************************/
mariob 0:36d465acb1a2 4
mariob 0:36d465acb1a2 5 //define the number of channels to use:
mariob 0:36d465acb1a2 6 // -) 1 channel: p20
mariob 0:36d465acb1a2 7 // -) 2 channels: p19 and p20
mariob 0:36d465acb1a2 8 #define NCHANNELS 1
mariob 0:36d465acb1a2 9 //#define nChannels 1
mariob 0:36d465acb1a2 10
mariob 0:36d465acb1a2 11 //number of samples to read before printing their average
mariob 0:36d465acb1a2 12 #define NSAMPLES 2500
mariob 0:36d465acb1a2 13
mariob 0:36d465acb1a2 14 //number of samplng operation after an user trigger
mariob 0:36d465acb1a2 15 //(total samples: NSAMPLES*NSAMPLING)
mariob 0:36d465acb1a2 16 #define NSAMPLING 50
mariob 0:36d465acb1a2 17
mariob 0:36d465acb1a2 18 //read NSAMPLES values when the pin p9 is high
mariob 0:36d465acb1a2 19 #define PIN_TRIGGERED 2
mariob 0:36d465acb1a2 20
mariob 0:36d465acb1a2 21 //the sampling is run only when the user presses the character 's'
mariob 0:36d465acb1a2 22 //the operation lasts for NSAMPLING iterations
mariob 0:36d465acb1a2 23 #define USER_TRIGGERED 1
mariob 0:36d465acb1a2 24
mariob 0:36d465acb1a2 25 //read all the time
mariob 0:36d465acb1a2 26 #define ENDLESS_READ 0
mariob 0:36d465acb1a2 27
mariob 0:36d465acb1a2 28 #define ACQ_TYPE USER_TRIGGERED
mariob 0:36d465acb1a2 29
mariob 0:36d465acb1a2 30
mariob 0:36d465acb1a2 31 /**********************************************************
mariob 0:36d465acb1a2 32 * CONFIGURATION CHECK
mariob 0:36d465acb1a2 33 **********************************************************/
mariob 0:36d465acb1a2 34 #if !defined(NCHANNELS) || (NCHANNELS!=1 && NCHANNELS!=2)
mariob 0:36d465acb1a2 35 #error "The number of channels is wrong"
mariob 0:36d465acb1a2 36 #endif
mariob 0:36d465acb1a2 37
mariob 0:36d465acb1a2 38 #if !defined(ACQ_TYPE) || (ACQ_TYPE!=ENDLESS_READ && \
mariob 0:36d465acb1a2 39 ACQ_TYPE!=PIN_TRIGGERED && ACQ_TYPE!=USER_TRIGGERED)
mariob 0:36d465acb1a2 40 #error "Acquisition type not detected or not valid"
mariob 0:36d465acb1a2 41 #endif
mariob 0:36d465acb1a2 42
mariob 0:36d465acb1a2 43 #if ACQ_TYPE==USER_TRIGGERED && !defined(NSAMPLING)
mariob 0:36d465acb1a2 44 #error "Define a number of samplings after the user trigger"
mariob 0:36d465acb1a2 45 #endif
mariob 0:36d465acb1a2 46
mariob 0:36d465acb1a2 47
mariob 0:36d465acb1a2 48 /**********************************************************
mariob 0:36d465acb1a2 49 * CODE
mariob 0:36d465acb1a2 50 **********************************************************/
mariob 0:36d465acb1a2 51
mariob 0:36d465acb1a2 52 #include "mbed.h"
mariob 0:36d465acb1a2 53
mariob 0:36d465acb1a2 54 AnalogIn ain_1(p20);
mariob 0:36d465acb1a2 55 #if NCHANNELS==2
mariob 0:36d465acb1a2 56 AnalogIn ain_2(p19);
mariob 0:36d465acb1a2 57 #endif
mariob 0:36d465acb1a2 58
mariob 0:36d465acb1a2 59 #if ACQ_TYPE==PIN_TRIGGERED
mariob 0:36d465acb1a2 60 DigitalIn firer (p9);
mariob 0:36d465acb1a2 61 #endif
mariob 0:36d465acb1a2 62
mariob 0:36d465acb1a2 63 #define V_max 3.3
mariob 0:36d465acb1a2 64
mariob 0:36d465acb1a2 65 int main()
mariob 0:36d465acb1a2 66 {
mariob 0:36d465acb1a2 67 double vref_1 = 0.0;
mariob 0:36d465acb1a2 68 #if NCHANNELS==2
mariob 0:36d465acb1a2 69 double vref_2 = 0.0;
mariob 0:36d465acb1a2 70 #endif
mariob 0:36d465acb1a2 71
mariob 0:36d465acb1a2 72 //acquiring v_ref
mariob 0:36d465acb1a2 73 for (int i=0; i<NSAMPLES; i++) {
mariob 0:36d465acb1a2 74 vref_1 += ain_1;
mariob 0:36d465acb1a2 75 #if NCHANNELS==2
mariob 0:36d465acb1a2 76 vref_2 += ain_2;
mariob 0:36d465acb1a2 77 #endif
mariob 0:36d465acb1a2 78 }
mariob 0:36d465acb1a2 79 vref_1 /= NSAMPLES;
mariob 0:36d465acb1a2 80 printf("Vref 1: %f\n\r", vref_1);
mariob 0:36d465acb1a2 81 #if NCHANNELS==2
mariob 0:36d465acb1a2 82 vref_2 /= NSAMPLES;
mariob 0:36d465acb1a2 83 printf("Vref 2: %f\n\r", vref_2);
mariob 0:36d465acb1a2 84 #endif
mariob 0:36d465acb1a2 85
mariob 0:36d465acb1a2 86 //main loop
mariob 0:36d465acb1a2 87 while(true) {
mariob 0:36d465acb1a2 88 //start condition
mariob 0:36d465acb1a2 89 #if ACQ_TYPE==USER_TRIGGERED
mariob 0:36d465acb1a2 90 printf("press \'s\' to start\n\r");
mariob 0:36d465acb1a2 91 int c;
mariob 0:36d465acb1a2 92 do {
mariob 0:36d465acb1a2 93 c = getchar();
mariob 0:36d465acb1a2 94 } while (c!='s');
mariob 0:36d465acb1a2 95 for (int i=0; i<NSAMPLING; i++) {
mariob 0:36d465acb1a2 96 #elif ACQ_TYPE==PIN_TRIGGERED
mariob 0:36d465acb1a2 97 while(firer==0);
mariob 0:36d465acb1a2 98 #elif ACQ_TYPE==ENDLESS_READ
mariob 0:36d465acb1a2 99 #else
mariob 0:36d465acb1a2 100 //already signaled
mariob 0:36d465acb1a2 101 //#error "Acquisition type not detected"
mariob 0:36d465acb1a2 102 #endif
mariob 0:36d465acb1a2 103
mariob 0:36d465acb1a2 104 //acquisition
mariob 0:36d465acb1a2 105 double sum_1 = 0.0;
mariob 0:36d465acb1a2 106 #if NCHANNELS==2
mariob 0:36d465acb1a2 107 double sum_2 = 0.0;
mariob 0:36d465acb1a2 108 #endif
mariob 0:36d465acb1a2 109 for (int samples = 0; samples<NSAMPLES; samples++) {
mariob 0:36d465acb1a2 110 sum_1 += ain_1;
mariob 0:36d465acb1a2 111 #if NCHANNELS==2
mariob 0:36d465acb1a2 112 sum_2 += ain_2;
mariob 0:36d465acb1a2 113 #endif
mariob 0:36d465acb1a2 114 }
mariob 0:36d465acb1a2 115 sum_1 /= NSAMPLES;
mariob 0:36d465acb1a2 116 sum_1 = (sum_1-vref_1)*V_max/0.185;
mariob 0:36d465acb1a2 117 #if NCHANNELS==2
mariob 0:36d465acb1a2 118 sum_2 /= NSAMPLES;
mariob 0:36d465acb1a2 119 sum_2 = (sum_2-vref_2)*V_max/0.185;
mariob 0:36d465acb1a2 120 printf("%f\n\r", sum_1+sum_2);
mariob 0:36d465acb1a2 121 #else
mariob 0:36d465acb1a2 122 printf("%f\n\r", sum_1);
mariob 0:36d465acb1a2 123 #endif
mariob 0:36d465acb1a2 124
mariob 0:36d465acb1a2 125 //final condition
mariob 0:36d465acb1a2 126 #if ACQ_TYPE==USER_TRIGGERED
mariob 0:36d465acb1a2 127 }
mariob 0:36d465acb1a2 128 #elif ACQ_TYPE==PIN_TRIGGERED
mariob 0:36d465acb1a2 129 #elif ACQ_TYPE==ENDLESS_READ
mariob 0:36d465acb1a2 130 #else
mariob 0:36d465acb1a2 131 #endif
mariob 0:36d465acb1a2 132 }
mariob 0:36d465acb1a2 133 }