网站:bbs.21ic.com
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_ADC16_CHANNEL 1U
#define DEMO_ADC16_CHANNEL_GROUP 0U
#define DEMO_ADC16_BASEADDR ADC0
#define DEMO_DMAMUX_BASEADDR DMAMUX0
#define DEMO_DMA_CHANNEL 1U
#define DEMO_DMA_ADC0_SOURCE 40U
#define DEMO_DMA_ADC1_SOURCE 41U
#define DEMO_DMA_BASEADDR DMA0
#define ADC16_RESULT_REG_ADDR 0x4003b010U
#define ADC16_RESULT_REG_ADDR1 0x40027010U//查询寄存器手册得到
#define DEMO_DMA_IRQ_ID DMA0_IRQn
#define DEMO_ADC16_SAMPLE_COUNT 8U /* The ADC16 sample count. */
/***********************************************************************************************************************
* ADC0 initialization code
**********************************************************************************************************************/
adc16_channel_config_t ADC0_channelsConfig[1] = {
{
.channelNumber = 1U, //传输通道
.enableDifferentialConversion = true, //差分模式
.enableInterruptOnConversionCompleted = false, //使能传输完成中断
}
};
const adc16_config_t ADC0_config = {
.referenceVoltageSource = kADC16_ReferenceVoltageSourceVref,
.clockSource = 0,
.enableAsynchronousClock = false,
.clockDivider = kADC16_ClockDivider1,
.resolution = kADC16_ResolutionSE16Bit,
.longSampleMode = kADC16_LongSampleDisabled,
.enableHighSpeed = true,
.enableLowPower = false,
.enableContinuousConversion = false//连续的转换
};
const adc16_channel_mux_mode_t ADC0_muxMode = kADC16_ChannelMuxA;
/* 硬件平均 8 */
const adc16_hardware_average_mode_t ADC0_hardwareAverageMode = kADC16_HardwareAverageDisabled;
void ADC0_init(void) {
/* Initialize ADC16 converter */
ADC16_Init(ADC0_PERIPHERAL, &ADC0_config);
/* Make sure, that software trigger is used */
ADC16_EnableHardwareTrigger(ADC0_PERIPHERAL, false);
/* Configure hardware average mode */
ADC16_SetHardwareAverage(ADC0_PERIPHERAL, ADC0_hardwareAverageMode);
/* Configure channel multiplexing mode */
ADC16_SetChannelMuxMode(ADC0_PERIPHERAL, ADC0_muxMode);
/* Initialize channel */
ADC16_SetChannelConfig(ADC0_PERIPHERAL, 0U, &ADC0_channelsConfig[0]);
/* Perform auto calibration */
ADC16_DoAutoCalibration(ADC0_PERIPHERAL);
/* Enable DMA. */
ADC16_EnableDMA(ADC0_PERIPHERAL, false);
}
/***********************************************************************************************************************
* ADC1 initialization code
**********************************************************************************************************************/
adc16_channel_config_t ADC1_channelsConfig[1] = {
{
.channelNumber = 2U,
.enableDifferentialConversion = true, //差分模式
.enableInterruptOnConversionCompleted = false,
}
};
const adc16_config_t ADC1_config = {
.referenceVoltageSource = kADC16_ReferenceVoltageSourceVref,
.clockSource = 0,
.enableAsynchronousClock = false,
.clockDivider = kADC16_ClockDivider1,
.resolution = kADC16_ResolutionSE16Bit,
.longSampleMode = kADC16_LongSampleDisabled,
.enableHighSpeed = true,
.enableLowPower = false,
.enableContinuousConversion = false//连续的转换
};
const adc16_channel_mux_mode_t ADC1_muxMode = kADC16_ChannelMuxA;
const adc16_hardware_average_mode_t ADC1_hardwareAverageMode = kADC16_HardwareAverageDisabled;
void ADC1_init(void) {
// EnableIRQ(ADC0_IRQn);
/* 初始化ADC16转换器 */
ADC16_Init(ADC1_PERIPHERAL, &ADC1_config);
/* 不使用软件触发器 */
ADC16_EnableHardwareTrigger(ADC1_PERIPHERAL, false);
/* 配置硬件平均模式 */
ADC16_SetHardwareAverage(ADC1_PERIPHERAL, ADC1_hardwareAverageMode);
/* 配置信道多路复用模式 */
ADC16_SetChannelMuxMode(ADC1_PERIPHERAL, ADC1_muxMode);
/* 初始化通道 */
ADC16_SetChannelConfig(ADC1_PERIPHERAL, 1U, &ADC1_channelsConfig[0]);
/* 自动校准 */
ADC16_DoAutoCalibration(ADC1_PERIPHERAL);
/* Enable DMA. */
ADC16_EnableDMA(ADC1_PERIPHERAL, false);
}
adc16_channel_config_t adc16ChannelConfigStruct;
adc16ChannelConfigStruct.channelNumber = 1; //ADC通道
adc16ChannelConfigStruct.channelNumber = 2;
adc16ChannelConfigStruct.enableDifferentialConversion = true;//使能差分
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = false;//失能中断
ADC16_SetChannelConfig(ADC1, 0U, &adc16ChannelConfigStruct);
ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct);
while (0U == (kADC16_ChannelConversionDoneFlag &
ADC16_GetChannelStatusFlags(ADC1, 0U)));
ADC_Value0 = ADC16_GetChannelConversionValue(ADC0, 0U);
ADC_Value1 = ADC16_GetChannelConversionValue(ADC1, 0U);
void EDMA_Configuration(void)
{
edma_config_t userConfig;
/* 配置 DMAMUX */
DMAMUX_Init(DMAMUX);
/* 通道CH1初始化 */
DMAMUX_SetSource(DMAMUX, 1, 40); /* Map ADC0 source to channel 1 */
DMAMUX_EnableChannel(DMAMUX, 1);
/* 通道CH2初始化 */
DMAMUX_SetSource(DMAMUX, 2, 41);/* Map ADC1 source to channel 2 */
DMAMUX_EnableChannel(DMAMUX, 2);
/* 获取eDMA默认配置结构 */
EDMA_GetDefaultConfig(&userConfig);
EDMA_Init(DMA0, &userConfig);
EDMA_CreateHandle(&g_EDMA_Handle, DMA0, 1);
/* 设置回调 */
EDMA_SetCallback(&g_EDMA_Handle, Edma_Callback, NULL);
/*eDMA传输结构配置 .设置dma通道1的adc值传到g_adc16SampleDataArray*/
EDMA_PrepareTransfer(&transferConfig, (void *)ADC16_RESULT_REG_ADDR, sizeof(uint32_t),
(void *)g_adc16SampleDataArray, sizeof(uint32_t), sizeof(uint32_t),
sizeof(g_adc16SampleDataArray), kEDMA_PeripheralToMemory);
EDMA_SubmitTransfer(&g_EDMA_Handle, &transferConfig);
/* Enable interrupt when transfer is done. */
EDMA_EnableChannelInterrupts(DEMO_DMA_BASEADDR, DEMO_DMA_CHANNEL, kEDMA_MajorInterruptEnable);
#if defined(FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT) && FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT
/* Enable async DMA request. */
EDMA_EnableAsyncRequest(DEMO_DMA_BASEADDR, DEMO_DMA_CHANNEL, true);
#endif /* FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT */
/* Enable transfer. */
EDMA_StartTransfer(&g_EDMA_Handle);
//将dma通道1链接到通道0
EDMA_SetChannelLink(DMA0, 1, kEDMA_MinorLink, 2);
EDMA_SetChannelLink(DMA0, 1, kEDMA_MajorLink,2);
//*********************************************************************************************/
EDMA_CreateHandle(&DMA_CH2_Handle, DMA0, 2);
EDMA_SetCallback(&DMA_CH2_Handle, Edma_Callback1, NULL);
/* 设置回调 */
EDMA_PrepareTransfer(&g_transferConfig, (void *)ADC16_RESULT_REG_ADDR1, sizeof(uint32_t),
(void *)g_adc16SampleDataArray1, sizeof(uint32_t), sizeof(uint32_t),
sizeof(g_adc16SampleDataArray1), kEDMA_PeripheralToMemory);
EDMA_SubmitTransfer(&DMA_CH2_Handle, &g_transferConfig);
//传输完后修正通道
DMA0->TCD[1].DLAST_SGA = -1* sizeof(g_adc16SampleDataArray);
DMA0->TCD[2].DLAST_SGA = -1* sizeof(g_adc16SampleDataArray1);
/* 当传输完成时启用中断. */
EDMA_EnableChannelInterrupts(DEMO_DMA_BASEADDR, 2, kEDMA_MajorInterruptEnable);
#if defined(FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT) && FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT
// /* 启用异步DMA请求 */
EDMA_EnableAsyncRequest(DEMO_DMA_BASEADDR, 2, true);
#endif /* FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT */
/* 使能数据传输 */
EDMA_StartTransfer(&DMA_CH2_Handle);
}
static void Edma_Callback(edma_handle_t *handle, void *userData, bool transferDone, uint32_t tcds)
{
EDMA_StartTransfer(&g_EDMA_Handle);
g_Transfer_Done = false;
if (transferDone)
{
g_Transfer_Done = true;
}
}
static void Edma_Callback1(edma_handle_t *handle, void *userData, bool transferDone, uint32_t tcds)
{
g_Transfer_Done1 = false;
if (transferDone)
{
g_Transfer_Done1 = true;
}
}
芯片的入门环境搭建
联系客服