|
stm32规则组转换 (DMA 多通道多次采集)程序:
stm32单片机源程序:
- //通过串口实时打印KEY3 KEY4电压值。
- //每个通道采集50次
- #include "pbdata.h"
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void USART_Configuration(void);
- void ADC1_Configuration(void);
- void DMA_Configuration(void);
- vu16 AD_Value[N][M]; //用来存放ADC转换结果,也是DMA的目标地址 define a N line and M row array
- vu16 filterAD[M]; //滤波后AD值
- int fputc(int ch,FILE *f)
- {
- USART_SendData(USART1,(u8)ch);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- return ch;
- }
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- USART_Configuration();
- NVIC_Configuration();
- ADC1_Configuration();
- DMA_Configuration();
-
- while(1)
- {
- filter();
- printf("key3=%f\r\n",filterAD[0]*3.3/4095);
- printf("key4=%f\r\n",filterAD[1]*3.3/4095);
- printf("pot =%f\r\n",filterAD[2]*3.3/4095);
- printf("\r\n");
- delay_ms(1000);
- }
- }
- void filter(void)
- {
- vu32 sum=0; //必须初始化为0,否则初值将保持上次调用值。
- vu8 i=0,j=0;
- for(i=0;i<M;i++) //M row
- {
- for(j=0;j<N;j++) //N line
- {
- sum+=AD_Value[j][i];
- }
- filterAD[i]=sum/N;
- sum=0;
- }
- }
- void RCC_Configuration(void)
- {
- SystemInit();//72m
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //USART
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //AFIO
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //GPIOC
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //12M 最大14M
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3;//ADin: POT K3 K4
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;//ADin: sensor1 sensor2 VOC
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;//中断向量
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- }
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate=9600;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- USART_InitStructure.USART_StopBits=USART_StopBits_1;
- USART_InitStructure.USART_Parity=USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1,ENABLE);
- USART_ClearFlag(USART1,USART_FLAG_TC);
- }
- void ADC1_Configuration(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- ADC_DeInit(ADC1); //将外设 ADC1 的全部寄存器重设为缺省值
-
- ADC_InitStructure.ADC_Mode=ADC_Mode_Independent; //ADC1 ADC2工作在独立模式
- ADC_InitStructure.ADC_ScanConvMode=ENABLE; //工作在单通道模式(还是扫描模式:多通道模式)
- ADC_InitStructure.ADC_ContinuousConvMode=ENABLE; //模数转换工作在单次模式(还是连续模式)
- ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//转换由软件而不是外部触发启动
- ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right; //ADC数据右对齐
- ADC_InitStructure.ADC_NbrOfChannel=M; //顺序进行转换的通道数
- ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
-
- //设置指定ADC的规则组通道,设置它们的转化顺序和采样时间
- //ADC1,ADC通道x,规则采样顺序值为y,采样时间为239.5周期
-
- ADC_RegularChannelConfig(ADC1,ADC_Channel_12,1,ADC_SampleTime_239Cycles5);//选择ADC1通道12-PC2 KEY3
- ADC_RegularChannelConfig(ADC1,ADC_Channel_13,2,ADC_SampleTime_239Cycles5);//选择ADC1通道13-PC3 KEY4
- ADC_RegularChannelConfig(ADC1,ADC_Channel_10,3,ADC_SampleTime_239Cycles5);//选择ADC1通道10-PC0 POT
- //
- ADC_DMACmd(ADC1, ENABLE); // 开启ADC的DMA支持(要实现DMA功能,还需独立配置DMA通道等参数)
- ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
-
- ADC_ResetCalibration(ADC1); //重置校准器
- while(ADC_GetResetCalibrationStatus(ADC1)); //获取校准状态
- ADC_StartCalibration(ADC1); //开始指定ADC的校准状态
- while(ADC_GetCalibrationStatus(ADC1)); //获取指定ADC的校准程序
-
- // ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); //中断使能
- ADC_SoftwareStartConvCmd(ADC1,ENABLE); //使能软件启动功能
- }
- void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- DMA_DeInit(DMA1_Channel1); //将DMA的通道1寄存器重设为缺省值
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA外设ADC基地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; //DMA内存基地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地 外设作为数据传输的来源
- DMA_InitStructure.DMA_BufferSize = M*N; //DMA通道的DMA缓存的大小
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据宽度为16位
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16位
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式,当传输完一次后,重新接着传送,永不停息。
- DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
- DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
完整代码下载:
4 规则组转换 (DMA 多通道多次采集).7z
(189.93 KB, 下载次数: 78)
|
|