Solved: Problem using 16 bit counter on Atmega 328P

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

For now I am basically trying to capture a random count off the 16 bit timer and send it to a 16x2 lcd display. Before this part of the code is entered the display has been initialized and the cpu waits for an input pulse at the sbis instruction. When I trigger the pulse the cpu exits this part of the code and goes through a hex to ascii conversion routine and then outputs the captured number to the display.

 

The problem is the counter does not seem to be running and all I get at the display output is all zeros. I have been working on this for quite a while with no success.

 

I have done this before on an Attiny 2313 and have been successful and I am using the same basic algorithm here on the Atmega 328. However it is a much a more complex chip and I am probably missing something simple. 

There are many experienced programmers on this board and could one of them spot what is wrong?

 

---------enters this code and waits for a trigger pulse on portb 0--------------------------------------------

 

               cbi          ddrb,0            ;sets ddrb port 0 as input

               lds          rmp,$01         ;sets cs10 to 1 for system clock drive of 16 bit counter.
               sts          tccr1b,rmp    

wait1:      sbis       pinb,0             ;input capture timer count on signal pulse
               rjmp       wait1              ;wait for trigger pulse signal

               lds         YL,tcnt1L        ;load low byte of counter
               lds         YH,tcnt1H       ;load high byte of counter

 

------------exits this code to a hex to ascii converter and displays on the lcd.-----------

Last Edited: Sun. Oct 6, 2019 - 11:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wait for ICF1 bit in TIFR1 to be set. That tells you when an actual capture has occurred.

 

Then, don't forget to clear ICF1 by writing a logic  1 to that bit.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Note that for this example, you could just inc a register, loop around, & finally read the value when the pin says its time to read the random value.  This would be counting a a pretty fast pace & wrapping around many 1000's of times a second (8 bits).  for 16 bit it would wrap 100's of times a sec, maybe not as good for "random"

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

Mopar_512 wrote:

               lds          rmp,$01         ;sets cs10 to 1 for system clock drive of 16 bit counter.

 

Wouldn't "ldi" be more appropriate there?  S.

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

Scroungre wrote:

Mopar_512 wrote:

               lds          rmp,$01         ;sets cs10 to 1 for system clock drive of 16 bit counter.

 

Wouldn't "ldi" be more appropriate there?  S.

Wouldn't "ldi" be more appropriate there?  S.

 

You are right. With that correction the code worked. Sometimes I cannot see the forest for the trees. 

Thank you for your help.

Roger

 

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

Mopar_512 wrote:
trying to capture a random count off the 16 bit timer
Mopar_512 wrote:
You are right. With that correction the code worked.

Isn't it hard to tell if the display of a RANDOM value 'works'?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

This was just a small step in getting to where I wanted to be. I don't have any sophisticated debugging tools to help me get something to work. So I just build programs in small segments and stitch them together. My overall project goal was to get a time measurement between two pulses. I have done that now and have verified that is is working correctly by verifying measurements between my scope and my project.

 

Roger