|
|
I2C設(shè)置模式及讀MAX30100 FIF,未進(jìn)行最后數(shù)據(jù)處理
0.png (6.01 KB, 下載次數(shù): 33)
下載附件
2018-7-24 17:34 上傳
單片機(jī)源程序如下:
- #include "max30100_iic.h"
- #include <stdio.h>
- #include <intrins.h>
- // 引腳定義
- sbit IIC_SCL =P2^1; //IIC的SCL
- sbit IIC_SDA =P2^0; //IIC的SDA
- bit IIC_ACK; //IIC的ACK
- //-------------------------------------------------------------------------------------//
- //函數(shù): delayms()
- //功能: 延時(shí)程序
- //-------------------------------------------------------------------------------------//
- void delayms(unsigned int ms)
- {
- unsigned char i=100,j;
- for(;ms;ms--)
- {
- while(--i)
- {
- j=10;
- while(--j);
- }
- }
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): void iic_start();
- //功能: I2C總線開始
- //-------------------------------------------------------------------------------------//
- // SCL --- --- ___
- // SDA --- ___ ___
- void iic_start()
- {
- IIC_SDA=1;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- IIC_SDA=0;
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): void iic_stop();
- //功能: I2C總線結(jié)束
- //需定義:
- //-------------------------------------------------------------------------------------//
- // SCL ___ --- ---
- // SDA ___ ___ ---
- void iic_stop()
- {
- IIC_SCL=0;
- _nop_();
- _nop_();
- IIC_SDA=0;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- IIC_SDA=1;
- _nop_();
- _nop_();
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): void iic_sendbyte(unsigned char c);
- //功能: 發(fā)送 8_BIT 數(shù)據(jù)
- //-------------------------------------------------------------------------------------//
- void iic_sendbyte(unsigned char c)
- {
- unsigned char bitcnt;
- for(bitcnt=0;bitcnt<8;bitcnt++)
- {
- if((c<<bitcnt)&0x80)
- IIC_SDA=1;
- else
- IIC_SDA=0;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- IIC_SCL=0;
- }
- _nop_();
- _nop_();
- IIC_SDA=1;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- if(IIC_SDA==0)
- IIC_ACK=0;
- else
- IIC_ACK=1;
- IIC_SCL=0;
- _nop_();
- _nop_();
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): int iic_rcvbyte_nack();
- //功能: 接收 8_BIT 數(shù)據(jù) 最后ack信號(hào)
- //-------------------------------------------------------------------------------------//
- int iic_rcvbyte_nack()
- {
- unsigned char retc;
- unsigned char bitcnt;
- retc=0;
- IIC_SDA=1;
- for(bitcnt=0;bitcnt<8;bitcnt++)
- {
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- retc=retc<<1;
- if(IIC_SDA==1)
- retc=retc+1;
- _nop_();
- _nop_();
- }
- //給出NACK信號(hào)
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- IIC_SDA=1;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- return(retc);
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): int iic_rcvbyte_ack();
- //功能: 接收 8_BIT 數(shù)據(jù) 最后ack信號(hào)
- //-------------------------------------------------------------------------------------//
- int iic_rcvbyte_ack()
- {
- unsigned char retc;
- unsigned char bitcnt;
- retc=0;
- IIC_SDA=1;
- for(bitcnt=0;bitcnt<8;bitcnt++)
- {
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- retc=retc<<1;
- if(IIC_SDA==1)
- retc=retc+1;
- _nop_();
- _nop_();
- }
- //給出ACK信號(hào)
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- IIC_SDA=0;
- _nop_();
- _nop_();
- IIC_SCL=1;
- _nop_();
- _nop_();
- IIC_SCL=0;
- _nop_();
- _nop_();
- return(retc);
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): wr_max30100_one_data()
- //功能: 寫一位max30100數(shù)據(jù)
- //address: 芯片從地址
- //saddress: 寫寄存器地址
- //w_data: 待寫數(shù)據(jù)
- //-------------------------------------------------------------------------------------//
- void wr_max30100_one_data(int address,int saddress,int w_data )
- {
- _nop_();
- iic_start();
- _nop_();
- iic_sendbyte(address);
- _nop_();
- iic_sendbyte(saddress);
- _nop_();
- iic_sendbyte(w_data);
- _nop_();
- iic_stop();
- _nop_();
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): rd_max30100_one_data()
- //功能: 讀一位max30100數(shù)據(jù)
- //address: 芯片從地址
- //saddress: 讀寄存器地址
- //rda: 讀出的數(shù)據(jù)
- //-------------------------------------------------------------------------------------//
- int rd_max30100_one_data(int address,int saddress)
- {
- int rda;
- iic_start();
- _nop_();
- iic_sendbyte(address);
- _nop_();
- iic_sendbyte(saddress);
- _nop_();
- address=address+1;
- _nop_();
- iic_start();
- _nop_();
- iic_sendbyte(address);
- _nop_();
- rda=iic_rcvbyte_nack();
- _nop_();
- iic_stop();
- return rda;
- }
- void readTemperature()
- {
- //溫度功能測(cè)試
- int rda;
- double temp,temp1,temp2;
- //temp 測(cè)量溫度
- //temp1 30100整數(shù)部分溫度
- //temp2 30100小數(shù)部分溫度
- wr_max30100_one_data(0xae,0x09,0x66); // 設(shè)置電流
- delayms(50); //
- rda = rd_max30100_one_data(0xae,0x16); // 讀出溫度信號(hào)
- //printf("temp1=%d\n",rda); // 串口顯示
- temp1=rda;
- rda = rd_max30100_one_data(0xae,0x17); // 讀出溫度小數(shù)部分?jǐn)?shù)據(jù)
- //printf("temp2=%d\n",rda); // 串口顯示
- temp2 = rda;
- temp = temp1+(temp2*0.0625); // 計(jì)算溫度,小數(shù)部分最小溫度值0.0625
- printf("當(dāng)前溫度=%.4f\n",temp); // 串口顯示當(dāng)前溫度
- delayms(100);
- printf("\n");
- delayms(50);
- }
復(fù)制代碼
所有資料51hei提供下載:
xinlv.zip
(3.02 KB, 下載次數(shù): 52)
2018-7-24 14:58 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|