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:
- Enable the clock to Timer0 block
- Disable timer while the configuration is being modified
- Select 16-bit mode
- Select one-shot mode and down-counter mode
- Set interval load register value
- Clear timeout flag
- Enable timer
- Wait for timeout flag to set
Source Code
-
/* This program demonstrates the use of TimerA of Timer0
-
* block to do a delay of the multiple of milliseconds.
-
Because 16-bit mode is used, it will only work up to 4 ms.
-
*/
-
#include <stdint.h>
-
#include "inc/tm4c123gh6pm.h"
-
void timer0A_delayMs(int ttime);
-
void delayMs(int n);
-
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(4); /* Timer A msec delay */
-
GPIO_PORTF_DATA_R = 0; /* turn off red LED */
-
delayMs(500); /* use old delay function */
-
}
-
}
-
/* millisecond delay using one-shot mode */
-
void timer0A_delayMs(int ttime)
-
{
-
SYSCTL_RCGCTIMER_R |= 1; /* (1). enable clock to Timer Block 0 */
-
TIMER0_CTL_R = 0; /* (2). disable Timer before initialization */
-
TIMER0_CFG_R = 0x04; /* (3). 16-bit option */
-
TIMER0_TAMR_R = 0x01; /* (4). one-shot mode and down-counter */
-
TIMER0_TAILR_R = 16000 * ttime - 1; /* (5). Timer A interval load value register*/
-
TIMER0_ICR_R = 0x1; /* (6). clear the TimerA timeout flag*/
-
TIMER0_CTL_R |= 0x01; /* (7). enable Timer A after initialization*/
-
while( (TIMER0_RIS_R & 0x1) == 0); /* (8). wait for TimerA timeout flag to set*/
-
}
-
/* delay n milliseconds (16 MHz CPU clock) */
-
void delayMs(int n)
-
{
-
int i, j;
-
for(i = 0 ; i < n; i++)
-
for(j = 0; j < 3180; j++)
-
{} /* do nothing for 1 ms */
-
}
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
-
/*
-
* 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 */
-
}
-
}
-
Recent Comments