STM32之系统中断处理

分类: C/C++

点击(此处)折叠或打开

  1. /*******************************************************************************
  2. * Function Name : SysTickHandler
  3. * Description : This function handles SysTick Handler.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SysTickHandler(void)
  9. {
  10. __SVC();
  11. /* Toggle PC.04 pin */ LED4
  12. GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_4)));
  13. }
  14. /*******************************************************************************
  15. * Function Name : SVCHandler
  16. * Description : This function handles SVCall exception.
  17. * Input : None
  18. * Output : None
  19. * Return : None
  20. *******************************************************************************/
  21. void SVCHandler(void)
  22. {
  23. /* Set the PSV system handler pending bit */
  24. NVIC_SetSystemHandlerPendingBit(SystemHandler_PSV);
  25. /* Toggle PC.05 pin */ LED3
  26. GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_5)));
  27. }
  28. /*******************************************************************************
  29. * Function Name : PendSVC
  30. * Description : This function handles PendSVC exception.
  31. * Input : None
  32. * Output : None
  33. * Return : None
  34. *******************************************************************************/
  35. void PendSVC(void)
  36. {
  37. /* Set the NMI system handler pending bit */
  38. NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI);
  39. /* Toggle PC.06 pin */ LED2
  40. GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
  41. }
  42. /*******************************************************************************
  43. * Function Name : NMIException
  44. * Description : This function handles NMI exception.
  45. * Input : None
  46. * Output : None
  47. * Return : None
  48. *******************************************************************************/
  49. void NMIException(void)
  50. {
  51. /* Toggle PC.07 pin */  LED1
  52. GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
  53. }
本例展示了如何运用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中断。

点击(此处)折叠或打开

  1. /* SysTick interrupt each 1 Hz with Counter clock equal to 72MHz/8 = 9MHz */
  2. SysTick_SetReload(9000000);
  3. /* Enable the SysTick Interrupt */
  4. SysTick_ITConfig(ENABLE);
  5. /* Enable the SysTick Counter */
  6. 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闪耀。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值