📄 main.c
字号:
/*----------------------------------------------------------------------------
例子使用TIM4,的通道3,通道4产生2个固定占空比的PWM脉冲,其中
通道 3(PB8)产生占空比50% 的PWM脉冲
通道 4(PB9)产生占空比25% 的PWM脉冲
本例最好使用示波器观察不同的占空比波形
*---------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#define SYS_GLOBALS
#include "include.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 2047;
u16 CCR2_Val = 1535;
u16 CCR3_Val = 1023;
u16 CCR4_Val = 511;
uint32 TIM_Period;
unsigned short Ccr = 0; // Capture , Compare value
int Dir = 1; // in- decrease CCR
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void PWM_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/*------------------------------------------------------------------------------
Timer4 Update Interrupt Handler
//考虑到效率问题,中断函数里面仍然直接操作寄存器
*------------------------------------------------------------------------------*/
void TIM4_IRQHandler(void)
{
if (TIM4->SR & (1 << 0))
{
// UIF set?
// lower limit reached ? move up
//判断占空比到最小
if (Ccr == 0)
Dir = 1;
// upper limit reached ? move down
//判断占空比到最大
if (Ccr == 100)
Dir = -1;
Ccr = Ccr + Dir;
TIM4->CCR3 = Ccr; // change ccr
TIM4->CCR4 = 100 - Ccr; // change ccr
TIM4->SR &= ~(1 << 0); // clear UIF flag
}
} // end TIM4_IRQHandler
void delay(uint32 time)
{uint32 i, j;
for(i=0; i<time; i++)
{
for(j=0; j<14000; j++);
}
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();//配置系统时钟
GPIO_Configuration();//配置GPIO
/* NVIC configuration */
NVIC_Configuration();//配置中断
PWM_Configuration();
while (1)
{
}
}
//PWM初始化
void PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOB clock enable */
//GPIOB使用的RCC时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
//配置使用的GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP;//;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 100-1;//1000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =7200-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM4, ENABLE); //使能TIMx在ARR上的预装载寄存器
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE); //使能TIMx外设
//TIM4->DIER = 0x0001; // enable interrupt
//NVIC->ISER[0] |= (1 << (TIM4_IRQChannel & 0x1F)); // enable interrupt
//使能更新中断
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
//配置中断
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);//使能外部时钟
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部时钟就绪
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer 启用预取缓冲器 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state FLASH设置2个等待周期*/
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK 设置系统设置*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK PCLK2时钟=主时钟*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 PCLK1时钟为主时钟1/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz 设置时钟为72M*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能PLL*/
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待PLL工作稳定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 选择PLL做为系统时钟源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 准备就绪,开始干活*/
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO_LED clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIO时钟
//LED_init-------------------------------------------------------
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_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure); //设置GPIO为输出
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//#ifdef VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //设置中断向量在RAM中
//#elif defined(VECT_TAB_FLASH_IAP)
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量在FLASH中
#endif
}
#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\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -