Browse Source

various changes

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7708 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 15 years ago
parent
commit
488e0fe9b0
5 changed files with 67 additions and 4 deletions
  1. +19
    -0
      firmware/adc.c
  2. +3
    -0
      firmware/adc.h
  3. +14
    -4
      firmware/calibrate.c
  4. +9
    -0
      firmware/mode_debug.c
  5. +22
    -0
      firmware/mode_normal.c

+ 19
- 0
firmware/adc.c View File

@@ -44,3 +44,22 @@ int16_t adc_get(void)
v &= ~0xF000;
return (int16_t) v;
}
uint16_t adc_get_raw(void)
{
uint16_t v = 0;
int i;
LAT_CS = IO_LOW;
for (i = 0; i < 16; i++) {
v <<= 1;
if (R_SDATA == IO_HIGH)
v |= 1;
LAT_SCLK = IO_LOW;
nop(); nop(); nop();
LAT_SCLK = IO_HIGH;
}
LAT_CS = IO_HIGH;
return (uint16_t) v;
}

+ 3
- 0
firmware/adc.h View File

@@ -10,4 +10,7 @@ void adc_init(void);
Result is a signed value (-2048 to +2047) */
int16_t adc_get(void);
/* Trigger conversion and get raw 16-bit value from ADC */
uint16_t adc_get_raw(void);
#endif

+ 14
- 4
firmware/calibrate.c View File

@@ -30,10 +30,19 @@ uint16_t adc_to_dac(uint16_t d1, int16_t a1, int16_t a2, float scale)
/* Calculate a new scale factor given two DAC and ADC points */
float calculate_scale(uint16_t d1, float a1, uint16_t d2, float a2)
{
if (d1 == d2)
return 1.0;
else
return (a2 - a1) / ((float)d2 - d1);
float scale;
float a = a2 - a1;
if (a < 0.1 && a > -0.1)
scale = 1.0;
else {
scale = ((float)d2 - d1) / a;
if (scale < 0.05)
scale = 0.05;
if (scale > 20)
scale = 20;
}
return scale;
}
/* Seek with the DAC to reach a specific ADC value. Uses g_scale as
@@ -84,6 +93,7 @@ uint16_t seek(uint16_t starting_dac, int16_t desired_adc)
/* if we're close, accept it */
if (abs(adc - desired_adc) <= SEEK_FUZZ_ADC) {
led_pattern(0b11111110);
break;
}


+ 9
- 0
firmware/mode_debug.c View File

@@ -147,6 +147,15 @@ void run_debug(void)
uart1_crlf();
sweep();
break;
// dump raw ADC value
case 'r':
case 'R':
while(!uart1_can_get()) {
uart1_put_hex16(adc_get_raw());
uart1_crlf();
}
uart1_get();
break;
}
}
}


+ 22
- 0
firmware/mode_normal.c View File

@@ -58,11 +58,16 @@ void TISR_HANDLER(5)
send_data = 1;
}
#define WINDOW
#ifdef WINDOW
/* If ADC value is outside the window, step DAC */
if (v < ADC_WINDOW_MIN)
dac_cmd = adc_to_dac(dac_cmd, v, ADC_WINDOW_MIN_STEPTO, g_scale);
else if (v > ADC_WINDOW_MAX)
dac_cmd = adc_to_dac(dac_cmd, v, ADC_WINDOW_MAX_STEPTO, g_scale);
#else
dac_cmd = adc_to_dac(dac_cmd, v, 1024, g_scale);
#endif
/* Send it out */
dac_write(dac_cmd);
@@ -74,7 +79,24 @@ void TISR_HANDLER(5)
void run_normal(void)
{
int i;
uart1_init(500000);
led_pattern(0b00110011);
/* Keep writing 32768 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);
if (uart1_can_get()) {
uart1_get();
break;
}
msleep(10);
}
led_on();
/* Assume startup current is 0 */


Loading…
Cancel
Save