adc.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //adc.c
  2. #include "adc.h"
  3. #include <stdio.h>
  4. #include <avr/io.h>
  5. #include <avr/pgmspace.h>
  6. uint8_t adc_state;
  7. uint8_t adc_count;
  8. uint16_t adc_values[ADC_CHAN_CNT];
  9. uint16_t adc_sim_mask;
  10. #ifdef ADC_CALLBACK
  11. extern void ADC_CALLBACK(void);
  12. #endif //ADC_CALLBACK
  13. void adc_init(void)
  14. {
  15. printf_P(PSTR("adc_init\n"));
  16. adc_sim_mask = 0x00;
  17. ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  18. ADMUX |= (1 << REFS0);
  19. ADCSRA |= (1 << ADEN);
  20. // ADCSRA |= (1 << ADIF) | (1 << ADSC);
  21. DIDR0 = (ADC_CHAN_MSK & 0xff);
  22. DIDR2 = (ADC_CHAN_MSK >> 8);
  23. adc_reset();
  24. // adc_sim_mask = 0b0101;
  25. // adc_sim_mask = 0b100101;
  26. // adc_values[0] = 1023 * 16;
  27. // adc_values[2] = 1023 * 16;
  28. // adc_values[5] = 1002 * 16;
  29. }
  30. void adc_reset(void)
  31. {
  32. adc_state = 0;
  33. adc_count = 0;
  34. uint8_t i; for (i = 0; i < ADC_CHAN_CNT; i++)
  35. if ((adc_sim_mask & (1 << i)) == 0)
  36. adc_values[i] = 0;
  37. }
  38. void adc_setmux(uint8_t ch)
  39. {
  40. ch &= 0x0f;
  41. if (ch & 0x08) ADCSRB |= (1 << MUX5);
  42. else ADCSRB &= ~(1 << MUX5);
  43. ADMUX = (ADMUX & ~(0x07)) | (ch & 0x07);
  44. }
  45. uint8_t adc_chan(uint8_t index)
  46. {
  47. uint8_t chan = 0;
  48. uint16_t mask = 1;
  49. while (mask)
  50. {
  51. if ((mask & ADC_CHAN_MSK) && (index-- == 0)) break;
  52. mask <<= 1;
  53. chan++;
  54. }
  55. return chan;
  56. }
  57. void adc_cycle(void)
  58. {
  59. if (adc_state & 0x80)
  60. {
  61. uint8_t index = adc_state & 0x0f;
  62. if ((adc_sim_mask & (1 << index)) == 0)
  63. adc_values[index] += ADC;
  64. if (++index >= ADC_CHAN_CNT)
  65. {
  66. index = 0;
  67. adc_count++;
  68. if (adc_count >= ADC_OVRSAMPL)
  69. {
  70. #ifdef ADC_CALLBACK
  71. ADC_CALLBACK();
  72. #endif //ADC_CALLBACK
  73. adc_reset();
  74. }
  75. }
  76. adc_setmux(adc_chan(index));
  77. adc_state = index;
  78. }
  79. else
  80. {
  81. ADCSRA |= (1 << ADSC); //start conversion
  82. adc_state |= 0x80;
  83. }
  84. }