timer1 and 2 are not working the same.

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using an ATMEGA328, I have two timers and only using them for back ground counters, no interrupts.

 

 

Timer 1

my init is
TCCR1B = (1<<CS12);

using it like so

if(TCNT1 >= 2000)
    {   
        TCNT1 = 0;//Reset the timer value
        _TURBO_PRESS=!_TURBO_PRESS;

    }

 

 

Timer 2

my init is

TCCR2B = (1<<CS20);

 

if (TCNT2 >= 150) //make this .5 seconds,
    {
        TCNT2=0;

        //do some stuff
    }

 

The first timer gives me a pulses around 30ms or so. Once the counter hits 200, a pulse is made and the timer is reset.

 

the second timer is set for around 1 half a second.  Once the counter hits 150, the pulse is made but it keeps counting and I get pulses some time after 150 before it loops over.  The TCNT2=0; does not seem to do what I would have thought it would do. My first thought was that it was read only but the other time seems to work fine. The check function that looks for the timer to elapse my value is not ran every tick (more like once every 1ms)  so I need to check for greater then as I will not get a resolution of one count.

 

 

 

 

This topic has a solution.
Last Edited: Tue. Sep 1, 2020 - 09:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You fail to say what chip this is, or what mode the timers are in.  What makes the pulse?  

 

How often are the if's being checked?

 

Once the counter hits 150, the pulse is made but it keeps counting   ...what does that mean, you mean you dont set it to zero?  Whay makes you say that?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah, I did in deed fail to mention that, added the info.

 

 

"What makes you say that" -

if (TCNT2 >= 150) //make this .5 seconds,
    {
        TCNT2=0;<--- this.

        //do some stuff
    }

 

Oh  and sorry about the pulse thing, I had debug in there I forgot to include.

 

this was placed just after the " //do some stuff"

    DDRB |= 0x04;
    PORTB &= ~0x04;
    PORTB |= 0x04;

 

 

Last Edited: Tue. Sep 1, 2020 - 05:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 TCNT2=0;<--- this.

Ok, you set it to zero...then it will count up thereafter.  Have you verified the zeroing is happening? 

 

or what mode the timers are in

You continue to avoid saying what mode the timers are set to (there are a dozen modes)...how do you expect help?

The port stuff is no good, use a pin toggle, so you can see it on the scope.  Why go looking for a submicrosecond pulse that happens every 200ms?  When all else fails, apply 12V crying​​​​​​​

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
TCCR2B = (1<<CS20);

I have not looked at the data sheet, but that seems like a pretty fast clock (/1 maybe) if so, and this is an 8 bit timer so max count = 255, you have less then 105 cpu clock cycles to catch it above 150 before it rolls over to zero.  Not much time to look for your timer above 150!   Even in a tight while() loop it will only test it every 3 or 4 counts due to the time the while loop takes.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do not know what mode they are in, but they should be in CTC mode.

Test the interrupt flag instead of the counter value.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>the second timer is set for around 1 half a second.

 

Your timer2 is set to increment on every clock. Unless you are manually flipping a switch to provide your cpu clock, you are not anywhere close to 1/2 second.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you even connected your scope to look at some of these timings?  Don't waste your time, time it.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Ok, you set it to zero...then it will count up thereafter.  Have you verified the zeroing is happening?  " yes, its not going to zero. I left the timer in the default mode. I only change the register bits I showed.

 

" I have not looked at the data sheet, but that seems like a pretty fast cloc " Was sure the was the slowest. I was cretin it occurred every .5 second. maybe I was wrong with that, my clock is (12MHz.). ill recheck.

 

I was toileting the PORT pin with the Pin in output mode and timed it with a scope. I saw it blip every .5 second and then a few after.

like     | ---------------------.5 second---------| | | || ---------------------.5 second---------| | | | and so on.

ill recheck, thx for the input.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
toileting

Cursed otto connect!

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
like     | ---------------------.5 second---------| | | || ---------------------.5 second---------| | | | and so on.

Me thinks you are missing when the timer is above 150 most of the time, the | | | | is probably when your catching it correctly!

As I said, you only have 105 clock cycles to catch it above 150 before it rolls over and is less then 150.  Try slowing down the timer using a higher value pre-scaler.

My wizard tells me T2 clocked at 12Mhz/1024 will time out just over 20ms, no way to get .5 seconds out of it!

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Tue. Sep 1, 2020 - 08:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, was pretty much I had the prescale wrong and was expiring too fast.