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.

problem with timer(Tm4c123GH6PM)

i have problem to check my output result....(isn't i write something wrong?)..Below is the code that i wrote....Anyone can guide me?

#include <stdint.h>

#include <stdbool.h>

#include <stdio.h>

#include "inc/hw_memmap.h"

#include "inc/hw_types.h"

#include "inc/hw_gpio.h"

#include "inc/hw_nvic.h"

#include "inc/hw_ints.h"

#include "driverlib/debug.h"

#include "driverlib/gpio.h"

#include "driverlib/pin_map.h"

#include "driverlib/sysctl.h"

#include "driverlib/timer.h"

 

 

unsigned long timer;

 

 

int

main(void)

{

 

       SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

 

       SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

       SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

       SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

 

       GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

       SysCtlDelay(200);

 

    //GPIO config

       GPIOPinConfigure(GPIO_PB7_T0CCP1);

       GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_7);

 

    //Timer config

       TimerConfigure(TIMER0_BASE,  TIMER_CFG_A_CAP_COUNT_UP);

       TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);

    TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/10);//8000000/10=80000=100 milliseconds

    TimerEnable(TIMER0_BASE, TIMER_A);

 

 

    while(1)

    {

 

        timer = TimerValueGet(TIMER0_BASE,TIMER_A);

              if(timer==0)

              {

                     GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x00);

 

              }

              else

              {

 

                     GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x04);

              }

       }

 

}

 

 

  • Hello Dorene,

    Yes indeed there is a problem. The configuration for TimerControlEvent is using TIMER_B but rest of the code is for TIMER_A.

    Also the CCP pin is for TIMER_B but it needs to be for TIMER_A if you plan to use the 32-bit mode. Please do look at the Signal Configuration for T0CCP0 Pin and not T0CCP1 pin.

    Regards

    Amit

  • Hi Amit,

    Opzz, i mixed up.....Thanks for your guide....I had changed to use timer_B but the result still cant get.....Here is my code:

    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_nvic.h"
    #include "inc/hw_ints.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/timer.h"
    unsigned long timer;
    int main(void)
    {
    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
    SysCtlDelay(200);
    GPIOPinConfigure(GPIO_PB7_T0CCP1);
    GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_7);
    TimerConfigure(TIMER0_BASE,  TIMER_CFG_B_CAP_COUNT_UP);
        // This function configures the signal edge(s) that triggers the
        // timer when in capture mode.
    TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
        // This function configures the timer load value; if the timer is running
        // then the value is immediately loaded into the timer.
    TimerLoadSet(TIMER0_BASE, TIMER_B,10000);
    // Enable the timers.
        TimerEnable(TIMER0_BASE, TIMER_B);
        while(1)
        {
        // Get the counter value
            timer = TimerValueGet(TIMER0_BASE,TIMER_B);
            // Reset the counter value to 10000
            TimerLoadSet(TIMER1_BASE, TIMER_B,10000);
    if(TimerValueGet(TIMER0_BASE,TIMER_B))
    {
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x00);
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x04);
    }
    }
    }

    Regards,

    Dorene

  • Hello Dorene,

    Please change the following line as you are using a 16-bit timer so the timer module needs to know it is a 16-bit timer

    TimerConfigure(TIMER0_BASE,  TIMER_CFG_B_CAP_COUNT_UP|TIMER_CFG_SPLIT_PAIR);

    Regards

    Amit

  • Hi Amit,

    Isnt the PB7 is a  locked pin?

    Regards,

    Dorene

  • Hi,

    No, PF0 and PD7 are locked, dedicated NMI pins.

    Also, not so clear what your goals are - timer events are best managed by interrupts. Your code has some problems, see my comments below:

    while(1)
        {
        // Get the counter value
            timer = TimerValueGet(TIMER0_BASE,TIMER_B);   // timer variable is not anymore used
            // Reset the counter value to 10000
            TimerLoadSet(TIMER1_BASE, TIMER_B,10000);     // re-initialization? [without reason]
    	if(TimerValueGet(TIMER0_BASE,TIMER_B))	      // so this is always true, never false
    	 {
    	   GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x00);
    	 }
    	else
    	 {
    	   GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2, 0x04); // this one is never taken			
    } }

    Petrei

  • Hello Petrei,

    The intent of the poster is to capture edges, based on the original code.

    Regards

    Amit

  • Hi,

    @Amit,

    I know that - but I expected the user to make a last-minute statement of its intents: "I want to count edges from x to y, (y=2^z, for instance), at finish (if ever) want to trigger some other event like…" - then should realize that another register should come to play an must be initialized…. and how to save/display the counted edges. Simple copy/paste "does not work". Maybe writing down would trigger some thoughts/verifications/reading again the user manual..

    Petrei

  • Dear Amit,

    im confusing with the CCP.....can u tell me ....why in T0CCP0 have 2 pin assignment? Actually which one suppose i use as my input? PB6 (7) or PF0 (7)?

    Regards,

    Dorene

  • Dear Petrei,

    owh~the timer events are best managed by interrupts. okok~i will try to change my code by using interrupts....

     

    Regards,

    Dorene

  • Hello Dorene,

    The 2 pin assignments are like alternate pin assignment options. For example if PF0 is being used as a GPIO then you can use PB6 for T0CCP0 function and vice-versa.

    This allows for a lot of flexibility.

    Regards

    Amit

  • Hello Petrei,

    Yes, that is true. The last minute statement is what I had not anticipated even though the original code did show the while loop where a GPIO is being toggled.

    Regards

    Amit

  • hii amit,

    i am doing similar program to measure of input signal frequency. I am using TIMER0 to count pulses between to edges of input signal. I am generating interrupt at edges of input signal. but i am not aware that how to count pulses. i am running TIMER at 1Khz.

    by this API

    TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES);

    how to get count value?

    regards

    prashant