找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2154|回复: 0
打印 上一主题 下一主题
收起左侧

ad7799单片机驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:170375 发表于 2017-3-13 17:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
例子一:


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

  3. //SPCR SPI控制寄存器

  4. //SPSR SPI状态寄存器

  5. //SPDAT SPI数据寄存器
  6. void SPI_init(void)
  7. {
  8. SPCR=0x5e;//SPI控制寄存器,中断禁止,SPI使能,高位在前,主机模式,时钟空闲时为高,后沿触发移位,时钟分频64
  9. SPSR=0x00;//清中断标志位
  10. }
  11. /*--------------------------------------------
  12. 写AD7799寄存器函数
  13. WriteData:要写的数据
  14. ----------------------------------------------*/
  15. void WriteByteToAd7799(unsigned char WriteData)
  16. {
  17. SPDAT= WriteData;
  18. while(~SPSR&0x80); //等待数据发送完
  19. SPSR=0x00; //清中断标志位

  20. }
  21. /*--------------------------------------------
  22. 防止时序混乱,实现再同步
  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初始化函数
  45. ----------------------------------------------*/
  46. void AD7799_init(void)
  47. {
  48. AD7799_CS=0;
  49. /*------------------------增益为128,通道0----------------------------------------*/
  50. WriteByteToAd7799(0x10); //写通信寄存器设置下一个操作为写配置寄存器

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

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

  53. /*------------------- 写模式寄存器初始化零值校准------------------------------------*/
  54. WriteByteToAd7799(0x08); //写通信寄存器设置下一个操作为写模式寄存器

  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); //写通信寄存器设置下一个操作为写模式寄存器

  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); //写通信寄存器设置下一个操作为写模式寄存器

  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); //写通信寄存器设置下一个操作为读状态STATUS寄存器

  75. WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟

  76. ADSAT=SPDAT; //读取接收到的数据
  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); //写通信寄存器设置下一个操作为读状态STATUS寄存器

  89. WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟

  90. ADSAT=SPDAT; //读取接收到的数据

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

  94. WriteByteToAd7799(0x58); //写通信寄存器设置下一个操作为连续读数据寄存器

  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); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
  102. ConverData=SPDAT;
  103. ConverData=ConverData<<8 ;

  104. WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
  105. ConverData=ConverData+SPDAT;
  106. ConverData=ConverData<<8 ;

  107. WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
  108. ConverData=ConverData+SPDAT;

  109. }
  110. if(ErrNUM>5)return(0);
  111. else return(ConverData);
  112. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表