标题: stm32光敏传感器源码 [打印本页]

作者: 849320464    时间: 2017-12-16 15:48
标题: stm32光敏传感器源码
  1. #include "lsens.h"
  2. #include "delay.h"
  3. //////////////////////////////////////////////////////////////////////////////////         
  4. //本程序只供学习使用,未经作者许可,不得用于其它任何用途
  5. //ALIENTEK战舰STM32开发板V3
  6. //LSENS(光敏传感器)驱动代码
  7. //版权所有,盗版必究。
  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. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1