1 ms CTC delay? Is this correct?

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

Quote:
Is this OK, with 1MHZ internal osc for atmega8?

#include 
#include 
#include 

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif


 static double glb_ms_counter;
 static double glb_max_ms;
 static int error_corrector;
ISR (TIMER1_COMPA_vect) //(SIG_OVERFLOW1)
{ 
	glb_ms_counter ++;
	error_corrector++;
	
	if(error_corrector==50)
	{
		error_corrector=0;
		glb_ms_counter--;
	}
	
	if(glb_ms_counter==glb_max_ms) 
	{
		sbi(DDRC,5);
		cbi(PORTC,5);
		while(1){}
	}
		
	
}

int main(void)
{
	sbi(DDRC,5);
	sbi(PORTC,5);
	
	sbi(DDRC,4);
	sbi(PORTC,4);
	
	glb_ms_counter=0;
	glb_max_ms=1000;
	error_corrector=0;
	
	
	
	sbi(DDRC,4);
	cbi(PORTC,4);
	
	TCNT1H=0x00; 
	TCNT1L=0x00; 	
	OCR1AH=0x00; 
	OCR1AL=0x01; 	//for 1ms CTC
	TCCR1A=0x00; 
	TCCR1B=0x0D; // CTC + 1024 presclar
	TIMSK|=0x10; // CTC interrupt	
	SREG |= 0x80; //global interrupt			
	
	
	while(1){
	_delay_us(200);
	
	}

	return 0;
}

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

The problem i am facing is, for 1000 counts (1000ms), code is creating delay of 2.12Sec???

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

There is no code for calibration of the internal oscillator.
Do you use it in the real code?

Regards,
--
Artur Lipowski

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

Yes. Using in real code.
Can you help me to write this calibration code....

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

yellowboy_75 wrote:
Can you help me to write this calibration code....

Just read calibration values using your programmer. Write it into EEPROM or FLASH (e.g. as a simple const). Then at the begining of your program use it by writing appopriate (for 1, 2, 4 or 8 MHz) value into OSCCAL.
That's all.

Regards,
--
Artur Lipowski

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

1) When using CTC mode to count up to N, you need to set OCR1A=N-1
(refer to Figure 42 in the data sheet), so in this case you need OCR1A=0.

2) It looks (?) as though you're using error_corrector to correct for the fact that
you're actually counting 1.024ms/OC event, rather than 1.000ms. If so, I
think you're correcting glb_ms_counter backwards -- you should be adding
this leap-ms instead. A simpler approach might be to just use prescale /1
and set OCR1A=(1000-1).

3) The internal oscillator can drift by +/- 5% due to temperature/voltage (refer
Figure 179-180); the big discrepancy you're seeing is due to (1)-(2) above.
If you need even higher accuracy than this, you need an external 32kHz
crystal or some other external reference.

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

Implemented nopresclar option.
Still I get 6.17 sec when i count for 6000 ms.
It could be error of the thirdparty timer and some in mega8's internal oscilator.

YB

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

I ran into the same problem using a 12Mhz crystal. I haven't figured out the problem yet - it hasn't become that important yet - but I thought mysery may love company. I figured that it was an error in the freq of the crystal. I plan to build a calibration board that has a cheap stopwatch built into it. The Uc would time an event and also start and stop the stopwatch. I would use the difference between the two to correct for the error.

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

This was using an external crystal? These are normally specified on the
order of 50ppm (0.005%), so a 3% error is huge. Did you set the clock
fuses appropriately?

The drift of the internal oscillator is not theoretical -- I've seen it in action
by carrying my device outdoors on a brisk November evening. It took about
90 seconds before my (moderately finicky) ultrasonics drifted completely
out of spec.

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

Quote:
I plan to build a calibration board that has a cheap stopwatch built into it.

How is a cheap stop watch going to be more accurate than the crystal? How much error are you getting? If it is more than 4 or 5 seconds per day, I would say that there is something wrong with the code, not the crystal.

Regards,
Steve A.

The Board helps those that help themselves.