#include "adc.h"
#include "usart.h"
#define ADC1_DR_Address ((u32)0x4001244C)
u32 ADC_DualConvertedValueTab[114];
int diff ;
int sum ;
int k ;
int j ;
int maxNum ;
float arryA[114] ;
float arryB[114] ;
float result[50]; //改4
void Adc_Init(void)
{
//结构体定义+时钟使能
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 |RCC_APB2Periph_GPIOA, ENABLE); //?6
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC最大时间不能超过14M ?1
// 端口配置PC.4,5模拟输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 ; // 改1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//DMA1初始化
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //外设地址 ?2
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_DualConvertedValueTab; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设作为数据传输源
DMA_InitStructure.DMA_BufferSize =114;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//外设数据宽度字,32位数据
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //内存数据宽度字,32位数据
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作于正常缓存---单次传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //此通道优先级高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //内存到内存失能
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); //使能DMA的发送结束中断
DMA_Cmd(DMA1_Channel1, ENABLE); /* Enable DMA1 channel1 */
//中断初始化
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//ADC1初始化
ADC_InitStructure.ADC_Mode =ADC_Mode_RegSimult ; //双AD同步规则模式 改3
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描单通道
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //软件启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对其
ADC_InitStructure.ADC_NbrOfChannel = 1; //规则转换通道数目
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_1Cycles5); /* ADC1 regular channel14 configuration */
ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 DMA */
// ADC2初始化
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //双AD同步规则模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描单通道
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //软件启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对其
ADC_InitStructure.ADC_NbrOfChannel = 1; //规则转换通道数目
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC2, ADC_Channel_7, 1, ADC_SampleTime_1Cycles5); /* ADC2 regular channels configuration */
ADC_ExternalTrigConvCmd(ADC2, ENABLE); /* Enable ADC2 external trigger conversion *///?3
//ADC校准
ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 */
//--------------------ADC1校准----------------------//
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
//----------------------------------------------------//
ADC_Cmd(ADC2, ENABLE); /* Enable ADC2 */
//--------------------ADC2校准----------------------//
/* Enable ADC2 reset calibaration register */
ADC_ResetCalibration(ADC2);
/* Check the end of ADC2 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC2));
/* Start ADC2 calibaration */
ADC_StartCalibration(ADC2);
/* Check the end of ADC2 calibration */
while(ADC_GetCalibrationStatus(ADC2));
//----------------------------------------------------//
}
void DMA1_Channel1_IRQHandler(void)
{
// u32 temp;
u16 temp0,temp1; //DR的低位和高位
float temp00,temp11;
u8 i;
if(DMA_GetITStatus(DMA1_IT_TC1)==1)
{
// printf("\n您发送的消息为:\n");
//temp1=(float)ADCConvertedValue*(3.3/4096);
for(i=0;i<114;i++) //改5
{ // temp=ADC_DualConvertedValueTab[i];
// printf("data= %d \r\n",temp);//打印总的高点平时间
// printf("\n您发送的消息为:\n");
temp0=(u16)ADC_DualConvertedValueTab[i]&0x0000ffff; //?4
temp00=(float)temp0*(3.3/4096);
arryA[i]=temp00;
temp1=ADC_DualConvertedValueTab[i]>>16; //?5
arryB[i]=temp11;
temp11=(float)temp1*(3.3/4096);
printf("%f V\r\n",temp00);//打印总的高点平时间
printf("%f V\r\n",temp11);//打印总的高点平时间
}
}
DMA_ClearITPendingBit(DMA1_IT_TC1); //清除全部中断标志
for(k=0,diff=0; k<50; k++)
{
for(j=0; j<64; j++)
{
sum=0;
sum += arryA[j] * arryB[j+diff];
}
result[k] = sum;
printf("%d%",result[k]);
printf("\t");
diff++;
}
printf("\n");
maxNum=result[0];
diff=0;
for(j=0; j<114; j++)
{
if(maxNum < result[j])
{
maxNum = result[j];
diff = j;
}
}
printf("The maximum number is: ");
printf("%d%",maxNum);
printf("\n");
printf("the difference: ");
printf("%d%",diff);
printf("\n");
}
以上是adc
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "wdg.h"
#include "timer.h"
#include "pwm.h"
#include "adc.h"
extern vu16 ADCConvertedValue[114];
int main(void)
{
// u16 adcx;
// float temp;
SystemInit();
delay_init(72); //延时初始化
NVIC_Configuration();
uart_init(9600);
Adc_Init();
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能
while(1)
{
}
}
以上是主函数
|