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.

TM4C123GH6PM: using Tiva C on edge timing mode

Part Number: TM4C123GH6PM

Hello , i have an issue with the timer on tiva c when using it on edge timing mode ,  according to the datasheet counter starts counting until a rising edge is detected and then moves the value of the counter currently to the GPTMTnR register , 

i am running my code at the moment and debugging to see whats going on , i know i should use the tivaware API but i like going down to register level 

my problem is this , since i'm using timer module 0 A then i should be using the PB pin 6 or PF Pin 0 as my rising edge detectors this what i understood from reading the timer part in the data sheet so is this correct ? because if it is, its not working accordingly the timer keeps running but doesn't move its current value to the designated register ever weather PB6 or PF0 are high or not  

so here is my code:

#include "tm4c123gh6pm.h"

void Timer0Init(void);

int main(void)
{
	int i=0;
	Timer0Init();
	while(1){
		for(i=0;i<100000;i++){
		}
	}
	return 0;
}

void Timer0Init(void)
{
	//initialize PORT B
	volatile unsigned long  delay;
	SYSCTL_RCGC2_R |= 0x00000002;      // 1) B clock
  delay = SYSCTL_RCGC2_R;            // delay to allow clock to stabilize     
  GPIO_PORTB_AMSEL_R &= 0x00;        // 2) disable analog function
  GPIO_PORTB_PCTL_R &= 0x00000000;   // 3) GPIO clear bit PCTL  
  GPIO_PORTB_DIR_R &= 0x00;          // 4.2) PB all input
  GPIO_PORTB_AFSEL_R &= 0x40;        // 5) no alternate function
  GPIO_PORTB_DEN_R |= 0xFF;          // 7) enable digital pins PF4-PF1
	GPIO_PORTB_PCTL_R = 7;
	//timer clock
	SYSCTL_RCGCTIMER_R |=0x01;
	delay = SYSCTL_RCGCTIMER_R;
//1. Ensure the timer is disabled (the TnEN bit is cleared) before making any changes.
	TIMER0_CTL_R &= 0xFE;
//2. Write the GPTM Configuration (GPTMCFG) register with a value of 0x0000.0004.
	TIMER0_CFG_R= 0x00000004;
//3. In the GPTM Timer Mode (GPTMTnMR) register, write the TnCMR field to 0x1 and the TnMR field to 0x3.
	TIMER0_TAMR_R= TIMER0_TAMR_R | 0x007;
//4. Configure the type of event that the timer captures by writing the TnEVENT field of the GPTM Control (GPTMCTL) register.
	TIMER0_CTL_R = TIMER0_CTL_R & 0xFFFFFFF3;
//5. If a prescaler is to be used, write the prescale value to the GPTM Timer n Prescale Register (GPTMTnPR).
	//no prescaler for now
//6. Load the timer start value into the GPTM Timer n Interval Load (GPTMTnILR) register.
	TIMER0_TAILR_R = 0xFF;
//7. If interrupts are required, set the CnEIM bit in the GPTM Interrupt Mask (GPTMIMR) register.
//no interrupts required	
//8. Set the TnEN bit in the GPTM Control (GPTMCTL) register to enable the timer and start counting.
	TIMER0_CTL_R= TIMER0_CTL_R & 0x00000001;
	TIMER0_CTL_R |= 0x01;
//9. Poll the CnERIS bit in the GPTMRIS register or wait for the interrupt to be generated (if enabled). In both cases,
//the status flags are cleared by writing a 1 to the CnECINT bit of the GPTM Interrupt Clear (GPTMICR) register.
//The time at which the event happened can be obtained by reading the GPTM Timer n (GPTMTnR) register.

/*In Input Edge Timing mode, the timer continues running after an edge event has been detected,
but the timer interval can be changed at any time by writing the GPTMTnILR register. The change
takes effect at the next cycle after the write.*/
}

Thanks in advance.

  • Hello Essam,

    You have already provided the answer in the code post. By not using TivaWare API and instead relying on Direct Register access, the chances of error going up has shown itself in the first few lines. Instead of setting PB6 you have set PB0.

    I would suggest that make the transition to TivaWare first. Otherwise going forward (as the code base starts to evolve) it is going to be incredibly complicated handling Direct Register access and we would not be able to provide much support.
  • yes i actually just used the tivaware for initializing portB for the down count it worked perfectly fine , when there is a high on PB pin 6 it will move the current timer value to the other register but what if i want to count up instead of down is the following logic correct
    1-timer is initialized to 0 since we are counting up
    2-rising edge comes timer starts counting and moves current value to the register (which will be 0 )
    3-next rising edge arrives and counter moves the next rising edge time which will be the value of counter at that point

    is this logic correct ? because the down counter works fine counting and detecting rising edges however the up counter doesnt work at all
    is there something i am missing here ? sorry for wasting your time by not using tivaware but i just did and it worked for the down now only the up counting remains
  • Hello Essam,

    The code shown above will not work if PB6 is not correctly initialized for Timer 0A module. Have you corrected the same?

    Also if the TivaWare was working for you earlier, then why change it at all?
  • yes i corrected the above mistake and used the tivaware api for port initialization , im just wondering its a curious question , what happens if i use the up counter not the down counter should the above logic in the previous reply be correct ?
  • Hello Essam

    In Up count mode, it should count from 0x0 to the TAILR load register value and then roll over back to 0.
  • Hi Amit,

    ok this is working perfectly fine now one last thing i would like to know , how do i force reset the counter value to start counting from 0 all over again ?
    in my logic im just calculating some stuff then i want to reinitialize the counter to 0 and start counting up all over again how do i do so ?
  • sorry amit nevermind i got it to work the TAV register is a read write register not a read only register so we can write the value of 0x0000 to it and it will start counting from the beginning sorry to disturb thank you very much !
  • Hello Essam,

    No problem. Perhaps another artifact of DRM.