|
我用的是德飛萊 七星蟲系列的stm32f103主板,型號(hào):尼莫M3S,想要連接心率傳感器,但是這款傳感器自帶的調(diào)試程序是51單片機(jī)的,想改成arm的程序。自帶程序中用51模擬了IIC接口,stm32主板自帶IIC接口,我現(xiàn)在要把原51代碼轉(zhuǎn)換成arm代碼,望各位指教
stm32主板
心率傳感器
單片機(jī)源程序如下:
- //-------------------------------------------------------------------------------------//
- //程序名: MAX30100測(cè)試程序
- //版本: V1.0
- //功能: 測(cè)試MAX30100的ID及溫度部分功能。
- //-------------------------------------------------------------------------------------//
- //RCWL-0530模塊與51接口連接:
- // 1:VCC --〉 1.8V-5.5V電源
- // 2:SCL --〉 P3.5
- // 3:SDA --〉 P3.7
- // 4:INT --〉 NC
- // 5:IRD --〉 NC
- // 6:RD --〉 NC
- // 7:GND --〉 地
- //-------------------------------------------------------------------------------------//
- # include <reg52.h>
- # include <stdio.h>
- # include <intrins.h>
- //定義IIC接口
- sbit IIC_SCL =P3^5; //IIC的SCL
- sbit IIC_SDA =P3^7; //IIC的SDA
- bit IIC_ACK; //IIC的ACK
- int rda; //IIC讀出
- //-------------------------------------------------------------------------------------//
- //函數(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ù)
- //-------------------------------------------------------------------------------------//
- void rd_max30100_one_data(int address,int saddress)
- {
- 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();
- }
- //-------------------------------------------------------------------------------------//
- //函數(shù): 主程序
- //功能: 讀max30100內(nèi)部溫度
- //-------------------------------------------------------------------------------------//
- main()
- {
- double temp,temp1,temp2;
- //temp 測(cè)量溫度
- //temp1 30100整數(shù)部分溫度
- //temp2 30100小數(shù)部分溫度
- TMOD=0x21;
- SCON=0x50;
- TH1=0xFD;
- TL1=0xFD;
- TR1=1;
- TI=1;
- //設(shè)置51的波特率為 9600 N 8 1
- //51主頻為11.0592MHz
- while(1)
- {
- wr_max30100_one_data(0xae,0x06,0x0a); // 0X06地址B3位TEMP_EN置1
- delayms(20); // 等待溫度轉(zhuǎn)換完成,不等待,讀出數(shù)據(jù)有誤
- rd_max30100_one_data(0xae,0x16); // 讀出溫度信號(hào)
- printf("temp1=%d\n",rda); // 串口顯示
- temp1=rda;
- rd_max30100_one_data(0xae,0x17); // 讀出溫度小數(shù)部分?jǐn)?shù)據(jù)
- printf("temp2=%d\n",rda); // 串口顯示
- temp2=rda;
- temp=temp1+(temp2/100);
- printf("temp=%.2f\n",temp); // 串口顯示
- rd_max30100_one_data(0xae,0xff); // 讀出芯片ID
- printf("MAX30100 ID =%d\n",rda); // 串口顯示
- delayms(100);
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
MAX30100溫度及ID功能測(cè)試程序.7z
(13.93 KB, 下載次數(shù): 10)
2019-6-3 20:51 上傳
點(diǎn)擊文件名下載附件
|
|