//DAC init
void MCU_DacInit( void )
{DAC_InitTypeDef DAC_Struct;
GPIO_InitTypeDef GPIO_InitStructure;
/* DAC Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Configure DAC output pin */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GP_GetPin(DA_APCTV);
GPIO_Init(GP_GetPort(DA_APCTV), &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GP_GetPin(DA_FREQ);
GPIO_Init(GP_GetPort(DA_FREQ), &GPIO_InitStructure);
//DAC默认配置
DAC_DeInit();
//填充默认参数
DAC_StructInit(&DAC_Struct); /*将DAC_Struct的值设为默认*/
DAC_Struct.DAC_Trigger = DAC_Trigger_Software; /*由软件触发*/
DAC_Struct.DAC_WaveGeneration = DAC_WaveGeneration_None; /*关闭波形生成*/
DAC_Struct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; //12位采样
DAC_Struct.DAC_OutputBuffer = DAC_OutputBuffer_Enable; /*使能DAC通道缓存 */
DAC_Struct.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
/*初始化DAC通道1*/
DAC_Init(DAC_Channel_1,&DAC_Struct);
/*初始化DAC通道二*/
DAC_Struct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; //12位采样
DAC_Init(DAC_Channel_2,&DAC_Struct);
/*通道1使能*/
DAC_Cmd(DAC_Channel_1,ENABLE);
/*通道2使能*/
DAC_Cmd(DAC_Channel_2,ENABLE);
//设置初始转换值为零
MCU_DacCh1Start(0);
MCU_DacCh2Start(0);
}
void MCU_DacCh1Start(INT16U value)
{
value&= 0x0FFF;
DAC_SetChannel1Data(DAC_Align_12b_R,Freq); /*12位右对齐,输出一半的基准电压*/
DAC->SWTRIGR |= 0X01;
}
void MCU_DacCh2Start(INT16U value)
{
value&= 0x0FFF;
DAC_SetChannel2Data(DAC_Align_12b_R,Freq); /*12位右对齐,输出一半的基准电压*/
DAC->SWTRIGR |= 0X02;
}
//单个通道ADC Init
void MCU_AdcInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); //开启ADC3时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //开启DAM2的时钟。
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_Period = 10500;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
/*填入缺省值,其中后几项只对TIM1和TIM8有效*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1为正常占空比模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出允许
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出允许
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 0x7F;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
/*CCR自动装载默认也是打开的*/
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);
/*ARR自动装载默认是打开的,可以不设置*/
TIM_ARRPreloadConfig(TIM4,ENABLE);
/*清楚中断标志*/
//TIM_ClearFlag(TIM4,TIM_FLAG_Update);
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);
/*产生一个更新事件用于装载预装值*/
//TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
/* DMA2 Stream2 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2; //ADC1 in dma map table:
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)AdTable;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 512; //n byte to be trasfer from peripheral to memory.
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // the address of the peripheral cannot increment automatically
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 16bit size to transfer at the side of peripheral.
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 16bit size to transfer at the side of memory.
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // loop for transaction
DMA_InitStructure.DMA_Priority = DMA_Priority_High; // high level.
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; // single transfer.
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; // single transfer.
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TC);
// DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
// DMA_Cmd(DMA2_Stream0, ENABLE);
/******************************ADC通道的GPIO初始化***************************/
/*CTCS/CDCSS 信令检测*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //maximum frequence = 36MHz, typic fre= 30MHz。
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC3 Init****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4; //timer4触发转换;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1; //一共转换的通道
ADC_Init(ADC3, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC3, ADC_Channel_10, 1, ADC_SampleTime_112Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
/* Enable ADC2 DMA */
ADC_DMACmd(ADC3, ENABLE); //open the DMA mode in ADC modul
}
//多个ADC通道
void MCU_AdcInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //开启ADC1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //开启DAM2的时钟。
// while(DMA2_Stream0->CR & 0x00000001);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //ADC1 in dma map table: Stream 0 vs channel2
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DataBufAddr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADCDRV_SMPCH * ADCDRV_SMPMAX; //n byte to be trasfer from peripheral to memory.
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // the address of the peripheral cannot increment automatically
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 16bit size to transfer at the side of peripheral.
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 16bit size to transfer at the side of memory.
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // loop for transaction
DMA_InitStructure.DMA_Priority = DMA_Priority_High; // high level.
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; // single transfer.
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; // single transfer.
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
//DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TC);
// DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
// DMA_Cmd(DMA2_Stream0, ENABLE);
/******************************ADC通道的GPIO初始化***************************/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //maximum frequence = 36MHz, typic fre= 30MHz。
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位采样
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; //timer1 出发转换;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 6; //一共转换的通道上述
ADC_Init(ADC1, &ADC_InitStructure);
//ADC1->SQR1 &= ~(0xF << 20); // clear bit [23, 20],
// ADC1->SQR1 |= 8 << 20; // set 8 in bit [23, 20], indicating there are the total amount of eight channel to convert.
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 6, ADC_SampleTime_480Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //open the DMA mode in ADC modul
/* Enable ADC3 */
}
//ADC软件触发
void DRV_ADC_Start(void)
{
DMA_Cmd(DMA2_Stream0, ENABLE); //open the ADC1 channel of the DMA.
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1); //软件启动转换开始
}
//ADC软件停止
void DRV_ADC_Stop(void)
{
DMA_Cmd(DMA2_Stream0, DISABLE); //open the ADC1 channel of the DMA.
ADC_DMACmd(ADC1, DISABLE); //open the DMA mode in ADC modul
ADC_Cmd(ADC1, DISABLE);
}
//ADC定时器触发
void DRV_ADC_Start(void)
{
DMA_Cmd(DMA2_Stream1, ENABLE);
DMA_ITConfig(DMA2_Stream1, DMA_IT_TC, ENABLE);
ADC_Cmd(ADC3, ENABLE);
TIM_Cmd(TIM4,ENABLE);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
//ADC定时器停止
void DRV_ADC_Stop(void)
{
TIM_CtrlPWMOutputs(TIM4,DISABLE);
TIM_Cmd(TIM4,DISABLE);
DMA_ITConfig(DMA2_Stream1, DMA_IT_TC, DISABLE);
DMA_Cmd(DMA2_Stream1, DISABLE);
ADC_Cmd(ADC3, DISABLE);
}