Problem with Timer/Counter1

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

i have a problem with timer/counter1 (8bits use)
i used timer/counter0 and timer/counter1 at the same properties(timer/counter0:FAST PWM Top:FF,non inverted output-----timer/counter1:FAST PWM TOP:00FF,non inverted output.....at the same clock)
and OCR1AL=OCR0
and wrote my program(generate Sinusoidal wave)
but the output wave form from OCR1 and OCR 0 is different!!
see these images:
[spam img links removed]
what`s wrong?
(i don`t want to use Timer Counter 2 or...because i need it for another use....i want to use T/C1 in 8bit mode..please help)
Regard

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

Is this simulated output, or output from a real AVR?

Let's see the actual timer setup code, not the Wizard spec.

What AVR model are you using?

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

Perhaps the cause of your current problem or perhaps not, but:
It is not safe to write only to OCR1AL. At the same time the content of a temporary register is transfered to OCR1AH. Since this temporary register is shared between different 16 bit registers, it can contain unexpected rubbish at that moment.

Stefan Ernst

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

Only using 255 of 256 values is interesting, also, but I think Stefan is closer to the root cause.

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

it`s simulated output and i use mega64(and test for atmega 32)
here is the source code:

#include  
int i;
    flash char sinewave[]={
        128,131,134,137,140,143,146,149,152,156,159,162,165,168,171,174,
	176,179,182,185,188,191,193,196,199,201,204,206,209,211,213,216,
	218,220,222,224,226,228,230,232,234,236,237,239,240,242,243,245,
	246,247,248,249,250,251,252,252,253,254,254,255,255,255,255,255,
	255,255,255,255,255,255,254,254,253,252,252,251,250,249,248,247,
	246,245,243,242,240,239,237,236,234,232,230,228,226,224,222,220,
	218,216,213,211,209,206,204,201,199,196,193,191,188,185,182,179,
	176,174,171,168,165,162,159,156,152,149,146,143,140,137,134,131,
	128,124,121,118,115,112,109,106,103,99, 96, 93, 90, 87, 84, 81, 
	79, 76, 73, 70, 67, 64, 62, 59, 56, 54, 51, 49, 46, 44, 42, 39, 
	37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 13, 12, 10, 
	9,  8,  7,  6,  5,  4,  3,  3,  2,  1,  1,  0,  0,  0,  0,  0,  
	0,  0,  0,  0,  0,  0,  1,  1,  2,  3,  3,  4,  5,  6,  7,  8,  
	9,  10, 12, 13, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35, 
	37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76, 
	79, 81, 84, 87, 90, 93, 96, 99, 103,106,109,112,115,118,121,124
};
// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
   OCR0=OCR1AL=sinewave[i];
   i++;
   if(i==255)
   i=0;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x30;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTE=0x00;
DDRE=0x00;

// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In 
// State4=T State3=T State2=T State1=T State0=T 
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
ASSR=0x00;
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x81;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: CTC top=OCR2
// OC2 output: Disconnected
TCCR2=0x0A;
TCNT2=0x00;
OCR2=0x09;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;
ETIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here

      };
}

how can i use T/C1 in 8bits mode?

-------------
Regard

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

Quote:
how can i use T/C1 in 8bits mode?

You are already in 8 bit mode. You just have to set all 16 bits of OCR1A, even if the upper 8 bits are 0.

Regards,
Steve A.

The Board helps those that help themselves.

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

Also:

Quote:

it`s simulated output

How have you proven that your simulator models the actual behaviour of the real chip?

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

After I have seen the complete code, I don't think the access to only OCR1AL is the cause, because there is no further access to any 16 bit register after the last access during initialization (OCR3CH=0x00;). So the temporary register should contain zero all the time.

But nevertheless I would change it of course.

Stefan Ernst

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

theusch wrote:
Also:
Quote:

it`s simulated output

How have you proven that your simulator models the actual behaviour of the real chip?

i simulate it in every mega series that have 16 bits T/C for 8-bit mode(mega16,32,64,128),and the result`s the same.
that`s because i think i don`t use 16-bit T/c True, in 8bits mode.
do you think it`s simulator fault?...maybe.but because i don`t have oscilloscope right now i have to use simulator!
but if anyone thinks something else,please share...

---------
Regard

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

Which simulator? When it comes to timers AVR Studio simulator 1 is pretty hopeless and you could waste a lot of time chasing its deficiencies. Simulator V2 is better (not always!) but has limited AVR support. Proteus seems to cause nothing but problems and there have been many threads where it's clearly the simulator in error.

To "see" what's going on inside an AVR a far better solution is to hook up an On Chip Debug interface between the PC and the AVR and see what happens in the real silicon rather than some half baked virtual model of it.

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

It looks like the glitch occurs when you load 0s into the timer register. For the heck of it, try changing the 0s in your table to 1s and see what happens. But I agree that trusting a simulator to tell you what the real chip would do is a mistake. At best it can give you a warning about a possible problem. It can also give you false negatives and false positives.

And yes, please load the 16-bit register as specified in the datasheet. Just change "OCR1AL=" to "OCR1=".

And I also wonder why you're ignoring the last entry in your table by going from 254 to 0.

Mike

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

Quote:
Which simulator?

I use proteus V7.2 SP6
Quote:
It looks like the glitch occurs when you load 0s into the timer register. For the heck of it, try changing the 0s in your table to 1s and see what happens

yes,it works true!
but what`s wrong with 0s??how can i fix it?do you think it`s simulator fault?
Quote:
I also wonder why you're ignoring the last entry in your table by going from 254 to 0

it`s my fault!thanks to say it.
Quote:
Just change "OCR1AL=" to "OCR1="

i`ve to Specify which register i`m using...A ,B,or...
with 0s in table,i change OCR1AL to OCR1A but it doesn`t work true
i think i`ve to use real chip,and it takes few days for me....but thanks all

-----------
Regard

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

Quote:

I use proteus V7.2 SP6

Given the number of threads that have been posted here about Proteus I would have almost 0 confidence in any result it may give you. See what the AVR Studio simulator (preferably V2) makes of it. Better yet put the code into an AVR and inspect its operation with an OCD interface