Actions

EmSys

Difference between revisions of "TM4C123 Using SysTick with Interrupt"

From EdWiki

m
m
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
In the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad|previous]] article, we used SysTick in a blocking mode. In this article, we will use SysTick with interrupt.
 +
 
The SysTick interrupt can be used to initiate an action on a periodic basis. This action is performed internally at a fixed rate without external signal. For example, in a given application we can use SysTick to read a sensor every 200 msec. SysTick is used widely for an operating system so that the system software may interrupt the application software periodically (often 10 ms interval) to monitor and control the system operations.
 
The SysTick interrupt can be used to initiate an action on a periodic basis. This action is performed internally at a fixed rate without external signal. For example, in a given application we can use SysTick to read a sensor every 200 msec. SysTick is used widely for an operating system so that the system software may interrupt the application software periodically (often 10 ms interval) to monitor and control the system operations.
  
Line 12: Line 14:
 
  * The system clock is running at 16 MHz.
 
  * The system clock is running at 16 MHz.
 
  * 1sec/62.5ns = 16,000,000 for RELOAD register.
 
  * 1sec/62.5ns = 16,000,000 for RELOAD register.
  * In the interrupt handler, the red LED is toggled.
+
  * In the interrupt handler, the red LED is toggled.
 
*/
 
*/
  
Line 51: Line 53:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
The program uses the SysTick to toggle the red LED of PF1 every second. We need the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick Reload Value Register|RELOAD]] value of 16,000,000-1 since 1sec / 62.5nsec = 16,000,000. We assume the CPU clock is 16MHz (1/16MHz=62.5ns). The COUNT flag is raised every 16,000,000 clocks and an interrupt occurs. Then the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick Reload Value Register|RELOAD]] register is loaded with 16,000,000-1 automatically.
+
The above program uses the SysTick to toggle the red LED of PF1 every second. We need the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick Reload Value Register|RELOAD]] value of 16,000,000-1 since 1sec / 62.5nsec = 16,000,000. We assume the CPU clock is 16MHz (1/16MHz=62.5ns). The COUNT flag is raised every 16,000,000 clocks and an interrupt occurs. Then the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick Reload Value Register|RELOAD]] register is loaded with 16,000,000-1 automatically.
  
Notice the enabling of interrupt in SysTick Control register. Since SysTick is an internal interrupt, the COUNT flag is cleared automatically when the interrupt occurs. Also notice the SysTick is part of INT1-INT15 and not part of IRQ. For this reason we must use the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick control and Status register|CTLR]] register  to enable it.
+
Notice the enabling of interrupt in SysTick [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick control and Status register|Control register]]. Since SysTick is an internal interrupt, the COUNT flag is cleared automatically when the interrupt occurs. Also notice the SysTick is part of INT1-INT15 and not part of IRQ. For this reason we must use the [[EmSys:Using Systick Timer with TM4C123GH6PM Launchpad#Systick control and Status register|CTLR]] register  to enable it.

Revision as of 10:45, 22 January 2019

In the previous article, we used SysTick in a blocking mode. In this article, we will use SysTick with interrupt.

The SysTick interrupt can be used to initiate an action on a periodic basis. This action is performed internally at a fixed rate without external signal. For example, in a given application we can use SysTick to read a sensor every 200 msec. SysTick is used widely for an operating system so that the system software may interrupt the application software periodically (often 10 ms interval) to monitor and control the system operations.

If INTEN=1, when the COUNT flag is set, it causes an interrupt via the NVIC. INTEN is D1 of the STCTRL register. Examining interrupt vector table for ARM Cortex, we see the SysTick is the interrupt #15 assigned to the system itself.

  1. /* systick_int.c
  2.  *
  3.  * Toggle the red LED using the SysTick interrupt
  4.  *
  5.  * This program sets up the SysTick to interrupt at 1 Hz.
  6.  * The system clock is running at 16 MHz.
  7.  * 1sec/62.5ns = 16,000,000 for RELOAD register.
  8.  * In the interrupt handler, the red LED is toggled.
  9. */
  10.  
  11. #include <stdint.h>
  12. #include "inc/tm4c123gh6pm.h"
  13.  
  14. void enable_irq(void);
  15.  
  16. int main (void)
  17. {
  18.     /* enable clock to GPIOF at clock gating control register */
  19.     SYSCTL_RCGC2_R |= 0x20;
  20.     /* enable the GPIO pins for the LED (PF3, 2, 1) as output */
  21.     GPIO_PORTF_DIR_R = 0x0E;
  22.     /* enable the GPIO pins for digital function */
  23.     GPIO_PORTF_DEN_R = 0x0E;
  24.  
  25.     /* Configure SysTick */
  26.     NVIC_ST_RELOAD_R = 16000000-1;  /* reload with number of clocks per second */
  27.     NVIC_ST_CTRL_R = 7;             /* enable SysTick interrupt, use system clock */
  28.  
  29.     enable_irq();                   /* global enable interrupt */
  30.  
  31.     while(1) {
  32.         ;
  33.     }
  34. }
  35.  
  36. void SysTick_Handler(void)
  37. {
  38.     GPIO_PORTF_DATA_R ^= 2;         /* toggle the red LED */
  39. }
  40.  
  41. /* global enable interrupts */
  42. void inline enable_irq(void)
  43. {
  44.     __asm  ("    CPSIE  I\n");
  45. }

The above program uses the SysTick to toggle the red LED of PF1 every second. We need the RELOAD value of 16,000,000-1 since 1sec / 62.5nsec = 16,000,000. We assume the CPU clock is 16MHz (1/16MHz=62.5ns). The COUNT flag is raised every 16,000,000 clocks and an interrupt occurs. Then the RELOAD register is loaded with 16,000,000-1 automatically.

Notice the enabling of interrupt in SysTick Control register. Since SysTick is an internal interrupt, the COUNT flag is cleared automatically when the interrupt occurs. Also notice the SysTick is part of INT1-INT15 and not part of IRQ. For this reason we must use the CTLR register to enable it.