Select Page

NVIC SAMPLE

sw1_int.c

  1. /* sw1_int.c
  2.  *
  3.  * Runs on EK-TM4C123GXL LaunchPad
  4.  *
  5.  * GPIO PORTF interrupt that will toggle the on board Blue LED
  6.  * on rising edge whenever a user switch (SW1) is pressed.
  7.  *
  8.  */
  9. #include <stdint.h>
  10. #include "inc/tm4c123gh6pm.h"
  11. 
    
  12. void DisableInterrupts(void);
  13. void EnableInterrupts(void);
  14. void WaitForInterrupt(void);
  15. void GPIOPortF_Init(void);
  16. void GPIOPortF_Handler(void);
  17. 
    
  18. void GPIOPortF_Init(void)
  19. {
  20. 
    
  21.     SYSCTL_RCGC2_R |= 0x00000020;   /* 1) activate clock for Port F */
  22. 
    
  23.     GPIO_PORTF_LOCK_R = 0x4C4F434B; /* 2) unlock GPIO Port F */
  24.     GPIO_PORTF_CR_R = 0x1F;         /* allow changes to PF4-0 */
  25.     GPIO_PORTF_AMSEL_R = 0x00;      /* 3) disable analog on PF */
  26.     GPIO_PORTF_PCTL_R = 0x00000000; /* 4) PCTL GPIO on PF4-0 */
  27.     GPIO_PORTF_DIR_R = 0x0E;        /* 5) PF4,PF0 in, PF3-1 out */
  28.     GPIO_PORTF_AFSEL_R = 0x00;      /* 6) disable alt funct on PF7-0 */
  29.     GPIO_PORTF_PUR_R = 0x11;        /* enable pull-up on PF0 and PF4 */
  30.     GPIO_PORTF_DEN_R = 0x1F;        /* 7) enable digital I/O on PF4-0 */
  31. 
    
  32.     GPIO_PORTF_IS_R &= ~0x10;       /*  PF4 is edge-sensitive */
  33.     GPIO_PORTF_IBE_R &= ~0x10;      /*  PF4 is not both edges */
  34.     GPIO_PORTF_IEV_R &= ~0x10;      /*  PF4 falling edge event */
  35.     GPIO_PORTF_ICR_R = 0x10;        /*  Clear flag4 */
  36.     GPIO_PORTF_IM_R |= 0x10;        /*  arm interrupt on PF4 */
  37.     NVIC_PRI7_R = (NVIC_PRI7_R & 0xFF1FFFFF) | 0x00A00000; /*  priority 5 */
  38.     NVIC_EN0_R = 0x40000000;        /*  Enable interrupt 30 in NVIC */
  39. 
    
  40.     EnableInterrupts();             /* Enable global Interrupt flag (I) */
  41. }
  42. 
    
  43. void GPIOPortF_Handler(void)
  44. {
  45.     volatile int readback;
  46. 
    
  47.     GPIO_PORTF_ICR_R = 0x10;        /* clear PF4 int */
  48.     GPIO_PORTF_DATA_R ^= (1<<2);    /* toggle Blue LED */
  49.     readback = GPIO_PORTF_ICR_R;    /* a read to force clearing of interrupt flag */
  50.     readback = readback;            /* suppress compiler warning "unused variable" */
  51. }
  52. 
    
  53. int main(void)
  54. {
  55.     GPIOPortF_Init();               /* initialize GPIO Port F interrupt */
  56. 
    
  57.     while(1) {
  58.         WaitForInterrupt();
  59.     }
  60. }
  61. 
    
  62. /*********** DisableInterrupts ***************
  63. *
  64. * disable interrupts
  65. *
  66. * inputs:  none
  67. * outputs: none
  68. */
  69. 
    
  70. void DisableInterrupts(void)
  71. {
  72.     __asm ("    CPSID  I\n");
  73. }
  74. 
    
  75. /*********** EnableInterrupts ***************
  76. *
  77. * emable interrupts
  78. *
  79. * inputs:  none
  80. * outputs: none
  81. */
  82. 
    
  83. void EnableInterrupts(void)
  84. {
  85.     __asm  ("    CPSIE  I\n");
  86. }
  87. 
    
  88. /*********** WaitForInterrupt ************************
  89. *
  90. * go to low power mode while waiting for the next interrupt
  91. *
  92. * inputs:  none
  93. * outputs: none
  94. */
  95. 
    
  96. void WaitForInterrupt(void)
  97. {
  98.     __asm  ("    WFI\n");
  99. }