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