» Advertenties

Zo 5 Februari 2012, 12:43

C code -
  1. //*****************************************************************************
  2. //
  3. // File Name : adc.h
  4. // Title : Analog-to-Digital-Converter functions
  5. // Author : Smoerijf.be (Orinal by Pascal Stang - edit by smoerijf)
  6. // Target MCU : Atmel AVR Series
  7. //
  8. //*****************************************************************************
  9. //@{
  10.  
  11. #ifndef ADC_H
  12. #define ADC_H
  13.  
  14. // defines
  15.  
  16. // A2D clock prescaler select
  17. //     *selects how much the CPU clock frequency is divided
  18. //     to create the A2D clock frequency
  19. //     *lower division ratios make conversion go faster
  20. //     *higher division ratios make conversions more accurate
  21. #define ADC_P2     0x00   ///< 0x01,0x00 -> CPU clk/2
  22. #define ADC_P4     0x02   ///< 0x02 -> CPU clk/4
  23. #define ADC_P8     0x03   ///< 0x03 -> CPU clk/8
  24. #define ADC_P16     0x04   ///< 0x04 -> CPU clk/16
  25. #define ADC_P32     0x05   ///< 0x05 -> CPU clk/32
  26. #define ADC_P64     0x06   ///< 0x06 -> CPU clk/64
  27. #define ADC_P128   0x07   ///< 0x07 -> CPU clk/128
  28. // do not change the mask value
  29. #define ADC_PRESCALE_MASK     0x07
  30.  
  31. // A2D voltage reference select
  32. //     *this determines what is used as the
  33. //     full-scale voltage point for A2D conversions
  34. #define ADC_AREF     0x00   ///< 0x00 -> AREF pin, internal VREF turned off
  35. #define ADC_AVCC     0x01   ///< 0x01 -> AVCC pin, internal VREF turned off
  36. //#define ADC_RSVD     0x02   ///< 0x02 -> Reserved
  37. #define ADC_256V     0x03   ///< 0x03 -> Internal 2.56V VREF
  38. // default value
  39.  
  40. // do not change the mask value
  41. #define ADC_REFERENCE_MASK     0xC0
  42. #define ADC_MUX_MASK       0x1F
  43.  
  44. // these channels supported only in ATmega128
  45. // differential with gain
  46. #define ADC_CH_0_0_DIFF10X     0x08
  47. #define ADC_CH_1_0_DIFF10X     0x09
  48. #define ADC_CH_0_0_DIFF200X     0x0A
  49. #define ADC_CH_1_0_DIFF200X     0x0B
  50. #define ADC_CH_2_2_DIFF10X     0x0C
  51. #define ADC_CH_3_2_DIFF10X     0x0D
  52. #define ADC_CH_2_2_DIFF200X     0x0E
  53. #define ADC_CH_3_2_DIFF200X     0x0F
  54. // differential
  55. #define ADC_CH_0_1_DIFF1X     0x10
  56. #define ADC_CH_1_1_DIFF1X     0x11
  57. #define ADC_CH_2_1_DIFF1X     0x12
  58. #define ADC_CH_3_1_DIFF1X     0x13
  59. #define ADC_CH_4_1_DIFF1X     0x14
  60. #define ADC_CH_5_1_DIFF1X     0x15
  61. #define ADC_CH_6_1_DIFF1X     0x16
  62. #define ADC_CH_7_1_DIFF1X     0x17
  63.  
  64. #define ADC_CH_0_2_DIFF1X     0x18
  65. #define ADC_CH_1_2_DIFF1X     0x19
  66. #define ADC_CH_2_2_DIFF1X     0x1A
  67. #define ADC_CH_3_2_DIFF1X     0x1B
  68. #define ADC_CH_4_2_DIFF1X     0x1C
  69. #define ADC_CH_5_2_DIFF1X     0x1D
  70.  
  71. // compatibility for new Mega processors
  72. // ADCSR hack apparently no longer necessary in new AVR-GCC
  73. #ifdef ADCSRA
  74. #ifndef ADCSR
  75.   #define ADCSR   ADCSRA
  76. #endif
  77. #endif
  78. #ifdef ADATE
  79.   #define ADFR   ADATE
  80. #endif
  81.  
  82. // function prototypes
  83.  
  84. void adcInit(u08 prescale, u08 ref);
  85. void adcOff(void);
  86. void adcSetPrescaler(u08 prescale);
  87. void adcSetReference(u08 ref);
  88. void adcSetChannel(u08 ch);
  89. void adcStartConvert(void);
  90. u08 adcIsComplete(void);
  91.  
  92. u16 adcGet(u08 ch);
  93. u08 adcGet8(u08 ch);
  94.  
  95. #endif
  96. //@}
Laatste wijziging: Za 25 April 2009, 11:35