//***************************************************************************************
//初始化Io脚
void APP_IOWith_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = APP_OUT_IO ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
APP_OUT_With(0);
//APP_IO_With(0);
}
//*************************************************************************
//计算定时参数,用于Tim3,传入要定时的时间,单位是频率 HZ
//*************************************************************************
unsigned int Get_Timer_Tick_TIM2(float freq){
unsigned int tick;
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
if(freq<80) freq=80;//限制频率的低限值!
tick=RCC_Clocks.PCLK1_Frequency/freq;//得到间隔常数!1000 是秒的转换,100是分频系数!
//tick /=2;
//if(tick<9) tick=9;
return tick+(signed char)Mp1.Fr; //DMA操作要8个时钟周期!!
}
//****************************************************************************
void TIM2_Config(unsigned int cnt){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
/* TIM2 Configuration ------------------------------------------------------*/
/* TIM3CLK = 72 MHz, Prescaler = 0, TIM3 counter clock = 72 MHz */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = cnt;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Input Capture Mode configuration: Channel1 */
/* 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 = 0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);*/
/* Enable TIM2 DMA */
TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);//TIM_DMA_CC1
/* Enable TIM2 counter */
TIM_Cmd(TIM2, DISABLE);
}
//*******************************************************************************
// 停止 启动Time2
void Start_SendOut(FunctionalState NewState){
TIM_Cmd(TIM2,NewState); /* Enable TIM2 */
}
//************************************************************************************
void DMA_SetChannel2_for_TIM2(unsigned int cnt){
DMA_InitTypeDef DMA_InitStructure;
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr =(unsigned int)&(GPIOB->BSRRL); //指向右对齐寄存器8位
DMA_InitStructure.DMA_MemoryBaseAddr =(unsigned int)RAM_HIGH_LOW_TAB;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // dac作为目的地!
DMA_InitStructure.DMA_BufferSize = cnt+1; //多传送一个脉冲!!!
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_Normal ; //数据采用覆盖方式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* Enable DMA1 Channel6 Transfer Complete interrupt */
DMA_ClearITPendingBit(DMA1_IT_GL2);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
/* Enable DMA1 Channel1 */
DMA_Cmd(DMA1_Channel2, DISABLE);
DMA_Cmd(DMA1_Channel2, ENABLE);
}
//***************************************************************************************************
//启动DMA中断!
//******************************************************************************
extern void Message_Power(unsigned int pwr);
//*******************************************************************************
//中断处理程序
void IRQ_DMA_2(void){
if(DMA_GetITStatus(DMA1_IT_TC2)){
/* DMA1 finished the transfer of SrcBuffer */
/* Clear DMA1 Channel6 Half Transfer, Transfer Complete and Global interrupt pending bits */
//Start_SendOut(DISABLE);
TIM_Cmd(TIM2,DISABLE);
//APP_IO_With(0);
APP_OUT_With(0);
//**********************************************
//进入延时等待,使用 TIM11
TIM_Cmd(TIM11,ENABLE);
DMA_ClearITPendingBit(DMA1_IT_GL2);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC,DISABLE);
}
}
/**********************************************************************
关闭DAC输出 DAC_Tab=0,1,2 对应3个输出表!
************************************************************************/
void DAC_Turn_ON_OFF(FunctionalState on,unsigned int freq,unsigned int dot){
unsigned int tick;
if(on == DISABLE){
Start_SendOut(DISABLE);
}else{
//***************************************************************************
//发送频率转换成定时器的定时系数!
tick=Get_Timer_Tick_TIM2(freq); //低于20K频率计算~!!
Set_Ram_Data(dot); //在RAM中生成数据表!!!
DMA_SetChannel2_for_TIM2(dot); //高低2个电平!
TIM2_Config(tick);
//APP_IO_With(1);
Start_SendOut(ENABLE);
}
}
//**************************************************************************
//对外部接口
void Send_Hz_Out_from_GPIO(void){
DAC_Turn_ON_OFF(ENABLE,(Mp1.F)*1000,(2*Mp1.P-1)*2);
}
|