找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2132|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

ad7799單片機驅(qū)動程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:170375 發(fā)表于 2017-3-13 17:42 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
例子一:


  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使能,高位在前,主機模式,時鐘空閑時為高,后沿觸發(fā)移位,時鐘分頻64
  9. SPSR=0x00;//清中斷標志位
  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; //清中斷標志位

  20. }
  21. /*--------------------------------------------
  22. 防止時序混亂,實現(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. /*------------------- 寫模式寄存器初始化零值校準------------------------------------*/
  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. /*------------------寫模式寄存器初始化全值校準-------------------------------------*/
  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ù)制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表