標(biāo)題: stm32光敏傳感器源碼 [打印本頁]

作者: 849320464    時間: 2017-12-16 15:48
標(biāo)題: stm32光敏傳感器源碼
  1. #include "lsens.h"
  2. #include "delay.h"
  3. //////////////////////////////////////////////////////////////////////////////////         
  4. //本程序只供學(xué)習(xí)使用,未經(jīng)作者許可,不得用于其它任何用途
  5. //ALIENTEK戰(zhàn)艦STM32開發(fā)板V3
  6. //LSENS(光敏傳感器)驅(qū)動代碼
  7. //版權(quán)所有,盜版必究。
  8. //Copyright(C) 廣州市星翼電子科技有限公司 2014-2024
  9. //All rights reserved
  10. //////////////////////////////////////////////////////////////////////////////////

  11. //初始化光敏傳感器
  12. void Lsens_Init(void)
  13. {
  14.   GPIO_InitTypeDef GPIO_InitStructure;
  15.         
  16.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);//使能PORTF時鐘        
  17.         
  18.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//PF8 anolog輸入
  19.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;                //模擬輸入引腳
  20.         GPIO_Init(GPIOF, &GPIO_InitStructure);        
  21.         Adc3_Init();
  22. }
  23. //讀取Light Sens的值
  24. //0~100:0,最暗;100,最亮
  25. u8 Lsens_Get_Val(void)
  26. {
  27.         u32 temp_val=0;
  28.         u8 t;
  29.         for(t=0;t<LSENS_READ_TIMES;t++)
  30.         {
  31.                 temp_val+=Get_Adc3(LSENS_ADC_CHX);        //讀取ADC值
  32.                 delay_ms(5);
  33.         }
  34.         temp_val/=LSENS_READ_TIMES;//得到平均值
  35.         if(temp_val>4000)temp_val=4000;
  36.         return (u8)(100-(temp_val/40));
  37. }
復(fù)制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1