STM32库函数祥解 下载本文

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure);

EXTI_InitTypeDef structure

EXTI_InitTypeDef定义于文件“stm32f10x_exti.h”:

typedef struct { u32 EXTI_Line;

EXTIMode_TypeDef EXTI_Mode; EXTIrigger_TypeDef EXTI_Trigger; FunctionalState EXTI_LineCmd; }

EXTI_InitTypeDef;

EXTI_Line

EXTI_Line选择了待使能或者失能的外部线路。 EXTI_Linex:外部中断线x(0--18)

EXTI_Mode

EXTI_Mode:设置了被使能线路的模式。

EXTI_Mode_Event:设置EXTI线路为事件请求 EXTI_Mode_Interrupt:设置EXTI线路为中断请求

EXTI_Trigger

EXTI_Trigger:设置了被使能线路的触发边沿。

EXTI_Trigger_Falling:设置输入线路下降沿为中断请求 EXTI_Trigger_Rising:设置输入线路上升沿为中断请求

EXTI_Trigger_Rising_Falling:设置输入线路上升沿和下降沿为中断请求

EXTI_LineCmd

EXTI_LineCmd用来定义选中线路的新状态。它可以被设为ENABLE或者DISABLE。

3 函数EXTI_StructInit

功能描述:把EXTI_InitStruct中的每一个参数按缺省值填入 例:

EXTI_InitTypeDef EXTI_InitStructure; EXTI_StructInit(&EXTI_InitStructure);

EXTI_InitStruct缺省值

EXTI_Line: EXTI_LineNone

EXTI_Mode: EXTI_Mode_Interrupt EXTI_Trigger: EXTI_Trigger_Falling EXTI_LineCmd: DISABLE

4 函数EXTI_GenerateSWInterrupt

功能描述:产生一个软件中断

9

例:

EXTI_GenerateSWInterrupt(EXTI_Line6);

5 函数EXTI_GetFlagStatus

功能描述:检查指定的EXTI线路标志位设置与否 例:

FlagStatus EXTIStatus;

EXTIStatus = EXTI_GetFlagStatus(EXTI_Line8);

6 函数EXTI_ClearFlag

功能描述:清除EXTI线路挂起标志位 例:

EXTI_ClearFlag(EXTI_Line2);

7 函数EXTI_GetITStatus

功能描述:检查指定的EXTI线路触发请求发生与否 例:

ITStatus EXTIStatus;

EXTIStatus = EXTI_GetITStatus(EXTI_Line8);

8 函数EXTI_ClearITPendingBit

功能描述:清除EXTI线路挂起位 例:

EXTI_ClearITpendingBit(EXTI_Line2);

三、通用定时器(TIM)

TIM寄存器结构

TIM_TypeDeff,在文件“stm32f10x_map.h”中定义如下:

typedef struct {

vu16 CR1;

u16 RESERVED0; vu16 CR2;

u16 RESERVED1; vu16 SMCR;

u16 RESERVED2; vu16 DIER;

u16 RESERVED3;

10

vu16 SR;

u16 RESERVED4; vu16 EGR;

u16 RESERVED5; vu16 CCMR1; u16 RESERVED6; vu16 CCMR2; u16 RESERVED7; vu16 CCER;

u16 RESERVED8; vu16 CNT;

u16 RESERVED9; vu16 PSC;

u16 RESERVED10; vu16 ARR;

u16 RESERVED11[3]; vu16 CCR1;

u16 RESERVED12; vu16 CCR2;

u16 RESERVED13; vu16 CCR3;

u16 RESERVED14; vu16 CCR4;

u16 RESERVED15[3]; vu16 DCR;

u16 RESERVED16; vu16 DMAR;

u16 RESERVED17; }

TIM_TypeDef;

TIM库函数

1 函数TIM_DeInit

功能描述:将外设TIMx寄存器重设为缺省值 例:

TIM_DeInit(TIM2);

2 函数TIM_TimeBaseInit

功能描述:根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 例:

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_Prescaler = 0xF;

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

11

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);

TIM_TimeBaseInitTypeDef structure

TIM_TimeBaseInitTypeDef定义于文件“stm32f10x_tim.h”:

typedef struct {

u16 TIM_Period; u16 TIM_Prescaler; u8 TIM_ClockDivision; u16 TIM_CounterMode; } TIM_TimeBaseInitTypeDef;

TIM_Period

TIM_Period设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x0000和0xFFFF之间。

TIM_Prescaler

TIM_Prescaler设置了用来作为TIMx时钟频率除数的预分频值。它的取值必须在0x0000和0xFFFF之间。

TIM_ClockDivision

TIM_ClockDivision设置了时钟分割。 TIM_CKD_DIV1:TDTS = Tck_tim TIM_CKD_DIV2:TDTS = 2Tck_tim TIM_CKD_DIV4:TDTS = 4Tck_tim

TIM_CounterMode

TIM_CounterMode选择了计数器模式。 TIM_CounterMode_Up:TIM向上计数模式 TIM_CounterMode_Down:TIM向下计数模式

TIM_CounterMode_CenterAligned1:TIM中央对齐模式1计数模式 TIM_CounterMode_CenterAligned2:TIM中央对齐模式2计数模式 TIM_CounterMode_CenterAligned3:TIM中央对齐模式3计数模式

3 函数TIM_OCInit

功能描述:根据TIM_OCInitStruct中指定的参数初始化外设TIMx 例:

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 0x3FFF;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM2, & TIM_OCInitStructure);

TIM_OCInitStruct:

12