Actions

EmSys

TM4C123 One-shot Mode of TimerA

From EdWiki

Revision as of 04:29, 7 March 2018 by Jshankar (Talk | contribs) (Source Code)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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. }