阿哲
发表于: 2018-2-4 17:13:25 | 显示全部楼层

在本教程中,我们主要介绍如何将光敏电阻LDR连接到ARM Cortex-M3 LPC1768微控制器。光敏电阻LDR的全称是Light Dependent Resistor,主要用来检测光的强度。它们也被称为光敏电阻或光导电池。 LDR的电阻与直接照射在其上的光的强度成反比。因此在黑暗条件下,电阻通常高达到500KΩ到5MΩ左右。在环境光下,电阻一般在2KΩ到10KΩ左右。在非常明亮的光线下,其电阻下降到几欧姆 - 大概在2到20欧姆的范围内。


本教程中的示例,我们将使用LPC1768 / LPC1769微控制器的内置12位ADC模块来连接LDR。您可以查阅LPC1768使用ADC进行编程的教程,以供参考。由于我们不能直接将LDR连接到ADC的输入,所以我们需要将电阻转换为等效电压。通过串联另一个固定电阻,我们可以形成一个分压器网络,并可以将电阻和LDR的公共端连接到ADC的输入引脚。使用LDR作为R1,固定电阻作为R2。公共端的电压Vout由下式给出:

ldr_voltage_divider.png

现在,为了实现这个,我们需要一个合适的R2值。分压器中R2的值主要取决于:

●    LDR的最大电流

●    LDR的响应曲线[电阻值与亮度](在黑暗、明亮的光线和环境光线中的电阻因子)


大多数情况下,使用3.3KΩ、4.7KΩ或10KΩ电阻作为R2。但是,请检查制造商的数据手册,了解其最大工作电流。如果您没有找到数据表,那么通常几个毫安必须安全工作。对于我使用过的(大约有5mm的封装),通常1mA到5mA是不成问题的。一些LDR也允许50mA至75mA +最大电流。在我的示例情况下,使用一个4.7KΩ的电阻就可以正常工作,并提供了一个很好的满量程范围。鉴于LPC176x使用3.3V作为Vcc,在非常明亮的条件下,4.7KΩ的电阻将通过LDR将电流限制在0.7mA(最大值)。


示例1 - 读取分压器输出:

在这个例子中,我们将读取分压器的输出,其随着光线强度的变化而变化。 ADC将把输入电压转换成0到4095之间的比例值。我们可以直接使用这个值(ADC转换结果)来检测光的强度。接近0的值表示黑暗状态,接近4096的值表示存在非常明亮的光。通过这个例子,你可以看到输出值随着改变光的强度而改变。使用这个例子,你可以选择一个合适的截止值或截止范围来触发一个函数。在这个例子中,我已经重定向了printf()来通过UART重定向它的输出。原理图如下所示:

lpc176x_ldr_ex1.png


相关源码:

  1. *(C) Umang Gajera- www.ocfreaks.com
  2. LPC1768/LPC1769 LDR Interfacing Example 1 Source Code for KEIL ARM.
  3. More Embedded tutorials @ www.ocfreaks.com/cat/embedded/
  4. Also see: http://www.ocfreaks.com/lpc1768-adc-programming-tutorial/
  5. License: GPL.*/

  6. #include <lpc17xx.h>
  7. #include <stdio.h> //For retargeted printf() - http://www.ocfreaks.com/retarget-redirect-printf-scanf-uart-keil/
  8. #include "ocf_lpc176x_lib.h" //contains initUART0(), initTimer0() & putc() to retarget printf()

  9. #define ADC_CLK_EN (1<<12)
  10. #define SEL_AD0_0  (1<<0) //Select Channel AD0.0
  11. #define CLKDIV     1 //ADC clock-divider (ADC_CLOCK=PCLK/CLKDIV+1) = 12.5Mhz @ 25Mhz PCLK
  12. #define PWRUP      (1<<21) //setting it to 0 will power it down
  13. #define START_CNV  (1<<24) //001 for starting the conversion immediately
  14. #define ADC_DONE   (1U<<31) //define it as unsigned value or compiler will throw #61-D warning
  15. #define ADCR_SETUP_SCM ((CLKDIV<<8) | PWRUP)
  16. //#define VREF       3.3 //Reference Voltage at VREFP pin, given VREFN = 0V(GND) - not used in this example.

  17. int main(void)
  18. {
  19.         //SystemInit(); //Gets called by Startup code, sets CCLK=100Mhz, PCLK=25Mhz
  20.         initUART0(); //Initialize UART0 for retargeted printf()
  21.         initTimer0(); //For delayMS()

  22.         LPC_SC->PCONP |= ADC_CLK_EN;
  23.         LPC_PINCON->PINSEL1 |= (1<<14); //select AD0.0 for P0.23
  24.         LPC_ADC->ADCR = ADCR_SETUP_SCM | SEL_AD0_0;
  25.         int result = 0;
  26.         
  27.         printf("OCFreaks.com LPC176x LDR Interfacing - Example 1.\n");
  28.         
  29.         while(1)
  30.         {
  31.                 LPC_ADC->ADCR |= START_CNV; //Start new Conversion (Software controlled)

  32.                 while((LPC_ADC->ADDR0 & ADC_DONE) == 0); //Wait untill conversion is finished
  33.                
  34.                 result = (LPC_ADC->ADDR0>>4) & 0xFFF; //12 bit Mask to extract result
  35.                
  36.                 printf("AD0.0 = %d\n",result);
  37.                
  38.                 delayMS(500); //Slowing down Updates to 2 Updates per second
  39.         }
  40.         
  41.         //return 0; //This won't execute
  42. }
复制代码

示例2 - 在黑暗时打开LED:

在这个连接示例中,我们将在检测到光线不明或黑暗条件时开启LED。 您也可以连接一个继电器,在寒冷地区的夜间,您可以接通一个灯泡或加热器等。 请确保将LDR远离LED或灯泡,因为我们只关心在这种情况下检测阳光而不是人造光。 这个例子的原理图如下:

lpc176x_ldr_ex2.png

相关源码:

  1. /*(C) Umang Gajera- www.ocfreaks.com
  2. LPC1768/LPC1769 LDR Interfacing Example 2 Source Code for KEIL ARM.
  3. More Embedded tutorials @ www.ocfreaks.com/cat/embedded/
  4. Also see: http://www.ocfreaks.com/lpc1768-adc-programming-tutorial/
  5. License: GPL.*/

  6. #include <lpc176x.h>
  7. #include "ocf_lpc176x_lib.h"

  8. #define ADC_CLK_EN (1<<12)
  9. #define SEL_AD0_0  (1<<0) //Select Channel AD0.0
  10. #define CLKDIV     1 //ADC clock-divider (ADC_CLOCK=PCLK/CLKDIV+1) = 12.5Mhz @ 25Mhz PCLK
  11. #define PWRUP      (1<<21) //setting it to 0 will power it down
  12. #define START_CNV  (1<<24) //001 for starting the conversion immediately
  13. #define ADC_DONE   (1U<<31) //define it as unsigned value or compiler will throw #61-D warning
  14. #define ADCR_SETUP_SCM ((CLKDIV<<8) | PWRUP)
  15. //#define VREF       3.3 //Reference Voltage at VREFP pin, given VREFN = 0V(GND) - not used in this example.

  16. #define LDR_CUT_OFF 750 //Define your cut-off value here

  17. int main(void)
  18. {
  19.         //SystemInit(); //Gets called by Startup code, sets CCLK=100Mhz, PCLK=25Mhz
  20.         initTimer0(); //For delayMS()

  21.         LPC_SC->PCONP |= ADC_CLK_EN;
  22.         LPC_PINCON->PINSEL1 |= (1<<14); //select AD0.0 for P0.23
  23.         LPC_ADC->ADCR = ADCR_SETUP_SCM | SEL_AD0_0;
  24.         LPC_GPIO0->FIODIR |= (1<<0); //Set P0.0 as output
  25.         LPC_GPIO0->FIOCLR |= (1<<0); //LED initially OFF
  26.         
  27.         int result = 0;
  28.         
  29.         while(1)
  30.         {
  31.                 LPC_ADC->ADCR |= START_CNV; //Start new Conversion (Software controlled)

  32.                 while((LPC_ADC->ADDR0 & ADC_DONE) == 0);
  33.                
  34.                 result = (LPC_ADC->ADDR0>>4) & 0xFFF; //12 bit Mask to extract result
  35.                
  36.                 if(result < LDR_CUT_OFF)
  37.                 {
  38.                         LPC_GPIO0->FIOSET |= (1<<0); //drive P0.0 HIGH - LED ON
  39.                 }
  40.                 else
  41.                 {
  42.                         LPC_GPIO0->FIOCLR |= (1<<0); //drive P0.0 LOW - LED OFF
  43.                 }
  44.                
  45.                 delayMS(50); //wait some time since LDRs don't react immediately.
  46.         }
  47.         //return 0; //This won't execute normally
  48. }
复制代码
跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 53 | 回复: 76



手机版|

GMT+8, 2024-4-25 16:55 , Processed in 0.055339 second(s), 11 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表