Actions

EmSys

Difference between revisions of "TM4C123 Periodic Mode of TimerA"

From EdWiki

(Created page with "== 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 a...")
 
m (Source Code)
 
Line 5: Line 5:
 
== Source Code ==
 
== Source Code ==
 
<syntaxhighlight lang="c" line start="1">
 
<syntaxhighlight lang="c" line start="1">
 +
/*
 +
* This program demonstrates the use of TimerA of Timer0 block to do
 +
* a delay of the multiple of milliseconds using periodic mode.
 +
*
 +
*/
 +
 +
#include <stdint.h>
 +
#include "inc/tm4c123gh6pm.h"
 +
 +
void timer0A_delayMs(int ttime);
 +
 +
int main (void)
 +
{
 +
 +
    SYSCTL_RCGC2_R |= 0x00000020;  /* enable clock to GPIOF at clock gating control register */
 +
 +
    GPIO_PORTF_DIR_R = 0x0E;        /* enable the GPIO pins for the LED (PF3, 2 1) as output */
 +
    GPIO_PORTF_DEN_R = 0x0E;        /* enable the GPIO pins for digital function */
 +
 +
    while(1) {
 +
        GPIO_PORTF_DATA_R = 2;      /* turn on red LED */
 +
        timer0A_delayMs(500);      /* TimerA 500 msec delay */
 +
        GPIO_PORTF_DATA_R = 0;      /* turn off red LED */
 +
        timer0A_delayMs(500);      /* TimerA 500 msec delay */
 +
    }
 +
}
 +
 +
/* multiple of millisecond delay using periodic mode */
 +
void timer0A_delayMs(int ttime)
 +
{
 +
    int i;
 +
 +
    SYSCTL_RCGCTIMER_R |= 1;    /* enable clock to Timer Block 0 */
 +
 +
    TIMER0_CTL_R = 0;            /* disable Timer before initialization */
 +
    TIMER0_CFG_R = 0x04;        /* 16-bit option */
 +
    TIMER0_TAMR_R = 0x02;        /* periodic mode and down-counter */
 +
    TIMER0_TAILR_R = 16000 - 1;  /* Timer A interval load value register */
 +
    TIMER0_ICR_R = 0x1;          /* clear the TimerA timeout flag*/
 +
    TIMER0_CTL_R |= 0x01;        /* enable Timer A after initialization */
 +
 +
    for(i = 0; i < ttime; i++) {
 +
        while ((TIMER0_RIS_R & 0x1) == 0)
 +
            ;                    /* wait for TimerA timeout flag */
 +
 +
        TIMER0_ICR_R = 0x1;      /* clear the TimerA timeout flag */
 +
    }
 +
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:15, 22 February 2018

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