|
- #include "config.h"
- #include "adc.h"
- #include "adcext.h"
- #include "dac.h"
- #include "uart.h"
- #include "timer.h"
- #include <stdio.h>
- #include <math.h>
- #include "calibrate.h"
- #include "util.h"
- #include "mode.h"
- #include "led.h"
- #include "zoom.h"
-
- int send_data = 0;
- uint16_t send_adc;
- uint16_t send_dac;
- int possible_overflow = 0;
-
- #define TIMER_RATE 8000 /* how often to read the ADC and update DAC */
- #define PC_RATE 8000 /* how often to send data to the PC */
-
- /* Run mode */
- void TISR_HANDLER(5)
- {
- static int count = 0;
- static uint16_t dac_cmd;
- int16_t v;
- 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();
-
- if (v < 0 || v >= 2047)
- possible_overflow = 1;
-
- /* Send data to PC at 1 Hz */
- if (++count >= (TIMER_RATE / PC_RATE)) {
- count = 0;
-
- /* Send most recent sample and old DAC value */
- send_adc = v;
- send_dac = dac_cmd;
- send_data = 1;
- }
-
- /* Convert ADC value to current */
- #pragma xxx
- #if 0
- i = adc12_to_current(v);
-
- #if 0
- /* Adjust DAC to null this current to 0 */
- dac_current -= i;
- #else
- /* If this current exceeds +-1A, cancel it out at the DAC */
- if (i < -1.0 || i > 1.0)
- dac_current -= i;
- #endif
-
- 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);
- #endif
- dac_write(dac_cmd);
-
- LATAbits.LATA9 = 0;
- }
-
- 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 | ((adc & 0x0FE0) >> 5));
- uart1_put(((adc & 0x001F) << 2) | ((dac & 0xC000) >> 14));
- uart1_put((dac & 0x3F80) >> 7);
- uart1_put((dac & 0x007F));
- }
-
- void run_normal(void)
- {
- uart1_init(500000);
- led_on();
-
- /* Assume startup current is 0 */
- // dac_current = 0.0;
- // dac_write(current_to_dac(dac_current));
- // msleep(100);
- // degauss();
-
- timer_setup_16bit(5, TIMER_RATE, 1);
- timer_set_priority(5, 6);
-
- while(1) {
- if (send_data) {
- /* 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);
- }
- }
- }
|