欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: 51單片機(jī)讀MAX30100原創(chuàng)代碼 [打印本頁(yè)]

作者: 15011959191    時(shí)間: 2018-7-24 14:59
標(biāo)題: 51單片機(jī)讀MAX30100原創(chuàng)代碼
I2C設(shè)置模式及讀MAX30100 FIF,未進(jìn)行最后數(shù)據(jù)處理

單片機(jī)源程序如下:
  1. #include "max30100_iic.h"
  2. #include <stdio.h>
  3. #include <intrins.h>

  4. // 引腳定義
  5. sbit IIC_SCL    =P2^1;     //IIC的SCL
  6. sbit IIC_SDA    =P2^0;     //IIC的SDA
  7. bit  IIC_ACK;              //IIC的ACK

  8. //-------------------------------------------------------------------------------------//
  9. //函數(shù):    delayms()
  10. //功能:    延時(shí)程序
  11. //-------------------------------------------------------------------------------------//
  12. void delayms(unsigned int ms)
  13. {
  14.         unsigned char i=100,j;
  15.         for(;ms;ms--)
  16.         {
  17.                 while(--i)
  18.                 {
  19.                         j=10;
  20.                         while(--j);
  21.                 }
  22.         }
  23. }

  24. //-------------------------------------------------------------------------------------//
  25. //函數(shù):    void iic_start();
  26. //功能:    I2C總線開(kāi)始
  27. //-------------------------------------------------------------------------------------//
  28. //   SCL   --- --- ___
  29. //   SDA   --- ___ ___
  30. void iic_start()
  31. {  
  32.         IIC_SDA=1;
  33.         _nop_();
  34.         _nop_();
  35.         IIC_SCL=1;         
  36.         _nop_();
  37.         _nop_();
  38.         IIC_SDA=0;
  39.         _nop_();
  40.         _nop_();
  41.         IIC_SCL=0;
  42.         _nop_();
  43.         _nop_();
  44. }
  45. //-------------------------------------------------------------------------------------//
  46. //函數(shù):    void iic_stop();
  47. //功能:    I2C總線結(jié)束
  48. //需定義:
  49. //-------------------------------------------------------------------------------------//
  50. //   SCL   ___ --- ---
  51. //   SDA   ___ ___ ---
  52. void iic_stop()
  53. {  
  54.         IIC_SCL=0;
  55.         _nop_();
  56.         _nop_();
  57.         IIC_SDA=0;
  58.         _nop_();
  59.         _nop_();
  60.         IIC_SCL=1;
  61.         _nop_();
  62.         _nop_();
  63.         IIC_SDA=1;
  64.         _nop_();
  65.         _nop_();
  66. }
  67. //-------------------------------------------------------------------------------------//
  68. //函數(shù):    void iic_sendbyte(unsigned char c);
  69. //功能:    發(fā)送 8_BIT 數(shù)據(jù)
  70. //-------------------------------------------------------------------------------------//
  71. void iic_sendbyte(unsigned char c)
  72. {
  73.         unsigned char bitcnt;
  74.         for(bitcnt=0;bitcnt<8;bitcnt++)
  75.         {
  76.                 if((c<<bitcnt)&0x80)
  77.                         IIC_SDA=1;
  78.                 else
  79.                          IIC_SDA=0;
  80.                 _nop_();
  81.                 _nop_();
  82.                 IIC_SCL=1;
  83.                 _nop_();
  84.                 _nop_();
  85.                 IIC_SCL=0;
  86.         }
  87.         _nop_();
  88.         _nop_();
  89.         IIC_SDA=1;
  90.         _nop_();
  91.         _nop_();
  92.         IIC_SCL=1;
  93.         _nop_();
  94.         _nop_();
  95.         if(IIC_SDA==0)
  96.                 IIC_ACK=0;
  97.         else
  98.                 IIC_ACK=1;
  99.                 IIC_SCL=0;
  100.                 _nop_();
  101.                 _nop_();
  102. }
  103. //-------------------------------------------------------------------------------------//
  104. //函數(shù):    int iic_rcvbyte_nack();
  105. //功能:    接收 8_BIT 數(shù)據(jù)  最后ack信號(hào)
  106. //-------------------------------------------------------------------------------------//
  107. int iic_rcvbyte_nack()
  108. {
  109.         unsigned char retc;
  110.         unsigned char bitcnt;
  111.         retc=0;
  112.         IIC_SDA=1;
  113.         for(bitcnt=0;bitcnt<8;bitcnt++)
  114.         {
  115.                 _nop_();
  116.                 _nop_();
  117.                 IIC_SCL=0;
  118.                 _nop_();
  119.                 _nop_();
  120.                 IIC_SCL=1;
  121.                 _nop_();
  122.                 _nop_();
  123.                 retc=retc<<1;
  124.                 if(IIC_SDA==1)
  125.                         retc=retc+1;
  126.                 _nop_();
  127.                 _nop_();
  128.         }
  129.         //給出NACK信號(hào)
  130.         _nop_();
  131.         _nop_();
  132.         IIC_SCL=0;
  133.         _nop_();
  134.         _nop_();
  135.         IIC_SDA=1;
  136.         _nop_();
  137.         _nop_();
  138.         IIC_SCL=1;
  139.         _nop_();
  140.         _nop_();
  141.         IIC_SCL=0;
  142.         _nop_();
  143.         _nop_();
  144.         return(retc);
  145. }
  146. //-------------------------------------------------------------------------------------//
  147. //函數(shù):    int iic_rcvbyte_ack();
  148. //功能:    接收 8_BIT 數(shù)據(jù) 最后ack信號(hào)
  149. //-------------------------------------------------------------------------------------//
  150. int iic_rcvbyte_ack()
  151. {
  152.         unsigned char retc;
  153.         unsigned char bitcnt;
  154.         retc=0;
  155.         IIC_SDA=1;
  156.         for(bitcnt=0;bitcnt<8;bitcnt++)
  157.         {
  158.                 _nop_();
  159.                 _nop_();
  160.                 IIC_SCL=0;
  161.                 _nop_();
  162.                 _nop_();
  163.                 IIC_SCL=1;
  164.                 _nop_();
  165.                 _nop_();
  166.                 retc=retc<<1;
  167.                 if(IIC_SDA==1)
  168.                 retc=retc+1;
  169.                 _nop_();
  170.                 _nop_();
  171.         }
  172.         //給出ACK信號(hào)
  173.         _nop_();
  174.         _nop_();
  175.         IIC_SCL=0;
  176.         _nop_();
  177.         _nop_();
  178.         IIC_SDA=0;
  179.         _nop_();
  180.         _nop_();
  181.         IIC_SCL=1;
  182.         _nop_();
  183.         _nop_();
  184.         IIC_SCL=0;
  185.         _nop_();
  186.         _nop_();
  187.         return(retc);
  188. }
  189. //-------------------------------------------------------------------------------------//
  190. //函數(shù):      wr_max30100_one_data()
  191. //功能:      寫(xiě)一位max30100數(shù)據(jù)
  192. //address:   芯片從地址
  193. //saddress:  寫(xiě)寄存器地址
  194. //w_data:    待寫(xiě)數(shù)據(jù)
  195. //-------------------------------------------------------------------------------------//
  196. void wr_max30100_one_data(int address,int saddress,int w_data )
  197. {
  198.         _nop_();
  199.         iic_start();
  200.         _nop_();
  201.         iic_sendbyte(address);
  202.         _nop_();
  203.         iic_sendbyte(saddress);
  204.         _nop_();
  205.         iic_sendbyte(w_data);
  206.         _nop_();
  207.         iic_stop();
  208.         _nop_();
  209. }
  210. //-------------------------------------------------------------------------------------//
  211. //函數(shù):      rd_max30100_one_data()
  212. //功能:      讀一位max30100數(shù)據(jù)
  213. //address:   芯片從地址
  214. //saddress:  讀寄存器地址
  215. //rda:       讀出的數(shù)據(jù)
  216. //-------------------------------------------------------------------------------------//
  217. int rd_max30100_one_data(int address,int saddress)
  218. {
  219.         int rda;
  220.         iic_start();
  221.         _nop_();
  222.         iic_sendbyte(address);
  223.         _nop_();
  224.         iic_sendbyte(saddress);
  225.         _nop_();
  226.         address=address+1;
  227.         _nop_();
  228.         iic_start();
  229.         _nop_();
  230.         iic_sendbyte(address);
  231.         _nop_();
  232.         rda=iic_rcvbyte_nack();
  233.         _nop_();
  234.         iic_stop();
  235.         return rda;
  236. }

  237. void readTemperature()
  238. {       
  239.         //溫度功能測(cè)試
  240.         int rda;
  241.         double temp,temp1,temp2;
  242.         //temp       測(cè)量溫度
  243.         //temp1      30100整數(shù)部分溫度
  244.         //temp2      30100小數(shù)部分溫度
  245.         wr_max30100_one_data(0xae,0x09,0x66);       // 設(shè)置電流
  246.         delayms(50);                                //
  247.         rda = rd_max30100_one_data(0xae,0x16);            // 讀出溫度信號(hào)
  248.         //printf("temp1=%d\n",rda);                   // 串口顯示
  249.         temp1=rda;
  250.         rda = rd_max30100_one_data(0xae,0x17);            // 讀出溫度小數(shù)部分?jǐn)?shù)據(jù)
  251.         //printf("temp2=%d\n",rda);                   // 串口顯示
  252.         temp2 = rda;
  253.         temp = temp1+(temp2*0.0625);                  // 計(jì)算溫度,小數(shù)部分最小溫度值0.0625
  254.         printf("當(dāng)前溫度=%.4f\n",temp);             // 串口顯示當(dāng)前溫度
  255.         delayms(100);  
  256.         printf("\n");                              
  257.         delayms(50);
  258. }
復(fù)制代碼

所有資料51hei提供下載:
xinlv.zip (3.02 KB, 下載次數(shù): 52)



作者: 小阿LO    時(shí)間: 2018-10-5 16:40
樓主有做出來(lái)嗎





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