#include "dual_adc_test.h"
#include "at32f4xx.h"
#include "stdio.h"
#include "at32_board.h"
/************************************************
ALIENTEK 战舰AT32F403开发板实验
ADC 实验(ADC1/2/3同时使用)
说明1:ADC1和ADC2采用同步规则模式,
说明2:ADC配置为扫描模式、非连续转换、软触发、右对齐、使用DMA模式、通道采样时间1.5个周期
说明3:ADC1规则组定义两个通道(通道0、通道2),ADC2定义两个通道(通道1、通道3)
说明4:ADC1通过DMA1的通道1传输数据,ADC2利用双ADC由ADC1的DMA1的通道1传输数据,
说明5:所有ADC转换数据均在DMA1传输完成中断内进行打印
************************************************/
#define ADC1_DR_Address ((uint32_t)0x4001244C)
#define ADC3_DR_Address ((uint32_t)0x40013C4C)
#define dual_adc_ADC_size 10
#define M 128
#define N 8
uint16_t value[N][M];
uint32_t ADC_ConvertedValue1;
uint32_t ADC_ConvertedValue2;
ADC_InitType ADC_InitStructure;
DMA_InitType DMA_InitStructure;
TMR_TimerBaseInitType TMR_TimerBaseInitStructure;
TMR_OCInitType TMR_OCInitStructure;
__IO uint32_t dual_adc_ADC_DualConvertedValueTab[dual_adc_ADC_size];
void dual_adc_RCC_Configuration(void)
{
/* ADCCLK = PCLK2/6 =120/6=20M */
RCC_ADCCLKConfig(RCC_APB2CLK_Div6);
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_DMA1, ENABLE);
/* Enable ADC1, ADC2 and GPIOC clock */
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_ADC1 | RCC_APB2PERIPH_ADC2 | RCC_APB2PERIPH_GPIOA, ENABLE);
}
void dual_adc_GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* Configure PA0, PA1 PA2, PA3
as analog input ----------------------------------------------------------*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0|GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_ANALOG;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void dual_adc_NVIC_Configuration(void)
{
NVIC_InitType NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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);
}
void dual_adc_DMA_Configuration(void)
{
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_Reset(DMA1_Channel1);
DMA_DefaultInitParaConfig(&DMA_InitStructure);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)dual_adc_ADC_DualConvertedValueTab;
DMA_InitStructure.DMA_Direction = DMA_DIR_PERIPHERALSRC;
DMA_InitStructure.DMA_BufferSize = 2;//pa0 pa2 两个通道
DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
DMA_InitStructure.DMA_PeripheralDataWidth = DMA_PERIPHERALDATAWIDTH_WORD;//一定要word,因为RM手册有说ADC2
DMA_InitStructure.DMA_MemoryDataWidth = DMA_MEMORYDATAWIDTH_WORD;
DMA_InitStructure.DMA_Mode = DMA_MODE_CIRCULAR;
DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_INTConfig(DMA1_Channel1, DMA_INT_TC, ENABLE);//实际应用不加DMA中断,这里只做打印测试
/* Enable DMA1 Channel1 */
DMA_ChannelEnable(DMA1_Channel1, ENABLE);
}
void dual_adc_Config_Configuration(void)
{
//ADC_Reset(ADC1);
//ADC_Reset(ADC2);
/* ADC1 configuration ------------------------------------------------------*/
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanMode = ENABLE;
ADC_InitStructure.ADC_ContinuousMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrig = ADC_ExternalTrig_TMR1_CC3;//TIM1-CC3触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NumOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1_5); //PA0
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_1_5); //PA2
ADC_ExternalTrigConvCtrl(ADC1, ENABLE);
/* ADC2 configuration ------------------------------------------------------*/
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanMode = ENABLE;
ADC_InitStructure.ADC_ContinuousMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrig = ADC_ExternalTrig_TMR1_CC3;//TIM1-CC3触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NumOfChannel = 2;
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC2 regular channels configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_1_5);//PA1
ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 2, ADC_SampleTime_1_5);//PA3
ADC_ExternalTrigConvCtrl(ADC2, ENABLE);
ADC_DMACtrl(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Ctrl(ADC1, ENABLE);
/* Enable ADC2 */
ADC_Ctrl(ADC2, ENABLE);//根据移植手册,需要把这句话放校准前面,不然程序会卡在CAL异常,实测必须放前面
//ADC_Ctrl(ADC1, ENABLE);
ADC_RstCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//ADC_Ctrl(ADC2, ENABLE);
ADC_RstCalibration(ADC2);
while(ADC_GetResetCalibrationStatus(ADC2));
ADC_StartCalibration(ADC2);
while(ADC_GetCalibrationStatus(ADC2));
}
//定时器1-cc3的设置触发
void dual_adc_tim_init(void)
{
/* Time Base configuration */
TMR_TimeBaseStructInit(&TMR_TimerBaseInitStructure);
TMR_TimerBaseInitStructure.TMR_Period = (1500000/15000)-1; // 15KHz
TMR_TimerBaseInitStructure.TMR_DIV = (SystemCoreClock/1500000)-1; // timer1_clk=1.5MHz
TMR_TimerBaseInitStructure.TMR_CounterMode = TMR_CounterDIR_Up;
TMR_TimerBaseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
TMR_TimerBaseInitStructure.TMR_RepetitionCounter = 0;
TMR_TimeBaseInit(TMR1,&TMR_TimerBaseInitStructure);
TMR_OCStructInit(&TMR_OCInitStructure);
TMR_OCInitStructure.TMR_Pulse = 10;
TMR_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
TMR_OCInitStructure.TMR_OCIdleState = TMR_OCIdleState_Reset;
TMR_OCInitStructure.TMR_OCNIdleState = TMR_OCNIdleState_Reset;
TMR_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;
TMR_OCInitStructure.TMR_OCNPolarity = TMR_OCNPolarity_High;
TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
TMR_OCInitStructure.TMR_OutputNState = TMR_OutputNState_Disable;
TMR_OC3Init(TMR1,&TMR_OCInitStructure);
TMR_CtrlPWMOutputs(TMR1,ENABLE);
TMR_Cmd(TMR1,ENABLE);
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_13_5);
ADC_SoftwareStartConvCtrl(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
}
void dual_adc_read_adc(void)
{
//uint16_t adc1_val[2];
//
//uint16_t adc2_val[2];
//ADC_SoftwareStartConvCtrl(ADC1, ENABLE);
//ADC_SoftwareStartConvCtrl(ADC2, ENABLE);
//
//adc1_val[0] =dual_adc_ADC_DualConvertedValueTab[0]&0xffff;//A1
//adc1_val[1] =(dual_adc_ADC_DualConvertedValueTab[0]>>16)&0xffff;
//
//
//adc2_val[0] =(int)(dual_adc_ADC_DualConvertedValueTab[0]>>16);
//adc2_val[1] =(int)(dual_adc_ADC_DualConvertedValueTab[1]>>16);
//
//printf("adc1-0:%d\n\r",adc1_val[0] );
//printf("adc1-1:%d\n\r",adc1_val[1]);
int i;
printf("\r\n 采样开始\r\n");
for(i=0;i<M;i++)
{
/* Start ADC1 Software Conversion 启动ADC1软件转换 */
// ADC_SoftwareStartConvCtrl(ADC1, ENABLE); //开始转换
//
// //ADC_GetDualModeConversionValue
//
// ADC_ConvertedValue1=ADC_GetConversionValue(ADC1);
//
//
// value[0][0] = (ADC_ConvertedValue1&0xffff); //获取ADC的值
// value[1][1] = ((ADC_ConvertedValue1>> 16)&0xffff); //获取ADC的值
ADC_ConvertedValue1 = (u32)Get_Adc(ADC_Channel_0);
ADC_ConvertedValue2 = (u32)Get_Adc(ADC_Channel_2);
printf("\r\n 编号%d \t 编号%d \t AD值: %d\r\n", 0,i, ADC_ConvertedValue1);
printf("\r\n 编号%d \t 编号%d \t AD值: %d\r\n", 1,i, ADC_ConvertedValue2);
}
printf("\r\n 采样结束\r\n");
}
void DMA1_Channel1_IRQHandler(void)
{
if ( DMA_GetITStatus(DMA1_INT_TC1) == SET)
{
printf("DMA1_Enter_IRQ\r\n");
printf("ADC1_Channel0_Value=%x\r\n", dual_adc_ADC_DualConvertedValueTab[0] & 0xFFFF);
printf("ADC2_Channel2_Value=%x\r\n", dual_adc_ADC_DualConvertedValueTab[0] >> 16);
printf("ADC1_Channel1_Value=%x\r\n", dual_adc_ADC_DualConvertedValueTab[1] & 0xFFFF);
printf("ADC2_Channel3_Value=%x\r\n", dual_adc_ADC_DualConvertedValueTab[1] >> 16);
DMA_ClearITPendingBit(DMA1_INT_TC1);
}
}
void ADC1_2_IRQHandler(void)
{
if(ADC_GetINTStatus(ADC1, ADC_INT_EC) == SET)
{
printf("ADC1 ADC_GetConversionValue: %d\r\n", ADC_GetConversionValue(ADC1));
ADC_ClearINTPendingBit(ADC1, ADC_INT_JEC);
}
if(ADC_GetINTStatus(ADC2, ADC_INT_EC) == SET)
{
printf("ADC2 ADC_GetConversionValue: %d\r\n", ADC_GetConversionValue(ADC2));
ADC_ClearINTPendingBit(ADC1, ADC_INT_EC);//这里为什么demo是ADC1
}
}
void dual_adc_main_init(void)//main函数初始化
{
dual_adc_RCC_Configuration();
dual_adc_GPIO_Configuration();
dual_adc_NVIC_Configuration();
dual_adc_DMA_Configuration();
dual_adc_Config_Configuration();
dual_adc_tim_init();
printf("Test_Start\r\n");
Delay_ms(1000);
}