void ENC1_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2); /*NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);*/ /* Timer configuration in Encoder mode */ TIM_DeInit(TIM2); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling TIM_TimeBaseStructure.TIM_Period = 40000-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 0xe; TIM_ICInit(TIM2, &TIM_ICInitStructure); // Clear all pending interrupts TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_SetCounter(TIM2,0); TIM2->CNT = 0; TIM_Cmd(TIM2, ENABLE); }
static void DacInitDmaAndTimer() { uint32_t prescaler; uint32_t period; prescaler = 1; period = (3500)/2; DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_Channel = DMA_Channel_7; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&DAC->DHR12R1; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&g_sinusBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize = SinusBufferSize; 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; 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(DMA1_Stream5, &DMA_InitStructure); DMA_Cmd(DMA1_Stream5, ENABLE); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_DMACmd(DAC_Channel_1, ENABLE); //168 MHz / TIM_Prescaler / TIM_Period TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = period-1; TIM_TimeBaseStructure.TIM_Prescaler = prescaler-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM_SetCounter(TIM2, 0); TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); g_dac_period = period * prescaler * SinusBufferSize; }
void TIM2_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 60000000/8000; TIM_TimeBaseStructure.TIM_Prescaler = 7; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); TIM_ITConfig(TIM2,TIM2_IRQn, ENABLE); NVIC_EnableIRQ(TIM2_IRQn); TIM_Cmd(TIM2, ENABLE); }
// Set sample Time via recieve Command // Set ADC Channel numbers via recieve Command void TIM2_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); //500000 TIM_TimeBaseStructure.TIM_Period = (84000000 / 200000) - 1; // 200 KHz, from 84 MHz TIM2CLK (ie APB1 = HCLK/4, TIM2CLK = HCLK/2) TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); // ADC_ExternalTrigConv_T2_TRGO /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); }
int32_t PIOS_DELAY_Init(void) { /* Enable timer clock */ PIOS_DELAY_TIMER_RCC_FUNC; /* Time base configuration */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 65535; // maximum value TIM_TimeBaseStructure.TIM_Prescaler = 72-1; // for 1 uS accuracy fixed to 72Mhz TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(PIOS_DELAY_TIMER, &TIM_TimeBaseStructure); /* Enable counter */ TIM_Cmd(PIOS_DELAY_TIMER, ENABLE); /* No error */ return 0; }
/** * @brief TIM6 Configuration * @note TIM6 configuration is based on CPU @168MHz and APB1 @42MHz * @note TIM6 Update event occurs each 37.5MHz/256 = 16.406 KHz * @param None * @retval None */ void TIM6_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* TIM6 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); /* TIM6 TRGO selection */ TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); /* TIM6 enable counter */ TIM_Cmd(TIM6, ENABLE); }
/* Configure and enable RCC for peripherals (ADC1, ADC2, Timer) */ static inline void adc_init_rcc( void ) { // {{{ #if defined (USE_AD1) || defined (USE_AD2) TIM_TypeDef * timer; uint32_t rcc_apb; #if defined(USE_AD_TIM4) timer = TIM4; rcc_apb = RCC_APB1Periph_TIM4; #elif defined(USE_AD_TIM1) timer = TIM1; rcc_apb = RCC_APB2Periph_TIM1; #else timer = TIM2; rcc_apb = RCC_APB1Periph_TIM2; #endif TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_ADCCLKConfig(RCC_PCLK2_Div2); RCC_APB1PeriphClockCmd(rcc_apb, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE); #ifdef USE_AD1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); #endif #ifdef USE_AD2 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); #endif /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x8; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(timer, &TIM_TimeBaseStructure); TIM_SelectOutputTrigger(timer, TIM_TRGOSource_Update); TIM_Cmd(timer, ENABLE); #endif // defined (USE_AD1) || defined (USE_AD2) } // }}}
/* ********************************************************************************************************* * 函 数 名: TIM6_Config * 功能说明: 配置定时器6,用于触发DAC。 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ void TIM6_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* 使能TIM6时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); /* -------------------------------------------------------- system_stm32f4xx.c 文件中 void SetSysClock(void) 函数对时钟的配置如下: HCLK = SYSCLK / 1 (AHB1Periph) PCLK2 = HCLK / 2 (APB2Periph) PCLK1 = HCLK / 4 (APB1Periph) 因为APB1 prescaler != 1, 所以 APB1上的TIMxCLK = PCLK1 x 2 = SystemCoreClock / 2; 因为APB2 prescaler != 1, 所以 APB2上的TIMxCLK = PCLK2 x 2 = SystemCoreClock; APB1 定时器有 TIM2, TIM3 ,TIM4, TIM5, TIM6, TIM7, TIM12, TIM13, TIM14 APB2 定时器有 TIM1, TIM8 ,TIM9, TIM10, TIM11 TIM6 更新周期是 = TIM6CLK / (TIM_Period + 1)/(TIM_Prescaler + 1) 根据如下的配置,更新周期是: TIM6CLK / (TIM_Period + 1)/(TIM_Prescaler + 1) = 84MHz /(55+1)/(0+1) = 1.5MHz ----------------------------------------------------------- */ /* 配置TIM6 */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 83; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); /* TIM6 TRGO 选择 */ TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); /* 使能TIM6 */ TIM_Cmd(TIM6, ENABLE); }
void BSP_Timer_Init() { NVIC_InitTypeDef NVICInit; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); NVICInit.NVIC_IRQChannel = TIM5_IRQn; NVICInit.NVIC_IRQChannelPreemptionPriority = 2; NVICInit.NVIC_IRQChannelSubPriority = 0; NVICInit.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVICInit); TIM_TimeBaseStructInit(&TIMInit); TIMInit.TIM_Prescaler = 0; TIMInit.TIM_CounterMode = TIM_CounterMode_Up; TIMInit.TIM_Period = 0; TIMInit.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM5, &TIMInit); TIM_InternalClockConfig(TIM5); }
//------------------------------------------------------------------------------ void tim_init(void) { TIM_TimeBaseInitTypeDef timInit; NVIC_InitTypeDef nvicInit; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructInit(&timInit); timInit.TIM_Period = 10000; // ms = 10 sec timInit.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1; TIM_TimeBaseInit(TIM2, &timInit); nvicInit.NVIC_IRQChannel = TIM2_IRQn; nvicInit.NVIC_IRQChannelPriority = 3; nvicInit.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicInit); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_Cmd(TIM2, ENABLE); }
void TS_Init() { TS_SPIx = TS_HAL_Init(); spi_init(TS_SPIx, 32); // timer for request touch coords time by time RCC_ClocksTypeDef clocks; RCC_GetClocksFreq(&clocks); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); TIM_TimeBaseInitTypeDef timer_struct; TIM_TimeBaseStructInit(&timer_struct); timer_struct.TIM_Period = 999; timer_struct.TIM_Prescaler = (clocks.SYSCLK_Frequency/TOUCH_UPDATE_FREQUENCY/(timer_struct.TIM_Period+1)) - 1; // 10 Hz TIM_TimeBaseInit(TIM5, &timer_struct); TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE); TIM_Cmd(TIM5, DISABLE); NVIC_EnableIRQ(TIM5_IRQn); NVIC_SetPriority(TIM5_IRQn, TS_IRQ_PRIORITY); }
/** * @brief Configures the TIM2 * @param None * @retval None */ void TIM_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* TIM2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0x19; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection: update event is selected as trigger for DAC */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); }
/* ********************************************************************************************************* * 函 数 名: SetSampRate * 功能说明: 修改采样频率 * 形 参:freq : 采样频率 单位Hz * 返 回 值: 无 ********************************************************************************************************* */ static void Set_SampRate(T_DSO *g_DsoA) { /* 第4步: 配置定时器2第2通道CC2, CC2作为ADC1的触发源 */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; // uint16_t Period; TIM_Cmd(TIM2, DISABLE); /* 采样频率计算公式 : period = 72000000 / freq ; 1200 == 60KHz 采样频率 */ // Period = 72000 / freq -1; /* 使能 TIM1 时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = g_DsoA->SimplePeriod; TIM_TimeBaseStructure.TIM_Prescaler = g_DsoA->SimplePrescal; /* 计数频率 = 72000 000 / 18 = 4000 000 */ TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM1 通道1配置为PWM模式 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period / 2; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM2, &TIM_OCInitStructure); /* TIM1 计数器使能 */ TIM_Cmd(TIM2, DISABLE); /* TIM1 输出使能 */ TIM_CtrlPWMOutputs(TIM2, ENABLE); }
void DAC_Init_StartUp(void) { DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); /* TIM2 外设时钟使能*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* 定时器基础配置 */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xffff; TIM_TimeBaseStructure.TIM_Prescaler = 0x1a; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2触发输出模式选择 */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2使能 */ TIM_Cmd(TIM2, ENABLE); DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ctrlSoftStart; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 136; 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_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel3, &DMA_InitStructure); DMA_Cmd(DMA1_Channel3, ENABLE); }
void cppmInit(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(CPPM_GPIO_RCC, ENABLE); RCC_APB1PeriphClockCmd(CPPM_TIMER_RCC, ENABLE); // Configure the GPIO for the timer input GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = CPPM_GPIO_PIN; GPIO_Init(CPPM_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(CPPM_GPIO_PORT, CPPM_GPIO_SOURCE, CPPM_GPIO_AF); // Time base configuration. 1us tick. TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = CPPM_TIM_PRESCALER; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(CPPM_TIMER, &TIM_TimeBaseStructure); // Setup input capture using default config. TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInit(CPPM_TIMER, &TIM_ICInitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_CPPM_PRI; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); captureQueue = xQueueCreate(64, sizeof(uint16_t)); TIM_ITConfig(CPPM_TIMER, TIM_IT_Update | TIM_IT_CC1, ENABLE); TIM_Cmd(CPPM_TIMER, ENABLE); }
/** * @brief Configures the TIM3 channel3 in PWM mode * @param None * @retval None */ static void TIM3_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOB clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* GPIOB Configuration: PB0(TIM3 CH3) as alternate function push-pull */ 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_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_2); /* Enable TIM3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF0; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Channel3 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0xF0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC3Init(TIM3, &TIM_OCInitStructure); /* Enable TIM3 */ TIM_Cmd(TIM3, ENABLE); }
/* Sets up tim7 to run at a high sample rate */ void tim7_config(void) { /* Set up tim7 */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* TIM7 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); /* -------------------------------------------------------- TIM7 input clock (TIM7CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1 (see system_stm32f4xx.c and Fig 13 clock tree figure in DM0031020.pdf). Sample rate Fs = 2*PCLK1/TIM_ClockDivision = (HCLK/2)/TIM_ClockDivision ----------------------------------------------------------- */ /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 41; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); /* TIM7 TRGO selection */ TIM_SelectOutputTrigger(TIM7, TIM_TRGOSource_Update); /* TIM7 enable counter */ TIM_Cmd(TIM7, ENABLE); }
inline void Tim8Config1() { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); uint16_t uhPrescalerValue = (uint16_t)((SystemCoreClock) / 1000000) - 1; TIM_TimeBaseStructure.TIM_Period = 1000; TIM_TimeBaseStructure.TIM_Prescaler = uhPrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM8, ENABLE); TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM8, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Disable); TIM_CtrlPWMOutputs(TIM8, ENABLE); TIM_Cmd(TIM8, ENABLE); }
void EncoderL_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStruct; /* ENCODER clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Enable the ENCODER Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //ENCODER_CHA, ENCODER_CHB端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Timer configuration in Encoder mode */ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0;//预分频器 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseStructure.TIM_Period = 0xffff;//设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 // TIM_ARRPreloadConfig(ENCODER_TIM, ENABLE);//使能ARR自动重装入缓冲器 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_ICFilter = 6;//ICx_FILTER; // TIM_ICFilter选择输入比较滤波器。该参数取值在0x0和0xF之间 TIM_ICInit(TIM3, &TIM_ICInitStruct); TIM3->CNT = ENCODER_INIT_VALUE; //使能定时器 TIM_Cmd(TIM3,ENABLE); //使能定时器 }
void TIMER6_init(void) { TIM_TimeBaseInitTypeDef timer; NVIC_InitTypeDef interrupt; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); TIM_TimeBaseStructInit(&timer); timer.TIM_Period = 1909; timer.TIM_Prescaler = 0; timer.TIM_ClockDivision = 0; timer.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &timer); interrupt.NVIC_IRQChannel = TIM6_DAC_IRQn; interrupt.NVIC_IRQChannelPreemptionPriority = 0; interrupt.NVIC_IRQChannelSubPriority = 0; interrupt.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&interrupt); TIM_Cmd(TIM6, ENABLE); TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); }
void TIM4_init(void){ TIM_TimeBaseInitTypeDef timer; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructInit(&timer); timer.TIM_Prescaler = TIMER_PRESCALER - 1; timer.TIM_Period = TIMER_TIMPERIOD - 1; timer.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM4, &timer); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); TIM_Cmd(TIM4, ENABLE); NVIC_EnableIRQ(TIM4_IRQn); }
void us_ticker_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; if (us_ticker_inited) return; us_ticker_inited = 1; // Enable timer clock TIM_MST_RCC; // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); NVIC_SetVector(TIM_MST_IRQ, (uint32_t)us_ticker_irq_handler); NVIC_EnableIRQ(TIM_MST_IRQ); // Enable timer TIM_Cmd(TIM_MST, ENABLE); }
void init_pwm() { TIM_TimeBaseInitTypeDef timebase_init_struct; TIM_OCInitTypeDef oc_init_struct; TIM_TimeBaseStructInit(&timebase_init_struct); timebase_init_struct.TIM_ClockDivision = 0; timebase_init_struct.TIM_Period = 2500 - 1; timebase_init_struct.TIM_Prescaler = 72 - 1; // 72MHz/400Hz/2500=72 TIM_TimeBaseInit(TIM3, &timebase_init_struct); TIM_TimeBaseInit(TIM4, &timebase_init_struct); TIM_TimeBaseInit(TIM2, &timebase_init_struct); TIM_OCStructInit(&oc_init_struct); oc_init_struct.TIM_OCMode = TIM_OCMode_PWM1; oc_init_struct.TIM_OutputState = TIM_OutputState_Enable; oc_init_struct.TIM_Pulse = 0; // PWM1: TIM3_CH1 TIM_OC1Init(TIM3, &oc_init_struct); // PWM2: TIM3_CH2 TIM_OC2Init(TIM3, &oc_init_struct); // PWM3: TIM3_CH3 TIM_OC3Init(TIM3, &oc_init_struct); // PWM4: TIM3_CH4 TIM_OC4Init(TIM3, &oc_init_struct); // PWM5: TIM4_CH3 TIM_OC3Init(TIM4, &oc_init_struct); // PWM6: TIM4_CH4 TIM_OC4Init(TIM4, &oc_init_struct); // PWM1: TIM2_CH1 TIM_OC1Init(TIM2, &oc_init_struct); // PWM2: TIM2_CH2 TIM_OC2Init(TIM2, &oc_init_struct); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE); TIM_Cmd(TIM2, ENABLE); }
void config_tim2( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* TIM4 Peripheral Configuration ----------------------------------------*/ /* TIM4 Slave Configuration: PWM1 Mode */ TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); TIM_TimeBaseStructure.TIM_Period = 65535; // 42 MHz, half of APB1. TIM_TimeBaseStructure.TIM_Prescaler = PRESCALER; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCStructInit( &TIM_OCInitStructure ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* Slave Mode selection: TIM2 */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0); #ifdef GTDEBUG NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM3 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); #endif TIM_Cmd(TIM2, ENABLE); }
/** * @brief Configures the Timer. * @param None * @retval None */ void TIMER_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // Timer base configuration TIM_OCInitTypeDef TIM_OCInitStructure; // Timer for PWM mode // Time Base configuration TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); TIM_TimeBaseStructure.TIM_Period = 0x231; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; TIM_TimeBaseInit( TIM1, &TIM_TimeBaseStructure ); // TIM1 channel1 configuration in PWM mode TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init( TIM1, &TIM_OCInitStructure ); // TIM1 counter enable TIM_Cmd( TIM1, ENABLE ); // TIM1 main Output Enable TIM_CtrlPWMOutputs( TIM1, ENABLE ); }
void init_encoder(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef encoder; encoder.GPIO_Mode = GPIO_Mode_IPU; encoder.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Init(GPIOA, &encoder); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitTypeDef timer; TIM_TimeBaseStructInit(&timer); timer.TIM_Prescaler = 0; timer.TIM_Period = 256; timer.TIM_CounterMode = TIM_CounterMode_Down | TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &timer); TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); // TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); // NVIC_EnableIRQ(TIM2_IRQn); TIM_Cmd(TIM2, ENABLE); }
void pwmEscInit(uint16_t escPwmRate) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_StructInit(&GPIO_InitStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); // Outputs // ESC PWM1 TIM8_CH4 PC9 // ESC PWM2 TIM8_CH3 PC8 // ESC PWM3 TIM8_CH2 PC7 // ESC PWM4 TIM8_CH1 PC6 // ESC PWM5 TIM2_CH2 PB3 // ESC PWM6 TIM3_CH1 PB4 // ESC PWM7 TIM3_CH2 PB5 // ESC PWM8 TIM2_CH1 PA15 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; 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_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; //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_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; //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_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8); GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8); GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM8); // Output timers TIM_TimeBaseStructure.TIM_Period = (uint16_t)(2000000 / escPwmRate) - 1; TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1; //TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM_TimeBaseStructure.TIM_RepititionCounter = 0x0000; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = ESC_PULSE_1MS; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC1Init(TIM8, &TIM_OCInitStructure); TIM_OC2Init(TIM8, &TIM_OCInitStructure); TIM_OC3Init(TIM8, &TIM_OCInitStructure); TIM_OC4Init(TIM8, &TIM_OCInitStructure); TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM8, ENABLE); TIM_CtrlPWMOutputs(TIM2, ENABLE); TIM_CtrlPWMOutputs(TIM3, ENABLE); TIM_CtrlPWMOutputs(TIM8, ENABLE); }
int init_DAC() { GPIO_InitTypeDef gpinit[1] = {{0}}; gpinit->GPIO_Pin = GPIO_Pin_4; gpinit->GPIO_Mode = GPIO_Mode_AIN; gpinit->GPIO_Speed = GPIO_Speed_50MHz; gpinit->GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, gpinit); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DAC_DeInit(); DAC_InitTypeDef dac[1]; dac->DAC_Trigger = DAC_Trigger_T6_TRGO; dac->DAC_WaveGeneration = DAC_WaveGeneration_None; dac->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0; dac->DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, dac); DAC_Cmd(DAC_Channel_1, ENABLE); DMA_DeInit(DMA1_Stream5); DMA_InitTypeDef dma[1]; dma->DMA_Channel = DMA_Channel_7; dma->DMA_PeripheralBaseAddr = (uint32_t) &DAC->DHR12L1; dma->DMA_Memory0BaseAddr = (uint32_t) buffer; dma->DMA_DIR = DMA_DIR_MemoryToPeripheral; dma->DMA_BufferSize = NUMEL(buffer); dma->DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma->DMA_MemoryInc = DMA_MemoryInc_Enable; dma->DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; dma->DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; dma->DMA_Mode = DMA_Mode_Circular; dma->DMA_Priority = DMA_Priority_High; dma->DMA_FIFOMode = DMA_FIFOMode_Disable; dma->DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; dma->DMA_MemoryBurst = DMA_MemoryBurst_Single; dma->DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, dma); DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Stream5, DMA_IT_HT, ENABLE); NVIC_InitTypeDef nvic[1]; nvic->NVIC_IRQChannel = DMA1_Stream5_IRQn; nvic->NVIC_IRQChannelPreemptionPriority = 7; nvic->NVIC_IRQChannelSubPriority = 7; nvic->NVIC_IRQChannelCmd = ENABLE; NVIC_Init(nvic); DMA_Cmd(DMA1_Stream5, ENABLE); DAC_DMACmd(DAC_Channel_1, ENABLE); TIM_DeInit(TIM6); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 1749; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); TIM_Cmd(TIM6, ENABLE); return 0; }
static void config_tim1( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; uint16_t TimerPeriod = 0; uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0; /* Compute the value to be set in ARR register to generate signal frequency at 16 Khz */ //TimerPeriod = (SystemCoreClock / 16000) - 1; // In center aligned mode, counts up and down, so * 2. // TimerPeriod = (84000000 / ( 2 * 14000 )) - 1; TimerPeriod = 1000; /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */ //Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10); Channel1Pulse = 500; /* Compute CCR2 value to generate a duty cycle at 25% for channel 2 */ //Channel2Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100); Channel2Pulse = 250; /* Compute CCR3 value to generate a duty cycle at 12.5% for channel 3 */ //Channel3Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod - 1)) / 1000); Channel3Pulse = 125; // pulse sent to other timer to trigger ADC; // 97 steps from 999 -> 999-97 = 902 Channel4Pulse = 902; /* Time Base configuration */ TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); // 14 MHz / 1000 steps == 14 kHz TIM_TimeBaseStructure.TIM_Prescaler = PRESCALER; //TIM_TimeBaseStructure.TIM_Prescaler = 65535; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2; TIM_TimeBaseStructure.TIM_Period = TimerPeriod; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Channel 1, 2 and 3 Configuration in PWM mode */ TIM_OCStructInit( &TIM_OCInitStructure ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OCStructInit( &TIM_OCInitStructure ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_Pulse = Channel4Pulse; TIM_OC4Init(TIM1, &TIM_OCInitStructure); /* Automatic Output enable, Break, dead time and lock configuration*/ TIM_BDTRStructInit( &TIM_BDTRInitStructure ); TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; TIM_BDTRInitStructure.TIM_DeadTime = 11; TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Master Mode selection and TRGO update output */ TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_OC4Ref); TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); }
/** * @brief Configures TIM1: channels in PWM mode * @param None * @retval None */ void TIM_Config(void) { TIM_BDTRInitTypeDef TIM_BDTRInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM1 channels pin configuration: TIM1_CH1 -> PA8 */ GPIO_StructInit(&GPIO_InitStructure); 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_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable Alternate function on PA8 to be controlled by TIM1 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 100; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Channel 1 Configuration in PWM mode */ TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 50; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* Automatic Output enable, Break, dead time and lock configuration*/ TIM_BDTRStructInit(&TIM_BDTRInitStructure); TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; TIM_BDTRInitStructure.TIM_DeadTime = 11; TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); }