@@ -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 th e DAC */
#define ADC_DAC_SCALING (1.0/21.234)
#define TIMER_RATE 10 /* how often to read the ADC and updat e 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 K Hz */
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;