欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2842|回復: 1
收起左側

基于stm32f407的GY-30光照模塊源程序

[復制鏈接]
ID:825611 發表于 2020-10-4 09:18 | 顯示全部樓層 |閱讀模式
這是我前段時間調的光照模塊,感覺還行,基本使用一下沒問題,代碼在壓縮包里

單片機源程序如下:

  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "stm32f4xx.h"

  8. #include "BH1750.h"

  9.         u8 t;
  10.         u8 len;       
  11.         u16 times=0;
  12. unsigned char        BUF[8];         /* 讀取光強中間量 */
  13. int                dis_data;       /* 讀取光強中間量 */
  14. int                mcy;            /* 讀取光強中間量 */
  15. float                lightPower;     /* 存儲光強的變量 */

  16. extern struct ds1302time time;  /* 1302時間 */

  17. char        xianshi[30];
  18. u8        tmp_buf[33];            /* 24l02接受buf */



  19. int main( void )
  20. {
  21.         u8        t                = 0;
  22.         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 );               /* 設置NVIC中斷分組2:2位搶占優先級,2位響應優先級 */
  23.         delay_init(168);                                                   /* 延時函數初始化 */


  24.         uart_init( 115200 );                                              /* uart */


  25. Init_BH1750();      /* init BH1750 */


  26.         while ( 1 )
  27.         {
  28.                

  29.                 delay_ms( 5 );
  30.                 t++;
  31.                 if ( t > 40 )
  32.                 {
  33.                        


  34.                         mread();                                        /* 連續讀取gy30數據 */
  35.                         dis_data        = BUF[0];
  36.                         dis_data        = (dis_data << 8) + BUF[1];     /* 合成數據 */
  37.                         lightPower        = (float) dis_data / 1.2;       /* 得出光強  單位是lx */
  38.                         Single_Write_BH1750( 0x01 );                    /* power on */
  39.                         Single_Write_BH1750( 0x10 );                    /* H- resolution mode */


  40.                         t = 0;
  41.                         if(USART_RX_STA&0x8000)
  42.                 {                                          
  43.                         len=USART_RX_STA&0x3fff;//得到此次接收到的數據長度
  44.                         for(t=0;t<len;t++)
  45.                         {
  46.                                 USART_SendData(USART1, USART_RX_BUF[t]);         //向串口2發送數據
  47.                                 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發送結束
  48.                         }
  49.                         printf("\r\n lightPower:%f",lightPower);//插入換行
  50.                         USART_RX_STA=0;
  51.                  delay_ms(5);
  52.           }
  53.                 }
  54.                
  55.                
  56. }

  57. }
復制代碼
  1. #include "BH1750.h"

  2. void GPIOConfig( void )
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStruct;

  5.         /*開啟GPIOB的外設時鐘*/
  6.         RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB, ENABLE );

  7.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  8.         GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  9.         GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz;
  10.         GPIO_InitStruct.GPIO_Pin        = sda | scl;
  11.         GPIO_Init( GPIOB, &GPIO_InitStruct );
  12. }


  13. void Delay_5us( void )
  14. {
  15.         delay_us(5);
  16. }


  17. void Delay_mms( unsigned int tmp )
  18. {
  19.         unsigned int i = 0;
  20.         while ( tmp-- )
  21.         {
  22.                 delay_us(5);
  23.         }
  24. }


  25. /***開始信號***/
  26. void BH1750_Start()
  27. {
  28.         GPIO_SetBits( GPIOB, sda );     /* 拉高數據線 */
  29.         GPIO_SetBits( GPIOB, scl );     /* 拉高時鐘線 */
  30.         Delay_5us();                    /* 延時 */
  31.         GPIO_ResetBits( GPIOB, sda );   /* 產生下降沿 */
  32.         Delay_5us();                    /* 延時 */
  33.         GPIO_ResetBits( GPIOB, scl );   /* 拉低時鐘線 */
  34. }


  35. /*****停止信號******/
  36. void BH1750_Stop()
  37. {
  38.         GPIO_ResetBits( GPIOB, sda );   /* 拉低數據線 */
  39.         GPIO_SetBits( GPIOB, scl );     /* 拉高時鐘線 */
  40.         Delay_5us();                    /* 延時 */
  41.         GPIO_SetBits( GPIOB, sda );     /* 產生上升沿 */
  42.         Delay_5us();                    /* 延時 */
  43. }


  44. /**************************************
  45. *  發送應答信號
  46. *  入口參數:ack (0:ACK 1:NAK)
  47. **************************************/
  48. void BH1750_SendACK( unsigned int ack )
  49. {
  50.         GPIO_InitTypeDef GPIO_InitStruct;

  51.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  52.         GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  53.         GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz;
  54.         GPIO_InitStruct.GPIO_Pin        = sda;
  55.         GPIO_Init( GPIOB, &GPIO_InitStruct );


  56.         if ( ack == 1 )                 /* 寫應答信號 */
  57.                 GPIO_SetBits( GPIOB, sda );
  58.         else if ( ack == 0 )
  59.                 GPIO_ResetBits( GPIOB, sda );
  60.         else
  61.                 return;

  62.         GPIO_SetBits( GPIOB, scl );     /* 拉高時鐘線 */
  63.         Delay_5us();                    /* 延時 */
  64.         GPIO_ResetBits( GPIOB, scl );   /* 拉低時鐘線 */
  65.         Delay_5us();                    /* 延時 */
  66. }


  67. /**************************************
  68. *  接收應答信號
  69. **************************************/
  70. unsigned int BH1750_RecvACK()
  71. {
  72.         GPIO_InitTypeDef GPIO_InitStruct;

  73.         GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_IN;        /*這里一定要設成輸入上拉,否則不能讀出數據*/
  74.         GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;      
  75.         GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz;
  76.         GPIO_InitStruct.GPIO_Pin        = sda;
  77.         GPIO_Init( GPIOB, &GPIO_InitStruct );

  78.         GPIO_SetBits( GPIOB, scl );                             /* 拉高時鐘線 */
  79.         Delay_5us();                                            /* 延時 */

  80.         if ( GPIO_ReadInputDataBit( GPIOB, sda ) == 1 )         /* 讀應答信號 */
  81.                 mcy = 1;
  82.         else
  83.                 mcy = 0;

  84.         GPIO_ResetBits( GPIOB, scl );                           /* 拉低時鐘線 */
  85.         Delay_5us();                                            /* 延時 */

  86.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  87.         GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  88.         GPIO_Init( GPIOB, &GPIO_InitStruct );

  89.         return(mcy);
  90. }


  91. /**************************************
  92. *  向IIC總線發送一個字節數據
  93. **************************************/
  94. void BH1750_SendByte( unsigned char dat )
  95. {
  96.         unsigned char i;

  97.         for ( i = 0; i < 8; i++ ) /* 8位計數器 */
  98.         {
  99.                 if ( 0X80 & dat )
  100.                         GPIO_SetBits( GPIOB, sda );
  101.                 else
  102.                         GPIO_ResetBits( GPIOB, sda );

  103.                 dat <<= 1;
  104.                 GPIO_SetBits( GPIOB, scl );     /* 拉高時鐘線 */
  105.                 Delay_5us();                    /* 延時 */
  106.                 GPIO_ResetBits( GPIOB, scl );   /* 拉低時鐘線 */
  107.                 Delay_5us();                    /* 延時 */
  108.         }
  109.         BH1750_RecvACK();
  110. }


  111. unsigned char BH1750_RecvByte()
  112. {
  113.         unsigned char        i;
  114.         unsigned char        dat = 0;
  115.         unsigned char        bit;

  116.         GPIO_InitTypeDef GPIO_InitStruct;

  117.         GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_IN;        /*這里一定要設成輸入上拉,否則不能讀出數據*/
  118.         GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  119.         GPIO_InitStruct.GPIO_Pin        = sda;
  120.         GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz;
  121.        
  122.         GPIO_Init( GPIOB, &GPIO_InitStruct );

  123.         GPIO_SetBits( GPIOB, sda );                             /* 使能內部上拉,準備讀取數據, */
  124.         for ( i = 0; i < 8; i++ )                               /* 8位計數器 */
  125.         {
  126.                 dat <<= 1;
  127.                 GPIO_SetBits( GPIOB, scl );                     /* 拉高時鐘線 */
  128.                 Delay_5us();                                    /* 延時 */

  129.                 if ( SET == GPIO_ReadInputDataBit( GPIOB, sda ) )
  130.                         bit = 0X01;
  131.                 else
  132.                         bit = 0x00;

  133.                 dat |= bit;                                     /* 讀數據 */

  134.                 GPIO_ResetBits( GPIOB, scl );                   /* 拉低時鐘線 */
  135.                 Delay_5us();                                    /* 延時 */
  136.         }

  137.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  138.         GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  139.         GPIO_Init( GPIOB, &GPIO_InitStruct );
  140.         return(dat);
  141. }


  142. void Single_Write_BH1750( unsigned char REG_Address )
  143. {
  144.         BH1750_Start();                         /* 起始信號 */
  145.         BH1750_SendByte( SlaveAddress );        /* 發送設備地址+寫信號 */
  146.         BH1750_SendByte( REG_Address );         /* 內部寄存器地址,請參考中文pdf22頁 */
  147.         /*  BH1750_SendByte(REG_data);       //內部寄存器數據,請參考中文pdf22頁 */
  148.         BH1750_Stop();                          /* 發送停止信號 */
  149. }


  150. /* 初始化BH1750,根據需要請參考pdf進行修改**** */
  151. void Init_BH1750()
  152. {
  153.         GPIOConfig();
  154.         Single_Write_BH1750( 0x01 );
  155. }


  156. /* 連續讀出BH1750內部數據 */
  157. void mread( void )
  158. {
  159.         unsigned char i;
  160.         BH1750_Start();                         /* 起始信號 */
  161.         BH1750_SendByte( SlaveAddress + 1 );    /* 發送設備地址+讀信號 */

  162.         for ( i = 0; i < 3; i++ )               /* 連續讀取6個地址數據,存儲中BUF */
  163.         {
  164.                 BUF[i] = BH1750_RecvByte();     /* BUF[0]存儲0x32地址中的數據 */
  165.                 if ( i == 3 )
  166.                 {
  167.                         BH1750_SendACK( 1 );    /* 最后一個數據需要回NOACK */
  168.                 }else  {
  169.                         BH1750_SendACK( 0 );    /* 回應ACK */
  170.                 }
  171.         }

  172.         BH1750_Stop();                          /* 停止信號 */
  173.         Delay_mms( 5 );
  174. }
復制代碼


所有資料51hei提供下載:
GY-30_f407.7z (301.84 KB, 下載次數: 53)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

ID:954693 發表于 2021-7-29 14:18 | 顯示全部樓層
為什么程序都沒有效果的
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表