|
- /*******************************************************************************
- STM32学习日志(12)---- TIM2 TimeBase
- 编译环境: EWARM V5.30
- 硬件环境: 南京万利 EK-STM32F
- 作者 : szlihongtao
- 时间 : 2010-07-05
- 说明 : 1. 在相应的引脚上可以观察到输出的方波波形
- // PC.06: STM32F103VBT6----pin63
- // PC.07: STM32F103VBT6----pin64
- // PC.08: STM32F103VBT6----pin65
- // PC.09: STM32F103VBT6----pin66
- 2. 输出信号的占空比为50%,不可以调整
- 3. 4路输出波形的频率不一样
- 4. 示波器实测通过
- 5. 输出33.027KHz的方波,对应常数为217,实际测量频率与理论上的计算是一致的
- *******************************************************************************/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- //******************************************************************************
- __IO uint16_t CCR1_Val = 49152;
- __IO uint16_t CCR2_Val = 32768;
- __IO uint16_t CCR3_Val = 16384;
- __IO uint16_t CCR4_Val = 8192;
- ErrorStatus HSEStartUpStatus;
- //******************************************************************************
- // 延时50ms
- //******************************************************************************
- static void delay(void)
- {
- INT32U i;
- static INT32U jjj=5240*50;
- for (i=0; i<jjj; i++);
- }
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 时钟设置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
- RCC_ITConfig 使能或者失能指定的RCC中断
- RCC_ClearFlag 清除RCC的复位标志位
- RCC_GetITStatus 检查指定的RCC中断发生与否
- RCC_ClearITPendingBit 清除RCC的中断待处理位
- */
- /* RCC system reset(for debug purpose) */
- // 时钟系统复位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 设置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能内部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 该函数将等待直到HSE就绪,或者在超时的情况下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 设置AHB时钟(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 设置低速AHB时钟(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 设置高速AHB时钟(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 设置ADC时钟(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 设置USB时钟(USBCLK)
- // USB时钟 = PLL时钟除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 设置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能内部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 设置RTC时钟(RTCCLK)
- // 选择HSE时钟频率除以128作为RTC时钟
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC时钟
- // RTC时钟的新状态
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 设置PLL时钟源及倍频系数
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 检查指定的RCC标志位设置与否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 设置系统时钟(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系统时钟的时钟源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外设时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- // 使能或者失能APB1外设时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 强制或者释放高速APB(APB2)外设复位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出复位状态
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 强制或者释放低速APB(APB1)外设复位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 退出复位状态
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,DISABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // tim2时钟使能
- // 强制或者释放后备域复位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能时钟安全系统
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // NVIC设置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- 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);
- }
- //******************************************************************************
- // GPIO设置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外设时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- //******************************************************************************
- static void Tim2_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* ---------------------------------------------------------------
- TIM2 Configuration: Output Compare Timing Mode:
- TIM2CLK = 72 MHz, Prescaler = 5
- TIM2 counter clock = 72/5 MHz= 14.4MHz
- 由于输出采用信号翻转的方法得到方波输出
- 所以相当于输入时钟再次2分频
- TIM2 counter clock' = TIM2 counter clock/2 = 7.2MHz
- CC1 update rate = TIM2 counter clock' / CCR1_Val = 146.48 Hz
- CC2 update rate = TIM2 counter clock' / CCR2_Val = 219.74 Hz
- CC3 update rate = TIM2 counter clock' / CCR3_Val = 439.48 Hz
- CC4 update rate = TIM2 counter clock' / CCR4_Val = 878.97 Hz
- --------------------------------------------------------------- */
- /* Prescaler configuration */
- TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 65535;
- TIM_TimeBaseStructure.TIM_Prescaler = 5-1;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; // 更改这个设定值好像没有什么用处,不知道为什么
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- /*
- TIM_TimeBaseInit,TIM_PrescalerConfig 这2个函数都会设定 TIMx->PSC 的值,以最后执行的函数为准
- */
- //------------------------------------------------------------------------------
- /* Output Compare Timing Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM2, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
- //------------------------------------------------------------------------------
- /* Output Compare Timing Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
- TIM_OC2Init(TIM2, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
- //------------------------------------------------------------------------------
- /* Output Compare Timing Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
- TIM_OC3Init(TIM2, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
- //------------------------------------------------------------------------------
- /* Output Compare Timing Mode configuration: Channel4 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
- TIM_OC4Init(TIM2, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
- //------------------------------------------------------------------------------
- /* TIM IT enable */
- TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
- /* TIM2 enable counter */
- TIM_Cmd(TIM2, ENABLE);
- }
- //******************************************************************************
- // LED5亮
- //******************************************************************************
- void Led_RW_ON(void)
- {
- GPIO_SetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // LED5灭
- //******************************************************************************
- void Led_RW_OFF(void)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- INT8U i;
- RCC_Configuration();
- GPIO_Configuration();
- Tim2_Configuration();
- NVIC_Configuration();
- delay();
- for (i=0;i<3;++i)
- {
- Led_RW_ON();
- delayms(200);
- Led_RW_OFF();
- delayms(200);
- }
- //------------------------------------------------------------------------------
- for (;;)
- {
- delayms(88);
- GPIOC->ODR ^= GPIO_Pin_5; // led4 toogle
- }
- }
- //******************************************************************************
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
- /*
- LED2---------PC7
- LED3---------PC6
- LED4---------PC5
- LED5---------PC4
- KEY2---------PD3
- KEY3---------PD4
- */
复制代码
|
|