Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
ADC
NXP 微控制器
FRDM-MCXA153移植ADC
发布于 2024-06-15 15:25:56 浏览:199
订阅该版
1、编写drv_adc.c ```c /* * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-05-16 shelton first version * 2024-6-15 liujianhua 添加MCXA153 adc0 */ #include "drv_adc.h" #include "fsl_lpadc.h" #include "fsl_spc.h" #include "fsl_common.h" #if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1) #define DEFAULT_HW_AVG (kLPADC_HardwareAverageCount4) #define DEFAULT_SAMPLE_TIME (kLPADC_SampleTimeADCK7) /* by default: cmd = chl+1 */ static uint8_t adc_chl2cmd[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; static uint8_t adc_cmd2trig[] = {0, 1, 2 ,3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}; struct mcx_adc { struct rt_adc_device mcx_adc_device; ADC_Type *adc_base; clock_attach_id_t clock_attach_id; clock_div_name_t clock_div_name; uint8_t clock_div; uint8_t referenceVoltageSource; /* 00, VREFH reference pin, 01, ANA_7(VREFI/VREFO) pin, 10, VDDA supply pin */ char *name; }; static struct mcx_adc mcx_adc_obj[] = { #ifdef BSP_USING_ADC0 { .adc_base = ADC0, .clock_attach_id = kFRO12M_to_ADC0, .clock_div_name = kCLOCK_DivADC0, .clock_div = 1, .referenceVoltageSource = 0, .name = "adc0", }, #endif }; static rt_err_t at32_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled) { struct mcx_adc *adc = (struct mcx_adc *)device->parent.user_data; lpadc_conv_command_config_t cmd_cfg; LPADC_GetDefaultConvCommandConfig(&cmd_cfg); cmd_cfg.channelNumber = channel; cmd_cfg.conversionResolutionMode = kLPADC_ConversionResolutionHigh; cmd_cfg.hardwareAverageMode = DEFAULT_HW_AVG; cmd_cfg.loopCount = 0; cmd_cfg.sampleTimeMode = DEFAULT_SAMPLE_TIME; /* kLPADC_SampleChannelSingleEndSideA = 0U, kLPADC_SampleChannelSingleEndSideB = 1U, kLPADC_SampleChannelDiffBothSide = 2U, kLPADC_SampleChannelDualSingleEndBothSide = */ cmd_cfg.sampleChannelMode = kLPADC_SampleChannelSingleEndSideA; LPADC_SetConvCommandConfig(adc->adc_base, adc_chl2cmd[channel], &cmd_cfg); lpadc_conv_trigger_config_t trig_config; LPADC_GetDefaultConvTriggerConfig(&trig_config); trig_config.targetCommandId = adc_chl2cmd[channel]; trig_config.enableHardwareTrigger = false; LPADC_SetConvTriggerConfig(adc->adc_base, adc_cmd2trig[trig_config.targetCommandId], &trig_config); /* Configurate the trigger0. */ return RT_EOK; } static rt_err_t at32_get_adc_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value) { struct mcx_adc *adc = (struct mcx_adc *)device->parent.user_data; lpadc_conv_result_t mLpadcResultConfigStruct; LPADC_DoSoftwareTrigger(adc->adc_base, 1<<(adc_cmd2trig[adc_chl2cmd[channel]])); /* 1U is trigger0 mask. */ while (!LPADC_GetConvResult(adc->adc_base, &mLpadcResultConfigStruct)) { } *value = mLpadcResultConfigStruct.convValue; return RT_EOK; } static const struct rt_adc_ops mcx_adc_ops = { .enabled = at32_adc_enabled, .convert = at32_get_adc_value, }; static int rt_hw_adc_init(void) { int result = RT_EOK; int i = 0; /* Enable VREF */ SPC0->ACTIVE_CFG1 |= 0xFFFFFFFF; SPC_SetActiveModeBandgapModeConfig(SPC0, kSPC_BandgapEnabledBufferEnabled); for (i = 0; i < sizeof(mcx_adc_obj) / sizeof(mcx_adc_obj[0]); i++) { CLOCK_SetClockDiv(mcx_adc_obj[i].clock_div_name, mcx_adc_obj[i].clock_div); CLOCK_AttachClk(mcx_adc_obj[i].clock_attach_id); lpadc_config_t adc_config; LPADC_GetDefaultConfig(&adc_config); adc_config.enableAnalogPreliminary = true; adc_config.referenceVoltageSource = mcx_adc_obj[i].referenceVoltageSource; adc_config.conversionAverageMode = kLPADC_ConversionAverage128; /* this is for calibartion avg mode */ adc_config.powerLevelMode = kLPADC_PowerLevelAlt4; adc_config.enableConvPause = false; adc_config.convPauseDelay = 0; LPADC_Init(mcx_adc_obj[i].adc_base, &adc_config); LPADC_DoOffsetCalibration(mcx_adc_obj[i].adc_base); LPADC_DoAutoCalibration(mcx_adc_obj[i].adc_base); rt_hw_adc_register(&mcx_adc_obj[i].mcx_adc_device, mcx_adc_obj[i].name, &mcx_adc_ops, &mcx_adc_obj[i]); } return result; } INIT_BOARD_EXPORT(rt_hw_adc_init); #endif /* BSP_USING_ADC */ ``` 2、编写drv_adc.h ```c /* * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-03-28 shelton first version */ #ifndef __ADC_CONFIG_H__ #define __ADC_CONFIG_H__ #include
#include
#ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif /* __ADC_CONFIG_H__ */ ``` 3、这次采集ADC0-0通道的值,向pin_mux.c添加P2_0的配置代码: ```c const port_pin_config_t port2_0_pin14_config = {/* Internal pull-up/down resistor is disabled */ kPORT_PullDisable, /* Low internal pull resistor value is selected. */ kPORT_LowPullResistor, /* Fast slew rate is configured */ kPORT_FastSlewRate, /* Passive input filter is disabled */ kPORT_PassiveFilterDisable, /* Open drain output is disabled */ kPORT_OpenDrainDisable, /* Low drive strength is configured */ kPORT_LowDriveStrength, /* Normal drive strength is configured */ kPORT_NormalDriveStrength, /* Pin is configured as ADC0_A0 */ kPORT_MuxAlt0, /* Digital input disabled; it is required for analog functions */ kPORT_InputBufferDisable, /* Digital input is not inverted */ kPORT_InputNormal, /* Pin Control Register fields [15:0] are not locked */ kPORT_UnlockRegister}; /* PORT2_0 (pin 14) is configured as ADC0_A0 */ PORT_SetPinConfig(PORT2, 0U, &port2_0_pin14_config); ``` 4、向rtconfig.h添加ADC的宏定义: ```c #define BSP_USING_ADC #define BSP_USING_ADC0 ``` 使用scons重新生成MDK工程。 编译后下载到开发板,进行串口终端,查看驱动: ``` msh >list device device type ref count -------- -------------------- ---------- i2c0 I2C Bus 0 pin Pin Device 0 adc0 ADC Device 0 uart0 Character Device 2 msh > ``` 发现在成功的加载了adc0的驱动 5、使用adc probe adc0 ``` msh >adc probe adc0 probe adc0 success ``` 发现成功加载ADC0 6、使能ADC0通道0 ``` msh >adc enable 0 adc0 channel 0 enables success ``` 7、读取他的ADC值,使用跳线把J4的8脚接地,读出值如下: ``` msh >adc read 0 adc0 channel 0 read value is 0x00000001 ``` 8、跳到3.3V,读出值如下: ``` msh >adc read 0 adc0 channel 0 read value is 0x0000FFFF ``` 到此成功的移值好ADC的驱动,经测试一切正常。
1
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
Lu_盼盼
这家伙很懒,什么也没写!
文章
35
回答
19
被采纳
0
关注TA
发私信
相关文章
1
试贴-消灭0主题
2
LPC M4的一些资料
3
LPC4088的临时分支
4
lpc1788 ad 不稳定
5
1788 LCD控制器缓冲区字节问题
6
一起来学习LPC4088吧
7
上传LPC4088的realtouch主工程
8
RealBoard 4088预定帖 [第一批板子不多,预定提前结束]
9
晒RealBoard LPC4088开箱照啦,速带小板凳前来围观
10
4088主程序需要的SD卡资源
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
freemodbus
PWM
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
10
个答案
1
次被采纳
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
YZRD
2
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部