Browse Source

Add degauss mode, scaling helpers, better handling of ADC result

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7009 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 15 years ago
parent
commit
19f41a087b
6 changed files with 100 additions and 12 deletions
  1. +8
    -2
      firmware/adc.c
  2. +1
    -1
      firmware/adc.h
  3. +31
    -0
      firmware/scaling.c
  4. +7
    -0
      firmware/scaling.h
  5. +38
    -1
      firmware/zoom.c
  6. +15
    -8
      firmware/zoom.mcp

+ 8
- 2
firmware/adc.c View File

@@ -21,7 +21,7 @@ void adc_init(void)
TRIS_SCLK = 0;
}
uint16_t adc_get(void)
int16_t adc_get(void)
{
uint16_t v = 0;
int i;
@@ -36,5 +36,11 @@ uint16_t adc_get(void)
LAT_SCLK = IO_HIGH;
}
LAT_CS = IO_HIGH;
return v;
/* Sign-extend the 12-bit value */
if (v & 0x0800)
v |= 0xF000;
else
v &= ~0xF000;
return (int16_t) v;
}

+ 1
- 1
firmware/adc.h View File

@@ -7,6 +7,6 @@
void adc_init(void);
/* Trigger conversion and return it */
uint16_t adc_get(void);
int16_t adc_get(void);
#endif

+ 31
- 0
firmware/scaling.c View File

@@ -0,0 +1,31 @@
#include "config.h"
#include "scaling.h"
#include "util.h"
uint16_t current_to_dac(float amps)
{
float tmp;
uint16_t dacc;
tmp = 32768 * (1 + amps / 20);
if (tmp < 0)
tmp = 0;
if (tmp > 65535)
tmp = 65535;
dacc = (uint16_t)(tmp + 0.5);
return dacc;
}
float adc12_to_current(int16_t picv)
{
float amps;
if (picv < 0)
picv = 0;
if (picv > 2047)
picv = 2047;
amps = (1024.0 - picv) / 3522.6;
return amps;
}

+ 7
- 0
firmware/scaling.h View File

@@ -0,0 +1,7 @@
#ifndef SCALING_H
#define SCALING_H
float adc12_to_current(int16_t picv);
uint16_t current_to_dac(float amps);
#endif

+ 38
- 1
firmware/zoom.c View File

@@ -6,6 +6,7 @@
#include "timer.h"
#include <stdio.h>
#include <math.h>
#include "scaling.h"

int send_data = 0;
uint16_t send_adc, send_dac;
@@ -70,6 +71,16 @@ void TISR_HANDLER(6)
send_data = 1;
}

/* Very basic, assumes timer1 is set up at 1 khz */
void wait_msec(int ms)
{
while (ms-- >= 0) {
IFS0bits.T1IF = 0;
while(IFS0bits.T1IF == 0)
continue;
}
}

void send_to_pc(void)
{
/* Sent data format:
@@ -83,9 +94,20 @@ void send_to_pc(void)
uart1_put((send_dac & 0x007F));
}

void degauss(void)
{
dac_write(65535); /* max */
wait_msec(25);
dac_write(0); /* min */
wait_msec(25);
dac_write(32768); /* middle */
wait_msec(25);
}

void run_debug(void)
{
uint16_t dac = 32768;
int16_t adc;
uart1_init(115200);

uart1_put_string("Zoom NILM Debug\r\n");
@@ -94,7 +116,10 @@ void run_debug(void)
dac_write(dac);
uart1_put_dec(dac);
uart1_put(' ');
uart1_put_hex16(adc_get());
adc = adc_get();
uart1_put_hex16(adc);
uart1_put(' ');
uart1_put_dec(adc);
uart1_crlf();
switch (uart1_get()) {
case '[':
@@ -135,6 +160,12 @@ void run_debug(void)
}
}
break;
case 'd':
uart1_put_string("degauss...");
degauss();
uart1_crlf();
dac = 32768;
break;
}
}
}
@@ -163,10 +194,16 @@ int main(void)
TRISCbits.TRISC13 = 1;
CNPU1bits.CN1PUE = 1;

/* millisecond timer */
timer_setup_16bit(1, 1000, 0);

adcext_init();
dac_init();
dac_write(32768);
adc_init();

degauss();

/* If PGD is externally tied to ground, run in debug mode.
(Short ICD pins 3 and 4) */
if (PORTCbits.RC13 == 0)


+ 15
- 8
firmware/zoom.mcp View File

@@ -33,6 +33,8 @@ file_012=no
file_013=no
file_014=no
file_015=no
file_016=no
file_017=no
[FILE_INFO]
file_000=zoom.c
file_001=config.c
@@ -42,14 +44,16 @@ file_004=dac.c
file_005=timer.c
file_006=adcext.c
file_007=adc.c
file_008=config.h
file_009=uart.h
file_010=util.h
file_011=dac.h
file_012=timer.h
file_013=adcext.h
file_014=adc.h
file_015=p33fj256gp710.gld
file_008=scaling.c
file_009=config.h
file_010=uart.h
file_011=util.h
file_012=dac.h
file_013=timer.h
file_014=adcext.h
file_015=adc.h
file_016=scaling.h
file_017=p33fj256gp710.gld
[SUITE_INFO]
suite_guid={479DDE59-4D56-455E-855E-FFF59A3DB57E}
suite_state=
@@ -62,3 +66,6 @@ TS{509E5861-1E2A-483B-8B6B-CA8DB7F2DD78}=
enable=0
transport=0
format=0
[CUSTOM_BUILD]
Pre-Build=
Post-Build=

Loading…
Cancel
Save