Suga koubou
/
Wattmeter_sample
AC current sensor
main.cpp@0:08313411c8a1, 2011-01-16 (annotated)
- Committer:
- okini3939
- Date:
- Sun Jan 16 03:09:47 2011 +0000
- Revision:
- 0:08313411c8a1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:08313411c8a1 | 1 | #include "mbed.h" |
okini3939 | 0:08313411c8a1 | 2 | #include <math.h> |
okini3939 | 0:08313411c8a1 | 3 | |
okini3939 | 0:08313411c8a1 | 4 | Ticker ticker; |
okini3939 | 0:08313411c8a1 | 5 | volatile long val; |
okini3939 | 0:08313411c8a1 | 6 | volatile int offset, count; |
okini3939 | 0:08313411c8a1 | 7 | |
okini3939 | 0:08313411c8a1 | 8 | DigitalOut myled(LED1); |
okini3939 | 0:08313411c8a1 | 9 | AnalogIn adc_offset(p15), adc_watt1(p16), adc_watt3(p18); |
okini3939 | 0:08313411c8a1 | 10 | Serial pc(USBTX, USBRX); |
okini3939 | 0:08313411c8a1 | 11 | |
okini3939 | 0:08313411c8a1 | 12 | void sample_watt () { |
okini3939 | 0:08313411c8a1 | 13 | int i; |
okini3939 | 0:08313411c8a1 | 14 | |
okini3939 | 0:08313411c8a1 | 15 | i = ((adc_watt3.read_u16() >> 4) & 0x0fff) - offset; |
okini3939 | 0:08313411c8a1 | 16 | if (i < -1 || i > 1) { |
okini3939 | 0:08313411c8a1 | 17 | val = val + (i * i); |
okini3939 | 0:08313411c8a1 | 18 | } |
okini3939 | 0:08313411c8a1 | 19 | count ++; |
okini3939 | 0:08313411c8a1 | 20 | } |
okini3939 | 0:08313411c8a1 | 21 | |
okini3939 | 0:08313411c8a1 | 22 | int main() { |
okini3939 | 0:08313411c8a1 | 23 | float v, f, g; |
okini3939 | 0:08313411c8a1 | 24 | |
okini3939 | 0:08313411c8a1 | 25 | offset = (adc_offset.read_u16() >> 4) & 0x0fff; |
okini3939 | 0:08313411c8a1 | 26 | val = 0; |
okini3939 | 0:08313411c8a1 | 27 | count = 0; |
okini3939 | 0:08313411c8a1 | 28 | |
okini3939 | 0:08313411c8a1 | 29 | ticker.attach_us(&sample_watt, 100); // 10kHz |
okini3939 | 0:08313411c8a1 | 30 | pc.printf("begin\r\n"); |
okini3939 | 0:08313411c8a1 | 31 | |
okini3939 | 0:08313411c8a1 | 32 | while(1) { |
okini3939 | 0:08313411c8a1 | 33 | if (count >= 10000) { |
okini3939 | 0:08313411c8a1 | 34 | myled = 1; |
okini3939 | 0:08313411c8a1 | 35 | // RMS |
okini3939 | 0:08313411c8a1 | 36 | __disable_irq(); |
okini3939 | 0:08313411c8a1 | 37 | v = sqrt((float)val / count) * (3.3 / 0x1000); |
okini3939 | 0:08313411c8a1 | 38 | f = v / (0.99 * 330.0 / 3000.0); |
okini3939 | 0:08313411c8a1 | 39 | val = 0; |
okini3939 | 0:08313411c8a1 | 40 | count = 0; |
okini3939 | 0:08313411c8a1 | 41 | |
okini3939 | 0:08313411c8a1 | 42 | g = ((adc_watt1.read_u16() >> 4) & 0x0fff) - offset; |
okini3939 | 0:08313411c8a1 | 43 | g = g * (3.3 / 0x1000); |
okini3939 | 0:08313411c8a1 | 44 | g = g / (0.99 * 330.0 / 3000.0); |
okini3939 | 0:08313411c8a1 | 45 | |
okini3939 | 0:08313411c8a1 | 46 | offset = (adc_offset.read_u16() >> 4) & 0x0fff; |
okini3939 | 0:08313411c8a1 | 47 | __enable_irq(); |
okini3939 | 0:08313411c8a1 | 48 | |
okini3939 | 0:08313411c8a1 | 49 | pc.printf("%.2f A ave , ", g); |
okini3939 | 0:08313411c8a1 | 50 | pc.printf("%.2f A rms\r\n", f); |
okini3939 | 0:08313411c8a1 | 51 | |
okini3939 | 0:08313411c8a1 | 52 | myled = 0; |
okini3939 | 0:08313411c8a1 | 53 | } |
okini3939 | 0:08313411c8a1 | 54 | } |
okini3939 | 0:08313411c8a1 | 55 | } |