Browse Source

Include .lst files in output

Add dac_to_current scaling so we can set limits easily.
Change interrupt to do everything in terms of current.  Much simpler to understand, and is preferable if we can get away with using the cycles.


git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7067 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 15 years ago
parent
commit
8977dea014
5 changed files with 69 additions and 63 deletions
  1. +3
    -2
      firmware/adc.h
  2. +7
    -0
      firmware/scaling.c
  3. +1
    -0
      firmware/scaling.h
  4. +55
    -59
      firmware/zoom.c
  5. +3
    -2
      firmware/zoom.mcp

+ 3
- 2
firmware/adc.h View File

@@ -3,10 +3,11 @@
#include "config.h"
/* Initialize external 12-bit ADC (AD7450R) */
/* Initialize external 12-bit ADC (AD7450) */
void adc_init(void);
/* Trigger conversion and return it */
/* Trigger conversion and return it.
Result is a signed value (-2048 to +2047) */
int16_t adc_get(void);
#endif

+ 7
- 0
firmware/scaling.c View File

@@ -17,6 +17,13 @@ uint16_t current_to_dac(float amps)
return dacc;
}
float dac_to_current(uint16_t dacv)
{
float amps;
amps = dacv * (20.0 / 32768.0) - 20.0;
return amps;
}
float adc12_to_current(int16_t picv)
{
float amps;


+ 1
- 0
firmware/scaling.h View File

@@ -2,6 +2,7 @@
#define SCALING_H
float adc12_to_current(int16_t picv);
float dac_to_current(uint16_t dacv);
uint16_t current_to_dac(float amps);
#endif

+ 55
- 59
firmware/zoom.c View File

@@ -9,35 +9,35 @@
#include "scaling.h"

int send_data = 0;
uint16_t send_adc, send_dac;
uint16_t send_adc;
uint16_t send_dac;

/* DAC limits */
#define DAC_MIN 0x0000
#define DAC_MAX 0xffff
#define DAC_MAX 0xFFFF
float dac_current;

/* A "step" in the DAC output at the resolution we want. */
#define DAC_STEP 0x0004
float dac_current_min;
float dac_current_max;

/* 1 bit at the ADC is this much change at the DAC */
#define ADC_DAC_SCALING (1.0/21.234)
#define TIMER_RATE 10 /* how often to read the ADC and update DAC */
#define PC_RATE 10 /* how often to send data to the PC */

uint16_t dac_cmd = ((uint32_t)DAC_MAX + DAC_MIN) / 2;

#define TIMER_RATE 8000L
void TISR_HANDLER(5)
{
static int count = 0;
static uint16_t dac_cmd;
uint16_t v;
int32_t adjustment;
int32_t newdac;
float i;

/* toggle A9 every time an interrupt occurs */
LATAbits.LATA9 ^= 1;
timer_clear_txif(5);
/* Get most recent sample from 12-bit ADC. */
v = adc_get();

/* Send data to PC at 8 KHz */
if (count++ >= (TIMER_RATE / 8000)) {
/* Send data to PC at 1 Hz */
if (++count >= (TIMER_RATE / PC_RATE)) {
count = 0;
/* Send most recent sample and old DAC value */
@@ -46,38 +46,29 @@ void TISR_HANDLER(5)
send_data = 1;
}

/* Update DAC. Add whatever is necessary to cause the ADC
to change by 150% towards the center of n0x0800
(intentional overshoot to increase slew rate capability) */
adjustment = ((int32_t) v - 0x0800) * 1.50 * ADC_DAC_SCALING;
newdac = dac_cmd + adjustment;
if (1 || adjustment) {
if (newdac < DAC_MIN)
dac_cmd = DAC_MIN;
else if (newdac > DAC_MAX)
dac_cmd = DAC_MAX;
else
dac_cmd = newdac;
dac_write(dac_cmd);
}
}
/* Convert ADC value to current */
i = adc12_to_current(v);

void TISR_HANDLER(6)
{
uint16_t v;
timer_clear_txif(6);
v = adc_get();
send_adc = v;
send_data = 1;
/* Adjust DAC to null this current to 0 */
dac_current -= i;

if (dac_current < dac_current_min)
dac_current = dac_current_min;
if (dac_current > dac_current_max)
dac_current = dac_current_max;

/* Now send it out */
dac_cmd = current_to_dac(dac_current);
dac_write(dac_cmd);
}

void send_to_pc(void)
{
/* Sent data format:
1Aaa aaaa 0aaa aaDd 0ddd dddd 0ddd dddd
Aaaaaaaaaaaa = 12-bit ADC value
Dddddddddddddddd = 16-bit DAC command
*/
Aaaaaaaaaaaa = 12-bit ADC value (2s compliment signed)
Dddddddddddddddd = 16-bit DAC command (unsigned)
*/
uart1_put(0x80 | ((send_adc & 0x0FE0) >> 5));
uart1_put(((send_adc & 0x001F) << 2) | ((send_dac & 0xC000) >> 14));
uart1_put((send_dac & 0x3F80) >> 7);
@@ -136,20 +127,6 @@ void run_debug(void)
case '0':
dac = 32768;
break;
case 'g':
/* change baudrate */
/* read ADC at 8KHz */
/* send DAC and ADC values to PC */
uart1_init(500000);
timer_setup_16bit(6, 8000, 1);
send_dac = dac;
for (;;) {
if (send_data) {
send_to_pc();
send_data = 0;
}
}
break;
case 'd':
uart1_put_string("degauss...");
degauss();
@@ -163,10 +140,18 @@ void run_debug(void)
void run_normal(void)
{
uart1_init(500000);
// uart1_init(115200);
// uart1_put_string("Zoom NILM Normal\r\n");

/* Assume startup current is 0 */
dac_current = 0.0;
dac_write(current_to_dac(dac_current));
msleep(100);
degauss();

/* Set min/max */
dac_current_min = dac_to_current(DAC_MIN);
dac_current_max = dac_to_current(DAC_MAX);

timer_setup_16bit(5, TIMER_RATE, 1);
// uart1_put_string("Timer OK\r\n");

while(1) {
if (send_data) {
@@ -176,6 +161,14 @@ void run_normal(void)
}
}

/* This mode is used when the programmer is connected,
or PGD is tied to ground (jumper between ICD pins 3 and 4) */
#define MODE_1 run_normal

/* This mode is used when the programmer is not connected
and PGD is left floating */
#define MODE_2 run_debug

int main(void)
{
config_init();
@@ -184,6 +177,9 @@ int main(void)
TRISCbits.TRISC13 = 1;
CNPU1bits.CN1PUE = 1;

/* Output RA9 (PICVREF-) is used for debugging */
TRISAbits.TRISA9 = 0;

adcext_init();
dac_init();
dac_write(32768);
@@ -193,12 +189,12 @@ int main(void)

degauss();

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

for (;;)
continue;


+ 3
- 2
firmware/zoom.mcp View File

@@ -58,8 +58,9 @@ file_017=p33fj256gp710.gld
suite_guid={479DDE59-4D56-455E-855E-FFF59A3DB57E}
suite_state=
[TOOL_SETTINGS]
TS{7D9C6ECE-785D-44CB-BA22-17BF2E119622}=-g
TS{25AC22BD-2378-4FDB-BFB6-7345A15512D3}=-g -Wall
TS{7D9C6ECE-785D-44CB-BA22-17BF2E119622}=-g -ahl="$(BINDIR_)$(INFILEBASE).lst"
TS{25AC22BD-2378-4FDB-BFB6-7345A15512D3}=-g -Wall -Wa,-ahl="$(BINDIR_)$(INFILEBASE).lst"
TS{25AC22BD-2378-4FDB-BFB6-7345A15512D3}_alt=yes
TS{7DAC9A1D-4C45-45D6-B25A-D117C74E8F5A}=--heap=1024 -Map="$(BINDIR_)$(TARGETBASE).map" --report-mem -o"$(BINDIR_)$(TARGETBASE).$(TARGETSUFFIX)"
TS{509E5861-1E2A-483B-8B6B-CA8DB7F2DD78}=
[INSTRUMENTED_TRACE]


Loading…
Cancel
Save