STM32学习2(GPIO EXTI SYSTICK)
Posted 我光吃饭不洗碗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32学习2(GPIO EXTI SYSTICK)相关的知识,希望对你有一定的参考价值。
GPIO的使用:
打开GPIO时钟根据手册寄存器映射看在哪根总线上,设一个GPIO_InitTypeDef结构体变量,根据实际给改结构体里面各项赋值,GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct),就初始化设置完了。
具体应用读写直接调用
/* GPIO Read and Write functions **********************************************/
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
EXTI外设上引脚电平变化中断:
先设置GPIO,记得pupd中设为浮空GPIO_PuPd_NOPULL;
EXTI_InitTypeDef EXTI_KEYSCAN_Struct;
/*enable syscfg clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
/*select pa0*/
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
/*Specifies the EXTI lines to be enabled or disabled.select EXTI0*/
EXTI_KEYSCAN_Struct.EXTI_Line = EXTI_Line0;
/*Specifies the new state of the selected EXTI lines.*/
EXTI_KEYSCAN_Struct.EXTI_LineCmd = ENABLE;
/*Specifies the mode for the EXTI lines.set mode to intterrupt*/
EXTI_KEYSCAN_Struct.EXTI_Mode = EXTI_Mode_Interrupt;
/*Specifies the trigger signal active edge for the EXTI lines.*/
EXTI_KEYSCAN_Struct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_KEYSCAN_Struct);
/*这部分设置中断优先级,这里我就随便写了一个,优先级组是0,抢占优先级是0,次优先级是1*/
NVIC_InitTypeDef NVIC_InitStruct;
/*Configures the priority grouping: pre-emption priority and subpriority.*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*!< Specifies the IRQ channel to be enabled or disabled.This parameter can be an enumerator of @ref IRQn_Type enumeration (For the complete STM32 Devices IRQ Channels
list, please refer to stm32f4xx.h file) */
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;//STM32F4XX Interrupt Number Definition
/*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled. This parameter can be set either to ENABLE or DISABLE */
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
/*!< Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table
A lower priority value indicates a higher priority */
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
/*!< Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table
A lower priority value indicates a higher priority */
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
然后就是中断函数了
void EXTI0_IRQHandler(void)//这名字不能变,不然就进到启动文件中中断弱定义的部分中去了。
{
if((EXTI_GetITStatus(EXTI_Line0))!=RESET)//看下是不是真的这个中断了
{
//该干嘛干嘛
}
EXTI_ClearITPendingBit(EXTI_Line0);//出中断之前清下标志位
}
SysTick:
这是在内核中的,core-m4通用;
只要调用这个函数配置 __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
void init_Systick(uint32_t time)
{
/*SystemCoreClock = 180mhz 1/time s interrupt*/
if(SysTick_Config(SystemCoreClock/time)) while(1);
}
void delay_ms(uint32_t time0)
{
DELAYTIME = time0;
while(DELAYTIME != 0);
}
中断有预先写好,往里面填就行了
void SysTick_Handler(void)
{
if(DELAYTIME!=0)
{
DELAYTIME--;
}
}
以上是关于STM32学习2(GPIO EXTI SYSTICK)的主要内容,如果未能解决你的问题,请参考以下文章
STM32 EXTI多少个?分别接到啥中断源,GPIO的EXTI有多少个?