This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Stellaris Launchpad, lm4f120h5qr, Timer not functioning as desired, can count down but not up.



I'm determined not to use the provided stellarisware functions, and am trying to figure out how to get my Timer to work. At this moment, all I'm attempting to do is have Timer0 A count to a specified value (That stored in TIMER0_TAILR_R) and blink the on board LED.

The program as it is now will count down from the specified value (by having TIMER0_TAMR_R |= 0x1), yet when in count up mode, the LED specified blinks at a constant rate (slowly) regardless of the value stored in TIMER0_TAILR_R.

#include "lm4f120h5qr.h" 
int main(void){ 
 SYSCTL_RCGCGPIO_R |= 0x20; // clock gate to port F 
 GPIO_PORTF_DIR_R |= 0x7 << 1; // Set Led's as outputs 
 GPIO_PORTF_DEN_R |= 0x7 << 1;// for LED's 
 GPIO_PORTF_DEN_R |= 0x1; // For timer 
 GPIO_PORTF_PCTL_R |= 0x7; //select T0CCP0 
 SYSCTL_RCGC1_R |= 0x1 << 16;
 SYSCTL_RCGCTIMER_R |= 0x1; 
 TIMER0_CTL_R = 0; 
 TIMER0_CFG_R = 0;
 TIMER0_CFG_R |= 0x4; 
 TIMER0_TAMR_R |= 0x11; // set mode 
 TIMER0_TAPR_R |= 0xF; // prescale 
 TIMER0_TAILR_R = 0x1F1; // load value 
 while(1){ 
 GPIO_PORTF_DATA_R |= 0x1 << 2; 
 TIMER0_CTL_R |= 0x1; 
 while(TIMER0_CTL_R & 0x1); 
 GPIO_PORTF_DATA_R &= ~(0x1 << 2); 
 TIMER0_CTL_R |= 0x1; 
 while(TIMER0_CTL_R & 0x1); 
}
As far as I know I've initialised the Timer correctly and am doing everything correctly (obviously this is incorrect). Any help as to where I'm going wrong is greatly appreciated as I've been racking my brains over this for quite some time and cannot find a solution. 
cheers, 
Luke.
  • Hi Luke,

    I believe the reason you are seeing the difference between count up and count down is stated on page 660 of the LM4F120H5QR datasheet.

    "The available modes for each GPTM block are shown in Table 11-3 on page 661. Note that when counting down in one-shot or periodic modes, the prescaler acts as a true prescaler and containsthe least-significant bits of the count. When counting up in one-shot or periodic modes, the prescaler acts as a timer extension and holds the most-significant bits of the count."

    Let me know if this helps,

    Sheldon

  • I'm not quite sure if I understand what this means. I'm more familiar with the prescalar containing the least significant bits of the count and can't really grasp how it can hold the most significant bits. Does this mean that changing the TIMER0_TAILR_R will have no effect on the timer?

  • Luke,

    It means TIMER_TAILR_R will have less effect in count up mode you will no longer have a prescaler but the value you have set for you prescaler will become the most significant Byte so you your count up to value will be 0x0F01F1 ( i believe you were using a 16 bit timer with an 8 bit prescaler). 

    Sheldon