class="markdown_views prism-atelier-sulphurpool-light">
STM32单片机PWM输出测试 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境:主机:XP开发环境:MDK4.23MCU:STM32F103CBT6 说明:使用内部8M晶振,倍频到64M供给TIM3定时器,PA6(通道1)上产生640K,50%方波
源代码:初始化时钟:void init_rcc(void){ RCC_DeInit(); RCC_HSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_LSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET); if(1) { FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable ) ; FLASH_SetLatency( FLASH_Latency_2 ) ; RCC_HCLKConfig( RCC_SYSCLK_Div1 ) ; RCC_PCLK2Config( RCC_HCLK_Div1 ) ; RCC_PCLK1Config( RCC_HCLK_Div2 ) ; RCC_ADCCLKConfig( RCC_PCLK2_Div8 ) ; RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_16) ; RCC_PLLCmd( ENABLE ) ; while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ) ; RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ) ; while( RCC_GetSYSCLKSource() != 0x08 ) ; }}
初始化IO口和定时器:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);TIM_DeInit(TIM3);TIM_InternalClockConfig(TIM3);timInitStruct.TIM_ClockDivision = TIM_CKD_DIV2; timInitStruct.TIM_Prescaler = 0; timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; timInitStruct.TIM_RepetitionCounter = 0;timInitStruct.TIM_Period = 100; TIM_TimeBaseInit(TIM3, &timInitStruct);TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = 50;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OC1Init(TIM3, &TIM_OCInitStructure);TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);TIM_ARRPreloadConfig(TIM3, ENABLE);TIM_Cmd(TIM3, ENABLE);
输出波形: 再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!
https://blog.csdn.net/jiangjunshow