一、什么是GY-39?
GY-39 是一款低成本,氣壓,溫濕度,光強度 傳感器模塊。工作電壓 3-5v,功耗小,安裝方便。 其工作原理是,MCU 收集各種傳感器數(shù)據(jù),統(tǒng)一處理,直接輸出計算后的結(jié)果,此模塊,有兩種方式讀取數(shù)據(jù),即串口 UART(TTL 電平)或者 IIC(2 線)。串口的波特率有 9600bps 與 115200bps,可配置,有連續(xù),詢問輸出兩種方式,可掉電保存設(shè)置。可適應(yīng)不同的工作環(huán)境,與單片機及電腦連接。模塊另外可以設(shè)置單獨傳感器芯片工作模式,作為簡單傳感器模塊,MCU 不參與數(shù)據(jù)處理工作。提供 arduino,51,stm32 單片機通訊程序,不提供原理圖及內(nèi)部單片機源碼。此 GY39 模塊另外贈送安卓手機軟件 app 查看數(shù)據(jù),且支持 wifi 局域內(nèi)網(wǎng)連接,手機及電腦同時顯示數(shù)據(jù)。
二、產(chǎn)品參數(shù)
1. 特點
- 高性價比
- 內(nèi)置 MCU 計算傳感器數(shù)據(jù)
- IIC、串口通信格式
- 統(tǒng)一數(shù)據(jù)輸出
- 配相應(yīng)的上位機軟件
2. 技術(shù)
3. 應(yīng)用
- 數(shù)字照明管理
- 物聯(lián)網(wǎng),智能家居應(yīng)用
- 氣象站監(jiān)測
- 數(shù)字光照度計
- 數(shù)字氣壓計,高度計
- 溫濕度計
- 大棚氣候監(jiān)測
三、引腳說明
四、通信協(xié)議
1. 串口通信
當 GY-39 模塊硬件 PinA(S0)=1 時候使用
2. IIC通信
當 GY-39 模塊硬件 PinA(S0)=0 時候使用
五、項目工程
以下IIC通信工程
1. 定義結(jié)構(gòu)體接收數(shù)據(jù)
typedef struct
{
uint32_t P;
uint16_t Temp;
uint16_t Hum;
uint16_t Alt;
} bme;
bme Bme={0,0,0,0};
2. 引腳初始化
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能與 I2C有關(guān)的時鐘 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
/* PC3-I2C_SCL、PC5-I2C_SDA*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIOB->BSRR = GPIO_Pin_6;
GPIOB->BSRR = GPIO_Pin_7;
}
3. 向IIC設(shè)備寫入一個字節(jié)數(shù)據(jù)
u8 Single_WriteI2C_byte(u8 Slave_Address,u8 REG_Address,u8 data)
{
if(I2C_Start()==0) //起始信號
{I2C_Stop(); return RESET;}
I2C_SendByte(Slave_Address); //發(fā)送設(shè)備地址+寫信號
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
I2C_SendByte(REG_Address); //內(nèi)部寄存器地址,
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
I2C_SendByte(data); //內(nèi)部寄存器數(shù)據(jù),
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
I2C_Stop(); //發(fā)送停止信號
return SET;
}
4. 從IIC設(shè)備讀取一個字節(jié)數(shù)據(jù)
u8 Single_ReadI2C(u8 Slave_Address,u8 REG_Address,u8 *REG_data,u8 length)
{
if(I2C_Start()==0) //起始信號
{I2C_Stop(); return RESET;}
I2C_SendByte(Slave_Address); //發(fā)送設(shè)備地址+寫信號
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
I2C_SendByte(REG_Address); //發(fā)送存儲單元地址
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
if(I2C_Start()==0) //起始信號
{I2C_Stop(); return RESET;}
I2C_SendByte(Slave_Address+1); //發(fā)送設(shè)備地址+讀信號
if(!I2C_WaitAck()){I2C_Stop(); return RESET;}
while(length-1)
{
*REG_data++=I2C_RecvByte(); //讀出寄存器數(shù)據(jù)
I2C_SendACK(0); //應(yīng)答
length--;
}
*REG_data=I2C_RecvByte();
I2C_SendACK(1); //發(fā)送停止傳輸信號
I2C_Stop(); //停止信號
return SET;
}
5. 數(shù)據(jù)轉(zhuǎn)換文章來源:http://www.zghlxwxcb.cn/news/detail-541534.html
while(1)
{
if(Single_ReadI2C(0xb6,0x04,raw_data,10))
{
Bme.Temp=(raw_data[0]<<8)|raw_data[1];
data_16[0]=(((uint16_t)raw_data[2])<<8)|raw_data[3];
data_16[1]=(((uint16_t)raw_data[4])<<8)|raw_data[5];
Bme.P=(((uint32_t)data_16[0])<<16)|data_16[1];
Bme.Hum=(raw_data[6]<<8)|raw_data[7];
Bme.Alt=(raw_data[8]<<8)|raw_data[9];
}
if(Single_ReadI2C(0xb6,0x00,raw_data,4))
data_16[0]=(((uint16_t)raw_data[0])<<8)|raw_data[1];
data_16[1]=(((uint16_t)raw_data[2])<<8)|raw_data[3];
Lux=(((uint32_t)data_16[0])<<16)|data_16[1];
printf("Temp: %.2f DegC ",(float)Bme.Temp/100);
printf(" P: %.2f Pa ",(float)Bme.P/100);
printf(" Hum: %.2f ",(float)Bme.Hum/100);
printf(" Alt: %.2f m\r\n ",(float)Bme.Alt);
printf("\r\n Lux: %.2f lux\r\n ",(float)Lux/100);
printf("\r\n \r\n ");
delay_ms(20000);
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-541534.html
到了這里,關(guān)于GY-39 氣壓,溫濕度,光強度 傳感器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!