Adjusting TCNT1, Any Issues???

Go To Last Post
60 posts / 0 new

Pages

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

yellowboy_75 wrote:
Quote:
If you only need a resolution of 1s (You're trying to count 12000ms = 12s)
you can use a prescaler of 256 for Timer2 and set OCR2 = 127.
This would be the most exact solution.

I have a much larger actual project, of which, i am stucked at 1ms exact delay part.
The 12000ms = 12s is used for testing, but in actual project it could be from 0.1 sec to several minutes.

So i simply need exact 1ms interrupt routine....

Well 32.768 is designed for RTC applications which use a 1s period minimum. You can get smaller increments than that as I'm showing above (for 1.024ms) but I don't know of any suitable low frequency crystals which would yield better results. If your application allows it you could switch to a real crystal as your main AVR clock, but this would require extra capacitors and may increase power consumption. You're either going to have to live with a slightly too long (by .024ms) delay time, or bare the cost (both price and power) of a proper main AVR clock crystal.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Have you tried it to run the Timer2 from MCLK for test purpose ?
Are you sure your sbi(...) works well ? Try ASSR|=(1<<AS2); !
Do you know how delay works (Timer ?) ?

Last Edited: Wed. Jul 5, 2006 - 10:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thing started workin now, initially crystal legs were too long, more tahn 10 cm wire conencted (dont laugh at me, i was avoiding soldering!!!) but now placed crystal closed to pins so, my code working.
Dean, in your code you have missed "WGM21" for CTC.
Hope I am correct now.......

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

In addition to what Dean has said:
You want one interrupt each 1ms, that means the interrupt will occur every 1000 clock cycles (assuming the AVR runs at 1MHz)
Accessing large global variables will increase the ISR overhead considerable (PUSH/POP Registers to/from stack).

The remaining non ISR clock cycles would be consumed fast and you'll get undetermined behavior of the AVR.
Better you use a 8MHz crystal to drive the AVR.
If your project is finished you can rewiew your code and check if you can reduce the clock frequency.
This would avoid much frustration.

Regards
Sebastian

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

Quote:
Better you use a 8MHz crystal to drive the AVR.

Not necessarily. With the timer accuracy being controlled by the watch crystal, the accuracy of the main clock frequency isn't important so the internal RC oscillator is fine for the job. I believe most AVRs with an internal oscillator have at least an 8MHz RC setting, so yellowboy check your fuses. You should be able to set the internal oscillator to 8MHz RC which will result in quick execution rate with no additional cost.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Agreed with your view Sebastian.
But if my client is happy with this, its all ok.

I applied a error correction in ISR.
as per the code .024ms is error every 1ms.
So for 1000ms, i adjusted 24ms.
Its working good.

if(error_corrector==1000)
{
error_corrector=0;
glb_ms_counter=glb_ms_counter+24;
}

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

At last I am 100% successful in achieving a 1ms exact delay using interrupt for ATmega8 with internal 8Mhz osc.

I was totally new when I started this Topic.
s-sohn and abcadminuser helpd me to get going.
Now I can figure out the problems. I gained good experience during working on my project in which I have to generate 1ms delay with 98% accuracy.

These are few words about AVR for all new comers:

Quote:

1. AVR is an excellent controller.
2. If you are coming from 8051 to AVR, you may feel AVR slightly difficult initially, but after a months head bang-bang, you will start loving it. Remember that you have to bang your head coz you DONT have enough knowledge, its NOT that AVR has any problem.
Remember that SYSTEMS are always perfect.
3. While using any AVR, if your project demands a PRECISE time DELAY e.g. something should happen every 1ms or Serial transmission at a particular speed or you have critical math calculations then ALWAYS use an EXTERNAL CRYSTAL instead of INTERNAL R-C Crystal. If you use internal R-C oscillator then you MUST write a OSCILLATOR calibration code and must be called as you enter in to main().
4. I will be posting the code i used for calibration in the next reply. I modified the Buttload's code.
5. AND last but NOT least, is, DONT MAKE YOUR HAND DIRTY WITH ASSEMBLY language in AVR. ALWAYS use C language to code for any AVR, may your project is very simple

Thanks friends for teaching me.... hence forth i will be able to guide newcomers at avrfreaks.net

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

Great to hear it all worked out so well! You are a welcome new addition to AVRFreaks - you'll be helping others like yourself in no time.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

abcminiuser wrote:
Great to hear it all worked out so well! You are a welcome new addition to AVRFreaks - you'll be helping others like yourself in no time.

- Dean :twisted:

Dito!

Sebastian

Pages