在线时间465 小时
UID3547866
注册时间2022-4-24
NXP金币3246
TA的每日心情 | 开心 昨天 14:34 |
---|
签到天数: 1100 天 连续签到: 18 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 5778
- 最后登录
- 2025-7-26
|
本帖最后由 suncat0504 于 2024-3-30 14:28 编辑
我使用的是双轴按键摇杆传感器,
感觉上就是两个可调电阻加上一个按键。因此使用MCX-N947开发板上的ADC功能就可以了。按照这个想法,根据板子的设计,准备使用P4_23和P4_15这两个GPIO作为ADC端口用,这两个口在可以从J1的70号和62号引出来。
初始化ADC功能,要使用对应的时钟总线,开放复用功能,和ADC通道等。ADC内置多个命令缓冲器,为不同的通道扫描和独立通道选择提供灵活性 触发源。这些命令缓冲器可配置用于: 单端转换。向SWTRIG写入1,可以以软件方式启动ADC转换。如果一次想要完成2个通道的转换,需要进行类似下面的设置:
mLpadcCommandConfigStruct_x.enableAutoChannelIncrement = true;
mLpadcCommandConfigStruct_x.loopCount = 1;
第一条指令是允许自动调整通道号加1,第二个指令是表示连续采集几个通道。假如读取的第一个通道是 channel0,那么loopCount=1,就会读取channel0-1,一共2个通道的数据。
ADC转换每次只会返回一个数据,如果连续采集多个通道的话,因为每次都读回来的数据,都会带着 commandIdSource 和 loopCountIndex,也就是前面说到的几号指令,和循环读取的次数。
这样我们就能知道是sideA或B,第几个通道的数据了
原理上是这样,但是程序上面没有调通。在发出开始转换指令后,无法取得第二个数据。所以我就系采用本办法,每次转换一个通道,转换完,在转换下一个通道。这样终于可以同时获得摇杆两个方向上的ADC结果了。
主程序代码如下:
- /*
- * Copyright 2019 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "fsl_debug_console.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "board.h"
- #include "fsl_lpadc.h"
- #include "fsl_common.h"
- #include "stdio.h"
- #include "oled.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define BOARD_LED_GPIO BOARD_LED_RED_GPIO
- #define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN
- #define DEMO_LPADC_BASE ADC0
- #define DEMO_LPADC_USER_CHANNEL_X 2U
- #define DEMO_LPADC_USER_CHANNEL_Y 1U
- #define DEMO_LPADC_USER_CMDID 1U
- /* Use VREF_OUT driven from the VREF block as the reference volatage */
- #define DEMO_LPADC_VREF_SOURCE kLPADC_ReferenceVoltageAlt3
- #define DEMO_LPADC_DO_OFFSET_CALIBRATION true
- #define DEMO_LPADC_OFFSET_VALUE_A 0x10U
- #define DEMO_LPADC_OFFSET_VALUE_B 0x10U
- #define DEMO_VREF_BASE VREF0
- uint32_t exec_adc_y(void);
- uint32_t exec_adc_x(void);
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- void BOARD_InitDebugConsole(void);
- const uint32_t g_LpadcFullRange = 4096U;
- const uint32_t g_LpadcResultShift = 3U;
- lpadc_conv_result_t mLpadcResultConfigStruct;
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- volatile uint32_t g_systickCounter;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- void SysTick_Handler(void) {
- if (g_systickCounter != 0U) {
- g_systickCounter--;
- }
- }
- void SysTick_DelayTicks(uint32_t n) {
- g_systickCounter = n;
- while (g_systickCounter != 0U) {
-
- }
- }
- /*!
- * @brief Main function
- */
- int main(void) {
- uint8_t ch;
- uint32_t adcValue = 0U;
- char temp_buf[128]={'\0'};
-
-
- /* Set systick reload value to generate 1ms interrupt */
- if (SysTick_Config(SystemCoreClock / 1000U)) {
- while (1) {
-
- }
- }
- /* Board pin init */
- CLOCK_EnableClock(kCLOCK_Gpio3);
- CLOCK_EnableClock(kCLOCK_Gpio0);
-
- // 配置端口
- BOARD_InitPins();
-
- lpadc_config_t mLpadcConfigStruct;
- lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct;
- lpadc_conv_command_config_t mLpadcCommandConfigStruct_x;
- lpadc_conv_command_config_t mLpadcCommandConfigStruct_y;
- /* attach FRO 12M to FLEXCOMM4 (debug console) */
- CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- /* attach FRO HF to ADC0 */
- CLOCK_SetClkDiv(kCLOCK_DivAdc0Clk, 1u);
- CLOCK_AttachClk(kFRO_HF_to_ADC0);
- /* enable VREF */
- SPC0->ACTIVE_CFG1 |= 0x1;
-
- BOARD_PowerMode_OD();
- BOARD_InitBootClocks();
- BOARD_InitDebugConsole();
- // 初始化接口,代码不可省略
- LED_RED_INIT(LOGIC_LED_OFF);
- LED_BLUE_INIT(LOGIC_LED_OFF);
- LED_GREEN_INIT(LOGIC_LED_OFF);
- Lcd_Init();
- QDTFT_Test_Demo();
-
- Lcd_Clear(BLACK);
-
- sprintf(temp_buf, "ADC Range: %d\r\n", g_LpadcFullRange);
- Gui_DrawFont_GBK16(0, 0, WHITE, BLACK, (uint8_t *)temp_buf);
-
- SysTick_DelayTicks(5000U);
-
- while (1) {
- //SysTick_DelayTicks(500U);
- exec_adc_y();
- exec_adc_x();
- }
- }
- uint32_t exec_adc_y(void) {
- uint32_t adcValue=0;
- char temp_buf[128]={'\0'};
- lpadc_config_t mLpadcConfigStruct;
- lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct;
- lpadc_conv_command_config_t mLpadcCommandConfigStruct_y;
-
- LPADC_GetDefaultConfig(&mLpadcConfigStruct);
- mLpadcConfigStruct.enableAnalogPreliminary = true;
- mLpadcConfigStruct.referenceVoltageSource = DEMO_LPADC_VREF_SOURCE;
- mLpadcConfigStruct.conversionAverageMode = kLPADC_ConversionAverage128;
- LPADC_Init(DEMO_LPADC_BASE, &mLpadcConfigStruct);
-
- /* Request offset calibration. */
- LPADC_DoOffsetCalibration(DEMO_LPADC_BASE);
- /* Request gain calibration. */
- LPADC_DoAutoCalibration(DEMO_LPADC_BASE);
- /* Set conversion CMD configuration. */
- LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct_y);
- mLpadcCommandConfigStruct_y.channelNumber = DEMO_LPADC_USER_CHANNEL_Y;
- LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID, &mLpadcCommandConfigStruct_y);
-
- /* Set trigger configuration. */
- LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct);
- mLpadcTriggerConfigStruct.targetCommandId = DEMO_LPADC_USER_CMDID;
- mLpadcTriggerConfigStruct.enableHardwareTrigger = false;
- LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 0U, &mLpadcTriggerConfigStruct); /* Configurate the trigger0. */
-
- // 触发转换
- LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U); /* 1U is trigger0 mask. A通道 */
- // 等待转换完成
- while (!LPADC_GetConvResult(DEMO_LPADC_BASE, &mLpadcResultConfigStruct, 0U));
- adcValue = ((mLpadcResultConfigStruct.convValue) >> g_LpadcResultShift);
- PRINTF("Y value: %d\r\n", adcValue);
- sprintf(temp_buf, "Y=%u ", adcValue);
- Gui_DrawFont_GBK16(0,16, WHITE, BLACK, (uint8_t *)temp_buf);
- return adcValue;
- }
- uint32_t exec_adc_x(void) {
- uint32_t adcValue=0;
- char temp_buf[128]={'\0'};
- lpadc_config_t mLpadcConfigStruct;
- lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct;
- lpadc_conv_command_config_t mLpadcCommandConfigStruct_x;
-
- LPADC_GetDefaultConfig(&mLpadcConfigStruct);
- mLpadcConfigStruct.enableAnalogPreliminary = true;
- mLpadcConfigStruct.referenceVoltageSource = DEMO_LPADC_VREF_SOURCE;
- mLpadcConfigStruct.conversionAverageMode = kLPADC_ConversionAverage128;
- LPADC_Init(DEMO_LPADC_BASE, &mLpadcConfigStruct);
-
- /* Request offset calibration. */
- LPADC_DoOffsetCalibration(DEMO_LPADC_BASE);
- /* Request gain calibration. */
- LPADC_DoAutoCalibration(DEMO_LPADC_BASE);
- /* Set conversion CMD configuration. */
- LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct_x);
- mLpadcCommandConfigStruct_x.channelNumber = DEMO_LPADC_USER_CHANNEL_X;
- LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID, &mLpadcCommandConfigStruct_x);
-
- /* Set trigger configuration. */
- LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct);
- mLpadcTriggerConfigStruct.targetCommandId = DEMO_LPADC_USER_CMDID;
- mLpadcTriggerConfigStruct.enableHardwareTrigger = false;
- LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 0U, &mLpadcTriggerConfigStruct); /* Configurate the trigger0. */
-
- // 触发转换
- LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U); /* 1U is trigger0 mask. A通道 */
- // 等待转换完成
- while (!LPADC_GetConvResult(DEMO_LPADC_BASE, &mLpadcResultConfigStruct, 0U));
- adcValue = ((mLpadcResultConfigStruct.convValue) >> g_LpadcResultShift);
- PRINTF("X value: %d\r\n", adcValue);
- sprintf(temp_buf, "X=%u ", adcValue);
- Gui_DrawFont_GBK16(0,32, WHITE, BLACK, (uint8_t *)temp_buf);
-
- return adcValue;
- }
复制代码
测试效果图:
|
|