STM32cube库配置双ADC的同步规则采样
http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=605203&extra=page%3D&page=1
ADC1与ADC2工作在规则同步扫描模式,TIM4CC4触发,DMA读取ADC结果;设计目标是每周波(20毫秒)采样32点或64点或128点等数据,TIM4的初始化程序自动计算采样间隔,例如如果每周期采样32个点的数据,那么TIM4的触发间隔是20*1000/32=625微秒,也就是每625微秒触发一次ADC,由于使能了ADC的扫描模式,所以一次触发转换6个ADC通道产生6个32位的数据(在F103上采用的是间断模式,而F407的间断模式没有使用起来,只好用扫描模式),每个通道转换结束后由DMA读取转换结果保存到内存缓冲区等待计算,TIM4一次触发ADC后DMA读取6个32位数据,DMA计数到32*6=192个数据后产生中断,DMA的实际中断间隔是20毫秒,在DMA中断内重新设置DMA参数同时设置转换结束标志供计算使用。
void TIMx_Configuration(void)
{
Uint32 Temp;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
if(((RCC->CFGR >> 10) & 0x04)==0)
{
Temp=1;
}
else
{
Temp=2;
}
TIM_TimeBaseStructure.TIM_Period = TIM4_ClkFre/AdcPrNum/50;
TIM_TimeBaseStructure.TIM_Prescaler = APB1CLK*Temp/TIM4_ClkFre-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=TIM_TimeBaseStructure.TIM_Period/10;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_Cmd(TIM4, ENABLE);
}
void ADC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = AdcChNum;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div6;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_DiscModeChannelCountConfig(ADC1,AdcChNum);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC1->SMPR2 |= ADC_SMPR_13_5 << 0;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 3;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 6;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 9;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 12;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 15;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 18;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 21;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 24;
ADC1->SMPR2 |= ADC_SMPR_13_5 << 27;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 0;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 3;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 6;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 9;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 12;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 15;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 18;
ADC1->SMPR1 |= ADC_SMPR_13_5 << 21;
ADC1->SQR3 |= UL1AinCH << 0;
ADC1->SQR3 |= UczAinCH << 5;
ADC1->SQR3 |= UaAinCH << 10;
ADC1->SQR3 |= UbAinCH << 15;
ADC1->SQR3 |= UcAinCH << 20;
ADC1->SQR3 |= U0AinCH << 25;
ADC1->SQR2 |= 7 << 0;
ADC1->SQR2 |= 8 << 5;
ADC1->SQR2 |= 9 << 10;
ADC1->SQR2 |= 10 << 15;
ADC1->SQR2 |= 11 << 20;
ADC1->SQR2 |= 12 << 25;
ADC1->SQR1 |= 13 << 0;
ADC1->SQR1 |= 14 << 5;
ADC1->SQR1 |= 15 << 10;
ADC1->SQR1 |= 16 << 15;
ADC1->SQR1 |= (AdcChNum-1) << 20;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = AdcChNum;
ADC_Init(ADC2,&ADC_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div6;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_DiscModeChannelCountConfig(ADC2,AdcChNum);
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
ADC_DMACmd(ADC2, ENABLE);
ADC_Cmd(ADC2, ENABLE);
ADC2->SMPR2 |= ADC_SMPR_13_5 << 0;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 3;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 6;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 9;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 12;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 15;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 18;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 21;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 24;
ADC2->SMPR2 |= ADC_SMPR_13_5 << 27;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 0;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 3;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 6;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 9;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 12;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 15;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 18;
ADC2->SMPR1 |= ADC_SMPR_13_5 << 21;
ADC2->SQR3 |= UL2AinCH << 0;
ADC2->SQR3 |= UdcAinCH << 5;
ADC2->SQR3 |= IaAinCH << 10;
ADC2->SQR3 |= IbAinCH << 15;
ADC2->SQR3 |= IcAinCH << 20;
ADC2->SQR3 |= I0AinCH << 25;
ADC2->SQR2 |= 7 << 0;
ADC2->SQR2 |= 8 << 5;
ADC2->SQR2 |= 9 << 10;
ADC2->SQR2 |= 10 << 15;
ADC2->SQR2 |= 11 << 20;
ADC2->SQR2 |= 12 << 25;
ADC2->SQR1 |= 13 << 0;
ADC2->SQR1 |= 14 << 5;
ADC2->SQR1 |= 15 << 10;
ADC2->SQR1 |= 16 << 15;
ADC2->SQR1 |= (AdcChNum-1) << 20;
}
void DMA2_Stream0_IRQHandler(void)
{
#if(DMA2_STREAM0)
if(DMA2->LISR & (1 << 5))
{
DMA2->LIFCR |= (1<<5);
DMA_Cmd(DMA2_Stream0, DISABLE);
DMA2_Stream0->NDTR = AdcPrNum*AdcChNum;
DMA2_Stream0->M0AR = (u32)&ADC1_Buf;
DMA_Cmd(DMA2_Stream0, ENABLE);
}
#endif
}
AdcPrNum 为每周期采样点数;AdcChNum 为通道个数,例如需要采样UA、UB、UC、IA、IB、IC6路模拟信号,则AdcChNum =6/2=3;
void DMA_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
DMA_DeInit(DMA2_Stream0);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_InitStructure.DMA_Channel=DMA2_STREAM0_CH0_ADC1;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC->CDR;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ADC1_Buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = AdcPrNum*AdcChNum;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_1QuarterFull;
DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC , ENABLE);
DMA_Cmd(DMA2_Stream0, ENABLE);
}
STM32cube库配置双ADC的同步规则采样的更多相关文章
- STM32 双ADC同步规则采样
最近需要用到两个ADC对电压电流进行同步采样,看了一下STM32的ADC介绍,发现STM32最多有3个独立ADC,有在双AD模式下可以进行同步测量,正好满足我的要求.参考官方给的例子在结合自己的需 ...
- STM32f103的数电采集电路的双ADC的设计与使用
STM32F103C8T6拥有3个ADC,其独立使用已经在本文的3.1.3里面有详细的介绍,这里主要是介绍双ADC的同时使用,即STM32的同步规则模式使用.在此模式在规则通道组上执行时,外部触发来自 ...
- STM32 ADC基础与多通道采样
12位ADC是一种逐次逼近型模拟数字数字转换器.它有多达18个通道,可测量16个外部和2个内部信号源.ADC的输入时钟不得超过14MHZ,它是由PCLK2经分频产生.如果被ADC转换的模拟电压低于低阀 ...
- Mysql主从同步在线实施步骤【适合大数据库从库配置】
Mysql主从同步在线实施步骤[适合大数据库从库配置] MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一,但比较适合在新实例中实施,对于较大的数据库则存在停机等不可 ...
- mysql-mmm 安装配置(双主)
原文地址:mysql-mmm 安装配置 作者:chinaunix1116 MMM即Master-Master Replication Managerfor MySQL(mysql主主复制管理器)关于m ...
- ubuntu搭建svn服务器并htpp访问版本库并svn与web同步
Ubuntu搭建SVN服务器多版本库 1 介绍 Subversion是一个自由,开源的版本控制系统,这个版本库就像一个普通的文件服务器,不同的是,它可以记录每一次文件和目录的修改情况.这样就可 ...
- spring项目配置双数据源读写分离
我们最早做新项目的时候一直想做数据库的读写分离与主从同步,由于一些原因一直没有去做这个事情,这次我们需要配置双数据源的起因是因为我们做了一个新项目用了另一个数据库,需要把这个数据库的数据显示到原来的后 ...
- LINUX环境下SVN安装与配置(利用钩子同步开发环境与测试环境)
安装采用YUM一键安装: 1.环境Centos 6.6 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve ...
- mysql 从库执行insert失败导致同步停止
服务配置:一主一从,版本都是 5.5 .主库配置了 binlog-do-db binlog-ignore-db 问题复述:运营人员发现,昨天的数据统计不对.数据分析服务查询的是从库的数据. 到tomc ...
随机推荐
- API设计原则
译序 Qt的设计水准在业界很有口碑,一致.易于掌握和强大的API是Qt最著名的优点之一.此文既是Qt官网上的API设计指导准则,也是Qt在API设计上的实践总结.虽然Qt用的是C++,但其中设计原则和 ...
- 凡人视角C++之string(上)
好久没有更新博客了,这段时间一直在忙图像处理的项目,近期空了下来.也是时候整合C++的相关内容,静心感受下编程语言的魅力.和大家共同探讨学习.我将以头文件的形式展开学习,且仅仅讲述相关接口的应用,至于 ...
- Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...
- webstorm 设置js或者html文件自动缩进为4个空格不生效
设置 tab 和自动缩进为4个空格不生效,解决办法如下 设置代码缩进 1. 依次打开files —- settings —- Editor —- Code Style —- JavaScript 2. ...
- mysqlslap 压力测试工具
[背景] 由于一些不可描述的原因,我要确定一条业务SQL在给定的MySQL环境下的执行效率:比如说200个session同时跑同样一条SQL 我们数据库的吞吐量怎么样? 刚收到这个需求的时候,感觉这个 ...
- PHP断点调试工具Xdebug的安装
Xdebug是一个PHP程序调试器,可以用来跟踪,调试和分析PHP程序的运行状况.其实调试PHP还有其他工具比如Zend Debugger,但zend官方不是免费发布的,后来更新为zend Serve ...
- iOS7.0中UILabel高度调整注意事项
转自:http://blog.csdn.net/k12104/article/details/33731833 http://herkuang.info/blog/2013/12/31/ios7%E4 ...
- Java方法内部需要重新请求的一种机制
有这样一个需求,当调用某个方法抛出异常,比如通过 HttpClient 调用远程接口时由于网络原因报 TimeOut 异常:或者所请求的接口返回类似于“处理中”这样的信息,需要重复去查结果时,我们希望 ...
- RequestDispatcher.forward和HttpServletResponse.sendRedirect
(1)RequestDispatcher.forward方法只能将请求转发给同一个WEB应用中的组件:而HttpServletResponse.sendRedirect 方法不仅可以重定向到当前应用程 ...
- .NET+MVC+Alipay的Sdk版单笔转账到支付宝账户接口
public class AliPayController : Controller { // GET: AliPay public ActionResult Index() { return Red ...