芯片:STM32F103GCT6
原理图:
实现:PC6——闪烁,PC7——常亮
main.c
//头文件
#include "stm32f10x.h"
#include "user.h"
//全局变量
uint8_t g_1s_flag = 0;
uint8_t g_uart1_rx_done = 0;
uint32_t g_ms = 0;
uint32_t g_s = 0;
uint8_t g_uart1_rx_buff[256] = {0};
uint16_t Compare = 0;
void User_tick_timer_handler(void)
{
g_ms++;
if((g_ms%500) == 0)
{
if(PCin(13) == 1)
{
PCout(13) = 0;
}
else
{
PCout(13) = 1;
}
}
if((g_ms%1000) == 0)
{
g_s++;
g_1s_flag = 1;
}
}
/******************************************************************************
* 函数名称:main
* 功能概要:主函数
* 参数说明:无
* 函数返回:无
******************************************************************************/
int main(void)
{
Delay_init(); //定时器延时初始化
// Usart1_init(9600); //Uart1初始化
// NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
GPIO_Configuration(); //IO配置
// TIM2_init(999, 71); //TIM2初始化,arr = 定时us*[72/(psc+1)]-1,这里定时1000us
TIM3_init(); //TIM3初始化,arr = 定时us*[72/(psc+1)]-1,这里定时50us
// delay_ms(10);
GPIO_SetBits(GPIOC, GPIO_Pin_7 );
while(1)
{
GPIO_ResetBits(GPIOC, GPIO_Pin_7 );
}
}
user.c
/******************************************************************************
* Source code file of user.c
******************************************************************************/
#include "user.h"
/******************************************************************************
* 函数名称:GPIO_Configuration
* 功能概要:GPIO初始化
* 参数说明:无
* 函数返回:无
******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
//==============
// LED01 -> PC7 LED02 -> PC6
//==============
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/******************************************************************************
* 函数名称:TIM3_init
* 功能概要:定时器3初始化
* 参数说明:arr - 自动重装值
* psc - 时钟预分频数
* 函数返回:无
* 备注: 这里时钟选择为APB1的2倍,而APB1为36M
******************************************************************************/
void TIM3_init(void )
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period =1000-1;
TIM_TimeBaseStructure.TIM_Prescaler =7200-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
// TIM_ClearFlag(TIM3,TIM_FLAG_Update);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式1:占空比模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
/*中断优先级NVIC设置*/
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级1级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能IRQ通道
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //使能TIMx指定的中断
// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
// TIM_ARRPreloadConfig(TIM3, ENABLE);
GPIO_SetBits(GPIOC, GPIO_Pin_6); //LED初始化
TIM_Cmd(TIM3, ENABLE); //使能TIMx
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)))); //取反PB.1
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除TIMx更新中断标志
}
}
/******************************************************************************
* End of file
******************************************************************************/