码迷,mamicode.com
首页 > 其他好文 > 详细

STM32学习2(GPIO EXTI SYSTICK)

时间:2016-06-30 16:07:05      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

  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)

标签:

原文地址:http://www.cnblogs.com/thorzhou/p/5630049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!