/**重映射 tim3 使得PC7 PC8輸出Pwm波
******************************************************************************
* @file Project/Template/main.c
* @author MCD Application Team
* @version V3.0.0
* @date 04/06/2009
* @brief Main program body
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*
COPYRIGHT 2009 STMicroelectronics
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "misc.h"
voidGPIO_AFIODeInit(void);//這個(gè)函數(shù)不能落下,否則不能成功
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//配置PWM需要使用的結(jié)構(gòu)體聲名
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR3_Val =250;
u16 CCR4_Val =250;
voidGPIO_Config(void);
voidTimer_Config(void);
/**
* @brief Delay program.
* @param None
* @retval : None
*/
voidDelay (u32 nCount)
{
for(; nCount != 0; nCount--);
}
/**
* @brief Main program.
* @param None
* @retval : None
*/
intmain(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
GPIO_Config();
Timer_Config();
/* Infinite loop */
while(1) {
}
}
/**
* @brief GPIO_Config program.
* @param None
* @retval : None
*/
voidGPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
GPIO_AFIODeInit();//剛開(kāi)始沒(méi)有這個(gè)語(yǔ)句,所以一直沒(méi)有重映射成功
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE);//這個(gè)就是重映射功能函數(shù)
/*GPIOA Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//這里也要注意
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//這四句是為了校驗(yàn)究竟PB0,PB1是否還有PWM輸出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
voidTimer_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 999;//在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0;// 設(shè)置了用來(lái)作為 TIMx 時(shí)鐘頻率除數(shù)的預(yù)分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCMode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;//設(shè)置了待裝入捕獲比較寄存器的值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity :TIM 輸出比較極性高
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能或者失能 TIM3 在 CCR3 上的預(yù)裝載寄存器
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);//開(kāi)啟時(shí)鐘,別落下
}
/**
* @brief EXTI_Config Program.
* @param None
* @retval : None
*/
/**
* @brief Configures the nested vectored interrupt controller.
* @param None
* @retval : None
*/
#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
*/
voidassert_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 %drn", file, line) */
/* Infinite loop */
while(1)
{
}
}
#endif
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
評(píng)論