前言
本文是基于嵌入式开发板CT117E,stm32f103RBT6。
一、原理图
二、由原理图得到的信息
- 一个数字信号线DO和一个模拟信号线AO。
- DO与PA3连接,AO与PA4连接。
- 电路方程(VDD - AO(化成以V为单位)) / 10K = (AO(化成以V为单位) - GDN) / R47。
- VDD=3.3,GDN=0,代进方程,求解R47 = AO*10 / (3.3-AO) K。
三、光敏电阻初始化
tr_ao_do.c
void trao_do_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PC.01, PC.02 and PC.04 (ADC Channel11, Channel12 and Channel14)
as analog input ----------------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
tr_ao_do.h
#ifndef TRAO_DO_H
#define TRAO_DO_H
#include "stm32f10x.h"
void trao_do_init(void);
u16 get_adcval(void);
u8 get_status(void);
#endif
四、光敏电阻应用函数
u16 get_adcval(void) //AO
{
u8 i;
u16 t[10];
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
for(i=0;i<10;i++)
{
t[i]=ADC_GetConversionValue (ADC1);
}
for(i=1;i<10;i++)
{
t[0] +=t[i];
}
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
return t[0]/10;
}
u8 get_status(void) //DO 1:灯关太暗 0:灯关太亮 ,这个暗和亮的程度主要靠RP7来调节
{
u8 t;
t=GPIO_ReadInputDataBit (GPIOA,GPIO_Pin_3);
return t;
}
五、光敏电阻在主函数的应用方法
while(1)
{
tr_val=get_adcval();
sprintf((char *)buff," V: %2.2fV ",(float)tr_val*3.3/0xfff); //输出光敏电阻的电压值
LCD_DisplayStringLine(Line4,buff);
sprintf((char *)buff," R: %2.2fK ",(10*(float)tr_val/(4096-tr_val))); //输出光敏电阻的电阻值
LCD_DisplayStringLine(Line5,buff);
if(get_status()==0)
{
LCD_DisplayStringLine(Line1," The light is hight! ");
}
else
{
LCD_DisplayStringLine(Line1," The light is low! ");
}
Delay_Ms(500);
}