帮忙看下12C5A60S2单片机AD转换程序有没有错误?

2020-01-26 13:03发布

  1. #include "reg51.h"
  2. #include "intrins.h"
  3. #include "math.h"
  4. typedef unsigned char BYTE;
  5. typedef unsigned int WORD;
  6. /*Declare SFR associated with the ADC */
  7. sfr ADC_CONTR = 0xBC; //ADC control register
  8. //ADC_POWER | SPEED1 | SPEED0 | ADC_FLAG | ADC_START | CHS2 | CHS1 | CHS0
  9. sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
  10. sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
  11. sfr P1ASF = 0x9D; //P1 secondary function control register
  12. sfr AUXR1 = 0XA2;
  13. /*Define ADC operation const for ADC_CONTR*/
  14. #define ADC_POWER 0x80 //ADC power control bit
  15. #define ADC_FLAG 0x10 //ADC complete flag
  16. #define ADC_START 0x08 //ADC start control bit
  17. #define ADC_SPEEDLL 0x00 //540 clocks
  18. #define ADC_SPEEDL 0x20 //360 clocks
  19. #define ADC_SPEEDH 0x40 //180 clocks
  20. #define ADC_SPEEDHH 0x60 //90 clocks
  21. extern void Init_ST7920();                                                           //液晶屏初始化
  22. extern void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s);
  23. extern void Write_Cmd(unsigned char Cmd);
  24. extern void Write_Data(unsigned char Data);
  25. unsigned char code Tab_LCD[]={0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};
  26. void Delay(WORD n);
  27. void InitADC();
  28. BYTE ch = 0; //ADC channel NO.
  29. WORD Data;
  30. WORD V_Data;
  31. BYTE num=0;
  32. bit flag=0;
  33. unsigned char code Tab_BiaoTi[]={"12C5A60S2_A/D"};
  34. unsigned char code Tab_Bit[]={"8bit_A/D:"};
  35. unsigned char Tab_Count_Data[]={0,0,0,0};
  36. extern void DelayMs(unsigned char t);
  37. extern void LED_ScanDisplay(char Num);
  38. extern void Count_Data(unsigned int Data);
  39. /*----------------------------
  40. Initial ADC sfr
  41. ----------------------------*/
  42. void InitADC( )
  43. {
  44.         P1ASF = 0xff; //Set  P10 as analog input port
  45.         ADC_RES = 0; //Clear previous result
  46. //        ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
  47. //        Delay(2); //ADC power-on delay and Start A/D conversion
  48. }
  49. void LCD_Update_Screen()
  50. {
  51.         Write_Cmd(0x8b);
  52.         Write_Data(Tab_LCD[Tab_Count_Data[3]]);
  53.         Write_Data(Tab_LCD[Tab_Count_Data[2]]);
  54.         Write_Data(Tab_LCD[Tab_Count_Data[1]]);
  55.         Write_Data(Tab_LCD[Tab_Count_Data[0]]);
  56.         DelayMs(5);
  57. }
  58. void Init_LCD_Screen()
  59. {
  60.         LCD_PutString(0,1,Tab_BiaoTi);               
  61.         LCD_PutString(0,2,Tab_Bit);
  62. //        LCD_Update_Screen();
  63. }
  64. void Count_Data(unsigned int Data)
  65. {
  66.         Tab_Count_Data[3]=Data/1000;       
  67.         Tab_Count_Data[2]=Data%1000/100;
  68.         Tab_Count_Data[1]=Data%1000%100/10;
  69.         Tab_Count_Data[0]=Data%10;       
  70. }
  71. void main()
  72. {
  73.         Init_ST7920();                                                           //液晶屏初始化
  74.         Init_LCD_Screen();
  75.         InitADC();  //Init ADC sfr
  76.         while (1)
  77.         {
  78.                  ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
  79.                 Delay(2); //ADC power-on delay and Start A/D conversion
  80.                 while(!(ADC_CONTR & ADC_FLAG));
  81.                 ADC_CONTR &= !ADC_FLAG;
  82.                 Data=ADC_RES;
  83.                 ADC_RES=0;
  84.                 V_Data=Data*5/256;
  85.                 Count_Data(V_Data);
  86.                
  87.                 LCD_Update_Screen();
  88.         }
  89. }
  90. /*----------------------------
  91. Software delay function
  92. ----------------------------*/
  93. void Delay(WORD n)
  94. {
  95.         WORD x;
  96.         while (n--)
  97.         {
  98.                 x = 5000;
  99.                 while (x--);
  100.         }
  101. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。