/*************************************************
函数: ADC1_Init
功能: ADC1初始化
参数: 无
返回: 无
**************************************************/
void ADC1_Init(void)
{ Num1=Num11;
// ADC1_RCC_Config(); //初始化时钟
ADC1_NVIC_Config(); //初始化中断
ADC1_GPIO_Config(); //初始化引脚
// ADC1_AFIO_Configuration();//ADC中TIM重映射
ADC1_DMA_Config(); //初始化DMA
}
* 文件名 :DAC.c
#include "dac.h"
extern float A; //用户定义锯齿波幅值,在adc.h中通过宏定义SET_A定义
extern float F; //用户定义锯齿波频率,在adc.h中通过宏定义SET_F定义
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DAC_DHR12R2_Address 0x40007414 //12bit-DAC单通道寄存器地址
#define DAD 720
/* Init Structure definition */
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
uint32_t SawtoothWave12bit[DAD]; //存放DAC锯齿波数据的数组
uint32_t i = 0;
uint16_t freq=0; // ferq: 频率的转换参数
float Amp=0.0; // 幅值的转换参数
/*
* 函数名:DAC2_RCC_Configuration
* 描述 :DAC的RCC配置
* 输入 : 无
* 输出 :无
* 调用 :内部调用
*/
static void DAC2_RCC_Configuration(void)
{
/* Enable peripheral clocks --------------------------------------------------*/
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
/* GPIOA Periph clock enable */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* DAC Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC|RCC_APB1Periph_TIM4, ENABLE);
}
/*
* 函数名:DAC2_GPIO_Configuration
* 描述 :DAC通道2的GPIO模式配置
* 输入 : 无
* 输出 :无
* 调用 :内部调用
*/
static void DAC2_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Once the DAC channel is enabled, the corresponding GPIO pin is automatically
connected to the DAC converter. In order to avoid parasitic consumption,
the GPIO pin should be configured in analog */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
//PA5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOA,GPIO_Pin_5);
}
/*
* 函数名:DAC2_Mode_Config
* 描述 :TIM4主/从模式配置,DAC通道2模式配置
* 输入 : 无
* 输出 :无
* 调用 :内部调用
*/
static void DAC2_Mode_Config(void)
{
/* 转换F频率值 */
//freq =((uint16_t) (17578.125/F));
freq =((uint16_t) (100000/F)); // 17578.125=72000000/720
/* 转换A频率值 */
Amp =(A*4095)/2.5; //3.16为示波器检测到的DAC电压满量程,可根据实际情况调整
//配置TIMER4为主模式,OC1作为触发输出
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period= freq; //set freq
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM4, 0x0,TIM_PSCReloadMode_Immediate); //时钟分频系数0,所以定时器时钟为72000000/freq;
TIM_ARRPreloadConfig(TIM4, DISABLE); //禁止ARR预装载缓冲器
/* TIM4 TRGO selection */
//配置TIMER4的输入通道1的参数
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter=8; //Fsampling=Fdts/4 N=8 滤除32ms的抖动
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_TIxExternalClockConfig(TIM4,TIM_TS_TI1FP1,TIM_ICPolarity_Rising,0); //配置外部触发信号为TI1FP1
TIM_SelectSlaveMode(TIM4,TIM_SlaveMode_Trigger); //配置TIMER4从模式为触发模式
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_Update); //选择触发输出为使能
// TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable); //选择主/从模式(既是主模式也是从模式,触发信号延迟一段时间等待从定时器以达到同步的目的)
/* DAC channel2 Configuration */
DAC_StructInit(&DAC_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Fill SawtoothWave12bit table */
/*锯齿波数据产生函数*/
for (i=0; i<DAD; i++)
{
// SawtoothWave12bit[i]=SawtoothWave12bit[0]+(uint32_t)((Amp/4096)*i);
SawtoothWave12bit[i]=700+(uint32_t)((Amp/DAD)*i);
}
SawtoothWave12bit[718]=700;//918;
SawtoothWave12bit[719]=700;
//SawtoothWave12bit[720]=0;
DAC_DMA2_Init(); // DMA2初始化
/* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* Enable DMA for DAC Channel2 */
DAC_DMACmd(DAC_Channel_2, ENABLE);
}
/*-------------------
* 函数名:DAC2_Init
* 描述 :DAC初始化
*-------------------*/
void DAC2_Init(void)
{
/* DAC2 RCC Configuration */
DAC2_RCC_Configuration();
/* Once the DAC channel is enabled, the corresponding GPIO pin is automatically
connected to the DAC converter. In order to avoid parasitic consumption,
the GPIO pin should be configured in analog */
DAC2_GPIO_Configuration();
DAC2_Mode_Config();
}
/*-------------------
* 函数名:TIM4_DAC_OPEN
* 描述 :通过使能TIM4启动DAC模块
*-------------------*/
void TIM4_DAC_OPEN(void)
{
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
/*-------------------
* 函数名:TIM4_DAC_CLOSE
* 描述 :通过失能TIM4停止DAC模块
*-------------------*/
void TIM4_DAC_CLOSE(void)
{
TIM_Cmd(TIM4, DISABLE);
}
/*-------------------
* 函数名:DAC_DMA2_Init
* 描述 :DMA通道2初始化
*-------------------*/
void DAC_DMA2_Init(void)
{
/* DMA2 channel4 configuration */
DMA_DeInit(DMA2_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_Address; //双通道 DAC_DHR12RD_Address
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&SawtoothWave12bit;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = DAD;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
/* Enable DMA2 Channel4 */
DMA_Cmd(DMA2_Channel4, ENABLE);
}
void opencaiji(void)
{
DMA_Cmd(DMA1_Channel1, ENABLE); //打开DMA1通道1 for ADC
TIM4_DAC_OPEN(); //开启TIM4,触发DAC和TIM1
}
void closecaiji(void)
{
DMA_Cmd(DMA1_Channel1, DISABLE);
TIM4_DAC_CLOSE(); //开启TIM4,触发DAC和TIM1
}
void Time5_RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
}
/*
* 函数名:Time5_Configuration
* 描述 :TIME5配置 ,定时2s
* 输入 :无
* 输出 :无
*/
void Time5_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM5);
TIM_TimeBaseStructure.TIM_Period=490;
//TIM_TimeBaseStructure.TIM_Period=999;
// TIM_TimeBaseStructure.TIM_Period=19999;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM5,719,TIM_PSCReloadMode_Immediate);
TIM_PrescalerConfig(TIM5,359,TIM_PSCReloadMode_Immediate);
TIM_ARRPreloadConfig(TIM5, DISABLE);
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM5, ENABLE); //使能TIME5
}
/*
* 函数名:Time5_NVIC_Configuration
* 描述 :配置嵌套向量中断控制器NVIC
* 输入 :无
* 输出 :无
* 调用 :内部调用
*/
void Time5_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* 函数名:Time5_Init
* 描述 :触发DAC&ADC的定时器Tim5的初始化
* 输入 :无
* 输出 :无
*/
void Time5_Init(void)
{
Time5_RCC_Configuration(); //触发DAC&ADC的定时器Tim5的RCC时钟使能
Time5_NVIC_Configuration(); //触发DAC&ADC的定时器Tim5中断向量寄存器初始化
Time5_Configuration(); //触发DAC&ADC的定时器Tim5初始化
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
DMA_Cmd(DMA1_Channel1, ENABLE); //打开DMA1通道1 for ADC
//DAC_DMA2_Init();
TIM4_DAC_OPEN(); //开启TIM4,触发DAC和TIM1
}
}
|