TCNT0 and TCNT1

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

Hi

 

I would like to know if an external interrupt on INT0 can make TCNT1 count.(internal clock source)

 

As far as the datasheet INT0 works with TCNT0 and INT1 works with TCNT1, but using an external interrupt can INT0 be used to make TCNT1 count?(internal clock source)

 

Then is it possible to read any 16 bit register direct or should the HIGH and LOW be read separate and then combined into one 16 bit register?

 

I found many web sites explaining counters but no clear explanation on the above.

 

Thanks.

This topic has a solution.
Last Edited: Wed. Mar 14, 2018 - 06:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mikedb wrote:

but using an external interrupt can INT0 be used to make TCNT1 count?(internal clock source)

 

If you are using an external interrupt then why even use TCNT1? Surely, in your ISR, you just increment a variable?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

Thanks but that was not what i asked ,i did state in brackets ---INTERNAL CLOCK SOURCE!

 

 

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

But if you are running on an internal clock source where does INT0 or INT1 come into it?

 

Also, since you haven't told us which chip you are using, you might be getting confused between INT0/INT1 and T0/T1.

 

Anyhow, why don't we back up a bit and you tell us EXACTLY what you want to do?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

mikedb wrote:
Then is it possible to read any 16 bit register direct or should the HIGH and LOW be read separate and then combined into one 16 bit register?
Most C compilers in their device headers define all of things like TCNT1L, TCNT1H and TCNT1. The former allow you to access the register 8bits at a time while the latter defines it as *(uint16_t *) so will make a 16 bit read/write. Of course the AVR itself does not make 16 bit transfers. So what you actually get is something like:

C:\SysGCC\avr\bin>type avr.c
#include <avr/io.h>

int main(void) {
    while (1) {
                TCNT1L = 0x5A;
                TCNT1H = 0xA5;
                TCNT1 = 12345;
    }
}

C:\SysGCC\avr\bin>avr-gcc -mmcu=atmega16 -g -Os avr.c -o avr.elf

C:\SysGCC\avr\bin>avr-objdump -S avr.elf

avr.elf:     file format elf32-avr


Disassembly of section .text:

00000000 <__vectors>:
   0:   0c 94 2a 00     jmp     0x54    ; 0x54 <__ctors_end>
   4:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
   8:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
   c:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  10:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  14:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  18:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  1c:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  20:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  24:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  28:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  2c:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  30:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  34:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  38:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  3c:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  40:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  44:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  48:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  4c:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>
  50:   0c 94 34 00     jmp     0x68    ; 0x68 <__bad_interrupt>

00000054 <__ctors_end>:
  54:   11 24           eor     r1, r1
  56:   1f be           out     0x3f, r1        ; 63
  58:   cf e5           ldi     r28, 0x5F       ; 95
  5a:   d4 e0           ldi     r29, 0x04       ; 4
  5c:   de bf           out     0x3e, r29       ; 62
  5e:   cd bf           out     0x3d, r28       ; 61
  60:   0e 94 36 00     call    0x6c    ; 0x6c <main>
  64:   0c 94 3f 00     jmp     0x7e    ; 0x7e <_exit>

00000068 <__bad_interrupt>:
  68:   0c 94 00 00     jmp     0       ; 0x0 <__vectors>

0000006c <main>:
#include <avr/io.h>

int main(void) {
    while (1) {
                TCNT1L = 0x5A;
  6c:   3a e5           ldi     r19, 0x5A       ; 90
                TCNT1H = 0xA5;
  6e:   25 ea           ldi     r18, 0xA5       ; 165
                TCNT1 = 12345;
  70:   89 e3           ldi     r24, 0x39       ; 57
  72:   90 e3           ldi     r25, 0x30       ; 48
#include <avr/io.h>

int main(void) {
    while (1) {
                TCNT1L = 0x5A;
  74:   3c bd           out     0x2c, r19       ; 44
                TCNT1H = 0xA5;
  76:   2d bd           out     0x2d, r18       ; 45
                TCNT1 = 12345;
  78:   9d bd           out     0x2d, r25       ; 45
  7a:   8c bd           out     0x2c, r24       ; 44
  7c:   fb cf           rjmp    .-10            ; 0x74 <main+0x8>

As you can see here the "double width" access simply does two OUT instructions instead of one.

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

mikedb wrote:
As far as the datasheet INT0 works with TCNT0 and INT1 works with TCNT1, but using an external interrupt can INT0 be used to make TCNT1 count?(internal clock source)

 

You seem to be confusing INT0 (interrupt input 0) with T0 (external clock to TCNT0),  INT0 has nothing to do with timer 0, likewise for INT1 and T1.

 

Please explain what it is your wanting to do.

 

Jim

 

 

edit: spelling

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

Last Edited: Tue. Mar 13, 2018 - 01:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
INT0 has nothing to do with timer 0, likewise for INT1 and T1.
+1

ki0bk wrote:
Please explain what it is your wanting to do.
  Agreed.  Especially with the "internal clock source".  The timer will use the system clock and increment the TCNT and then there are certain events that can be used -- compare match and overflow.

 

As mentioned, the timers may have the capability to count "ticks" on the Tn pin.  "May", because not all AVR8 timers have an associated Tn pin.  But that isn't an internal clock source. 

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

Yes...I was confused with thinking INT0 and INT1 is linked to TCNT0 and TCNT1.

 

I am playing with a ultrasonic distance meter and the code sample i found is for a Atmega32 and i must change the code to work on a Atmega328p.

 

The registers is not the same... so by comparing the 2 different datasheets(mega32 and mega328p) i somehow got confused by thinking that the INT0 interrupt req is linked to timer0 ...which is not the case.

 

Thanks for the help...just needed a little push to get going!

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

mikedb wrote:
I am playing with a ultrasonic distance
mikedb wrote:
work on a Atmega328p
If you are talking about a device such as HC-SR04 then I cannot help noticing that 328P is the Arduino Processor so you are bound to find tons of existing Arduino examples for sensors such as that on 328P. Don't be concerned that it is "Arduino". Apart from the fact that AS7 can simply import Arduino code anyway it's usually not too great a stretch to take code that's designed for use in the Arduino world and simply break it out for the plain C++ world.

 

Assuming my guess at HC-SR04 is right then a quick Google hits things like:

 

https://howtomechatronics.com/tu...

http://www.instructables.com/id/...

https://create.arduino.cc/projec...

https://arduinobasics.blogspot.d...

etc. etc.

 

Of course if it isn't HC-SR04 then whatever device it is I'll bet 100 people have added one to an Arduino.

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

Thanks for all the advice.

 

I did play around with ARDUINO a little but i like to do things the old way..i started off with PIC'S and assembler and each and every register must be setup and understood.

 

The way i see ARDUINO is that if one only eat takeaways you will never be able to bake an egg.

 

The ARDUINO pcb i love all these years of building on vero board ...lots of hours just to get to the point of testing your source code.

 

I do my programming with a STK500..and yes the ultrasonic device is a HC-SR04.

 

I could not find any sample code for Atmega328p distance meter done with AVR Studio.

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

mikedb wrote:
I could not find any sample code for Atmega328p distance meter done with AVR Studio.
I find that astonishing. Sure Arduino has kind of "taken over everything" these days but I've been seeing code for these Ultasonic "pingers" for a decade or more - there must still be remnants out on the internet.

BTW in google you could search "AVR HC-SR04 -Arduino" and the latter would filter away anything that mentions Arduino (so "modern"). You could also try "search tools" and pick a date range that ignores the last 3..5 years.

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

clawson wrote:
I find that astonishing.

So do I, since we've had a couple threads on it here in the past week, and in at least one of them I remember doing the search of this site and gave the number of articles.  Many predate "arduino".

 

mikedb wrote:
The way i see ARDUINO is that if one only eat takeaways you will never be able to bake an egg.

To some extent I agree. When you start to learn to cook, are you going to forge your own knife?  Then taking the next step(s) mine your own coal for the forge, and grow your own tree for making the handle?  There has to be a balance, especially when starting out.  One follows in the footsteps of those that have gone before.  After doing that for a while, >>then<< it is time to leave the beaten path and blaze your own trail to Nirvana.

 

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

I will do some more searching and see what pops out.

 

My project is working accurate up to -+ 1cm but the max distance is just over 100cm.

 

I will post my code on a new thread and get some feedback on possible fixes.

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

mikedb wrote:
My project is working accurate up to -+ 1cm but the max distance is just over 100cm.

So what is the problem?  What accuracy do you expect?  What distance can you expect with your sensor?  And indeed, in all probability the "best" way is to use input capture, which you haven't mentioned.  And max distance could be related to scaling and overflow when calculating the response pulse width.

 

So I guess we do need to see the code, along with an explanation on what is wrong.

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

+-1 cm up to 100 cm

Seems like OP might be using an 8-bit timer.

"For every Christmas tree lit before Thanksgiving,
an elf drowns a baby reindeer"

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

Must i start a new post or just post the code on this one?

 

It started off with TCNT0 and TCNT1 now it is a distance meter for Atmega328p.

 

Does the ''notify me when a reply is posted ''still work?

 

I dont get any notifications but i receive other mails on the same e-mail adress,i did receive notification a few month back but not any more.

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

Is there any video on youtube that shows the distance up to 3-4m.

 

Most of the videos only show the distance on a ruler up to 300mm or movement of a hand around 100 to300mm.

 

Some web sites claim a distance of 4 meters and others say 4 meter round trip the datasheet of the HC-SR04 also give a distance of 4 meter.

 

I will investigate with my scope to see at what distance the sensor can work.

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

Does the ''notify me when a reply is posted ''still work?

seems to have stopped working for me a few weeks ago...not sure why 

When in the dark remember-the future looks brighter than ever.

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

mikedb wrote:
Must i start a new post or just post the code on this one?

 

It started off with TCNT0 and TCNT1 now it is a distance meter for Atmega328p.

If the topic of discussion no longer matches the title of the thread, then that seems like an obvious cue for a new thread!

 

You can, of course, give a link back to this thread in your new one - and, perhaps, also summarise any pertinent details.

 

And give a link here - to tell where the discussion has gone ...

 

 

EDIT

 

typos

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 14, 2018 - 04:32 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for all the help my confusion with TCNT0 and TCNT1 is now resolved .

 

I will start a new post under GENERAL ELECTRONICS regarding a HY-SRF05 ultra sonic sensor.

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

Hi

I will start a new post on GENERAL ELECTRONICS concerning the distance of a HY-SRF05 sensor.

 

https://www.avrfreaks.net/forum/...