Select Page

GPTM Sample Programs

One-shot Mode of TimerA

Use the one-shot mode to create a delay:

  • Uses 16-bit TimerA of Timer0 to create the delay.
  • The CPU frequency of 16 MHz means clock period of 1/16MHz = 62.5ns being fed to TimerA.
  • To get 1-msec delay we need 1msec/62.5ns = 16,000 clocks.

The steps to program the timer for one-shot mode are:

  1. Enable the clock to Timer0 block
  2. Disable timer while the configuration is being modified
  3. Select 16-bit mode
  4. Select one-shot mode and down-counter mode
  5. Set interval load register value
  6. Clear timeout flag
  7. Enable timer
  8. Wait for timeout flag to set

Source Code

  1. /* This program demonstrates the use of TimerA of Timer0
  2.  * block to do a delay of the multiple of milliseconds.
  3.    Because 16-bit mode is used, it will only work up to 4 ms.
  4. */
  5. 
    
  6. #include <stdint.h>
  7. #include "inc/tm4c123gh6pm.h"
  8. 
    
  9. void timer0A_delayMs(int ttime);
  10. void delayMs(int n);
  11. 
    
  12. int main (void)
  13. {
  14.     SYSCTL_RCGC2_R |= 0x00000020;   /* enable clock to GPIOF at clock gating control register */
  15. 
    
  16.     GPIO_PORTF_DIR_R = 0x0E;        /* enable the GPIO pins for the LED (PF3, 2 1) as output */
  17.     GPIO_PORTF_DEN_R = 0x0E;        /* enable the GPIO pins for digital function */
  18. 
    
  19.     while(1) {
  20.         GPIO_PORTF_DATA_R = 2;      /* turn on red LED */
  21.         timer0A_delayMs(4);         /* Timer A msec delay */
  22.         GPIO_PORTF_DATA_R = 0;      /* turn off red LED */
  23.         delayMs(500);               /* use old delay function */
  24.     }
  25. }
  26. 
    
  27. /* millisecond delay using one-shot mode */
  28. void timer0A_delayMs(int ttime)
  29. {
  30.     SYSCTL_RCGCTIMER_R |= 1;        /* (1). enable clock to Timer Block 0 */
  31.     TIMER0_CTL_R = 0;               /* (2). disable Timer before initialization */
  32.     TIMER0_CFG_R = 0x04;            /* (3). 16-bit option */
  33.     TIMER0_TAMR_R = 0x01;           /* (4). one-shot mode and down-counter */
  34.     TIMER0_TAILR_R = 16000 * ttime - 1; /* (5). Timer A interval load value register*/
  35.     TIMER0_ICR_R = 0x1;             /* (6). clear the TimerA timeout flag*/
  36.     TIMER0_CTL_R |= 0x01;           /* (7). enable Timer A after initialization*/
  37. 
    
  38.     while( (TIMER0_RIS_R & 0x1) == 0);  /* (8). wait for TimerA timeout flag to set*/
  39. }
  40. 
    
  41. /* delay n milliseconds (16 MHz CPU clock) */
  42. void delayMs(int n)
  43. {
  44.     int i, j;
  45.     for(i = 0 ; i < n; i++)
  46.         for(j = 0; j < 3180; j++)
  47.             {}  /* do nothing for 1 ms */
  48. }
    
    

    Periodic Mode of TimerA of Timer0 Block

    Set up the timer for 1 ms timeout in periodic mode and do 500 timeouts to achieve a one half second delay. The LED should blink at 1 Hz.

    Source Code

    1. /* 
    2. * This program demonstrates the use of TimerA of Timer0 block to do
    3. * a delay of the multiple of milliseconds using periodic mode.
    4. *
    5. */
    6. 
      
    7. #include <stdint.h>
    8. #include "inc/tm4c123gh6pm.h"
    9. 
      
    10. void timer0A_delayMs(int ttime);
    11. 
      
    12. int main (void)
    13. {
    14. 
      
    15.     SYSCTL_RCGC2_R |= 0x00000020;   /* enable clock to GPIOF at clock gating control register */
    16. 
      
    17.     GPIO_PORTF_DIR_R = 0x0E;        /* enable the GPIO pins for the LED (PF3, 2 1) as output */
    18.     GPIO_PORTF_DEN_R = 0x0E;        /* enable the GPIO pins for digital function */
    19. 
      
    20.     while(1) {
    21.         GPIO_PORTF_DATA_R = 2;      /* turn on red LED */
    22.         timer0A_delayMs(500);       /* TimerA 500 msec delay */
    23.         GPIO_PORTF_DATA_R = 0;      /* turn off red LED */
    24.         timer0A_delayMs(500);       /* TimerA 500 msec delay */
    25.     }
    26. }
    27. 
      
    28. /* multiple of millisecond delay using periodic mode */
    29. void timer0A_delayMs(int ttime)
    30. {
    31.     int i;
    32. 
      
    33.     SYSCTL_RCGCTIMER_R |= 1;     /* enable clock to Timer Block 0 */
    34. 
      
    35.     TIMER0_CTL_R = 0;            /* disable Timer before initialization */
    36.     TIMER0_CFG_R = 0x04;         /* 16-bit option */
    37.     TIMER0_TAMR_R = 0x02;        /* periodic mode and down-counter */
    38.     TIMER0_TAILR_R = 16000 - 1;  /* Timer A interval load value register */
    39.     TIMER0_ICR_R = 0x1;          /* clear the TimerA timeout flag*/
    40.     TIMER0_CTL_R |= 0x01;        /* enable Timer A after initialization */
    41. 
      
    42.     for(i = 0; i < ttime; i++) {
    43.         while ((TIMER0_RIS_R & 0x1) == 0)
    44.             ;                    /* wait for TimerA timeout flag */
    45. 
      
    46.         TIMER0_ICR_R = 0x1;      /* clear the TimerA timeout flag */
    47.     }
    48. }