/******************************************************************************* * 名 称: TIM4_Config * 功 能: 配置TIM4,为定时中断10ms, 频率100Hz * 入口参数: 无 * 出口参数: 无 * 作 者: Roger-WY * 创建日期: 2014-08-20 * 修 改: * 修改日期: * 备 注: *******************************************************************************/ static void TIM4_Config(void) { #if 1 /* 空间不足,优化代码 */ /* TIM4是自动重载的8位定时器,计数方向为递增,当递增到0时可以产生定时器中断,并自动重装定时器初值 */ /* 计算期望的计数时钟个数, 由于TIM4是8位的定时器,因此如果该值大于255,则需要进行分频 */ TIM4_DeInit(); /* 复位TIM4所有寄存器 */ TIM4_ARRPreloadConfig(ENABLE); /* 预先装载使能 */ TIM4_TimeBaseInit(TIM4_PRESCALER_128, (16000000u / (1000 / SYSTICK_PERIOD)) / 128); /* 设置预分频和定时器重载 */ /* Clear TIM4 update flag */ TIM4_ClearFlag(TIM4_FLAG_UPDATE); TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); /* 使能TIM4中断 */ //TIM4_UpdateDisableConfig(ENABLE); /* 使能TIM4自动溢出事件 */ TIM4_Cmd(ENABLE); /* 使能TIM4 */ #else /* 下面这个分支,可以根据时钟自动计算TIM4定时器初值 */ uint32_t uiSysClkFreq; uint32_t uiCount; uiSysClkFreq = CLK_GetClockFreq(); /* 获得当前的系统时钟频率 */ TIM4_DeInit(); /* 复位TIM4所有寄存器 */ TIM4_ARRPreloadConfig(ENABLE); /* 预先装载使能 */ /* TIM4是自动重载的8位定时器,计数方向为递增,当递增到0时可以产生定时器中断,并自动重装定时器初值 */ /* 计算期望的计数时钟个数, 由于TIM4是8位的定时器,因此如果该值大于255,则需要进行分频 */ uiCount = uiSysClkFreq / (1000 / SYSTICK_PERIOD); if (uiCount <= (1u << 8)) { TIM4_TimeBaseInit(TIM4_PRESCALER_1, uiCount); /* 设置预分频和定时器重载 */ } else if (uiCount <= (1u << 9)) { TIM4_TimeBaseInit(TIM4_PRESCALER_2, uiCount / 2); /* 设置预分频和定时器重载 */ } else if (uiCount <= (1u << 10)) { TIM4_TimeBaseInit(TIM4_PRESCALER_4, uiCount / 4); /* 设置预分频和定时器重载 */ } else if (uiCount <= (1u << 11)) { TIM4_TimeBaseInit(TIM4_PRESCALER_8, uiCount / 8); /* 设置预分频和定时器重载 */ } else if (uiCount <= (1u << 12)) { TIM4_TimeBaseInit(TIM4_PRESCALER_16, uiCount / 16); /* 设置预分频和定时器重载 */ } else if (uiCount <= (1u << 13)) { TIM4_TimeBaseInit(TIM4_PRESCALER_32, uiCount / 32); /* 设置预分频和定时器重载 */ } else if (uiCount < (1u << 14)) { TIM4_TimeBaseInit(TIM4_PRESCALER_64, uiCount / 64); /* 设置预分频和定时器重载 */ } else if (uiCount < (1u << 15)) { TIM4_TimeBaseInit(TIM4_PRESCALER_128, uiCount / 128); /* 设置预分频和定时器重载 */ } else { while (1); /* 异常,死机等待排错 */ } /* Clear TIM4 update flag */ TIM4_ClearFlag(TIM4_FLAG_UPDATE); TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); /* 使能TIM4中断 */ //TIM4_UpdateDisableConfig(ENABLE); /* 使能TIM4自动溢出事件 */ TIM4_Cmd(ENABLE); /* 使能TIM4 */ #endif }
void setFrameCheck(Mod_Timer_Check_TypeDef act) { if (act == MOD_TIMER_START) { TIM4_Cmd(DISABLE); //TIM4_UpdateDisableConfig(ENABLE); TIM4_SetCounter(0); //TIM4_ClearITPendingBit(TIM4_IT_UPDATE); //TIM4_ClearFlag(TIM4_FLAG_UPDATE); //TIM4_UpdateDisableConfig(DISABLE); TIM4_Cmd(ENABLE); modInts.frame_int_en = TRUE; } else if (act == MOD_TIMER_PAUSE) { TIM4_Cmd(DISABLE); modInts.frame_int_en = FALSE; } else if (act == MOD_TIMER_RESUM) { TIM4_Cmd(ENABLE); modInts.frame_int_en = TRUE; } else if (act == MOD_TIMER_STOP) { TIM4_Cmd(DISABLE); modInts.frame_int_en = FALSE; } }
/** * @brief Delay x sec * @param Seconds : number of seconds to delay * @retval None. * Note : TIM4 is configured for a system clock = 2MHz */ void Delay_Seconds(uint8_t Seconds) { uint8_t i = 0; /* Enable TIM4 Clock */ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); /* Configure TIM4 to generate an update event each 1 s */ TIM4_TimeBaseInit(TIM4_Prescaler_16384, 123); /* Enable TIM4 */ TIM4_Cmd(ENABLE); /* Clear the Flag */ TIM4_ClearFlag(TIM4_FLAG_Update); for (i = 0; i < Seconds; i++) { /* Wait 1 sec */ while ( TIM4_GetFlagStatus(TIM4_FLAG_Update) == RESET ) {} /* Clear the Flag */ TIM4_ClearFlag(TIM4_FLAG_Update); } /* Disable TIM4 */ TIM4_Cmd(DISABLE); /* Disable TIM4 Clock */ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, DISABLE); }
void MOD_TIM_Config(Mod_Master_Frame_TypeDef* aFrame) { TIM2_DeInit(); TIM2_TimeBaseInit(0x0F, 1500); //2ms comm response time TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_REGULAR); TIM2_GenerateEvent(TIM2_EVENTSOURCE_UPDATE); TIM2_Cmd(DISABLE); TIM2_ClearITPendingBit(TIM2_IT_UPDATE); TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE); TIM2_ClearFlag(TIM2_FLAG_UPDATE); TIM4_DeInit(); TIM4_TimeBaseInit(0x07, 126); //128 * 126 = 16128 = 2ms (8MHz), frame check TIM4_UpdateRequestConfig(TIM4_UPDATESOURCE_REGULAR); TIM4_GenerateEvent(TIM4_EVENTSOURCE_UPDATE); TIM4_Cmd(DISABLE); TIM4_ClearITPendingBit(TIM4_IT_UPDATE); TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); TIM4_ClearFlag(TIM4_FLAG_UPDATE); }
void resetTim4For500us(void) { TIM4_DeInit(); TIM4_TimeBaseInit(TIM4_PRESCALER_32,250); TIM4_ITConfig(TIM4_IT_UPDATE,ENABLE); TIM4_ARRPreloadConfig(ENABLE); TIM4_Cmd(ENABLE); }
void T4_Init(void) { TIM4_ARRPreloadConfig(1); TIM4_PrescalerConfig(TIM4_PRESCALER_32,TIM4_PSCRELOADMODE_IMMEDIATE); //128分频 TIM4_SetAutoreload((F_CPU/32/OS_TICKS_PER_SEC)); TIM4_Cmd(1); TIM4_ITConfig(TIM4_IT_UPDATE,1); }
void CAN_Node_Clock_init(void) { /* Configure TIM4 to run every 2 ms at 8 MHz clock. */ TIM4_TimeBaseInit(TIM4_PRESCALER_128, Tim4Period); TIM4_ClearFlag(TIM4_FLAG_UPDATE); //TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); TIM4_Cmd(ENABLE); }
void Init_Timer4(void) { //1ms中断一次 TIM4_TimeBaseInit(TIM4_PRESCALER_128, 124); /* Clear TIM4 update flag */ TIM4_ClearFlag(TIM4_FLAG_UPDATE); /* Enable update interrupt */ TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); TIM4_Cmd(ENABLE); }
/** * @brief Wait 1 sec for LSE stabilisation . * @param None. * @retval None. * Note : TIM4 is configured for a system clock = 2MHz */ void LSE_StabTime(void) { CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); /* Configure TIM4 to generate an update event each 1 s */ TIM4_TimeBaseInit(TIM4_Prescaler_16384, 123); /* Enable TIM4 */ TIM4_Cmd(ENABLE); /* Wait 1 sec */ while ( TIM4_GetFlagStatus(TIM4_FLAG_Update) == RESET ); TIM4_ClearFlag(TIM4_FLAG_Update); /* Disable TIM4 */ TIM4_Cmd(DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, DISABLE); }
/** * @brief Configure TIM4 to generate a software COM event each 180 ms * @param None * @retval None */ static void TIM4_Config(void) { /* TIM4 Peripheral Configuration */ /* Time Base configuration */ TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0xFF); /*TIM4 counter enable */ TIM4_Cmd(ENABLE); TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); }
void TIM4_Init(void) { TIM4_DeInit(); // 24000000/128/1000 = 187.5 /* Time base configuration */ TIM4_TimeBaseInit(TIM4_PRESCALER_128, FMASTER/128/1000); // 1ms中断 TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); TIM4_Cmd(ENABLE); }
void dev_vtimerInit(void) { TIM4_DeInit(); /* Time base configuration */ TIM4_TimeBaseInit(TIM4_PRESCALER_128,ARRVALUE); // Setting for 1ms Delta time //ITC_SetSoftwarePriority(ITC_IRQ_TIM4_OVF, ITC_PRIORITYLEVEL_1); ITC->ISPR6 |= 0xC0; ITC->ISPR6 &= 0x7F; TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); enableInterrupts(); /* Enable TIM4 */ TIM4_Cmd(ENABLE); }
/** * @brief Configure TIM4 to generate an update interrupt each 1ms * @param None * @retval None */ void TIM4_Config(void) { /* TIM4 configuration: - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter clock used is 16 MHz / 128 = 125 000 Hz - With 125 000 Hz we can generate time base: max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms - In this example we need to generate a time base equal to 1 ms so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */ /* Time base configuration */ TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD); /* Clear TIM4 update flag */ TIM4_ClearFlag(TIM4_FLAG_UPDATE); /* Enable update interrupt */ TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); /* Enable TIM4 */ TIM4_Cmd(ENABLE); }
void Config() { //System clock at power up is HSI/8 = 16MHz/8 = 2MHz //CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); //set HSI/4 = 4MHz SysClk to Core and Memory, minimum clock = 125KHz for CLK_SYSCLKDiv_128 //PWR_PVDCmd(ENABLE); //Power voltage detector and brownout Reset unit supply current 2,6uA //PWR_PVDLevelConfig(PWR_PVDLevel_2V26); //set Programmable voltage detector threshold to 2,26V //PWR_GetFlagStatus(PWR_FLAG_PVDOF); //checks whether the specified PWR flag is set or not //Configure external interrupts - BTN1 and BTN2 presses //EXTI_SetPinSensitivity(EXTI_Pin_2, EXTI_Trigger_Falling_Low); //EXTI_SetPinSensitivity(EXTI_Pin_4, EXTI_Trigger_Falling_Low); //EXTI_SelectPort(EXTI_Port_B); //EXTI_SetHalfPortSelection(EXTI_HalfPort_B_MSB, ENABLE); // Timer 4 Configuration CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); // 8bit: used for system periodic flags and timeouts TIM4_TimeBaseInit(TIM4_Prescaler_16, 125); // Overflow at 1ms, 2MHz/16/125 = 1000 TIM4_ITConfig(TIM4_IT_Update, ENABLE); TIM4_Cmd(ENABLE); // Timer 2 Configuration CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE); // 16bit: used for runtime measurement TIM2_TimeBaseInit(TIM2_Prescaler_2, TIM2_CounterMode_Up, 0xFFFF); // timebase of 1us, 2MHz/2=1M TIM2_Cmd(ENABLE); // Timer3 Configuration CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE); TIM3_TimeBaseInit(TIM3_Prescaler_2, TIM3_CounterMode_Up, 0xFFFF); // 1us timebase @ 2MHz system clock TIM3_ICInit(TIM3_Channel_2, TIM3_ICPolarity_Falling, TIM3_ICSelection_IndirectTI, TIM3_ICPSC_Div1, 0); TIM3_ICInit(TIM3_Channel_1, TIM3_ICPolarity_Rising, TIM3_ICSelection_DirectTI, TIM3_ICPSC_Div1, 0); TIM3_SelectInputTrigger(TIM3_TRGSelection_TI1FP1); TIM3_SelectSlaveMode(TIM3_SlaveMode_Reset); // Reset timer on selected trigger signal TIM3_ITConfig(TIM3_IT_CC1, ENABLE); TIM3_ITConfig(TIM3_IT_CC2, ENABLE); TIM3_Cmd(ENABLE); /* Output PP High - bicolor LED to GND */ GPIO_Init(LED_PORT, LED_PIN1, GPIO_Mode_Out_PP_Low_Slow); GPIO_Init(LED_PORT, LED_PIN2, GPIO_Mode_Out_PP_Low_Slow); /* Input pull up no IT - onboard Button, external pull-up */ GPIO_Init(BTN1_PORT, BTN1_PIN, GPIO_Mode_In_FL_No_IT); /* RFM85W pins */ GPIO_Init(RFM_DATA_PORT, RFM_DATA_PIN, GPIO_Mode_In_FL_No_IT); /* DEBUG pin */ GPIO_Init(DEBUG_PIN_PORT, DEBUG_PIN_PIN, GPIO_Mode_Out_PP_Low_Fast); /* H bridge outputs */ GPIO_Init(PMOS_L_PORT, PMOS_L_PIN, GPIO_Mode_Out_PP_High_Fast); GPIO_Init(PMOS_R_PORT, PMOS_R_PIN, GPIO_Mode_Out_PP_High_Fast); GPIO_Init(NMOS_L_PORT, NMOS_L_PIN, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(NMOS_R_PORT, NMOS_R_PIN, GPIO_Mode_Out_PP_Low_Fast); }
/** * @brief Main program. * @param None * @retval None */ void main() { /* TIM4 & I2C clock Enable*/ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE); #ifdef FAST_I2C_MODE /* system_clock / 1 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); #else /* system_clock / 2 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2); #endif /* Initialize LEDs mounted on STM8L1526-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize push-buttons mounted on STM8L1526-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); /* Initialize TIM4 peripheral to generate an interrupt each 1ms */ TIM4_TimeBaseInit(TIM4_Prescaler_128, TIM4_PERIOD); /* Enable Update interrupt */ TIM4_ITConfig(TIM4_IT_Update, ENABLE); /* Initialize I2C peripheral */ I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); /* Set I2C IT software priority as highest */ #ifdef STM8L15X_MD ITC_SetSoftwarePriority(I2C1_IRQn, ITC_PriorityLevel_3); #elif defined (STM8L15X_HD) || defined (STM8L15X_MDP) ITC_SetSoftwarePriority(I2C1_SPI2_IRQn, ITC_PriorityLevel_3); #endif /* STM8L15X_MD */ enableInterrupts(); /* Enable TIM4 */ TIM4_Cmd(ENABLE); while (1) { switch (PressedButton) { case BUTTON_RIGHT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x01; ButtonPressed = 1; break; case BUTTON_LEFT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x02; ButtonPressed = 1; break; case BUTTON_UP: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x03; ButtonPressed = 1; break; case BUTTON_DOWN: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x04; ButtonPressed = 1; break; case BUTTON_SEL: PressedButton = NO_BUTTON; NumOfBytes = BUFFERSIZE; TxBuffer[0] = 0xAA; ButtonPressed = 1; for (i = 1; i < NumOfBytes; i++) { TxBuffer[i] = i; } break; default: break; } if (ButtonPressed == 1) { /* Enable Buffer and Event Interrupt*/ I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE); /* Generate the Start condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Data transfer is performed in the I2C interrupt routine */ /* Wait until end of data transfer */ while (NumOfBytes); while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); /* Make sure that the STOP bit is cleared by Hardware before CR2 write access */ while ((I2C1->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP); ButtonPressed = 0; } } }
void stopTim4(void) { TIM4_ITConfig(TIM4_IT_UPDATE,DISABLE); TIM4_Cmd(DISABLE); }
void Tim4_Stop(void) { TIM4_Cmd(DISABLE); }
void Tim4_Start(void) { TIM4_Cmd(ENABLE); }
void vMBM_Timers_Cmd( BOOL NewState ) { TIM4_Cmd( NewState ); }
void tim4_config(void) { TIM4_TimeBaseInit(TIM4_Prescaler_1024,0xFF); TIM4_ITConfig(TIM4_IT_Update,ENABLE); TIM4_Cmd(DISABLE); };