|
BH1750是一款数字式传感器,并采用I2C接口进行信息的交换,该例程是由STM32 F103来驱动的,并由串口来输出检测值,其整体构成如图所示。 整体构成图
! k8 [4 i7 c- C% J: O
BH1750与STM32 F103的连接关系是: SDA--- PB8 SCL--- PB7 使2个引脚输出高低电平的语句是: #define SCL_Set() GPIO_SetBits(GPIOB,GPIO_Pin_7); // PB7 #define SCL_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_7); #define SDA_Set() GPIO_SetBits(GPIOB,GPIO_Pin_8); // PB8 #define SDA_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_8); BH1750引脚功能的配置函数是: - void BH1750_GPIO_Config(void)
- Q/ \: l/ i3 N' J# a - {. s2 `8 s2 y8 P5 {$ T& t; i
- GPIO_InitTypeDef GPIO_InitStructure;+ m) H( p& D+ Y) s
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
$ E# ^; f' _$ g- U6 L( y - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8; ' ~* E9 G. |; G/ A( R V
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 7 F8 v8 L* k3 D
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
6 f+ [! ~& {4 k! \, r - GPIO_Init(GPIOB, &GPIO_InitStructure);- Q, Z' E& J e
- }4 q' Y# D! ]+ v6 Q# `' S
- + i$ T& V8 z, i p W1 u, W
- 切换SDA引脚输入输出功能的函数是:5 Y/ q/ |* ]; c
- void IIC_INPUT_MODE_SET(void)
' I! B7 W" N+ P3 m5 Z% T' Y1 ] - {( y9 y7 o4 n; q# `4 t
- GPIO_InitTypeDef GPIO_InitStructure;
6 @6 F# u# f2 {8 V6 [6 H - RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); @6 W! M2 `3 R# ], q: u1 \2 z
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8; ' w) |2 D- j$ A) I4 w% H; f
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;) d0 T& Q, M( m D
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
3 }7 V- I# m7 n) Q* W. p( ~ - GPIO_Init(GPIOB, &GPIO_InitStructure);
, _7 N# \. H) O9 u5 v+ f5 `5 w - }
% A3 l4 t8 V* @: [ ~% } -
4 Y7 S& o' X' K, O- ~3 @: s - void IIC_INPUT_MODE_SET(void)
" u R1 S* u) H7 f6 \( g4 } - {
$ e0 V0 v( ]* j - GPIO_InitTypeDef GPIO_InitStructure;5 O$ S9 g' ^1 e: \8 P. s
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);& ^, |7 e* f2 `# Z% C; x; T
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8; 3 W: X- s; N# @) Z5 Q* I# _. @3 T
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;5 U3 ^* K0 F6 n
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 6 B, c5 q9 j. E7 c
- GPIO_Init(GPIOB, &GPIO_InitStructure);
A6 q/ {4 H% [ - }
复制代码
" Z5 j4 C7 P* N' V9 t 读取光强数据的函数是: - void Get_Sunlight_Value()
4 O4 Q5 k. U2 f - {, I0 A i6 W3 {: B7 D- m- X7 ~
- int dis_data=0;
! i! T$ Q" n# D - float temp;! ^$ z$ f/ q# ]! m: C# c
- unsigned int sd;
5 y. Z$ |8 m/ i# ] - Single_Write_BH1750(0x01); // power on- |. K1 K% w& L
- Single_Write_BH1750(0x10); // H- resolution mode
/ w6 g1 X' d2 A+ A - Delay_us(180000);
~$ _$ p4 k3 T - Multiple_Read_BH1750();2 o2 k2 f# \! m* O% w1 y2 ~7 B
- dis_data=BUF[0];! `, ?* i) z' z. G$ `3 [
- dis_data=(dis_data<<8)+BUF[1]; T. l2 F6 t7 a' K* V
- temp=(float)dis_data/1.2;! ~8 a; i- H: O0 J6 [9 d T
- sd=temp;
. O/ M; V$ F, M' q - UART1SendByte((u8)(sd/1000)+'0');9 r3 H) _' o, j
- UART1SendByte((u8)(sd%1000/100)+'0');, g& Q0 V6 X$ m4 ~% s
- UART1SendByte((u8)(sd%100/10)+'0');
% W8 Z7 g. o2 j( [: P4 g - UART1SendByte((u8)(sd%10)+'0');6 i* d6 n' ?" |4 B; \/ y7 L
- UART1SendByte(' ');& H0 m' ^' K) a" r6 H
- UART1SendByte('l');
$ X! V# }, ]+ p* S5 w# \ - UART1SendByte('x');
X3 h. A: R+ V -
/ Z8 ~, c- ~- a9 W) v- M8 y2 J - UART1SendByte(0x0d);
' t$ W: O% V/ S N; X - UART1SendByte(0x0a);
1 O8 ]* v; ~8 p5 f4 J9 r2 \' h2 J/ q, z - }
复制代码 显示光强检测的主程序是: - int main(void)
, E/ a- H- Z( T- m" i - {
/ G/ P! r5 w% f% [ - SystemInit();" b- Y1 t1 `. A& A5 U& T
- SysTick_Init();
z7 M' D% W% P$ g - USART1_Config();
7 t8 I' r/ j* P6 |+ P - UART1SendByte('B');7 W) Y1 P5 B- X; D
- UART1SendByte('H');
) e6 q' ?, C1 | r - UART1SendByte('0');
/ P/ R0 D1 ]) c( x& M - UART1SendByte('1');/ [8 ]7 N, C& N3 J4 i. Q
- UART1SendByte('7');
3 _; k+ t1 k% x+ h - UART1SendByte('5');
3 D6 \! n8 i, a9 n+ F( a% t - UART1SendByte(' ');
% m$ S& b& c4 @7 ~) y - UART1SendByte('T');
- L7 c3 f/ |( n+ F* h* { - UART1SendByte('E');9 L: h! h( @" C4 L: I- I
- UART1SendByte('S'); n$ x( q7 B9 q V# T5 `
- UART1SendByte('T');
8 ~, a) G0 j% Y. s5 M - UART1SendByte(0x0d);
. o. Q; x9 f+ `+ Q7 z - UART1SendByte(0x0a); B; ^: \8 `& z" C
- BH1750_GPIO_Config();
& _! p& M7 _ t8 I5 |) y1 e, A: J" i - while(1)
6 ]# D9 ^. ]. B6 f - {/ r- x4 v: X! E5 @
- Get_Sunlight_Value();6 K! R* f3 p" d) i' R* f6 c, G |
- Delay_us(1000000); " y, h6 U8 ^4 h8 p7 c( F+ o
- }
; b- d" P) Q+ a& {3 E4 r - }
复制代码 编译下载后,运行的效果如图所示。 光强检测输出图 工程文件:8 `* X) A6 \: Y2 {4 ?4 f4 T
|