diff --git a/firmware/adc.c b/firmware/adc.c index 99f3bda..8c8e770 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -26,15 +26,15 @@ uint16_t adc_get(void) uint16_t v = 0; int i; - LAT_CS = 0; + LAT_CS = IO_LOW; for (i = 0; i < 16; i++) { - if (R_SDATA) - v |= 1; v <<= 1; - LAT_SCLK = 0; - nop(); - LAT_SCLK = 1; + if (R_SDATA == IO_HIGH) + v |= 1; + LAT_SCLK = IO_LOW; + nop(); nop(); nop(); + LAT_SCLK = IO_HIGH; } - LAT_CS = 1; + LAT_CS = IO_HIGH; return v; } diff --git a/firmware/zoom.c b/firmware/zoom.c index 593b895..ee55f69 100644 --- a/firmware/zoom.c +++ b/firmware/zoom.c @@ -22,19 +22,21 @@ uint16_t send_adc, send_dac; uint16_t dac_cmd = ((uint32_t)DAC_MAX + DAC_MIN) / 2; -/* 64 KHz */ -void timer(void) +#define TIMER_RATE 8000L +void TISR_HANDLER(5) { static int count = 0; uint16_t v; int32_t adjustment; int32_t newdac; + timer_clear_txif(5); + /* Get most recent sample from 12-bit ADC. */ v = adc_get(); /* Send data to PC at 8 KHz */ - if (count++ >= 8) { + if (count++ >= (TIMER_RATE / 8000)) { count = 0; /* Send most recent sample and old DAC value */ @@ -77,6 +79,8 @@ void run_debug(void) uint16_t dac = 32768; uart1_init(115200); + uart1_put_string("Zoom NILM Debug\r\n"); + while (1) { dac_write(dac); uart1_put_dec(dac); @@ -115,6 +119,10 @@ void run_debug(void) void run_normal(void) { uart1_init(500000); +// uart1_init(115200); +// uart1_put_string("Zoom NILM Normal\r\n"); + timer_setup_16bit(5, TIMER_RATE, 1); +// uart1_put_string("Timer OK\r\n"); while(1) { if (send_data) { @@ -127,14 +135,17 @@ void run_normal(void) int main(void) { config_init(); + + /* Pull PGD high internally. */ + TRISCbits.TRISC13 = 1; + CNPU1bits.CN1PUE = 1; + adcext_init(); dac_init(); adc_init(); - /* Pull PGD high internally. If it is externally tied - to ground, run in special debug mode. */ - TRISCbits.TRISC13 = 1; - CNPU1bits.CN1PUE = 1; + /* If PGD is externally tied to ground, run in debug mode. + (Short ICD pins 3 and 4) */ if (PORTCbits.RC13 == 0) run_debug(); else diff --git a/firmware/zoom.mcw b/firmware/zoom.mcw index a15336a..7039e0f 100644 Binary files a/firmware/zoom.mcw and b/firmware/zoom.mcw differ