#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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
", file, line) */
* STM32定时器学习 *
* 通过TIM1产生四路不同频率不同占空比的PWM波 *
* 10KHz 15KHz 18KHz 24KHz *
* CH1=40%;CH2=30%;CH3=10%;CH4=50% *
*****************************************************************/
/************************头文件**********************************/
#include <stm32f10x_lib.h>
/************************宏定义**********************************/
#define Pulse1 720
#define Pulse2 360
#define Pulse3 100
#define Pulse4 375
#define Compare1 1080
#define Compare2 840
#define Compare3 900
#define Compare4 375
/************************声明变量********************************/
/************************函数声明********************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM_Configuration(void);
/****************************************************************
* *
* 主函数 *
* *
*****************************************************************/
int main()
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM_Configuration();
while(1)
{
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08)
{
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_InternalClockConfig(TIM1);
TIM_BaseInitStructure.TIM_Prescaler=4; //设置TIM时钟频率除数的预分频值
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//选择计数器模式
TIM_BaseInitStructure.TIM_Period=0xffff;//设置下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_BaseInitStructure.TIM_ClockDivision=0;//设置时钟分割
TIM_BaseInitStructure.TIM_RepetitionCounter=0;//设置周期计数器值
TIM_TimeBaseInit(TIM1,&TIM_BaseInitStructure);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4);
TIM_ITConfig(TIM1,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
TIM_OCStructInit(&TIM_OCInitStructure);
//通道1
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//选择定时器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_Pulse=Pulse1;//设置了待装入捕获比较器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//选择空闲状态下得非工作状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;//选择互补空闲状态下得非工作状态
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//通道2
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//选择定时器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_Pulse=Pulse2;//设置了待装入捕获比较器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//选择空闲状态下得非工作状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;//选择互补空闲状态下得非工作状态
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
//通道3
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//选择定时器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_Pulse=Pulse3;//设置了待装入捕获比较器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//选择空闲状态下得非工作状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;//选择互补空闲状态下得非工作状态
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
//通道4
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//选择定时器模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_Pulse=Pulse4;//设置了待装入捕获比较器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//选择空闲状态下得非工作状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;//选择互补空闲状态下得非工作状态
TIM_OC4Init(TIM1,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
TIM_SetCompare1(TIM1,Pulse1+Compare1);
}
if(TIM_GetITStatus(TIM1,TIM_IT_CC2)!=RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC2);
TIM_SetCompare1(TIM1,Pulse2+Compare2);
}
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC3);
TIM_SetCompare1(TIM1,Pulse3+Compare3);
}
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC4);
TIM_SetCompare1(TIM1,Pulse4+Compare4);
}
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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 ", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
一周热门 更多>