Browse Source

Fake 10-bit mode, using random number lookup for low bits.

Untested.  Need to test 16-bit mode (current compilation) first
then go back to 10-bit.

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@8027 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 13 years ago
parent
commit
af9b2b1ce4
9 changed files with 73 additions and 27 deletions
  1. +5
    -1
      firmware/calibrate.c
  2. +5
    -5
      firmware/calibrate.h
  3. +17
    -6
      firmware/dac.c
  4. +28
    -4
      firmware/dac.h
  5. +12
    -7
      firmware/mode_debug.c
  6. +2
    -2
      firmware/mode_normal.c
  7. +1
    -1
      firmware/zoom.c
  8. +3
    -1
      firmware/zoom.mcp
  9. BIN
      firmware/zoom.mcw

+ 5
- 1
firmware/calibrate.c View File

@@ -12,7 +12,7 @@ float g_scale; /* delta(DAC) / delta(ADC) */
/* Initialize. Assume some relatively-safe scaling if no calibration is run */
void calibrate_init(void)
{
g_scale = 0.5; /* 1 count at DAC means 4 counts at ADC */
g_scale = 0.5 * (DAC_RANGE / 65536); /* 1 count at 16-bit DAC means 2 counts at ADC */
}
/* Given the current DAC and ADC values d1 and a1,
@@ -32,6 +32,10 @@ float calculate_scale(uint16_t d1, float a1, uint16_t d2, float a2)
{
float scale;
float a = a2 - a1;
/* Correct for known errors */
d1 = dac_get_actual(d1);
d2 = dac_get_actual(d2);
if (a < 0.1 && a > -0.1)
scale = 1.0;


+ 5
- 5
firmware/calibrate.h View File

@@ -4,14 +4,14 @@
#define ADC_CLAMP_MIN 256
#define ADC_CLAMP_MAX 1792
#define DAC_MIN 0
#define DAC_MAX 65535
#define OVERSAMPLE_COUNT 256
#define DAC_MIN DAC_LOW
#define DAC_MAX DAC_HIGH
#define SEEK_MAX_STEPS 1000
#define SEEK_FUZZ_ADC 10
#define SEEK_FUZZ_DAC 5
#define SEEK_FUZZ_ADC 100
#define SEEK_FUZZ_DAC ((DAC_RANGE * 5) / 65536)
#define CALIBRATE_ADC_ZERO 1024
#define CALIBRATE_ADC_LOW 512


+ 17
- 6
firmware/dac.c View File

@@ -30,20 +30,31 @@ void dac_init(void)
dac_write(0x0000);
}
/* Write raw 16-bit value to DAC:
0xFFFF 4.9998v
0x8000 0v
0x0000 -5v
static const uint16_t dac_lookup[1024] = {
#include "lookup.inc"
};
/* Write raw 16-bit desired value to DAC:
DAC_HIGH 4.9998v
DAC_MID 0v
DAC_LOW -5v
*/
void dac_write(uint16_t val)
{
LATGbits.LATG9 = IO_LOW;
if (IO_HIGH == 1)
SPI2BUF = val;
SPI2BUF = __dac_actual(val);
else
SPI2BUF = ~val;
SPI2BUF = ~__dac_actual(val);
while (!SPI2STATbits.SPIRBF)
continue;
(void) SPI2BUF;
LATGbits.LATG9 = IO_HIGH;
}
uint16_t dac_get_actual(uint16_t val)
{
return __dac_actual(val);
}

+ 28
- 4
firmware/dac.h View File

@@ -3,14 +3,38 @@
#include "config.h"
//#define FAKE_10_BIT
#ifdef FAKE_10_BIT
/* fake compressed range with random lookup bits */
#define DAC_BITS 10
#define __dac_actual(x) (dac_lookup[(x)&1023])
#else
/* normal range */
#define DAC_BITS 16
#define __dac_actual(x) (x)
#endif
#define DAC_LOW 0
#define DAC_HIGH ((uint16_t)(((uint32_t)1 << DAC_BITS) - 1))
#define DAC_MID ((uint16_t)((DAC_LOW + DAC_HIGH + (uint32_t)1) / 2))
#define DAC_RANGE (DAC_HIGH - DAC_LOW + 1)
/* Initialize DAC (AD5542) */
void dac_init(void);
/* Write raw 16-bit value to DAC:
0xFFFF 4.9998v
0x8000 0v
0x0000 -5v
/* Write raw value to DAC:
DAC_HIGH 4.9998v
DAC_MID 0v
DAC_LOW -5v
*/
void dac_write(uint16_t val);
/* From DAC value, get the expected actual output voltage:
0xffff 4.9998v
0x8000 0v
0x0000 -5v
*/
uint16_t dac_get_actual(uint16_t val);
#endif

+ 12
- 7
firmware/mode_debug.c View File

@@ -11,21 +11,25 @@
#include "mode.h"
#include "zoom.h"
static uint16_t dac = 32768;
static uint16_t dac = DAC_MID;
void sweep(void)
{
int32_t d;
int16_t a;
#define SWEEP ((DAC_HIGH - DAC_LOW) * (int32_t)2000 / 65535)
/* sweep range */
for (d = (int32_t)dac - 2000; d < (int32_t)dac + 2000; d++) {
if (d < 0x0000) {
uart1_put_string("0 0\r\n");
for (d = (int32_t)dac - SWEEP; d < (int32_t)dac + SWEEP; d++) {
if (d < DAC_LOW) {
uart1_put_dec(DAC_LOW);
uart1_put_string(" 0\r\n");
continue;
}
if (d > 0xffff) {
uart1_put_string("65535 0\r\n");
if (d > DAC_HIGH) {
uart1_put_dec(DAC_HIGH);
uart1_put_string(" 0\r\n");
continue;
}
dac_write(d);
@@ -50,6 +54,7 @@ void run_debug(void)
uart1_put_string("Zoom NILM Debug\r\n");
while (1) {
dac = dac & DAC_HIGH; // mask off invalid bits
dac_write(dac);
uart1_put_hex16(dac);
uart1_put(' ');
@@ -92,7 +97,7 @@ void run_debug(void)
// set DAC to midpoint
case '0':
dac = 32768;
dac = DAC_MID;
break;
// set DAC to specified hex value


+ 2
- 2
firmware/mode_normal.c View File

@@ -84,12 +84,12 @@ void run_normal(void)
led_pattern(0b00110011);
/* Keep writing 32768 to the DAC for about 30 seconds after startup,
/* Keep writing zero to the DAC for about 30 seconds after startup,
or until we receive a character on the UART */
while(uart1_can_get())
uart1_get();
for (i = 0; i < 1500; i++) {
dac_write(32768);
dac_write(DAC_MID);
if (uart1_can_get()) {
uart1_get();
break;


+ 1
- 1
firmware/zoom.c View File

@@ -24,7 +24,7 @@ int main(void)

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

/* Detect jumper from B8 to GND */


+ 3
- 1
firmware/zoom.mcp View File

@@ -43,6 +43,7 @@ file_022=no
file_023=no
file_024=no
file_025=no
file_026=no
[FILE_INFO]
file_000=zoom.c
file_001=config.c
@@ -69,7 +70,8 @@ file_021=mode.h
file_022=zoom.h
file_023=calibrate.h
file_024=packet.h
file_025=p33fj256gp710.gld
file_025=lookup.inc
file_026=p33fj256gp710.gld
[SUITE_INFO]
suite_guid={479DDE59-4D56-455E-855E-FFF59A3DB57E}
suite_state=


BIN
firmware/zoom.mcw View File


Loading…
Cancel
Save