A tricky problem with Attiny2313_what/ is this frequency

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

Hello there,

 

I'm too ashamed to post my own guess but please tell me, what would be the frequency on PINB.0 in the below code??. The code is very simple yet it won't work as intended, many many thanks in advance.

//// Weird problem with generated frequency !!!



#undef F_CPU
#define F_CPU 4000000UL // 4Mhz

#include <avr/interrupt.h>

#include <avr/io.h>





volatile unsigned int i=0 ;



void ioInit (void) ;
void Timer1_Comp_Init(void) ;

int main(void)
{
	
	ioInit () ;
	Timer1_Comp_Init() ;
	
    while(1)
    {
		PORTB = i ;// My intention is to get the "X" frequuncy on PINB.0; "X/2" on PINB.1 and ....

    }
}


///////=======================

void ioInit (void) ///PortB as output
{
	DDRB = 0xFF;
}


////===============================

void Timer1_Comp_Init(void) {

	OCR1A=500;

	TCCR1A = 0b00000000 ;
	TCCR1B = 0b00001001 ; ///Mode: CTC ; Top :OCR1A

	TIMSK  = 0b01000000 ; // to activate the "TIMER1_COMPA" interrupt
	
	sei ();
	
}

////=====================================

ISR(TIMER1_COMPA_vect ) // A simple increament; not more than 255
{
	i++ ;
	
	if (i>=256)
	{i = 0 ;
	}
}

 

This topic has a solution.
Last Edited: Wed. Jul 12, 2017 - 09:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a trick question?

 

Yes, your F_CPU says 4MHz.  Is that the speed that your AVR is really running at?  How have you proven that?

 

We can speculate much better if you tell us what you expect to happen, and what is happening.

 

OK, I'll bite for solving the exercises at the end of the chapter.  First a trip to the datasheet to decode the magic numbers...

 

So, 501 clocks at 4MHz is a bit over 125us, so 250us period, or a hair less than 4kHz?  There may well be a bit of jitter when the interrupt hits.

 

Does my homework get a passing grade?

 

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

theusch wrote:

Is this a trick question?

 

Yes, your F_CPU says 4MHz.  Is that the speed that your AVR is really running at?  How have you proven that?

 

We can speculate much better if you tell us what you expect to happen, and what is happening.

 

OK, I'll bite for solving the exercises at the end of the chapter.  First a trip to the datasheet to decode the magic numbers...

 

So, 501 clocks at 4MHz is a bit over 125us, so 250us period, or a hair less than 4kHz?  There may well be a bit of jitter when the interrupt hits.

 

Does my homework get a passing grade?

 

 

I didn't mean to be rude sir, I didn't post my version of calculation  ( which is also 4Khz) because the problem is so easy and yet:

 

 

1_ I built the circuit and stuff ... ; It says something about 500Hz

2_ I simulated  an attiny2313 using Proteus and this code; It says about 500Hz,

 

 

3_ I used the default "simulator" in the atmel studio (I'm not sure what it is, the default one) ; using some pause/start and changing the breakpoints ; it says about 4khz.

 

And, here I am ?!?!?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

500Hz to 4kHz is about a factor of 8

What is the CKDIV8 fuse?

 

Edit: Did you set the CLKPS bits in CLKPR?

David (aka frog_jr)

Last Edited: Wed. Jul 12, 2017 - 08:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

M3rry wrote:
It says something about 500Hz

Who is "It" saying this?

 

Note that 500 is [nearly] exactly 1/8 of the expected hair over 4kHz. 

M3rry wrote:
3_ I used the default "simulator" in the atmel studio (I'm not sure what it is, the default one) ; using some pause/start and changing the breakpoints ; it says about 4khz.

So your AVR really isn't running at the speed you think it is?  Remember that >>you tell<< the simulator what speed to use.

 

If "It" is a 'scope or logic analyzer (or if you have a frequency meter), it might be useful to set the fuses for "clock out" and meter the CKOUT pin.

Alternatively, if you have such a tool, you can set up a timer with CTC mode and connect the output pin (no interrupt) and generate clk/2.

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.

Last Edited: Wed. Jul 12, 2017 - 09:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply,

 

CKDIV was the tricky thing , 

Problem solved

 

 

Last Edited: Wed. Jul 12, 2017 - 09:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

M3rry wrote:
It says something about 500Hz

Who is "It" saying this?

 

Note that 500 is [nearly] exactly 1/8 of the expected hair over 4kHz. 

M3rry wrote:
3_ I used the default "simulator" in the atmel studio (I'm not sure what it is, the default one) ; using some pause/start and changing the breakpoints ; it says about 4khz.

So your AVR really isn't running at the speed you think it is?  Remember that >>you tell<< the simulator what speed to use.

 

If "It" is a 'scope or logic analyzer (or if you have a frequency meter), it might be useful to set the fuses for "clock out" and meter the CKOUT pin.

Alternatively, if you have such a tool, you can set up a timer with CTC mode and connect the output pin (no interrupt) and generate clk/2.

 

Sorry I took quite a lot of your time over an unimportant matter,
Thanks for your kind reply,

I try all these ways on my next problems

:)

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

The AS7 Simulator works fine.   It will report the correct number of cycles.

However  you need to tell it the correct F_CPU.   For it to convert cycles into microseconds.

 

I would guess that Proteus would simulate correctly too.    Likewise,  you have to tell it the actual frequency.   Which will depend on the clock source and any prescaler.    These are set by fuses.   So I would guess that you must tell Proteus and AS7 the fuse settings.

 

Note that the GCC Compiler will produce complete rubbish if you set the Optimisation to -O0

So you need to inspect the ASM view of your while(1) loop.

 

I do not have a Proteus licence.

We do not know your AS7 project settings.

 

David.