Ejemplo n.º 1
0
/**
  * @brief  Configure the TIMER peripheral.
  * @param  None
  * @retval None
  */
void TIMER_Configuration(void)
{
    /* TIMER1 DMA Transfer example -------------------------------------------------
    TIMER1CLK = 72MHz, Prescaler = 72 
    TIMER1 counter clock = SystemCoreClock/72 = 1MHz.

    The objective is to configure TIMER1 channel 1 to generate PWM
    signal with a frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is 
    changed by the DMA after a specific number of Update DMA request.

    The number of this repetitive requests is defined by the TIMER1 Repetition counter,
    each 2 Update Requests, the TIMER1 Channel 1 Duty Cycle changes to the next new 
    value defined by the buffer . 
    -----------------------------------------------------------------------------*/
    TIMER_BaseInitPara TIMER_TimeBaseStructure;
    TIMER_OCInitPara  TIMER_OCInitStructure;
    /* TIMERS clock enable */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE);
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE);

    /* TIMER1  configuration */
    TIMER_DeInit(TIMER1);
    TIMER_TimeBaseStructure.TIMER_Prescaler         = 71;
    TIMER_TimeBaseStructure.TIMER_CounterMode       = TIMER_COUNTER_UP;
    TIMER_TimeBaseStructure.TIMER_Period            = 999;
    TIMER_TimeBaseStructure.TIMER_ClockDivision     = TIMER_CDIV_DIV1;
    TIMER_TimeBaseStructure.TIMER_RepetitionCounter = 1;
    TIMER_BaseInit(TIMER1,&TIMER_TimeBaseStructure);

    /* CH1 Configuration in PWM mode */
    TIMER_OCInitStructure.TIMER_OCMode       = TIMER_OC_MODE_PWM1;
    TIMER_OCInitStructure.TIMER_OCPolarity   = TIMER_OC_POLARITY_HIGH;
    TIMER_OCInitStructure.TIMER_OCNPolarity  = TIMER_OCN_POLARITY_HIGH;
    TIMER_OCInitStructure.TIMER_OutputState  = TIMER_OUTPUT_STATE_ENABLE;
    TIMER_OCInitStructure.TIMER_OutputNState = TIMER_OUTPUTN_STATE_ENABLE;
    TIMER_OCInitStructure.TIMER_OCIdleState  = TIMER_OC_IDLE_STATE_SET;
    TIMER_OCInitStructure.TIMER_OCNIdleState = TIMER_OCN_IDLE_STATE_RESET;
     
    TIMER_OCInitStructure.TIMER_Pulse = buffer[0];
    TIMER_OC1_Init(TIMER1, &TIMER_OCInitStructure);
    TIMER_OC1_Preload(TIMER1,TIMER_OC_PRELOAD_DISABLE);

    /* TIMER1 output enable */
    TIMER_CtrlPWMOutputs(TIMER1,ENABLE);
    /* Auto-reload preload enable */
    TIMER_CARLPreloadConfig(TIMER1,ENABLE);
    /* TIMER1 Update DMA Request enable */
    TIMER_DMACmd( TIMER1, TIMER_DMA_UPDATE, ENABLE);
    /* TIMER enable counter*/
    TIMER_Enable( TIMER1, ENABLE );
}
Ejemplo n.º 2
0
void pwm_init(void)
{
	
    GPIO_InitPara GPIO_InitStructure;

	// timer 3 pin PB1 config ( ch4 )
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLDOWN;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
		
    GPIO_PinAFConfig(GPIOB,GPIO_PINSOURCE1,GPIO_AF_1);
	
	// GPIO pins PA8 , 9 ,10 setup ( Timer1 ch 1 , 2 , 3)
	  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;

    GPIO_Init(GPIOA,&GPIO_InitStructure);
		
    GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE8,GPIO_AF_2);
	  GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE9,GPIO_AF_2);
		GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE10,GPIO_AF_2);
	
    TIMER_BaseInitPara TIM_TimeBaseStructure;

    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1,ENABLE);
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER3,ENABLE);

// TIMER3 for pins A8 A9 A10

    TIM_TimeBaseStructure.TIMER_Prescaler = TIMER_PRESCALER - 1;  //
    TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
    TIM_TimeBaseStructure.TIMER_Period = PWMTOP;
    TIM_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
    TIMER_BaseInit(TIMER3,&TIM_TimeBaseStructure);
		

 //Ch1 , 2 , 3 
    TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
    TIM_OCInitStructure.TIMER_OCPolarity = OUT_POLARITY;
    TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET;
		
    TIM_OCInitStructure.TIMER_Pulse = 0;
    TIMER_OC4_Init(TIMER3, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER3,ENABLE);

    TIMER_CARLPreloadConfig(TIMER3,ENABLE);

    TIMER_Enable( TIMER3, ENABLE );
		
    TIMER_BaseInit(TIMER1,&TIM_TimeBaseStructure);

    TIMER_OC1_Init(TIMER1, &TIM_OCInitStructure);

    TIMER_OC2_Init(TIMER1, &TIM_OCInitStructure);

    TIMER_OC3_Init(TIMER1, &TIM_OCInitStructure);
		
	TIMER_CtrlPWMOutputs(TIMER1,ENABLE);	
		
    TIMER_CARLPreloadConfig(TIMER1,ENABLE);

	TIMER_OC1_Preload(TIMER1,ESC_PRELOAD);
	TIMER_OC2_Preload(TIMER1,ESC_PRELOAD);
	TIMER_OC3_Preload(TIMER1,ESC_PRELOAD);
	TIMER_OC4_Preload(TIMER3,ESC_PRELOAD);
	
  TIMER_Enable( TIMER1, ENABLE );
}
Ejemplo n.º 3
0
void pwm_init(void)
{
	GPIO_InitPara GPIO_InitStructure;


//pin A0 TM2_ch1
	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLDOWN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE1, GPIO_AF_2);


	TIMER_BaseInitPara TIM_TimeBaseStructure;

	RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2 | 
															RCC_APB1PERIPH_TIMER14 |
																RCC_APB1PERIPH_TIMER3, ENABLE);
	
	RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1, ENABLE);

// timer 2
	TIM_TimeBaseStructure.TIMER_Prescaler = TIMER_PRESCALER - 1;	//
	TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_CENTER_ALIGNED2;
	TIM_TimeBaseStructure.TIMER_Period = PWMTOP;
	TIM_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
// init timer 2
	TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
	TIM_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
	TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
	TIM_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET;

	TIM_OCInitStructure.TIMER_Pulse = 0;

	TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER2, ENABLE);

	TIMER_CARLPreloadConfig(TIMER2, ENABLE);


	TIMER_OC2_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE);


	// pin A3

	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE3, GPIO_AF_2);

	TIMER_OC4_Init(TIMER2, &TIM_OCInitStructure);


	TIMER_OC4_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE);

//   TIMER_Enable( TIMER2, ENABLE );

// timer 1 ch3 , ch1 
// pins A10 , A8         
	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_8;

	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE10, GPIO_AF_2);
	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE8, GPIO_AF_2);


// timer 1

// init timer 1
	TIMER_BaseInit(TIMER1, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIMER_Pulse = 0;

	TIMER_OC3_Init(TIMER1, &TIM_OCInitStructure);

	TIMER_OC1_Init(TIMER1, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER1, ENABLE);

	TIMER_CARLPreloadConfig(TIMER1, ENABLE);


	TIMER_OC3_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE);

	TIMER_OC1_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE);


// bridge dir 2
///////////////

	//pin A9 TM1_ch2
	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;

	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE9, GPIO_AF_2);

// timer 1

// init timer 1
	TIMER_BaseInit(TIMER1, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIMER_Pulse = 0;

	TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER1, ENABLE);

	TIMER_CARLPreloadConfig(TIMER1, ENABLE);


	TIMER_OC2_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE);



// timer 3 PB1 af1 ch4

	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1;

	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE1, GPIO_AF_1);


// timer 3

// init timer 3
	TIMER_BaseInit(TIMER3, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIMER_Pulse = 0;

	TIMER_OC4_Init(TIMER3, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER3, ENABLE);

	TIMER_CARLPreloadConfig(TIMER3, ENABLE);


	TIMER_OC4_Preload(TIMER3, TIMER_OC_PRELOAD_DISABLE);

// A0
// Timer2 ch1

	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0;

	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE0, GPIO_AF_2);


// timer 2

// init timer 2
	TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIMER_Pulse = 0;

	TIMER_OC1_Init(TIMER2, &TIM_OCInitStructure);

	TIMER_CtrlPWMOutputs(TIMER2, ENABLE);

	TIMER_CARLPreloadConfig(TIMER2, ENABLE);


	TIMER_OC1_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE);

// PA2 tm2 ch3

	GPIO_InitStructure.GPIO_Pin = GPIO_PIN_2;

	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE2, GPIO_AF_2);


	TIMER_OC3_Init(TIMER2, &TIM_OCInitStructure);

	TIMER_OC3_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE);



	TIMER_Enable(TIMER3, ENABLE);

	TIMER_Enable(TIMER1, ENABLE);

	TIMER_Enable(TIMER2, ENABLE);


}
Ejemplo n.º 4
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{       
    /* Configure System clocks -----------------------------------------------*/
    RCC_Configuration();
    
    /* Configure NVIC --------------------------------------------------------*/
    NVIC_Configuration();
  
    /* Configure GPIO ports --------------------------------------------------*/
    GPIO_Configuration();
    
    /* Configure TIM2 --------------------------------------------------------*/ 
    /* Configure Time Base */
    TIMER_BaseStructInit(&TIM_TimeBaseStructure); 
    TIM_TimeBaseStructure.TIMER_Period = 256;          
    TIM_TimeBaseStructure.TIMER_Prescaler = 6;       
    TIM_TimeBaseStructure.TIMER_ClockDivision = 0x0;    
    TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;  
    TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure);
    
    /* Configure TIM2 channel2 in PWM mode */
    TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; 
    TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;                
    TIM_OCInitStructure.TIMER_Pulse = 128; 
    TIM_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_LOW;         
    TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure);
    
    /* Configure DMA1 Channel1 -----------------------------------------------*/
    DMA_DeInit(DMA1_CHANNEL1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_RDTR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC;
    DMA_InitStructure.DMA_BufferSize = 64;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
    DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD;
    DMA_InitStructure.DMA_Mode = DMA_MODE_NORMAL;
    DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH;
    DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
    DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure);
    
    /* Enable DMA1 channel1 */
    DMA_Enable(DMA1_CHANNEL1, ENABLE);
    
    /* Configure ADC ---------------------------------------------------------*/
    ADC_InitStructure.ADC_Mode_Scan = ENABLE;
    ADC_InitStructure.ADC_Mode_Continuous = DISABLE;
    ADC_InitStructure.ADC_Trig_External  = ADC_EXTERNAL_TRIGGER_MODE_T2_CC2;
    ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT;
    ADC_InitStructure.ADC_Channel_Number = 1;
    ADC_Init(&ADC_InitStructure);
    
    /* Configure ADC regular channel14 */ 
    ADC_RegularChannel_Config(ADC_CHANNEL_14, 1, ADC_SAMPLETIME_55POINT5);
    
    /* Set inserted sequencer length */
    ADC_InsertedSequencerLength_Config(1);
    /* Configure ADC inserted channel */ 
    ADC_InsertedChannel_Config(ADC_CHANNEL_11, 1, ADC_SAMPLETIME_55POINT5);
    /* Configure ADC inserted external trigger */
    ADC_ExternalTrigInsertedConv_Config(ADC_EXTERNAL_TRIG_INSERTCONV_NONE);
    
    /* Enable automatic inserted conversion start after regular one */
    ADC_AutoInsertedConv_Enable(ENABLE);
    
    /* Enable ADC DMA */
    ADC_DMA_Enable(ENABLE);   
    
    /* Enable ADC external trigger */ 
    ADC_ExternalTrigConv_Enable( ENABLE);
    
    /* Enable EOIC interrupt */
    ADC_INTConfig(ADC_INT_EOIC, ENABLE);
    
    /* Enable ADC */
    ADC_Enable(ENABLE); 
    
    ADC_Calibration();
    
    /* TIM2 counter enable */
    TIMER_Enable(TIMER2, ENABLE);
    /* TIM2 main Output enable */
    TIMER_CtrlPWMOutputs(TIMER2, ENABLE);  
    
    /* Test on channel1 transfer complete flag */
    while(!DMA_GetBitState(DMA1_FLAG_TC1));
    /* Clear channel1 transfer complete flag */
    DMA_ClearBitState(DMA1_FLAG_TC1); 
    
    /* TIM2 counter disable */
    TIMER_Enable(TIMER2, DISABLE);
    /* Turn on the Led1 */
    GD_EVAL_LEDOn(LED1);
    while (1)
    {
    }
}