這是我前段時間調的光照模塊,感覺還行,基本使用一下沒問題,代碼在壓縮包里
單片機源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stm32f4xx.h"
- #include "BH1750.h"
- u8 t;
- u8 len;
- u16 times=0;
- unsigned char BUF[8]; /* 讀取光強中間量 */
- int dis_data; /* 讀取光強中間量 */
- int mcy; /* 讀取光強中間量 */
- float lightPower; /* 存儲光強的變量 */
- extern struct ds1302time time; /* 1302時間 */
- char xianshi[30];
- u8 tmp_buf[33]; /* 24l02接受buf */
- int main( void )
- {
- u8 t = 0;
- NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 ); /* 設置NVIC中斷分組2:2位搶占優先級,2位響應優先級 */
- delay_init(168); /* 延時函數初始化 */
- uart_init( 115200 ); /* uart */
- Init_BH1750(); /* init BH1750 */
- while ( 1 )
- {
-
- delay_ms( 5 );
- t++;
- if ( t > 40 )
- {
-
- mread(); /* 連續讀取gy30數據 */
- dis_data = BUF[0];
- dis_data = (dis_data << 8) + BUF[1]; /* 合成數據 */
- lightPower = (float) dis_data / 1.2; /* 得出光強 單位是lx */
- Single_Write_BH1750( 0x01 ); /* power on */
- Single_Write_BH1750( 0x10 ); /* H- resolution mode */
- t = 0;
- if(USART_RX_STA&0x8000)
- {
- len=USART_RX_STA&0x3fff;//得到此次接收到的數據長度
- for(t=0;t<len;t++)
- {
- USART_SendData(USART1, USART_RX_BUF[t]); //向串口2發送數據
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發送結束
- }
- printf("\r\n lightPower:%f",lightPower);//插入換行
- USART_RX_STA=0;
- delay_ms(5);
- }
- }
-
-
- }
- }
復制代碼- #include "BH1750.h"
- void GPIOConfig( void )
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /*開啟GPIOB的外設時鐘*/
- RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB, ENABLE );
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = sda | scl;
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- }
- void Delay_5us( void )
- {
- delay_us(5);
- }
- void Delay_mms( unsigned int tmp )
- {
- unsigned int i = 0;
- while ( tmp-- )
- {
- delay_us(5);
- }
- }
- /***開始信號***/
- void BH1750_Start()
- {
- GPIO_SetBits( GPIOB, sda ); /* 拉高數據線 */
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- GPIO_ResetBits( GPIOB, sda ); /* 產生下降沿 */
- Delay_5us(); /* 延時 */
- GPIO_ResetBits( GPIOB, scl ); /* 拉低時鐘線 */
- }
- /*****停止信號******/
- void BH1750_Stop()
- {
- GPIO_ResetBits( GPIOB, sda ); /* 拉低數據線 */
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- GPIO_SetBits( GPIOB, sda ); /* 產生上升沿 */
- Delay_5us(); /* 延時 */
- }
- /**************************************
- * 發送應答信號
- * 入口參數:ack (0:ACK 1:NAK)
- **************************************/
- void BH1750_SendACK( unsigned int ack )
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = sda;
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- if ( ack == 1 ) /* 寫應答信號 */
- GPIO_SetBits( GPIOB, sda );
- else if ( ack == 0 )
- GPIO_ResetBits( GPIOB, sda );
- else
- return;
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- GPIO_ResetBits( GPIOB, scl ); /* 拉低時鐘線 */
- Delay_5us(); /* 延時 */
- }
- /**************************************
- * 接收應答信號
- **************************************/
- unsigned int BH1750_RecvACK()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; /*這里一定要設成輸入上拉,否則不能讀出數據*/
- GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = sda;
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- if ( GPIO_ReadInputDataBit( GPIOB, sda ) == 1 ) /* 讀應答信號 */
- mcy = 1;
- else
- mcy = 0;
- GPIO_ResetBits( GPIOB, scl ); /* 拉低時鐘線 */
- Delay_5us(); /* 延時 */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- return(mcy);
- }
- /**************************************
- * 向IIC總線發送一個字節數據
- **************************************/
- void BH1750_SendByte( unsigned char dat )
- {
- unsigned char i;
- for ( i = 0; i < 8; i++ ) /* 8位計數器 */
- {
- if ( 0X80 & dat )
- GPIO_SetBits( GPIOB, sda );
- else
- GPIO_ResetBits( GPIOB, sda );
- dat <<= 1;
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- GPIO_ResetBits( GPIOB, scl ); /* 拉低時鐘線 */
- Delay_5us(); /* 延時 */
- }
- BH1750_RecvACK();
- }
- unsigned char BH1750_RecvByte()
- {
- unsigned char i;
- unsigned char dat = 0;
- unsigned char bit;
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; /*這里一定要設成輸入上拉,否則不能讀出數據*/
- GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_InitStruct.GPIO_Pin = sda;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- GPIO_SetBits( GPIOB, sda ); /* 使能內部上拉,準備讀取數據, */
- for ( i = 0; i < 8; i++ ) /* 8位計數器 */
- {
- dat <<= 1;
- GPIO_SetBits( GPIOB, scl ); /* 拉高時鐘線 */
- Delay_5us(); /* 延時 */
- if ( SET == GPIO_ReadInputDataBit( GPIOB, sda ) )
- bit = 0X01;
- else
- bit = 0x00;
- dat |= bit; /* 讀數據 */
- GPIO_ResetBits( GPIOB, scl ); /* 拉低時鐘線 */
- Delay_5us(); /* 延時 */
- }
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
- GPIO_Init( GPIOB, &GPIO_InitStruct );
- return(dat);
- }
- void Single_Write_BH1750( unsigned char REG_Address )
- {
- BH1750_Start(); /* 起始信號 */
- BH1750_SendByte( SlaveAddress ); /* 發送設備地址+寫信號 */
- BH1750_SendByte( REG_Address ); /* 內部寄存器地址,請參考中文pdf22頁 */
- /* BH1750_SendByte(REG_data); //內部寄存器數據,請參考中文pdf22頁 */
- BH1750_Stop(); /* 發送停止信號 */
- }
- /* 初始化BH1750,根據需要請參考pdf進行修改**** */
- void Init_BH1750()
- {
- GPIOConfig();
- Single_Write_BH1750( 0x01 );
- }
- /* 連續讀出BH1750內部數據 */
- void mread( void )
- {
- unsigned char i;
- BH1750_Start(); /* 起始信號 */
- BH1750_SendByte( SlaveAddress + 1 ); /* 發送設備地址+讀信號 */
- for ( i = 0; i < 3; i++ ) /* 連續讀取6個地址數據,存儲中BUF */
- {
- BUF[i] = BH1750_RecvByte(); /* BUF[0]存儲0x32地址中的數據 */
- if ( i == 3 )
- {
- BH1750_SendACK( 1 ); /* 最后一個數據需要回NOACK */
- }else {
- BH1750_SendACK( 0 ); /* 回應ACK */
- }
- }
- BH1750_Stop(); /* 停止信號 */
- Delay_mms( 5 );
- }
復制代碼
所有資料51hei提供下載:
GY-30_f407.7z
(301.84 KB, 下載次數: 53)
2020-10-4 15:12 上傳
點擊文件名下載附件
|