微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 定时器2+串口

STM32 定时器2+串口

时间:11-09 来源:互联网 点击:

LED1_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_8)
//置一
#define LED1_ON GPIO_SetBits(GPIOA, GPIO_Pin_8)

#define LED2_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_7)
#define LED2_ON GPIO_SetBits(GPIOA, GPIO_Pin_7)

#define LED3_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_7)
#define LED3_ON GPIO_SetBits(GPIOC, GPIO_Pin_7)

#define LED4_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_5)
#define LED4_ON GPIO_SetBits(GPIOC, GPIO_Pin_5)

#define LED5_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define LED5_ON GPIO_SetBits(GPIOB, GPIO_Pin_9)

#define LED6_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define LED6_ON GPIO_SetBits(GPIOB, GPIO_Pin_8)

#define LED7_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define LED7_ON GPIO_SetBits(GPIOB, GPIO_Pin_5)

#define LED8_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_0)
#define LED8_ON GPIO_SetBits(GPIOB, GPIO_Pin_0)

//串口
extern void USART1_Putc(u8 c);
extern void USART1_Puts(char * str);
extern void USART2_Putc(u8 c);
extern void USART2_Puts(char * str);

extern u8 flag;
extern u32 count;
extern u8 Uart1_Get_Flag;
extern u8 Uart1_Get_Data;

#endif

TIM.c

#include "STM32Lib\\stm32f10x.h"

void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 4000;//捕获比较值1
u16 CCR2_Val = 2000;//捕获比较值2
u16 CCR3_Val = 1000;//捕获比较值3
u16 CCR4_Val = 500; //捕获比较值4

/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/* 基础设置*/
TIM_TimeBaseStructure.TIM_Period = 8000; //计数值
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //预分频,此值+1为分频的除数,分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
//向上计数,从0计数到自动加载值,然后重新从0开始计数,并产生一个计数溢出事件
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* 比较通道1*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; //输出比较非主动模式
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性为正

TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //禁止OC1重装载,其实可以省掉这句,因为默认是4路都不重装的.

/*比较通道2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

/* 比较通道3 */
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);

/* 比较通道4 */
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);

/*使能预装载*/
TIM_ARRPreloadConfig(TIM2, ENABLE);
/*预先清除所有中断位*/
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update);

/* 4个通道和溢出都配置中断*/
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update, ENABLE);

/* 允许TIM2开始计数 */
TIM_Cmd(TIM2, ENABLE);
}

NVIC.c

#include "STM32Lib\\stm32f10x.h"

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Timer2中断*/
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/*UART1*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

GPIO.c

#include "STM32Lib\\stm32f10x.h"
#include "hal.h"
/*******************************************************************************
* Function Name : GPIO_Configuration
* 设置PD3,PD4,PD5,PD6为键盘输入
* 设置PB0,5,8,9; PC5,7; PD7 ;PA8 为输出LED灯
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top