TM4C123 One-shot Mode of TimerA
From EdWiki
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 */
}