@@ -7,10 +7,12 @@
#include <stdio.h>
#include <math.h>
#include "scaling.h"
#include "util.h"
int send_data = 0;
uint16_t send_adc;
uint16_t send_dac;
int possible_overflow = 0;
#define DAC_MIN 0x0000
#define DAC_MAX 0xFFFF
@@ -19,7 +21,7 @@ float dac_current;
float dac_current_min;
float dac_current_max;
#define TIMER_RATE 1 0 /* how often to read the ADC and update DAC */
#define TIMER_RATE 800 0 /* how often to read the ADC and update DAC */
#define PC_RATE 10 /* how often to send data to the PC */
void TISR_HANDLER(5)
@@ -36,6 +38,9 @@ void TISR_HANDLER(5)
/* Get most recent sample from 12-bit ADC. */
v = adc_get();
if (v < 0 || v >= 2047)
possible_overflow = 1;
/* Send data to PC at 1 Hz */
if (++count >= (TIMER_RATE / PC_RATE)) {
count = 0;
@@ -62,17 +67,17 @@ void TISR_HANDLER(5)
dac_write(dac_cmd);
}
void send_to_pc(void )
void send_to_pc(uint16_t adc, uint16_t dac )
{
/* Sent data format:
1Aaa aaaa 0aaa aaDd 0ddd dddd 0ddd dddd
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);
uart1_put((send_ dac & 0x007F));
uart1_put(0x80 | ((adc & 0x0FE0) >> 5));
uart1_put(((adc & 0x001F) << 2) | ((dac & 0xC000) >> 14));
uart1_put((dac & 0x3F80) >> 7);
uart1_put((dac & 0x007F));
}
void degauss(void)
@@ -155,8 +160,22 @@ void run_normal(void)
while(1) {
if (send_data) {
send_to_pc();
send_data = 0;
/* There's data to send. Disable the ISR briefly
while we grab it */
uint16_t a, d;
disable_int({
if (possible_overflow) {
/* Mark a possible overflow in the output
by setting ADC to an unlikely value */
possible_overflow = 0;
a = 0x0800;
} else {
a = send_adc;
}
d = send_dac;
send_data = 0;
});
send_to_pc(a, d);
}
}
}