当前位置:网站首页>STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
2022-07-21 19:49:00 【chfens】
This article introduces two ways to use
One 、 Write read AD The value of the function , After judgment AD value , Carry out the corresponding operation , Like lighting a light .
Two 、 use ADC Read photosensitive sensor AO output , And configuration ADC The channel watchdog monitors this channel , Turn it on when the light is too dark LED The lamp .
Then we will introduce programmable RGB Lamp with WS2812B.
Introduction to photosensitive sensor
The photosensitive sensor has two output ports , One is DO(Digital Output), One is AO(Analog Output),DO It's digital output , Only output 0 or 1,AO Is analog output , Can cooperate with stm32 Of ADC The value obtained by the converter is stored in 12 position In the register of , therefore AD The range of values is 4095 - 0, namely 2 Of 12 Power -1 To 2 Of 0 Power .
Program code
#include "LightAD.h"
#include "stm32f10x.h" // Device header
#include "LED.h"
#define RCC_LightADC RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA
#define GPIO_LightADC GPIOA
#define PIN_LightADC GPIO_Pin_0
#define Channel_LightADC ADC_Channel_0
unsigned int HighThreshold = 3000;
unsigned int LowThreshold = 0;
// use AD Conversion with analog watchdog Single conversion non scanning
extern char BrightEnough;
First define the pins and channels , Define three variables , Two are monitored by the watchdog High and low threshold , One is main.c Flag bit defined in , Judge whether the current light is bright enough .
void Light_GPIOInit()
{
GPIO_InitTypeDef GPIO_AD;
GPIO_AD.GPIO_Mode = GPIO_Mode_AIN;
GPIO_AD.GPIO_Pin = PIN_LightADC;
GPIO_AD.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LightADC,&GPIO_AD);
}
To configure GPIO mouth , Be careful GPIO Mode to choose Analog input .
void WatchDog_ADInit()
{
ADC_AnalogWatchdogSingleChannelConfig(ADC1, Channel_LightADC);
ADC_AnalogWatchdogThresholdsConfig(ADC1,HighThreshold,LowThreshold);
ADC_AnalogWatchdogCmd(ADC1,ADC_AnalogWatchdog_SingleRegEnable);
ADC_ITConfig(ADC1,ADC_IT_AWD,ENABLE);
}
Configure watchdog , choice ADC1 or 2, Select the corresponding channel , There is only one photosensitive sensor , Generally choose the channel 0.
void Light_NVICInit()
{
NVIC_InitTypeDef nvic_struct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
nvic_struct.NVIC_IRQChannel = ADC1_2_IRQn;
nvic_struct.NVIC_IRQChannelCmd = ENABLE;
nvic_struct.NVIC_IRQChannelPreemptionPriority = 3;
nvic_struct.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&nvic_struct);
}
Turn on NVIC passageway , Fill in the priority casually .
void ADC1_2_IRQHandler()
{
if(ADC_GetITStatus(ADC1,ADC_IT_AWD) == 1)
{
BrightEnough = 0;
LEDOn();
ADC_ClearITPendingBit(ADC1,ADC_IT_AWD);
}
}
Configure interrupt function , When entering interrupt , The light is not bright enough , And open LED The lamp , Finally, clear the flag bit , Be careful Interrupt flag bit yes ADC_IT_AWD.
void Light_ADInit()
{
//ADC1 passageway 0 stay PA0
RCC_APB2PeriphClockCmd(RCC_LightADC,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);// The highest 14Mhz,72 Sextant frequency 12Mhz
Light_GPIOInit();//1
// Select the input channel of the rule group
// passageway 0, Sequence 1, Medium sampling time
ADC_RegularChannelConfig(ADC1, Channel_LightADC, 1, ADC_SampleTime_55Cycles5);
// initialization ADC
ADC_InitTypeDef adc_struct;
adc_struct.ADC_Mode = ADC_Mode_Independent;
adc_struct.ADC_DataAlign = ADC_DataAlign_Right;
adc_struct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
adc_struct.ADC_NbrOfChannel = 1;
adc_struct.ADC_ContinuousConvMode = DISABLE;
adc_struct.ADC_ScanConvMode = DISABLE;
// Single conversion non scanning , There's only one way
ADC_Init(ADC1,&adc_struct);
// Configure watchdog and interrupt
WatchDog_ADInit();//2
Light_NVICInit();//3
ADC_Cmd(ADC1,ENABLE);
// Reset the calibration first
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1) == 1);
// Wait for reset calibration to complete , When the register software is set 1 Start calibration , After calibrating the hardware setting 0
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1) == 1);
}
To configure ADC, choice Rule group The input channel of , choice ADC Independent mode , instead of ADC1 and ADC2 Cooperative mode of working together , The advantage of the latter is faster conversion , But there's no need to ,ADC Conversion time It's very short , There will be calculations below . Set the data alignment to right . Generally, right alignment is set , You can search for the knowledge of left-right alignment by yourself . Choose not to set external trigger source , Select single trigger , Non scan mode . The number of channels is set to 1 strip . Then remember Reset calibration , Otherwise, it may cause numerical drift .
// Multi channel acquisition can be realized by changing the scanning channel in a single scan , Pay attention to revision GPIO Initialization pin
unsigned int Light_GetADVal()
{
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == 0);
// Wait for the rule group conversion to complete
// The sampling time is 55.5, Conversion fixed period is 12.5, altogether 68 A cycle
//72MHZ6 frequency division ,12Mhz 68 A cycle , The time is about 1/12M * 68, about 5.6us
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
return ADC_GetConversionValue(ADC1);
}
Read AD value ,AD The conversion cycle is shown in the notes in the program , The result time is about 5.6us, The time is very short . meanwhile , If there are multiple input devices , A single non scanning method can be used to achieve the effect of a single continuous scanning , You only need to change the channel to be read next time after a device finishes reading , Update after each reading AD Values and channels .
Method 1 just get the read in the main function AD Value judgment and then execute the function , Method 2: configure the function in the watchdog interrupt after configuring with the watchdog code above .
Conclusion
STM32 Of ADC A lot of function , Here is just a brief introduction to the basic methods , The rest is left for readers to explore .
边栏推荐
- Momenta“飞轮式L4”接受夜间长尾场景「像素级」挑战,表现堪比老司机
- 作业正则 sed
- Boundary value analysis of common test case design methods
- 无线定位技术实验二 TDOA最小二乘定位法
- How to find out whether there is the same user name in the database during user registration in PHP
- odoo神操作后台调用路由接口
- 笔试强训第20天
- Lombok simplifies development
- Siyuan synchronization problem: cloud object not found v2.1.0
- es6相关面试题
猜你喜欢
ctfshow MengXIn misc1
How Jenkins sends e-mail? The automation veteran will teach you
Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 2)
我的UI自動化測試的感悟
45. Record the training process of orienmask and the process of deploying Yunshi technology depth camera
学习笔记-Explain的介绍
项目管理成熟度模型及项目量化管理
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
Poste technique | a40i les trois problèmes de logiciel de carte réseau les plus courants, analysez - les un par un pour vous
常用功能测试的检查点与用例设计思路
随机推荐
Content with element type "resultmap" must match "(constructor?, id*, result*, association*, collection*, discriminato?) “
NR modulation 4-AM
The 22 pictures show you in-depth analysis of prefix, infix, suffix expressions and expression evaluation
What is the difference between the tag attribute href of a reference URL and Src?
动作活体检测能力,构建安全可靠的支付级“刷脸”体验
(1) Analysis of Damon database model
数据中台、BI业务访谈(二):组织架构梳理的坑
The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience
元素类型为 “resultMap“ 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminato?)“
Click the model mode box and the other areas will not disappear except the mode box
shell中算术运算小结
input: kMAX dimensions in profile 0 are [2,3,128,128] but input has static dimensions [1,3,128,128]
How to remove Baidu advertisements
ECCV 2022 开源 | 给1万帧视频做目标分割
TikTok怎么开启社交电商?
PWM输出实验
armv8 DVFS
ENS阅读笔记
剑指offer专项突击版第5天
【To .NET】.NET Core Web API开发流程知识点整理[进阶]