標(biāo)題: ad7799單片機(jī)驅(qū)動程序 [打印本頁]

作者: 微風(fēng)寒    時間: 2017-3-13 17:42
標(biāo)題: ad7799單片機(jī)驅(qū)動程序
例子一:


  1. sbit AD7799_CS=P1^4;
  2. sbit AD7799_RDY=P1^6;

  3. //SPCR SPI控制寄存器

  4. //SPSR SPI狀態(tài)寄存器

  5. //SPDAT SPI數(shù)據(jù)寄存器
  6. void SPI_init(void)
  7. {
  8. SPCR=0x5e;//SPI控制寄存器,中斷禁止,SPI使能,高位在前,主機(jī)模式,時鐘空閑時為高,后沿觸發(fā)移位,時鐘分頻64
  9. SPSR=0x00;//清中斷標(biāo)志位
  10. }
  11. /*--------------------------------------------
  12. 寫AD7799寄存器函數(shù)
  13. WriteData:要寫的數(shù)據(jù)
  14. ----------------------------------------------*/
  15. void WriteByteToAd7799(unsigned char WriteData)
  16. {
  17. SPDAT= WriteData;
  18. while(~SPSR&0x80); //等待數(shù)據(jù)發(fā)送完
  19. SPSR=0x00; //清中斷標(biāo)志位

  20. }
  21. /*--------------------------------------------
  22. 防止時序混亂,實(shí)現(xiàn)再同步
  23. ----------------------------------------------*/
  24. void WaiteRDY(void)
  25. {
  26. unsigned int count=0 ;
  27. while(AD7799_RDY)
  28. {
  29. count++;
  30. if(count>20000)
  31. {
  32. //reset ad7799
  33. WriteByteToAd7799(0xff);

  34. WriteByteToAd7799(0xff);
  35. /*----------防止時序混亂,重新同步----------*/
  36. WriteByteToAd7799(0xff);

  37. WriteByteToAd7799(0xff);

  38. AD7799_init();
  39. break ;
  40. }
  41. }
  42. }

  43. /*--------------------------------------------
  44. AD7799初始化函數(shù)
  45. ----------------------------------------------*/
  46. void AD7799_init(void)
  47. {
  48. AD7799_CS=0;
  49. /*------------------------增益為128,通道0----------------------------------------*/
  50. WriteByteToAd7799(0x10); //寫通信寄存器設(shè)置下一個操作為寫配置寄存器

  51. WriteByteToAd7799(0x37); //增益為128

  52. WriteByteToAd7799(0x30); //通道0

  53. /*------------------- 寫模式寄存器初始化零值校準(zhǔn)------------------------------------*/
  54. WriteByteToAd7799(0x08); //寫通信寄存器設(shè)置下一個操作為寫模式寄存器

  55. WriteByteToAd7799(0x80);

  56. WriteByteToAd7799(0x0A);

  57. WaiteRDY(); //Wait for RDY pin to Go low to indicate end of calibration cycle*/
  58. /*------------------寫模式寄存器初始化全值校準(zhǔn)-------------------------------------*/
  59. WriteByteToAd7799(0x08); //寫通信寄存器設(shè)置下一個操作為寫模式寄存器

  60. WriteByteToAd7799(0xA0);

  61. WriteByteToAd7799(0x0A);

  62. WaiteRDY(); // Wait for RDY pin to go low to indicate end of calibration cycle
  63. /*------------------模式0,Continuous-Conversion Mode,Fadc=16.7HZ------------------*/
  64. WriteByteToAd7799(0x08); //寫通信寄存器設(shè)置下一個操作為寫模式寄存器

  65. WriteByteToAd7799(0x00);

  66. WriteByteToAd7799(0x0A);

  67. }
  68. unsigned long ReadAd7799ConversionData(void)
  69. {
  70. unsigned long ConverData;
  71. unsigned char ADSAT ;
  72. unsigned char ErrNUM=0;
  73. WaiteRDY();

  74. WriteByteToAd7799(0x40); //寫通信寄存器設(shè)置下一個操作為讀狀態(tài)STATUS寄存器

  75. WriteByteToAd7799(0xff); //偽寫通信寄存器,為讀狀態(tài)寄存器提供時鐘

  76. ADSAT=SPDAT; //讀取接收到的數(shù)據(jù)
  77. while((ADSAT&0x40)||(!(ADSAT&0x08))) //出錯或者讀寫異常
  78. {
  79. //reset ad7799
  80. WriteByteToAd7799(0xff);

  81. WriteByteToAd7799(0xff);
  82. /*----------防止時序混亂,重新同步----------*/
  83. WriteByteToAd7799(0xff);

  84. WriteByteToAd7799(0xff);

  85. //-------------------------------------------------------------------------------------
  86. AD7799_init();

  87. WaiteRDY();
  88. WriteByteToAd7799(0x40); //寫通信寄存器設(shè)置下一個操作為讀狀態(tài)STATUS寄存器

  89. WriteByteToAd7799(0xff); //偽寫通信寄存器,為讀狀態(tài)寄存器提供時鐘

  90. ADSAT=SPDAT; //讀取接收到的數(shù)據(jù)

  91. ErrNUM++;
  92. if(ErrNUM>5)break;
  93. }

  94. WriteByteToAd7799(0x58); //寫通信寄存器設(shè)置下一個操作為連續(xù)讀數(shù)據(jù)寄存器

  95. WaiteRDY();
  96. /* Wait for RDY pin to go low to indicate end of calibration cycle*/
  97. if(!AD7799_RDY)
  98. {
  99. ConverData=0 ;
  100. /*-----------------Read Conversion Result from AD7799's Data Register----------------*/

  101. WriteByteToAd7799(0xff); //偽寫通信寄存器,為讀數(shù)據(jù)寄存器寄存器提供時鐘
  102. ConverData=SPDAT;
  103. ConverData=ConverData<<8 ;

  104. WriteByteToAd7799(0xff); //偽寫通信寄存器,為讀數(shù)據(jù)寄存器寄存器提供時鐘
  105. ConverData=ConverData+SPDAT;
  106. ConverData=ConverData<<8 ;

  107. WriteByteToAd7799(0xff); //偽寫通信寄存器,為讀數(shù)據(jù)寄存器寄存器提供時鐘
  108. ConverData=ConverData+SPDAT;

  109. }
  110. if(ErrNUM>5)return(0);
  111. else return(ConverData);
  112. }
復(fù)制代碼





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1