Jump to content
Sign in to follow this  
fednaldo

timer counter

Recommended Posts

Hi everyone,

I faced with trouble about frequency of timer as a counter mode.

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "inc/hw_memmap.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_gpio.h"
#include "driverlib/timer.h"

#define SW1			GPIO_PIN_4
#define SW2 		GPIO_PIN_0

#define LED_RED		GPIO_PIN_1
#define LED_GREEN	GPIO_PIN_2
#define LED_BLUE	GPIO_PIN_3


uint32_t ui32Period,t;
uint32_t val;

void INIT_LED()
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
	GPIOPinWrite(GPIO_PORTF_BASE,  LED_RED|LED_GREEN|LED_BLUE, 0);
}

void CFG_TIM0()
{
		SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
		TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
		ui32Period = (SysCtlClockGet()/200);
		TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period-1);
 		TimerEnable(TIMER0_BASE, TIMER_A);
}
int main(void) {
	SysCtlClockSet(SYSCTL_SYSDIV_2_5| SYSCTL_USE_PLL | SYSCTL_OSC_INT | SYSCTL_XTAL_16MHZ);
	INIT_LED();
	CFG_TIM0();
    t = LED_RED;
    while(1)
    {
    	val=TimerValueGet(TIMER0_BASE,TIMER_A);
    	if(val==0)
    	{
    		GPIOPinWrite(GPIO_PORTF_BASE,  LED_RED, t);
    		t = t ^ LED_RED;
    	}
    }
}

As my knownledge, timer will counter from 0 to ui32Period -1, with frequency = 1/SysCtlClockGet(); but after some tests, I realized that frequency for counter is a constant = 2Mhz ( not equal 80Mhz as clock of CPU). Please help to explain for my trouble.

Thanks in advanced!

Share this post


Link to post
Share on other sites

@@fednaldo It will be more or less down to pure luck if you happen to read the timer when it is zero - you need to add an interrupt handler to reliably catch that event. In addition you may set the clock source and prescaler explicitly - unless you are sure the default settings for those are what you want.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...