Old fashion design for PC-104
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.

132 lines
2.5 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "StdAfx.h"
  2. #include "SysClock.h"
  3. #include "A2D.h"
  4. #include "Timer.h"
  5. static WORD wValueA2D;
  6. sfr16 ADC0 = 0xBE; // ADC0 data
  7. #define ADC_SAR_CLOCK 2500000L
  8. void ADC0_Init()
  9. {
  10. // Save Current SFR page
  11. XBYTE SFRPAGE_SAVE = SFRPAGE;
  12. XBYTE nSAR = SYSCLK / (2 * ADC_SAR_CLOCK) - 1;
  13. // Set ADC page
  14. SFRPAGE = ADC0_PAGE;
  15. // enable temp sensor, on-chip VREF,
  16. // and VREF output buffer
  17. REF0CN = 0x07;
  18. // select TEMP sens as ADC mux output
  19. AMX0SL = 0x0F;
  20. // all inputs a independant
  21. AMX0CF = 0x00;
  22. // ADC0 disabled; normal tracking
  23. // mode; ADC0 conversions are initiated
  24. // on Busy bit; ADC0 data is
  25. // right-justified
  26. ADC0CN = 0x00;
  27. // ADC conversion clock = 2.5 MHz
  28. ADC0CF = 0x20;//nSAR << 3;
  29. // PGA gain = 1
  30. ADC0CF |= 0x00;
  31. // set tracking mode
  32. AD0TM = 0;//1; // for mirror was 1, for normal system is 0. ???
  33. // enable ADC
  34. AD0EN = 1;
  35. // Restore SFR page
  36. SFRPAGE = SFRPAGE_SAVE;
  37. }
  38. /*
  39. void ADC0_Start( BYTE bChannel )
  40. {
  41. XBYTE SFRPAGE_SAVE = SFRPAGE;
  42. SFRPAGE = ADC0_PAGE;
  43. AMX0SL = bChannel;
  44. AD0INT = 0;
  45. AD0BUSY = 1;
  46. SFRPAGE = SFRPAGE_SAVE;
  47. }
  48. BOOL ADC0_GetValue( PWORD pValue )
  49. {
  50. XBYTE SFRPAGE_SAVE = SFRPAGE;
  51. SFRPAGE = ADC0_PAGE;
  52. if( !AD0INT )
  53. {
  54. SFRPAGE = SFRPAGE_SAVE;
  55. return FALSE;
  56. }
  57. AD0INT = 0;
  58. *pValue = ADC0;
  59. SFRPAGE = SFRPAGE_SAVE;
  60. return TRUE;
  61. }
  62. */
  63. WORD ADC0_Convert( BYTE bChannel )
  64. {
  65. static BYTE SFRPAGE_SAVE;
  66. SFRPAGE_SAVE = SFRPAGE;
  67. SFRPAGE = ADC0_PAGE;
  68. // select channel
  69. AMX0SL = bChannel;
  70. AD0INT = 0;
  71. // settling time for accurate conversion (around 2.5 uS on 100 MHz)
  72. // HiResTimer0_Wait( MICRO_SEC_TO_HIRES(2) );
  73. HiResTimer1_Wait( 5 );
  74. // start conversion
  75. AD0BUSY = 1;
  76. // wait for end of conversion
  77. while( !AD0INT );
  78. // clear condition
  79. AD0INT = 0;
  80. // copy result
  81. wValueA2D = ADC0;
  82. SFRPAGE = SFRPAGE_SAVE;
  83. return wValueA2D;
  84. }
  85. /*
  86. LONG ADC0_Adc2Temperature( WORD wValue )
  87. {
  88. LONG lTemperature = ( ((LONG)(INT)wValue) * V_REF * 1000L - 7760L * MAX_ADC * PGAIN ) / ( 286L * MAX_ADC * PGAIN / 10L );
  89. // compensate self heating
  90. return lTemperature;// * 10000L / 11875L;
  91. }
  92. WORD ADC0_Temperature2Adc( LONG lTemperature )
  93. {
  94. wValueA2D = (lTemperature * ( 286L * MAX_ADC * PGAIN / 10L ) + 7760L * MAX_ADC * PGAIN) / (V_REF * 1000L);
  95. // compensate self heating
  96. return wValueA2D;// * 11875L / 10000L;
  97. }
  98. */