/* Config IO for related timers */
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Timer1 Channel 2, PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer3 Channel 4, PB1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Setup Timer3 channel 4, Timer3 is master timer
This timer is used to control the waveform count of timer1 */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = PWM_Period/2 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = waveNumber*2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = waveNumber*2 - 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
}
/* Setup timer1 channel 2, Timer1 is slave timer
This timer is used to output waveforms */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = PWM_Period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_Period/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
timer1 is work under gate control mode, and controled by timer3
timer3's channel 4 is used as the control signal
*/
/* Enable timer's master/slave work mode */
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* timer3's channel 4 is used as the control signal */
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );
/* Check the master/slave is valid or not */
compile_assert((u16)GetInternalTrigger(TIM1,TIM3) != (u16)-1);
/* Config timer1's external clock */
TIM_ITRxExternalClockConfig(TIM1, GetInternalTrigger(TIM1,TIM3));
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);
/* Enable the slave tiemr*/
TIM_Cmd(TIM1,ENABLE);
//SetupAlltimers();
while(1){
/* Check whether the previous action is done or not */
if(!(TIM3->CR1 & 1)){
TIM1->CNT = 0; /* It would be very perfect if gate mode can
reset the slave timer automatically */
TIM3->ARR = waveNumber*2; /* Reload wave number*/
TIM3->CCR4 = waveNumber*2 - 1;
TIM3->CR1|=1; /* Re-enable the timer */
/* update waveform number */
waveNumber++;
if(waveNumber == 13){
waveNumber = 10;
}
}
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();