#include "stm32f10x_conf.h"
#define LED0_OFF GPIO_SetBits(GPIOA,GPIO_Pin_8)// LED0关
#define LED0_ON GPIO_ResetBits(GPIOA,GPIO_Pin_8)//LED0开
u16 i=0;
void GPIO_CFG(void);
void RCC_CFG(void);
void TIM_CFG(void);void NVIC_Configuration(void);
int main (void)
{
RCC_CFG();
GPIO_CFG();
NVIC_Configuration(); //配置中断向量表。决定将程序下载到RAM中还是FLASH中。
TIM_CFG();
//TIM_Cmd(TIM3,ENABLE);
//TIM_CtrlPWMOutputs(TIM3,ENABLE);
LED0_OFF;
while(1)
{
}
//while(1);
}
void RCC_CFG(void)
{
//初始化时钟
//SystemInit(); //取消好像也可以
SystemInit();//频率设定有system_stm32f10x.c文件中的宏定义 开放某个宏 当调用SystemInit()时即可设置好频率
RCC_ClockSecuritySystemCmd(ENABLE);//使能或者失能时钟安全系统
// //----------使用外部RC晶振-----------
// RCC_DeInit(); //初始化为缺省值
// RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速时钟
// while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪
// // FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能预取指缓存
// // FLASH_SetLatency(FLASH_Latency_2); //设置FLASH存储器2延时周期
// RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB时钟等于系统时钟HCLK = SYSCLK
// RCC_PCLK2Config(RCC_HCLK_Div1); //高速AHB时钟(PCLK2)PCLK2 = HCLK
// RCC_PCLK1Config(RCC_HCLK_Div2); //低速AHB时钟(PCLK1)PCLK1 = HCLK/2
// RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 8MHZ * 9 =72MHZ
// RCC_PLLCmd(ENABLE); //Enable PLLCLK
// while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //等待 PLLCLK 准备好
// RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择 PLL 为系统时钟
// while(RCC_GetSYSCLKSource()!=0x08); //等待系统使用PLL为系统时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void GPIO_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void TIM_CFG(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period=71; //TIM_Period=71 TIM_Prescaler=0 周期1Mhz 72-1
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0x00;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); //使能或者失能指定的 TIM 中断
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
//
// TIM_OCInitStructure.TIM_Pulse=60; //PA.6
// TIM_OC1Init(TIM3,&TIM_OCInitStructure);
//
//
// TIM_OCInitStructure.TIM_Pulse=35; //PA.7
// TIM_OC2Init(TIM3,&TIM_OCInitStructure);
//
// TIM_OCInitStructure.TIM_Pulse=6; //PB.0
// TIM_OC3Init(TIM3,&TIM_OCInitStructure);
//
TIM_OCInitStructure.TIM_Pulse=36; //PB.1
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
}
//配置中断向量表。决定将程序下载到RAM中还是FLASH中。
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //定义NVIC_InitTypeDef 的参数结构
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);//设定中断向量表基址0x08000000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);// 先占优先级0位 从优先级4位
//使能TIM3中断
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x0F; //先占优先级0位,从优先级4位
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x0F; //
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//TIM3中断处理函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //检查指定的 TIM 中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
i++;
if(i==10)
{
LED0_ON;
}
if(i==20)
{
LED0_OFF;
i=0;
}
}
}
#define LED0_OFF GPIO_SetBits(GPIOA,GPIO_Pin_8)// LED0关
#define LED0_ON GPIO_ResetBits(GPIOA,GPIO_Pin_8)//LED0开
u16 i=0;
void GPIO_CFG(void);
void RCC_CFG(void);
void TIM_CFG(void);void NVIC_Configuration(void);
int main (void)
{
RCC_CFG();
GPIO_CFG();
NVIC_Configuration(); //配置中断向量表。决定将程序下载到RAM中还是FLASH中。
TIM_CFG();
//TIM_Cmd(TIM3,ENABLE);
//TIM_CtrlPWMOutputs(TIM3,ENABLE);
LED0_OFF;
while(1)
{
}
//while(1);
}
void RCC_CFG(void)
{
//初始化时钟
//SystemInit(); //取消好像也可以
SystemInit();//频率设定有system_stm32f10x.c文件中的宏定义 开放某个宏 当调用SystemInit()时即可设置好频率
RCC_ClockSecuritySystemCmd(ENABLE);//使能或者失能时钟安全系统
// //----------使用外部RC晶振-----------
// RCC_DeInit(); //初始化为缺省值
// RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速时钟
// while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪
// // FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能预取指缓存
// // FLASH_SetLatency(FLASH_Latency_2); //设置FLASH存储器2延时周期
// RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB时钟等于系统时钟HCLK = SYSCLK
// RCC_PCLK2Config(RCC_HCLK_Div1); //高速AHB时钟(PCLK2)PCLK2 = HCLK
// RCC_PCLK1Config(RCC_HCLK_Div2); //低速AHB时钟(PCLK1)PCLK1 = HCLK/2
// RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 8MHZ * 9 =72MHZ
// RCC_PLLCmd(ENABLE); //Enable PLLCLK
// while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //等待 PLLCLK 准备好
// RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择 PLL 为系统时钟
// while(RCC_GetSYSCLKSource()!=0x08); //等待系统使用PLL为系统时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void GPIO_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void TIM_CFG(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period=71; //TIM_Period=71 TIM_Prescaler=0 周期1Mhz 72-1
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0x00;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); //使能或者失能指定的 TIM 中断
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
//
// TIM_OCInitStructure.TIM_Pulse=60; //PA.6
// TIM_OC1Init(TIM3,&TIM_OCInitStructure);
//
//
// TIM_OCInitStructure.TIM_Pulse=35; //PA.7
// TIM_OC2Init(TIM3,&TIM_OCInitStructure);
//
// TIM_OCInitStructure.TIM_Pulse=6; //PB.0
// TIM_OC3Init(TIM3,&TIM_OCInitStructure);
//
TIM_OCInitStructure.TIM_Pulse=36; //PB.1
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
}
//配置中断向量表。决定将程序下载到RAM中还是FLASH中。
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //定义NVIC_InitTypeDef 的参数结构
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);//设定中断向量表基址0x08000000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);// 先占优先级0位 从优先级4位
//使能TIM3中断
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x0F; //先占优先级0位,从优先级4位
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x0F; //
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//TIM3中断处理函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //检查指定的 TIM 中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
i++;
if(i==10)
{
LED0_ON;
}
if(i==20)
{
LED0_OFF;
i=0;
}
}
}