我由DMA+GPIO 方式:
F205 :
//***************************************************************************************
//初始化Io脚 TIM8 32位定时器 ,60MHZ工作!!
//****************************************************************************************
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_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*
GPIO_InitStructure.GPIO_Pin = APP_IOWith ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
*/
APP_OUT_With(0);
// APP_IO_With(0);
}
//*************************************************************************
//计算定时参数,用于Tim2,传入要定时的时间,单位是频率 HZ
//*************************************************************************
unsigned int Get_Timer_Tick_TIM8(float freq,unsigned int Prescaler){
unsigned int tick;
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
if(freq<10) freq=10;//限制频率的低限值!
tick=RCC_Clocks.PCLK2_Frequency/freq/Prescaler;//得到间隔常数!1000 是秒的转换,100是分频系数!
tick /=4;
if(tick==0) tick=2;
return tick-1; //DMA操作要8个时钟周期!!
}
//****************************************************************************
//F=60MHZ
void TIM8_Config(unsigned int cnt,unsigned int prescaler){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM8);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* TIM8 Configuration ------------------------------------------------------*/
/* TIM3CLK = 72 MHz, Prescaler = 0, TIM3 counter clock = 72 MHz */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = cnt;
TIM_TimeBaseStructure.TIM_Prescaler = prescaler; //频率低了自动添加分频系数!
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &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 TIM8 DMA */
TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE);//TIM_DMA_CC1
/* Enable TIM8 counter */
TIM_Cmd(TIM8, DISABLE);
}
//*******************************************************************************
// 停止 启动Time8
void Start_SendOut(FunctionalState NewState){
TIM_Cmd(TIM8,NewState); /* Enable TIM8 */
}
//************************************************************************************
void DMA_SetChannel2_for_TIM8(unsigned int cnt){
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the DMA Stream IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //最高级别!
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable DMA1 clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA2_Stream1); //DMA_DeInit(DMA2_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_7; //
DMA_InitStructure.DMA_PeripheralBaseAddr =(unsigned int)&(GPIOA->BSRRL); //指向右对齐寄存器8位
DMA_InitStructure.DMA_Memory0BaseAddr =(unsigned int)RAM_HIGH_LOW_TAB;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//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_MemoryInc_Disable;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
/* Enable DMA1 Channel6 Transfer Complete interrupt */
DMA_ClearITPendingBit(DMA2_Stream1,DMA_IT_TCIF1);
DMA_ITConfig(DMA2_Stream1,DMA_IT_TC, ENABLE);
/* Enable DMA2 Channel1 */
DMA_Cmd(DMA2_Stream1, DISABLE);
DMA_Cmd(DMA2_Stream1, ENABLE);
}
//***************************************************************************************************
//启动DMA中断!
//******************************************************************************
extern void Message_Power(unsigned int pwr);
extern void Get_NTC_DATA(unsigned char pwr);
extern void Start_TIM5(void);
//*******************************************************************************
//中断处理程序
void IRQ_DMA_2(void){
if(DMA_GetITStatus(DMA2_Stream1,DMA_IT_TCIF1)){
/* DMA1 finished the transfer of SrcBuffer */
/* Clear DMA1 Channel6 Half Transfer, Transfer Complete and Global interrupt pending bits */
//Start_SendOut(DISABLE);
TIM_Cmd(TIM8,DISABLE);
//APP_IO_With(0);
APP_OUT_With(0);
//**********************************************
//进入延时等待,使用 TIM11
TIM_Cmd(TIM7,ENABLE);
//**********************************************
//Get_NTC_DATA(ENABLE); //启动NTC采集!!!
//**********************************************
DMA_ClearITPendingBit(DMA2_Stream1,DMA_IT_TCIF1);//DMA_IT_TCIFx DMA_IT_TCIF0
DMA_ITConfig(DMA2_Stream1,DMA_IT_TCIF1, DISABLE);
}
}
/**********************************************************************
采用DMA---gpio 控制输出
************************************************************************/
void DMA_to_gpio_ON_OFF(FunctionalState on,float freq,unsigned int dot){
unsigned int tick,prescaler;
if(on == DISABLE){
Start_SendOut(DISABLE);
}else{
//***************************************************************************
//发送频率转换成定时器的定时系数!
prescaler=0;
if(freq<1000) prescaler=99;
tick=Get_Timer_Tick_TIM8(freq,prescaler+1); //低于20K频率计算~!!
Set_Ram_Data(dot); //在RAM中生成数据表!!!
DMA_SetChannel2_for_TIM8(dot); //高低2个电平!
TIM8_Config(tick,prescaler);
Start_SendOut(ENABLE);
Start_TIM5(); //启动32位定时器
}
}
//**************************************************************************
//对外部接口
void Send_Hz_Out_from_GPIO(void){
DMA_to_gpio_ON_OFF(ENABLE,(Mp1.P3.F*(Mp1.P3.Fr/100+1))*1000,(2*User.P+1)*2);
}
|