您的位置:首页 > 其它

STM32之系统中断处理

2017-03-31 14:41 106 查看
分类: C/C++














点击(此处)折叠或打开

/*******************************************************************************

* Function Name : SysTickHandler

* Description : This function handles SysTick Handler.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void SysTickHandler(void)

{

__SVC();

/* Toggle PC.04 pin */ LED4

GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_4)));

}

/*******************************************************************************

* Function Name : SVCHandler

* Description : This function handles SVCall exception.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void SVCHandler(void)

{

/* Set the PSV system handler pending bit */

NVIC_SetSystemHandlerPendingBit(SystemHandler_PSV);

/* Toggle PC.05 pin */ LED3

GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_5)));

}

/*******************************************************************************

* Function Name : PendSVC

* Description : This function handles PendSVC exception.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void PendSVC(void)

{

/* Set the NMI system handler pending bit */

NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI);

/* Toggle PC.06 pin */ LED2

GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));

}

/*******************************************************************************

* Function Name : NMIException

* Description : This function handles NMI exception.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void NMIException(void)

{

/* Toggle PC.07 pin */  LED1

GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));

}

本例展示了如何运用NVIC和系统Handler(system handler):
各个system handlers拥有如下的强制优先级(preemption priority):



   - NMI preemption priority    -2   固定优先级
   - PSV preemption priority     0   可设置
   - SVCall preemption priority  1   可设置
   - SysTick preemption priority 2   可设置

首先把Systick定时器设定为每当其计数器为零,这里是1s产生一个Systick中断。

点击(此处)折叠或打开

/* SysTick interrupt each 1 Hz with Counter clock equal to 72MHz/8 = 9MHz */

SysTick_SetReload(9000000);

/* Enable the SysTick Interrupt */

SysTick_ITConfig(ENABLE);

/* Enable the SysTick Counter */

SysTick_CounterCmd(SysTick_Counter_Enable);

在Systick handler routine中,设置与PC.04相连的LED4以1秒为周期闪耀。随后执行_SVC(System Service Call)指令。这个指令在cortexm3_macro.h中



执行_SVC()会激活SVCall handler来打断当前的指令流。在SVCall handler routine中,设置与PC.05相连的LED3闪耀,同时设置PSV handler的pending比特。由于PSV(Pendable request for system service)的优先级更高,因此他又会打断SVCall
handler。在PSV handler routine中,设置与PC.06相连的LED2闪耀,并设置NMI(Non maskable interrupt)pending比特,进一步由NMI handler打断当前handler。
最后,在NMI handler中设置与PC.07相连的LED1闪耀。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: