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.
 
 
 
 

143 lines
2.8 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 "packet.h"
  14. int send_data = 0;
  15. uint16_t send_adc;
  16. uint16_t send_dac;
  17. int possibly_clamped = 0;
  18. uint16_t dac_cmd;
  19. #define TIMER_RATE 8000 /* how often to read the ADC and update DAC */
  20. #define PC_RATE 8000 /* how often to send data to the PC */
  21. #define DEBUG_ISR_TIME
  22. #define ADC_WINDOW_MIN 512
  23. #define ADC_WINDOW_MIN_STEPTO 1280
  24. #define ADC_WINDOW_MAX 1536
  25. #define ADC_WINDOW_MAX_STEPTO 768
  26. /* Run mode */
  27. void TISR_HANDLER(5)
  28. {
  29. static int count = 0;
  30. int16_t v;
  31. #ifdef DEBUG_ISR_TIME
  32. LATAbits.LATA9 = 1;
  33. #endif
  34. timer_clear_txif(5);
  35. /* Get most recent sample from 12-bit ADC. */
  36. v = adc_get();
  37. if (v < ADC_CLAMP_MIN || v >= ADC_CLAMP_MAX)
  38. possibly_clamped = 1;
  39. /* Send data to PC */
  40. if (++count >= (TIMER_RATE / PC_RATE)) {
  41. count = 0;
  42. /* Send most recent sample and old DAC value */
  43. send_adc = v;
  44. send_dac = dac_cmd;
  45. send_data = 1;
  46. }
  47. #define WINDOW
  48. #ifdef WINDOW
  49. /* If ADC value is outside the window, step DAC */
  50. if (v < ADC_WINDOW_MIN)
  51. dac_cmd = adc_to_dac(dac_cmd, v, ADC_WINDOW_MIN_STEPTO,
  52. g_scale);
  53. else if (v > ADC_WINDOW_MAX)
  54. dac_cmd = adc_to_dac(dac_cmd, v, ADC_WINDOW_MAX_STEPTO,
  55. g_scale);
  56. #else
  57. dac_cmd = adc_to_dac(dac_cmd, v, 1024, g_scale);
  58. #endif
  59. /* Send it out */
  60. dac_write(dac_cmd);
  61. #ifdef DEBUG_ISR_TIME
  62. LATAbits.LATA9 = 0;
  63. #endif
  64. }
  65. void run_normal(void)
  66. {
  67. int i;
  68. uart1_init(500000);
  69. led_pattern(0b00110011);
  70. /* Keep writing zero to the DAC for about 30 seconds after startup,
  71. or until we receive a character on the UART */
  72. while(uart1_can_get())
  73. uart1_get();
  74. for (i = 0; i < 1500; i++) {
  75. dac_write(DAC_MID);
  76. if (uart1_can_get()) {
  77. uart1_get();
  78. break;
  79. }
  80. msleep(10);
  81. }
  82. led_on();
  83. /* Assume startup current is 0 */
  84. msleep(100);
  85. dac_cmd = do_calibrate();
  86. timer_setup_16bit(5, TIMER_RATE, 1);
  87. timer_set_priority(5, 6);
  88. while(1) {
  89. if (send_data) {
  90. /* There's data to send. Disable the ISR briefly
  91. while we grab it */
  92. uint16_t a, d, o;
  93. disable_int({
  94. if (possibly_clamped) {
  95. /* Mark a possible
  96. * overflow in the
  97. * output */
  98. o = 1;
  99. possibly_clamped = 0;
  100. } else o = 0;
  101. a = send_adc;
  102. d = send_dac;
  103. send_data = 0;
  104. });
  105. packet_send_adc_dac(a, d, o);
  106. }
  107. if (uart1_can_get()) {
  108. switch (uart1_get()) {
  109. case 'c':
  110. disable_int({
  111. send_data = 0;
  112. dac_cmd = do_calibrate();
  113. });
  114. packet_send_calibration(g_scale);
  115. default:
  116. break;
  117. }
  118. }
  119. }
  120. }