|
|
@@ -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) |
|
|
|