Actions

EmSys

TM4C123 Timer1 using prescaler

From EdWiki

Use prescaler register for TimerA to create 1-second delay

  • Using prescaler of 250 gives us 16MHz/250 = 64000Hz. That means the clock fed to timer A is 1/64000Hz = 15.625 msec.
  • To get one second delay, we need 1sec/15.625ms = 64000 for the match register.


  1. /*
  2.  * Timer using prescaler
  3.  * This program incorporates the use of prescaler to create a longer delay.
  4.  * Also the use of timer has been changed to TimerA of timer Block 1.
  5.  *
  6.  */
  7.  
  8. #include <stdint.h>
  9. #include "inc/tm4c123gh6pm.h"
  10.  
  11. void timer1A_delaySec(int ttime);
  12.  
  13. int main (void)
  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, and 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 = 4;      /* turn on blue LED */
  22.         timer1A_delaySec(1);        /* TimerA 500 msec delay */
  23.         GPIO_PORTF_DATA_R = 0;      /* turn off blue LED */
  24.         timer1A_delaySec(1);        /* TimerA 500 msec delay */
  25.     }
  26. }
  27.  
  28. /* multiple of second delays using periodic mode and prescaler*/
  29. void timer1A_delaySec(int ttime)
  30. {
  31.     int i;
  32.  
  33.     SYSCTL_RCGCTIMER_R |= 2;        /* enable clock to Timer Block 1 */
  34.  
  35.     TIMER1_CTL_R = 0;               /* disable Timer before initialization */
  36.     TIMER1_CFG_R = 0x04;            /* 16-bit option */
  37.     TIMER1_TAMR_R = 0x02;           /* periodic mode and down-counter */
  38.     TIMER1_TAILR_R = 64000 - 1;     /* TimerA interval load value reg */
  39.     TIMER1_TAPR_R = 250 - 1;        /* TimerA Prescaler 16MHz/250=64000Hz */
  40.     TIMER1_ICR_R = 0x1;             /* clear the TimerA timeout flag */
  41.     TIMER1_CTL_R |= 0x01;           /* enable Timer A after initialization */
  42.  
  43.     for(i = 0; i < ttime; i++) {
  44.         while ((TIMER1_RIS_R & 0x1) == 0)
  45.             ;                       /* wait for TimerA timeout flag */
  46.  
  47.         TIMER1_ICR_R = 0x1;         /* clear the TimerA timeout flag */
  48.     }
  49. }