找回密码
 立即注册

QQ登录

只需一步,快速开始

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

自己写的传感器代码

[复制链接]
跳转到指定楼层
楼主
ID:347687 发表于 2018-6-8 16:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void Single_Write_BH1750(uint8_t REG_Data)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)!=RESET);//检测是否总线忙
I2C_TransferHandling(I2C1,SlaveAddress,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXIS)==RESET);//检测发送
I2C_SendData(I2C1,REG_Data);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TC)==RESET);
I2C_GenerateSTOP(I2C1,ENABLE);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_STOPF)==RESET);//是否停止
}
uint16_t Read_BH1750()
{
uint16_t Recev=0x00;//光强为16位。先发送高8后低8
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)!=RESET);//IFBUSY
I2C_TransferHandling(I2C1,SlaveAddress+1,2,I2C_AutoEnd_Mode,I2C_Generate_Start_Read);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE)==RESET);
Recev|=I2C_ReceiveData(I2C1);Recev<<=8;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE)==RESET);
Recev|=I2C_ReceiveData(I2C1);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_STOPF)==RESET);
return Recev;
}
void BH1750_Get_Guang(float*Zhaodu)
#include"stm32f10x_lib.h"
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0//LCD1602数据端口
#define SlaveAddress 0x46#define BH1750_SCL GPIO_Pin_14
#define BH1750_SDA GPIO_Pin_15
#define GPIO_I2C GPIOB
#defineBH1750_SCL_0()GPIOF->BRR=BH1750_SCL
#define BH1750_SCL_1()GPIOF->BSRR=BH1750_SCL
#define BH1750_SDA_0()GPIOF->BRR=BH1750_SDA
#define BH1750_SDA_1()GPIOF->BSRR=BH1750_SDA
#define BH1750_SDA_STATE()(GPIOF->IDR&BH1750_SDA)
return(GPIOB->IDR typedefunsigned char BYTE;
typedef unsigned short WORD;BYTE BUF[8];//接收数据缓存区
uchar ge,shi,bai,qian,wan;//显示变量
int dis_data;//变量
void Init_BH1750(void);
void conversion(uint temp_data);void Single_Write_BH1750(ucharREG_Address); uchar Single_Read_BH1750(uchar REG_Address);
void Multiple_Read_BH1750(void);//连续的读取内部寄存器数据
u8BH1750_Start(void);
void BH1750_Stop(void);
u8 BH1750_SendByte(u8 Data);
u8 BH1750_ReceiveByte(void);
void BH1750_SendACK(void);
void BH1750_SendNACK(void);
void BH1750_Nop
(void voidBH1750_Delay(unsigned int u8BH1750_ACK u8BH1750_READYu8BH1750_NACK
u8BH1750_BUS_BUSY
u8BH1750_BUS_ERROR u8BH1750_RETRY_COUNT//重试次数
void BH1750_Nop(void)u8i=50;
while(i)
voidBH1750_Delay(unsigned
int unsignedintu8BH1750_Start(void)BH1750_SDA_1();BH1750_Nop();
BH1750_SCL_1();BH1750_Nop();
if(!BH1750_SDA_STATE())//
//DebugPrint("TWI_START:BUSY\n");
return BH1750_BUS_BUSY;
BH1750_SDA_0();
BH1750_Nop();
BH1750_SCL_0();BH1750_Nop();
if(BH1750_SDA_STATE())//
//DebugPrint("TWI_START:BUSERROR\n");
return BH1750_BUS_ERROR;
returnBH1750_READY;
voidBH1750_Stop(void)
BH1750_SDA_0();BH1750_Nop();
BH1750_SCL_1();BH1750_Nop();
BH1750_SDA_1();BH1750_Nop();//
//DebugPrint("TWI_STOP\n");
voidBH1750_SendACK(void)BH1750_SDA_0();
BH1750_Nop();
BH1750_SCL_1();
BH1750_Nop();
BH1750_SCL_0();
BH1750_Nop();
BH1750_SDA_1();
//DebugPrint("TWI_SendACK\n");
voidBH1750_SendNACK(void)BH1750_SDA_1();
BH1750_Nop();
BH1750_SCL_1();
BH1750_Nop();
BH1750_SCL_0();
BH1750_Nop();//
//DebugPrint("TWI_SendNACK\n");
//---------数据建立----------if(Data&0x80)Data<<=1;BH1750_Nop();//---数据建立保持一定延时----//
BH1750_SCL_1();
BH1750_Nop();
BH1750_SCL_0();
BH1750_Nop();//延时,防止SCL还没变成低时改变SDA,从而产生START/STOP信号//接收从机的应答BH1750_SDA_1();
BH1750_Nop();
BH1750_SCL_1();
BH1750_Nop();
if(BH1750_SDA_STATE())BH1750_SCL_0();BH1750_SDA_1();//
//DebugPrint("TWI_NACK!n");return BH1750_NACK;
BH1750_SCL_0();
BH1750_SDA_1();//
//DebugPrint("TWI_ACK!n");
return BH1750_ACK;
u8BH1750_ReceiveByte(void)u8i,Dat;
BH1750_SDA_1();
BH1750_SCL_0();
Dat=0;BH1750_SCL_1();//产生时钟上升沿[正脉冲],让从机准备好数据BH1750_Nop();
Dat<<=1;if(BH1750_SDA_STATE())//读引脚状态BH1750_SCL_0();//准备好再次接收数据BH1750_Nop();//等待数据准备好//
//DebugPrint("TWI_Dat:%xn",Dat);
return Dat;
//voidconversion(uint temp_data)temp_data=temp_data%10000;
//取余运算
temp_data=temp_data%1000;//取余运算
temp_data=temp_data%100;//取余运算
temp_data=temp_data%10;//取余运算
voidSingle_Write_BH1750(uchar
REG_Address)BH1750_Start();//起始信号
BH1750_SendByte(SlaveAddress);//发送设备地址+写信号
BH1750_SendByte(REG_Address);//内部寄存器地址,
BH1750_SendByte(REG_data);//内部寄存器数据,
BH1750_Stop();//发送停止信号
ucharSingle_Read_BH1750(uchar REG_Address)
ucharREG_data;BH1750_Start();//起始信号
BH1750_SendByte(SlaveAddress);//发送设备地址+写信号
BH1750_SendByte(REG_Address);//发送存储单元地址,从0开始
BH1750_Start();//起始信号
BH1750_SendByte(SlaveAddress+1);//发送设备地址+读信号
REG_data=BH1750_RecvByte();//读出寄存器数
BH1750_SendACK(1);BH1750_Stop();//停止信号
return REG_data;//连续读出BH1750内部数据
voidMultiple_Read_BH1750(void)BH1750_Start();
//起始信号
BH1750_SendByte(SlaveAddress+1);//发送设备地址+读信号
BH1750_ReceiveByte();//BUF[0]存储0x32地址中的数据
BH1750_SendNACK();//最后一个数据需要回NOACK
BH1750_SendACK();//回应
ACK BH1750_Stop();//停止信号//
Delay5ms();BH1750_Delay(500)//初始化BH1750,
void Init_BH1750()
voidBH1750_Get_Guang(float*Zhaodu)//延时100ms
Init_BH1750();//初始化BH1750
Single_Write_BH1750(0x01);
resolutionmode BH1750_Delay(200)//延时180ms
Multiple_Read_BH1750();//连续读出数据,存储在BUF dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];
//合成数据,即光照数据
*Zhaodu=(float)dis_data/1.2;

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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