You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

126 lines
2.7 KiB

  1. #include "config.h"
  2. #include "adc.h"
  3. #include "adcext.h"
  4. #include "dac.h"
  5. #include "uart.h"
  6. #include "timer.h"
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include "calibrate.h"
  10. #include "util.h"
  11. #include "mode.h"
  12. #include "led.h"
  13. #include "zoom.h"
  14. int send_data = 0;
  15. uint16_t send_adc;
  16. uint16_t send_dac;
  17. int possible_overflow = 0;
  18. #define TIMER_RATE 8000 /* how often to read the ADC and update DAC */
  19. #define PC_RATE 8000 /* how often to send data to the PC */
  20. /* Run mode */
  21. void TISR_HANDLER(5)
  22. {
  23. static int count = 0;
  24. static uint16_t dac_cmd;
  25. int16_t v;
  26. float i;
  27. /* toggle A9 every time an interrupt occurs */
  28. LATAbits.LATA9 = 1;
  29. timer_clear_txif(5);
  30. /* Get most recent sample from 12-bit ADC. */
  31. v = adc_get();
  32. if (v < 0 || v >= 2047)
  33. possible_overflow = 1;
  34. /* Send data to PC at 1 Hz */
  35. if (++count >= (TIMER_RATE / PC_RATE)) {
  36. count = 0;
  37. /* Send most recent sample and old DAC value */
  38. send_adc = v;
  39. send_dac = dac_cmd;
  40. send_data = 1;
  41. }
  42. /* Convert ADC value to current */
  43. #pragma xxx
  44. #if 0
  45. i = adc12_to_current(v);
  46. #if 0
  47. /* Adjust DAC to null this current to 0 */
  48. dac_current -= i;
  49. #else
  50. /* If this current exceeds +-1A, cancel it out at the DAC */
  51. if (i < -1.0 || i > 1.0)
  52. dac_current -= i;
  53. #endif
  54. if (dac_current < dac_current_min)
  55. dac_current = dac_current_min;
  56. if (dac_current > dac_current_max)
  57. dac_current = dac_current_max;
  58. /* Now send it out */
  59. dac_cmd = current_to_dac(dac_current);
  60. #endif
  61. dac_write(dac_cmd);
  62. LATAbits.LATA9 = 0;
  63. }
  64. void send_to_pc(uint16_t adc, uint16_t dac)
  65. {
  66. /* Sent data format:
  67. 1Aaa aaaa 0aaa aaDd 0ddd dddd 0ddd dddd
  68. Aaaaaaaaaaaa = 12-bit ADC value (2s compliment signed)
  69. Dddddddddddddddd = 16-bit DAC command (unsigned)
  70. */
  71. uart1_put(0x80 | ((adc & 0x0FE0) >> 5));
  72. uart1_put(((adc & 0x001F) << 2) | ((dac & 0xC000) >> 14));
  73. uart1_put((dac & 0x3F80) >> 7);
  74. uart1_put((dac & 0x007F));
  75. }
  76. void run_normal(void)
  77. {
  78. uart1_init(500000);
  79. led_on();
  80. /* Assume startup current is 0 */
  81. // dac_current = 0.0;
  82. // dac_write(current_to_dac(dac_current));
  83. // msleep(100);
  84. // degauss();
  85. timer_setup_16bit(5, TIMER_RATE, 1);
  86. timer_set_priority(5, 6);
  87. while(1) {
  88. if (send_data) {
  89. /* There's data to send. Disable the ISR briefly
  90. while we grab it */
  91. uint16_t a, d;
  92. disable_int({
  93. if (possible_overflow) {
  94. /* Mark a possible overflow in the output
  95. by setting ADC to an unlikely value */
  96. possible_overflow = 0;
  97. a = 0x0800;
  98. } else {
  99. a = send_adc;
  100. }
  101. d = send_dac;
  102. send_data = 0;
  103. });
  104. send_to_pc(a, d);
  105. }
  106. }
  107. }