文章目录
只做学习记录,记录自己如何从零学会使用一个模块,仅仅只是会用,缺乏专业知识。如果需要了解更多原理,可以从我推荐的技术大佬的文章中获取。
我的学习目的就是通过STM32的ADC功能,学会使用光敏传感器,并在无光时用oled屏显示“黑暗”,在有光时用oled屏显示“明亮”。
我使用的是三线光敏传感器,通过光敏电阻在不同光照环境下的不同阻值,从而影响电压,再通过ADC功能获取电压值。用途:光线亮度检测,光线亮度传感器,智能小车寻光模块。接线方面:vcc接3.3v,GND接地,DO引脚直接与单片机相连,通过单片机来检测高低电平,由此来检测环境的光线亮度改变。我定义的IO口是PA0(即模块的DO引脚接单片机的PA0)
lsens.h(光敏传感器初始化)
#ifndef __LSENS_H
#define __LSENS_H
#include "sys.h"
#include "adc.h"
void Lsens_Init(void); //初始化光敏传感器
#endif
lsens.c
#include "lsens.h"
#include "delay.h"
void Lsens_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
adc.h
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
void Adc_Init(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
#endif
adc.c
#include "adc.h"
#include "delay.h"
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1, ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
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);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
// ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t
这里主要是初始化ADC,相信大家已经比较了解了。由于我的能力有限,我只作学习记录,如果需要了解相关知识,可以看看下面这篇文章。
STM32—ADC详解_stm32f103有几个adc_Aspirant-GQ的博客-CSDN博客
main.c
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "adc.h"
#include "oled.h"
#include "gui.h"
#include "lsens.h"
int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
Adc_Init();
OLED_Init();
OLED_Clear(0);
while(1)
{
if( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0)
{
GUI_ShowCHinese(20,0,16,"明亮",16);
}
else
{
GUI_ShowCHinese(20,0,16,"黑暗",16);
}
delay_ms(100);
}
}
相关oled的汉字取模
"明",0x00,0x00,0x00,0xFC,0x7C,0x84,0x44,0x84,0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,0x01,0x04,0x01,0x04,0x02,0x14,0x04,0x08,
"亮",0x02,0x00,0x01,0x00,0x7F,0xFC,0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x00,0x00,
0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x08,0x20,0x08,0x20,0x10,0x22,0x20,0x22,0xC0,0x1E,
"黑",0x00,0x00,0x3F,0xF8,0x21,0x08,0x29,0x28,0x25,0x48,0x3F,0xF8,0x01,0x00,0x01,0x00,
0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x48,0x88,0x44,0x44,0x84,0x44,
"暗",0x00,0x80,0x00,0x40,0x7B,0xFC,0x48,0x00,0x49,0x08,0x48,0x90,0x4F,0xFE,0x78,0x00,
0x49,0xF8,0x49,0x08,0x49,0x08,0x49,0xF8,0x79,0x08,0x49,0x08,0x01,0xF8,0x01,0x08,
由于专业知识能力有限,仅仅只能做到会使用一个模块,并且可能会有些许错误。