/*
* Timer using prescaler
* This program incorporates the use of prescaler to create a longer delay.
* Also the use of timer has been changed to TimerA of timer Block 1.
*
*/
#include <stdint.h>
#include "inc/tm4c123gh6pm.h"
void timer1A_delaySec(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, and 1) as output */
GPIO_PORTF_DEN_R = 0x0E; /* enable the GPIO pins for digital function */
while(1) {
GPIO_PORTF_DATA_R = 4; /* turn on blue LED */
timer1A_delaySec(1); /* TimerA 500 msec delay */
GPIO_PORTF_DATA_R = 0; /* turn off blue LED */
timer1A_delaySec(1); /* TimerA 500 msec delay */
}
}
/* multiple of second delays using periodic mode and prescaler*/
void timer1A_delaySec(int ttime)
{
int i;
SYSCTL_RCGCTIMER_R |= 2; /* enable clock to Timer Block 1 */
TIMER1_CTL_R = 0; /* disable Timer before initialization */
TIMER1_CFG_R = 0x04; /* 16-bit option */
TIMER1_TAMR_R = 0x02; /* periodic mode and down-counter */
TIMER1_TAILR_R = 64000 - 1; /* TimerA interval load value reg */
TIMER1_TAPR_R = 250 - 1; /* TimerA Prescaler 16MHz/250=64000Hz */
TIMER1_ICR_R = 0x1; /* clear the TimerA timeout flag */
TIMER1_CTL_R |= 0x01; /* enable Timer A after initialization */
for(i = 0; i < ttime; i++) {
while ((TIMER1_RIS_R & 0x1) == 0)
; /* wait for TimerA timeout flag */
TIMER1_ICR_R = 0x1; /* clear the TimerA timeout flag */
}
}