Reading 16 bit timer registers

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

There seems to be lots of talk about writing to 16 bit registers but not much on reading. What I have gleaned so far is that "trying" to write to the H half just puts the value in a temporary register that will transfer to the actual H register when the corresponding L register is also written to. So what happens if I want to read ? will reading the L register first trigger the H register to be copied at the same time to the temp register so that it can be read in the next read operation without error. Correct ?

I'm trying to time something so as i already have a 16 bit counter running might as well just pull a start and stop time off it and work out the difference,

My alternative means trying to update a variable every few uS with an interupt routine and then check start and stop times with a second interupt routine, somehow trying to run an interupt routine every few uS and another every 33uS on 8MHz clock does not seem to be a very good idea and i have a counter running at one tick per 1uS already and I only need to take a reading when I fancy.

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

Quote:

So what happens if I want to read ?

You do not mention what AVR model you use, so I chose to quote from the ATmega328 data sheet. Your data sheet will have a similar formulation:
Quote:
15.3 Accessing 16-bit Registers
The TCNT1, OCR1A/B, and ICR1 are 16-bit registers that can be accessed by the AVR CPU via the 8-bit data bus. The 16-bit register must be byte accessed using two read or write operations. Each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. The same temporary register is shared between all 16-bit registers within each 16-bit timer. Accessing the low byte triggers the 16-bit read or write operation. When the low byte of a 16-bit register is written by the CPU, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit register in the same clock cycle. When the low byte of a 16-bit register is read by the CPU, the high byte of the 16-bit register is copied into the temporary register in the same clock cycle as the low byte is read.

Not all 16-bit accesses uses the temporary register for the high byte. Reading the OCR1A/B 16-bit registers does not involve using the temporary register.

To do a 16-bit write, the high byte must be written before the low byte. For a 16-bit read, the low byte must be read before the high byte.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

OK that confirms it for me thank you, Seems things are not sinking into my brain today even if I read them.

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

Are you using C or Asm? Most C compilers provide (where adjacent) conglomerate registers that when accessed do a full 16 bit read/write. So there may be TCNT1H and TCNT1L and you can likely do things like:

uint16_t value = TCNT1L;
value |= (TCNT1H << 8);

or

TCNT1H = (value >> 8);
TCNT1L = (value & 0xFF);

but surely life is just a whole lot simpler if you use:

uint16_t value = TCNT1;

and

TCNT1 = value;

??

Let the compiler worry about getting the register access order correct - this is the very reason to use a High Level Language.

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

if AVR-GCC will do that all the better, the thought crossed my mind as I've done it with the ADC registers before.

posted next day due to ball ache catchpa popping up that went unnoticed last night, seriously when will this crap be removed ?

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

Quote:

posted next day due to ball ache catchpa popping up that went unnoticed last night,

I've now lost count of how often that's happened to me too - it is VERY annoying.

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

The thing is I have made many posts from this computer and as I'm at work it's probably a stable IP or something, If you need a catchpa to stop spam then there is something wrong with how the forum setup. I help run the eevblog forum and we have never had any major spam problems on the simple machine forum platform (SMF), only when someone physically registers and manually tries to post spam which is hard for security software to stop and they don't last long as I get a report within minutes of it being spotted and ban the user and remove their less than a handful of posts.

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

Much has been said about Captcha here. We're close to the point where Freaks moves to the new system. It too has captcha but it does not seem to be so "aggressive". I guess we'll see.

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

Well usually the first 10-50 posts is acceptable, you won't stop human spammers and they are few but you will stop bots which do the most damage, on SMF you can install mods that link into a world wide database so that as soon as one forum bans the email address or IP or even username (which are generally random generated so nothing "normal") they are blocked from the rest pending approval, works a treat.

The annoying thing about this one is that it does not come up when you start a post only when you try and submit so you have to submitt twice and that is the nightmare.

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

Quote:
The annoying thing about this one is that it does not come up when you start a post only when you try and submit so you have to submitt twice and that is the nightmare.

This is my main gripe. I don't mind typing a 5-letter Captcha with a submit. But I really dislike the 'submit first and Captcha later to re-submit'.

Anything that reduces Spam bots is worthwhile. I can't see that multiple submissions will make any difference to a bot. But it certainly annoys humans.

David.

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

Well hop on over to the eevblog forum and tell me how much spam you spot, and I'm not deleting spam all day long, we get moderator reports of spam every few weeks, less often than reports of arguments.