光敏电阻是用硫化隔或硒化隔等半导体材料制成的特殊电阻器,其工作原理是基于内光电效应。随着光照强度的升高,电阻值迅速降低,由于光照产生的载流子都参与导电,在外加电场的作用下作漂移运动,电子奔向电源的正极,空穴奔向电源的负极,从而使光敏电阻器的阻值迅速下降。其在无光照时,几乎呈高阻状态,暗时电阻很大。光敏电阻模块一般用来检测周围环境的光线的亮度,触发单片机或继电器模块等。
一、模块来源
二、规格参数
工作电压:3.3-5V
工作电流:1MA
模块尺寸:31.1475 x 14.097mm
输出方式: DO接口为数字量输出 AO接口为模拟量输出
读取方式:ADC
管脚数量:4 Pin(2.54mm间距排针)
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【判断当前光照强度的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
这个模块采用的光敏电阻的型号是5516,对应下图,可以知道在光亮时的阻值在8到20KΩ左右,在光暗时的阻值在1MΩ左右。
其对应的原理图,其中U2.1为LM393,R3为光敏电阻。AO输出为R2和R3分压后直接输出电压,所以为模拟量;DO为经过LM393进行电压比较后,输出高低电平,所以为数字量。具体原理是,393的3号引脚电压与2号引脚进行电压比较。当3号引脚电压比2号引脚电压高时,1号引脚输出高电平;当3号引脚电压比2号引脚电压低时,1号引脚输出低电平;可以通过调整R4控制2号引脚的电压。
因此DO引脚可以配置为GPIO的输入模式,AO引脚需要配置为ADC模拟输入模式。
2、引脚选择
当前只有AO引脚需要使用到ADC接口,所以DO引脚可以使用开发板上其他的GPIO。这里选择使用PB0的附加ADC功能。
VCC | 3V3 |
GND | GND |
DO | PB2 |
AO | PB0 |
3、代码编写
新建两个文件 bsp_illume.c
和 bsp_illume.h
,并且将头文件路径添加到编译器中。
在文件 bsp_illume.c
和 bsp_illume.h
中,编写如下代码。
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_ILLUME_H_
#define BSP_CODE_BSP_ILLUME_H_
#include "gd32vw55x.h"
#include "systick.h"
#ifndef delay_ms
#define delay_ms(x) delay_1ms(x)
#endif
#ifndef delay_us
#define delay_us(x) delay_1us(x)
#endif
#define BSP_ILLUME_RCU_ENABLE() \
rcu_periph_clock_enable(BSP_ADC_GPIO_RCU); \
rcu_periph_clock_enable(BSP_DO_GPIO_RCU); \
rcu_periph_clock_enable(BSP_ADC_RCU);
/* PB0 ADC_IN8 */
#define BSP_ADC_GPIO_RCU RCU_GPIOB
#define BSP_ADC_GPIO_PORT GPIOB
#define BSP_ADC_GPIO_PIN GPIO_PIN_0
/* PB0 ADC_IN8 */
#define BSP_ADC_RCU RCU_ADC
#define BSP_ADC ADC
#define BSP_ADC_CHANNEL ADC_CHANNEL_8
#define BSP_DO_GPIO_RCU RCU_GPIOB
#define BSP_DO_GPIO_PORT GPIOB
#define BSP_DO_GPIO_PIN GPIO_PIN_2
#define BSP_DO_READ() gpio_input_bit_get(BSP_DO_GPIO_PORT, BSP_DO_GPIO_PIN)
void illume_Init(void);
int Get_illume_Adc_Value(uint8_t Count);
int Get_illume_Percentage_value(void);
uint8_t Get_illume_DO_In(void);
#endif /* BSP_CODE_BSP_ILLUME_H_ */
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_illume.h"
void illume_Init(void)
{
/* 使能ADC和GPIO时钟 */
BSP_ILLUME_RCU_ENABLE();
// 配置DO引脚为输入模式
gpio_mode_set(BSP_DO_GPIO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BSP_DO_GPIO_PIN);
// 配置ADC时钟为4分频
adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
// 配置引脚为模拟浮空输入模式
gpio_mode_set(BSP_ADC_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BSP_ADC_GPIO_PIN);
// 使能扫描模式
adc_special_function_config(ADC_SCAN_MODE, ENABLE);
// 数据右对齐
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
// ADC设置为12位分辨率
adc_resolution_config(ADC_RESOLUTION_12B);
// ADC设置为规则组 一共使用 1 个通道
adc_channel_length_config(ADC_ROUTINE_CHANNEL, 1);
// ADC外部触发禁用, 即只能使用软件触发
adc_external_trigger_config(ADC_ROUTINE_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
// 使能软件触发
adc_software_trigger_enable(ADC_ROUTINE_CHANNEL);
// ADC使能
adc_enable();
}
/**********************************************************
* 函 数 名 称:ADC_GET
* 函 数 功 能:读取一次ADC数据
* 传 入 参 数:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:LP
**********************************************************/
static int ADC_GET(void)
{
uint16_t gAdcResult = 0;
uint16_t timeOut = 1000;
// 设置采集通道
adc_routine_channel_config(0, BSP_ADC_CHANNEL, ADC_SAMPLETIME_14POINT5);
// 开始软件转换
adc_software_trigger_enable(ADC_ROUTINE_CHANNEL);
// 等待ADC总线处理完成
while(!adc_flag_get(ADC_FLAG_EOC) && timeOut--)
{
delay_us(1);
}
// 清除ADC采样完成标志位
adc_flag_clear(ADC_FLAG_EOC);
if(!timeOut)
{
printf("ADC_GET Failed!!!\r\n");
return 0;
}
// 获取通道的转换结果
gAdcResult = adc_routine_data_read();
// printf("gAdcResult = %d\r\n",gAdcResult);
return gAdcResult;
}
/**********************************************************
* 函 数 名 称:Get_illume_Adc_Value
* 函 数 功 能:获得某个通道的值
* 传 入 参 数:Count:采集次数
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:LP
**********************************************************/
int Get_illume_Adc_Value(uint8_t Count)
{
int gAdcResult = 0;
uint8_t i = 0;
for(i = 0; i < Count; i++)
{
//获取数据
gAdcResult += ADC_GET();
}
return (gAdcResult / Count);
}
/******************************************************************
* 函 数 名 称:Get_illume_Percentage_value
* 函 数 说 明:读取光敏电阻值,并且返回百分比
* 函 数 形 参:无
* 函 数 返 回:返回百分比
* 作 者:LCKFB
* 备 注:最亮100 最暗0
******************************************************************/
int Get_illume_Percentage_value(void)
{
//ADC精度都是12位
//2的12次方 = 4096
//因为单片机是从0开始算,所以要4096-1=4095
int adc_max = 4095;
int adc_new = 0;
int Percentage_value = 0;
adc_new = Get_illume_Adc_Value(5);
//百分比 = ( 当前值 / 最大值 )* 100
Percentage_value = ( 1 - ( (float)adc_new / adc_max ) ) * 100;
return Percentage_value;
}
/******************************************************************
* 函 数 名 称:Get_illume_DO_In
* 函 数 说 明:读取DO引脚的电平状态
* 函 数 形 参:无
* 函 数 返 回:1=检测过暗 0=检测过亮
* 作 者:LCKFB
* 备 注:无
******************************************************************/
uint8_t Get_illume_DO_In(void)
{
if( BSP_DO_READ() == 1 )
{
return 1;
}
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
四、移植验证
在 src\main.c
中输入代码如下:
#include "gd32vw55x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32vw553h_eval.h"
#include "bsp_illume.h"
/*!
\brief toggle the led every 500ms
\param[in] none
\param[out] none
\retval none
*/
void led_spark(void)
{
static __IO uint32_t timingdelaylocal = 0U;
if(timingdelaylocal) {
if(timingdelaylocal < 500U) {
gd_eval_led_on(LED1);
} else {
gd_eval_led_off(LED1);
}
timingdelaylocal--;
} else {
timingdelaylocal = 1000U;
}
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
#ifdef __FIRMWARE_VERSION_DEFINE
uint32_t fw_ver = 0;
#endif /* __FIRMWARE_VERSION_DEFINE */
/* configure systick */
systick_config();
eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
/* initilize the LEDs, USART and key */
gd_eval_led_init(LED1);
gd_eval_com_init(EVAL_COM0);
gd_eval_key_init(KEY_TAMPER_WAKEUP, KEY_MODE_GPIO);
#ifdef __FIRMWARE_VERSION_DEFINE
fw_ver = gd32vw55x_firmware_version_get();
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n\n");
printf("\r\n=== Welcome to use the LC-GD32VW553-HMQ6 development board ====\r\n\n");
printf("\r\n======================= www.lckfb.com =========================\r\n\n");
printf("\r\n======================= wiki.lckfb.com ========================\r\n\n");
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
/* print firmware version */
printf("\r\nGD32VW55X series firmware version: V%d.%d.%d", (uint8_t)(fw_ver >> 24), (uint8_t)(fw_ver >> 16), (uint8_t)(fw_ver >> 8));
#endif /* __FIRMWARE_VERSION_DEFINE */
/* print out the clock frequency of system, AHB, APB1 and APB2 */
printf("\r\nCK_SYS is %d\r\n", rcu_clock_freq_get(CK_SYS));
printf("\r\nCK_AHB is %d\r\n", rcu_clock_freq_get(CK_AHB));
printf("\r\nCK_APB1 is %d\r\n", rcu_clock_freq_get(CK_APB1));
printf("\r\nCK_APB2 is %d\r\n", rcu_clock_freq_get(CK_APB2));
/* initialize the illumination sensor */
illume_Init();
printf("\r\nIllumination sensor initialized.\r\n");
while(1)
{
printf("\nADC Value = %d\r\n",Get_illume_Adc_Value(5));
printf("Percentage value = %d%%\r\n",Get_illume_Percentage_value());
printf("DO = %d\r\n",Get_illume_DO_In());
delay_ms(500);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
上电效果:
【代码下载】
- 跳转到
下载中心
去下载模块移植代码:【点击跳转🚀】