1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
应用背景:
一个双通道DO输出芯片,能反馈两个通道的电流,但是只有一个反馈引脚,不同的通道需要用单片机控制切换 。把反馈引脚接到单片机的ADC,然后在程序中进行切换。代码如下: void Adc2_init(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //先初始化切换管脚 Adc2_SwitchInit(); /*-----------------------启动ADC 的DMA传输 -----------------*/ /*Enable ADC2, DMA2, GPIO clocks*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); /*DMA2 Stream2 channel configuration*/ DMA_DeInit(DMA2_Stream2); DMA_InitStructure.DMA_Channel = DMA_Channel_1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&adc2Buf[0][0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = ADC2_CH_NUM*DO_SAMP_NUM*2; //需要切换通道,所以*2 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA_Priority_Medium; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream2, &DMA_InitStructure); DMA_Cmd(DMA2_Stream2, ENABLE); //是能半传输和传输完成中断,在半传输时切换通道 DMA_ITConfig(DMA2_Stream2,DMA_IT_TC|DMA_IT_HT,ENABLE); DMA_ClearITPendingBit(DMA2_Stream2,DMA_IT_TCIF2); DMA_ClearITPendingBit(DMA2_Stream2,DMA_IT_HTIF2); /* Configure GPIO ******************************/ GPIO_InitStructure.GPIO_Pin = fb_Current_Pin[0] | fb_Current_Pin[1] | fb_Current_Pin[2] | fb_Current_Pin[3]; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//20->5 ADC_CommonInit(&ADC_CommonInitStructure); /* ADC Init ****************************************************************/ //ADC_DeInit(); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE;//多通道肯定要enable ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = 0;//必须设置值,否则采样值会超过12位 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = ADC2_CH_NUM; ADC_Init(ADC2, &ADC_InitStructure); /* ADC1 regular configuration *************************************/ ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);//do12 ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 2, ADC_SampleTime_480Cycles);//do24 ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 3, ADC_SampleTime_480Cycles);//do56 ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 4, ADC_SampleTime_480Cycles);//do78 /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE); /* Enable ADC DMA */ ADC_DMACmd(ADC2, ENABLE); /* Enable ADC */ ADC_Cmd(ADC2, ENABLE); /*statrt ADC Sample*/ Adc2_Switch(0); ADC_SoftwareStartConv(ADC2); } void DMA2_Stream2_IRQHandler(void) { if(DMA_GetITStatus(DMA2_Stream2,DMA_IT_HTIF2)) { DMA_ClearITPendingBit(DMA2_Stream2,DMA_IT_HTIF2); //转换到一半切换下一组通道 Adc2_Switch(1); } if(DMA_GetITStatus(DMA2_Stream2,DMA_IT_TCIF2)) { DMA_ClearITPendingBit(DMA2_Stream2,DMA_IT_TCIF2); //转换完再切换回来 Adc2_Switch(0); } } 实际应用中一共用了4个通道的ADC,对4个DO芯片的8通道反馈电流进行采集。在DMA半传输中断和传输完成中断中切换DO芯片的不同通道进行采集。需要注意的是ADC的采样周期需要设置的长一点,这里设置的是ADC_SampleTime_480Cycles,采样周期短的话在DMA里切换通道的时候会有延迟。 |
|
|
|
只有小组成员才能发言,加入小组>>
3310 浏览 9 评论
2991 浏览 16 评论
3492 浏览 1 评论
9055 浏览 16 评论
4086 浏览 18 评论
1174浏览 3评论
603浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
596浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2333浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1894浏览 2评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 21:09 , Processed in 0.912156 second(s), Total 48, Slave 39 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号