VD6283TX环境光传感器驱动开发(4)----移植闪烁频率代码

VD6283TX环境光传感器驱动开发----4.移植闪烁频率代码

  • 闪烁定义
  • 视频教学
  • 样品申请
  • 源码下载
  • 开发板设置
  • 开发板选择
  • IIC配置
  • 串口配置
  • 开启X-CUBE-ALS软件包
  • 时钟树配置
  • ADC使用定时器触发采样
  • KEIL配置
  • FFT代码配置
  • app_x-cube-als.c
  • 需要添加函数

闪烁定义

光学闪烁被定义为人造光源的脉动或波动的光现象。
在低频闪烁中,光是可见的(人眼能够察觉光的闪烁)。超过100 Hz的光学闪烁对于人眼来说不再可见,但仍然存在,可能对人体产生影响。
大多数类型的人造光源在连接到电力主网(家庭或商业办公室)时会发出闪烁,这主要取决于国家的电力频率,通常是50 Hz或60 Hz。
由于电流在光源中的交替流动,所有人造光源都会分别在50 Hz和60 Hz电力主网下发出100 Hz或120 Hz的闪烁频率。
为了消除可见的闪烁并减少对人体的影响,大多数发光二极管(LED)使用脉冲宽度调制(PWM)调光方法,以实现更高的闪烁频率。
VD6283传感器可以检测光的闪烁频率,最高可达2 kHz。
最近在弄ST的课程,需要样片的可以加群申请:615061293 。

在这里插入图片描述

视频教学

https://www.bilibili.com/video/BV1xu4y1t75n/

VD6283TX环境光传感器驱动开发(2)----获取光强和色温

样品申请

https://www.wjx.top/vm/OhcKxJk.aspx#

源码下载

开发板设置

在手册种给出了,闪烁手册可以查看AN5639,资料链接如下。
https://www.st.com/content/ccc/resource/technical/document/application_note/group1/9f/7e/8c/ce/36/85/4c/08/DM00776948/files/DM00776948.pdf/jcr:content/translations/en.DM00776948.pdf

在这里插入图片描述

在AN5639手册中,需要对SB3进行连接。

在这里插入图片描述
同时GPIO2需要接到MCU的ADC通道中。

在这里插入图片描述

查看X-NUCLEO-6283A1手册,可以看到VD6283TX的GPIO2连接到MCU的ADC端口0-2。

在这里插入图片描述

在VD6283TX-SATEL中,可以看到VD6283TX通过SB3连接到了AFLR_1V8。
需要将AFLR_1V8接到开发板的A0端口中。

在这里插入图片描述

开发板选择

这里使用NUCLEO-F401RE 开发板。
在这里插入图片描述

IIC配置

在这里插入图片描述

串口配置

在这里插入图片描述

开启X-CUBE-ALS软件包

在这里插入图片描述

时钟树配置

在这里插入图片描述

ADC使用定时器触发采样

在app_als_adc_utils.c中,定义了ADC使用的频率,为8000Hz。
在这里插入图片描述

定时器的arr设置为10500-1,那么定时器频率为8000Hz。
Trigger Event Selection :update event 定时器自动更新。
在这里插入图片描述
配置ADC检测VD6283TX的GPIO2管脚的AD值。
设置触发方式为外部触发,选择刚刚配置的TIM2,触发方式为上升沿触发。

在这里插入图片描述

开启中断。
在这里插入图片描述

KEIL配置

在这里插入图片描述

FFT代码配置

arm_cortexM4lf_math.lib 库包含了一系列数学函数,特别是适用于基于Cortex-M4和Cortex-M7处理器的浮点运算单元的优化数学例程。这些例程涵盖了常见的数学运算,如信号处理、滤波、变换等。
arm_math.h 这个头文件包含了CMSIS-DSP库的函数声明、宏定义和结构体定义等,可以通过包含这个头文件,使用库中提供的各种数学函数,包括信号处理、滤波、变换等。
添加arm_cortexM4lf_math.lib文件。
在这里插入图片描述
在这里插入图片描述

同时导入arm_math.h文件。
在这里插入图片描述

app_x-cube-als.c

由于需要进行FFT算法,所以需要添加对应数学头文件。

#define ARM_MATH_CM4
#include "arm_math.h"
#include "app_als_adc_utils.h"

添加对应的函数申明。


#define FLK_CHANNEL	(5U)/** Increasing the value of the FLK_DATA_SIZE symbol will increase* processing time, flicker accuracy and memory footprint*/
#define FLK_DATA_SIZE (1024U)
#define FFT_SIZE (FLK_DATA_SIZE)/* Private variables ---------------------------------------------------------*/
static uint8_t is_quit_requested;
static uint8_t is_autogain_requested;
static int16_t flk_data[FLK_DATA_SIZE];
volatile uint8_t ALS_EventDetected;/** The FFT of a real N-point sequence has even symmetry in the frequency domain.* The second half of the data equals the conjugate of the first half flipped in frequency.* Looking at the data, we see that we can uniquely represent the FFT using only N/2 complex numbers.* These are packed into the output array in alternating real and imaginary components:* X = { real[0], imag[0], real[1], imag[1], real[2], imag[2] ... real[(N/2)-1], imag[(N/2)-1 }*/
static arm_rfft_fast_instance_f32 instance_fft;
static float32_t fft_in[FLK_DATA_SIZE];
static float32_t fft_out_tmp[FFT_SIZE];
static float32_t fft_out[FFT_SIZE/2];/** The FFT of a real N-point sequence has even symmetry in the frequency domain.* The second half of the data equals the conjugate of the first half flipped in frequency.* Looking at the data, we see that we can uniquely represent the FFT using only N/2 complex numbers.* These are packed into the output array in alternating real and imaginary components:* X = { real[0], imag[0], real[1], imag[1], real[2], imag[2] ... real[(N/2)-1], imag[(N/2)-1 }*/
static arm_rfft_fast_instance_f32 instance_fft;static void MX_VD6283A1_AnalogFlicker_Process(void); static float32_t complex_abs(float32_t real, float32_t complex);
static void init_fft(arm_rfft_fast_instance_f32 *instance, uint32_t size);
static void perform_fft(arm_rfft_fast_instance_f32 *instance, int16_t *data, float32_t *ffti, float32_t *ffto, uint32_t size);
static void find_flk_freq(uint32_t fs, float32_t *ffto, uint32_t *freq, uint8_t skip_dc, uint32_t size);static int32_t flicker_autogain(uint8_t Instance, uint32_t *pAppliedGain, uint32_t timeoutMs);static void display_gain(uint32_t gain);

在MX_VD6283A1_LuxCCT_Init()函数中添加init_fft快速傅里叶变换初始化。
在这里插入图片描述

static void MX_VD6283A1_LuxCCT_Init(void)
{/* Initialize Virtual COM Port */BSP_COM_Init(COM1);printf("VD6283TX Lux / CCT Example\n\n");display_commands_banner();/* initialize ARM FFT library */init_fft(&instance_fft, FFT_SIZE);status = VD6283A1_LIGHT_SENSOR_Init(LIGHT_SENSOR_INSTANCE_0);if (status){printf("VD6283A1_LIGHT_SENSOR_Init failed\n");while(1);}
}

初始化完毕之后,添加频率获取函数。

static void MX_VD6283A1_AnalogFlicker_Process(void)
{uint32_t fs; /* sampling frequency */uint32_t pos = 0; uint32_t flk_freq = 0;uint32_t index;uint32_t current_gain;uint32_t current_exposure;/* initialize exposure time */VD6283A1_LIGHT_SENSOR_SetExposureTime(LIGHT_SENSOR_INSTANCE_0, 100000);VD6283A1_LIGHT_SENSOR_GetExposureTime(LIGHT_SENSOR_INSTANCE_0, &current_exposure);printf("Exposure set to %lu us\n", (unsigned long)current_exposure);/* initialize gain */flicker_autogain(LIGHT_SENSOR_INSTANCE_0, &current_gain, 1);printf("Channel %u gain set to", FLK_CHANNEL);display_gain(current_gain);status = als_adc_start(&fs);if (status){printf("ADC Start failed\n");while (1);}VD6283A1_LIGHT_SENSOR_StartFlicker(LIGHT_SENSOR_INSTANCE_0, FLK_CHANNEL, LIGHT_SENSOR_FLICKER_ANALOG);while (!is_quit_requested){status = als_adc_get_frame(&flk_data[pos], &index);/* fill the ADC frame buffer */if (status == 0){pos += ADC_FRAME_SIZE;}/* if the ADC frame buffer is full, then process it */if (pos == FLK_DATA_SIZE){perform_fft(&instance_fft, flk_data, fft_in, fft_out, FFT_SIZE);find_flk_freq(fs, fft_out, &flk_freq, 1, FFT_SIZE);pos = 0; /* reset position index */printf("Flicker freq: %4lu Hz\r", (unsigned long)flk_freq);fflush(stdout);if (is_autogain_requested == 1){VD6283A1_LIGHT_SENSOR_StopFlicker(LIGHT_SENSOR_INSTANCE_0);flicker_autogain(LIGHT_SENSOR_INSTANCE_0, &current_gain, 1);printf("Channel %u gain set to", FLK_CHANNEL);display_gain(current_gain);VD6283A1_LIGHT_SENSOR_StartFlicker(LIGHT_SENSOR_INSTANCE_0, FLK_CHANNEL, LIGHT_SENSOR_FLICKER_ANALOG);is_autogain_requested = 0;}}handle_cmd(get_key());}als_adc_stop();VD6283A1_LIGHT_SENSOR_StopFlicker(LIGHT_SENSOR_INSTANCE_0);VD6283A1_LIGHT_SENSOR_DeInit(LIGHT_SENSOR_INSTANCE_0);printf("Quitting the demo...\n");while (1);
}

在MX_X_CUBE_ALS_Process函数中开启频率获取函数,关闭光强获取函数MX_VD6283A1_LuxCCT_Process。
在这里插入图片描述

添加增益设置函数。

/** @brief find and apply appropriate gain value depending on saturation value* @warning this function mustn't be called when a capture is ongoing*/
static int32_t flicker_autogain(uint8_t Instance, uint32_t *pAppliedGain, uint32_t timeoutMs)
{int32_t res;uint8_t i, j;uint8_t idx = 7; /* start with mid-table value */const uint8_t sat_limit = 2;uint32_t saturation;/* duplicate 0x42AB to avoid 100x and keep multiples of 2 for array size */const uint16_t Gains[] = {0x42AB, 0x42AB, 0x3200, 0x2154, 0x1900, 0x10AB, 0x0A00, 0x0723,0x0500, 0x0354, 0x0280, 0x01AB, 0x0140, 0x0100, 0x00D4, 0x00B5};/* clip timeout value */timeoutMs = timeoutMs == 0 ? 1 : timeoutMs;timeoutMs = timeoutMs >= 100 ? 100 : timeoutMs;for (i = 0; i <= 3; i++){VD6283A1_LIGHT_SENSOR_SetGain(Instance, FLK_CHANNEL, Gains[idx]);VD6283A1_LIGHT_SENSOR_GetGain(Instance, FLK_CHANNEL, pAppliedGain);res = VD6283A1_LIGHT_SENSOR_StartFlicker(Instance, FLK_CHANNEL, LIGHT_SENSOR_FLICKER_ANALOG);if (res)return res;/* read saturation value each ms so we can exit early if saturation detected */for (j = 0; j < timeoutMs; j++){HAL_Delay(1);res = VD6283A1_LIGHT_SENSOR_GetSaturation(Instance, &saturation);if (res)return res;if (saturation > sat_limit)break;}res = VD6283A1_LIGHT_SENSOR_StopFlicker(Instance);if (res)return res;/* update index to next value */if (i)idx += saturation > sat_limit ? 1 << (i - 1) : -(1 << (i - 1));else if (saturation > sat_limit)idx++;}/* clip index if it reaches max value */if (idx > 15) idx = 15;VD6283A1_LIGHT_SENSOR_SetGain(Instance, FLK_CHANNEL, Gains[idx]);res = VD6283A1_LIGHT_SENSOR_GetGain(Instance, FLK_CHANNEL, pAppliedGain);return res;
}

在下方添加函数的定义。

/** @brief initilize arm rfft library*/
static void init_fft(arm_rfft_fast_instance_f32 *instance, uint32_t size)
{arm_rfft_fast_init_f32(instance, size);
}

打印增益函数。

/** @brief normalize, convert and dislay gain */
static void display_gain(uint32_t gain)
{uint32_t g = (gain * 100) / 256;printf(" %3lu.%02lu\n", (unsigned long)g / 100, (unsigned long)(g % 100));
}

执行FFT。

/** @brief perform fft on the input buffer using arm rfft library*/
static void perform_fft(arm_rfft_fast_instance_f32 *instance, int16_t *flk, float32_t *ffti, float32_t *ffto, uint32_t size)
{uint32_t i;uint32_t index = 0;/* copy the ADC sampled signal into the fft input buffer* this allows to convert the data from int16_t to float32_t */for (i = 0; i < size; i++){ffti[i] = flk[i];}/* Perform the FFT on the input buffer:* results are packed in a way so that even indexes contain real values* and odd indexes contain the complex value of each bin.* Therefore the fft_output array contains FFT_SIZE / 2 bins */arm_rfft_fast_f32(instance, ffti, fft_out_tmp, 0);/* Calculate the magnitude for each bin from the temp fft output buffer */for (i = 0; i < size; i += 2){ffto[index] = complex_abs(fft_out_tmp[i], fft_out_tmp[i+1]);if (ffto[index] < 0) ffto[index] = 0;index++;}
}

查找峰值频率值。

/** @brief find peak frequency value*/
static void find_flk_freq(uint32_t fs, float32_t *ffto, uint32_t *freq, uint8_t skip_dc, uint32_t size)
{uint32_t i;uint32_t res;uint32_t index_max = 0;uint32_t limit = size / 2;float32_t max_value = -1;/* do not take account of the DC value if the flag skip_dc is set */skip_dc ? (i = 1) : (i = 0);/* run through the output array to detect the peak */for (; i < limit; i++){if (ffto[i] > max_value){index_max = i;max_value = ffto[i];}}/* convert index of the bin into frequency */res = (index_max * fs) / size;/* return the result if the pointer is valid */if (freq){*freq = res;}
}

计算一个复数的绝对值。

/** @brief compute absolute value of a complex number*/
static float32_t complex_abs(float32_t real, float32_t complex)
{float32_t res;arm_sqrt_f32(real * real + complex * complex, &res);return res;
}

需要添加函数

arm_cortexM4lf_math.lib 库包含了一系列数学函数,特别是适用于基于Cortex-M4和Cortex-M7处理器的浮点运算单元的优化数学例程。这些例程涵盖了常见的数学运算,如信号处理、滤波、变换等。
arm_math.h 这个头文件包含了CMSIS-DSP库的函数声明、宏定义和结构体定义等,可以通过包含这个头文件,使用库中提供的各种数学函数,包括信号处理、滤波、变换等。
app_als_adc_utils.c功能主要包括启动和停止ADC采样,获取采样数据,ADC采样速度设置,以及处理相关的硬件中断。
app_als_adc_utils.h是app_als_adc_utils.c对应头文件。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/148861.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

基于j2ee的交通管理信息系统/交通管理系统

摘 要 随着当今社会的发展&#xff0c;时代的进步&#xff0c;各行各业也在发生着变化&#xff0c;比如交通管理这一方面&#xff0c;利用网络已经逐步进入人们的生活。传统的交通管理&#xff0c;都是工作人员线下手工统计&#xff0c;这种传统方式局限性比较大且花费较多。计…

雷达编程实战之提高探测速度

有效帧频率作为雷达一个非常核心的指标&#xff0c;它代表了雷达探测识别的速度&#xff0c;速度越快&#xff0c;后级各项智能驾驶功能就能得到更快、更有效的判断。本篇文章首先从硬件的角度&#xff0c;提供了一种合理利用片上资源提高探测识别速度的常用方法&#xff0c;然…

Python无废话-办公自动化Excel图表制作

openpyxl 支持用Excel工作表中单元格的数据&#xff0c;创建条形图、折线图、散点图和饼图等。 图表制作步骤 在openpyxl模块中创建图表&#xff0c;步骤如下: ①选择一个单元格区域&#xff0c;创建Reference 对象&#xff0c;作为图形数据a)(Value)。 ②创建一个Chart对象…

web漏洞-PHP反序列化

目录 PHP反序列化序列化反序列化原理涉及技术利用危害CTF靶场 PHP反序列化 序列化 将对象转换成字符串 反序列化 相反&#xff0c;将字符串转换成对象。 数据格式的转换对象的序列化有利于对象的保存和传输&#xff0c;也可以让多个文件共享对象。 原理 未对用户输入的序列化字…

数据结构 1.2 算法

算法的基本概念 算法的定义 算法是对特定问题求解步骤的一种描述&#xff0c;它是指定的有限序列&#xff0c;其中的每条指令表示一个或多个操作。 例、 算法的特性 &#xff08;5个&#xff09; 1.有穷性 一个算法总在执行有穷步之后结束&#xff0c;且每一步都可以在有穷…

力扣-383.赎金信

Idea 使用一个hashmap 或者一个int数组存储第二次字符串中每一个字符及其出现的次数 遍历第一个字符串&#xff0c;讲出现的重复字符减1&#xff0c;若该字符次数已经为0&#xff0c;则返回false AC Code class Solution { public:bool canConstruct(string ransomNote, strin…

使用关键字abstract 声明抽象类-PHP8知识详解

抽象类只能作为父类使用&#xff0c;因为抽象类不能被实例化。抽象类使用关键字abstract 声明&#xff0c;具体的使用语法格式如下&#xff1a; abstract class 抽象类名称{ //抽象类的成员变量列表 abstract function 成员方法1(参数); //抽象类的成员方法 abstract functi…

十天学完基础数据结构-第五天(栈(Stack)和队列(Queue))

栈的定义和特点 栈是一种线性数据结构&#xff0c;它遵循后进先出&#xff08;LIFO&#xff09;原则。栈具有以下基本概念和特点&#xff1a; 栈顶&#xff1a;栈的顶部元素&#xff0c;是唯一可访问的元素。 入栈&#xff1a;将元素添加到栈顶。 出栈&#xff1a;从栈顶移除…

导出视频里的字幕

导出视频里的字幕 如何利用剪映快速提取并导出视频里的字幕 https://jingyan.baidu.com/article/c35dbcb0881b6fc817fcbcd2.html 如何快速提取视频中的字幕&#xff1f;给大家介绍一种简单高效又免费的提取方法。需要利用到“剪映”&#xff0c;以下是具体的操作步骤和指引&a…

嵌入式中如何用C语言操作sqlite3(07)

sqlite3编程接口非常多&#xff0c;对于初学者来说&#xff0c;我们暂时只需要掌握常用的几个函数&#xff0c;其他函数自然就知道如何使用了。 数据库 本篇假设数据库为my.db,有数据表student。 nonamescore4嵌入式开发爱好者89.0 创建表格语句如下&#xff1a; CREATE T…

更直观地学习 Git 命令

theme: condensed-night-purple 前言 本文参考于 Learn Git Branching 这个有趣的 Git 学习网站。 在该网站&#xff0c;可以使用 show command 命令展示所有可用命令。 你也可以直接访问网站的sandbox&#xff0c;自由发挥。 本地篇 基础篇 git commit git commit将暂…

intel 一些偏门汇编指令总结

intel 汇编手册下载链接&#xff1a;https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html LDS指令&#xff1a; 手册中可以找到 位于 3-588 根据手册内容猜测&#xff1a;lds r16 m16:16 的作用&#xff0c;是把位于 [m16:16] 内存地址的数…

模块化编程+LCD1602调试工具——“51单片机”

各位CSDN的uu们你们好呀&#xff0c;小雅兰又来啦&#xff0c;刚刚学完静态数码管显示和动态数码管显示&#xff0c;感觉真不错呢&#xff0c;下面&#xff0c;小雅兰就要开始学习模块化编程以及LCD1602调试工具的知识了&#xff0c;让我们进入51单片机的世界吧&#xff01;&am…

【数据结构】布隆过滤器

布隆过滤器的提出 在注册账号设置昵称的时候&#xff0c;为了保证每个用户昵称的唯一性&#xff0c;系统必须检测你输入的昵称是否被使用过&#xff0c;这本质就是一个key的模型&#xff0c;我们只需要判断这个昵称被用过&#xff0c;还是没被用过。 方法一&#xff1a;用红黑…

2024级199管理类联考之数学基础(下篇)

平面几何(平均2题) 三角形(性质、特殊三角形、全等与相似) 性质 由不在同一直线的三条线段首尾依次连接所组成的图形三条边、三个内角、三个定点三角形内角和为180度,外角和为360度,多边形的外角和为360度,n多边形的内角和为(n-2)*180度一个外角等于不相邻的两个内角之和任意…

WSL安装异常:WslRegisterDistribution failed with error: 0xc03a001a

简介&#xff1a;如果文件夹右上角是否都有两个相对的蓝色箭头&#xff0c;在进行安装wsl时&#xff0c;设置就会抛出 Installing WslRegisterDistribution failed with error: 0xc03a001a的异常 历史攻略&#xff1a; 卸载WSL WSL&#xff1a;运行Linux文件 WSL&#xff1…

全志ARM926 Melis2.0系统的开发指引②

全志ARM926 Melis2.0系统的开发指引② 编写目的4. 编译工具链使用4.1.工具链通用配置4.2.模块的工具链配置4.3.简单的 makefile 5. 固件烧录工具的安装5.1.PhoenixSuit 的安装步骤5.2.检验 USB 驱动安装5.3.使用烧录软件 PhoenixSuit -全志相关工具和资源-.1 全志固件镜像修改工…

【Vue组件化编程】

Vue组件化编程 1 对组件的理解2 非单文件组件2.1 基本使用2.2 几个注意点2.3 组件的嵌套2.4 VueComponent构造函数2.5 一个重要的内置关系 3 单文件组件 1 对组件的理解 组件&#xff1a;实现应用中局部功能代码和资源的集合。优点&#xff1a;文件好维护&#xff1b;依赖关系不…

Scala第十六章节

Scala第十六章节 scala总目录 文档资料下载 章节目标 掌握泛型方法, 类, 特质的用法了解泛型上下界相关内容了解协变, 逆变, 非变的用法掌握列表去重排序案例 1. 泛型 泛型的意思是泛指某种具体的数据类型, 在Scala中, 泛型用[数据类型]表示. 在实际开发中, 泛型一般是结合…

vue重修004上部

文章目录 版权声明组件的三大组成部分scoped解决样式冲突scoped原理2.代码演示 组件data函数说明演示 组件通信组件关系分类通信解决方案父子通信流程子向父通信代 props详解props校验props&data、单向数据流 小黑记事本&#xff08;组件版&#xff09;基础组件结构需求和实…