Why is OCR0 is a signed byte

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

 

 

Hello All,

 

I'm having trouble trying to use PWM on an AtMega328P using AVR Studio 7. When I set up timer0 in an attempt to use OCR0A and OCR0B in CTC mode the values are saved to those registers as SIGNED values.

Whenever I try to place any value higher than 127 in OCR0A or OCR0B it rolls over into the negative domain.

1. Is there a way I can change these two registers to hold unsigned values?
2. Is this a compiler issue or a on-chip register issue?

 

#include <avr/io.h>
#include <avr/interrupt.h>

ISR(TIMER0_COMPB_vect)
{
 PORTD &= ~((0x01<<PORTD6) | (0x01<< PORTD7));
}

ISR(TIMER0_COMPA_vect)
{
 PORTD |= (0x01<<PORTD6) | (0x01<<PORTD7);
}

void PWM(void)
{
 //OCR0A = top of period in CTC mode
 uint8_t period = 255;
 OCR0A = period; //watch added to OCR0A, IT'S NEGATIVE ???????????????????????
 OCR0B = 129; //ALSO A SIGNED VALUE ??????????????????????????????????
 //3. Ratio of OCR0B/OCR0A gives duty cycle

 sei();
}

int main(void)
{
 //TIMER 0 set up:
 TCCR0A |= (0x01<<WGM01); //set up CTC mode for OCR0A
 TCCR0B |= (0x01<<CS00); //enable timer w/ 1 prescaler. Smallest resolution for PWM
 TIMSK0 |= (0x01<<OCIE0A) | (0x01<<OCIE0B); //Enable interrupts for OCR0A and B
  
    PWM();
   
    while (1)
    {
    }
}

 

If you run the above code and add watches to OCR0A and OCR0B you will see that they are negative values...All the documentation EVERYWHERE states that the register max is 255.

Please help. This is severely screwing up my PWM.

 

This topic has a solution.
Last Edited: Wed. May 31, 2017 - 06:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only hint you've given is 'watch' - are we to assume you've drawn your conclusion from the value shown in the debugger watch window? You put 129  which represented as a signed value would be displayed as -127. Both values are still 0x81. So where is the problem?

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

Nothing to do with Studio bugs, moving to mega forum.

 

Sorry it seems I was wrong, movedback to Studio forum.

 

 

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Wed. May 31, 2017 - 06:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is entirely a presentation problem.

If the signed value presentation is in Atmel Studio while debugging/simulating then you can modify how the value is displayed. The studio help has more om modifying the display in e.g. the watch window.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

js wrote:
Nothing to do with Studio bugs

You keep saying that, but I can't see any forum that is restricted specifically to Studio bugs:

 

Since the OP is asking specifically about a feature of Atmel Studio, it would seem that it should be in the 'Atmel Studio (AVR-related)' forum - surely?

 

(in fact, it's probably more of a 'General Programming' issue - as the same thing can arise in anything that displays "raw" values...)

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right click the watch and you have the option to switch to a hex display. In that you will just see 00..FF which are much easier to interpret. (programmers don't really use decimal do they?)

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

I'm suspecting the implementation is the cause of the upset - prescale of 1 on an 8 bit timer with two interrupts - if those interrupts get too close together in time, then the execution time of the isrs come into play. If we guess at around 50 cycles per isr, the range of pwm is rather limited.

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

Thanks for the speedy reply. Yes, I was using a watch with the debugger and noticed that the value was negative rather than what I expected it to be. Does this mean that regardless of what the debugger is displaying the register holds the full unsigned value?

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

Yes.

Again: The interpretation and display as signed or unsigned is in the debugger display. The register is literally eight binary bits - nothing more.

The signed - 1 and the unsigned 255 is the exact same bit pattern in eight bits.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

LeydenJar039 wrote:
Does this mean that regardless of what the debugger is displaying the register holds the full unsigned value?

I think you missed the point.

 

The point is that what the debugger displays does not in any way affect the bit values in the underlying hardware.

 

You have to adjust the debugger to show the particular interpretation/representation that is most convenient for you at the time.

 

0xFF, 255, -128 (sic) are all ways to interpret the same underlying bit pattern.

 

EDIT

 

Ahem: Of course, I mean: 0xFF, 255, -1 are all ways to interpret the same underlying bit pattern.

 

blush

 

https://www.avrfreaks.net/comment...

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: Tue. May 30, 2017 - 12:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> 0xFF, 255, -128 are all ways to interpret the same underlying bit pattern.

Double-check your two's complement just above, awneil... ;-)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. May 30, 2017 - 12:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First, thanks for the speedy reply.

 

If I'm running the chip at 16MHz, shouldn't the 50 cycles be inconsequential because they're so small...ahhh, but the Timer0 "sees" cycles so if the two OCR0n values are within 50 of each other then there will be an issue.

If I use the function _delay_ms(), how will that affect the ISR. Can I use it to build a bit of space between OCR0A and B interrupts?

 

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

Excellent! I get it.

Thanks for help guys. 

 

 

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

Why on earth would you want to call _delay_ms()? Already your ISR() times could be "borderline". You don't want to compromise that further.

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

JohanEkdahl wrote:
Double-check your two's complement just above, awneil... ;-)

blush

 

post updated

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only reason i could see that you would implement pwm in the manner you have is to have the freedom to choose the port pins. You now know the limitations. The timers have specific pwm modes that do exactly what you want in hardware - no interrupts needed. The downside is that you need to use specific port pins.

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

You keep saying that, but I can't see any forum that is restricted specifically to Studio bugs:

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

 

I guess a lawyer could interpret or misinterpret what point 2 says.

 

2. This is forum is only for questions relating to AVR Studio - problems, clarifications, bugs, etc. If your topic is a general AVR question please post it in the proper forum (ie: not this one).  

oops I forgot people don't read stickies. wink

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

John, the question could well be deemed one about clarification of a Studio functionality.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

js wrote:

2. This is forum is only for questions relating to AVR Studio - problems, clarifications, bugs, etc. If your topic is a general AVR question please post it in the proper forum (ie: not this one).  

I think that's pretty clear that it's for any questions relating to AVR (sic) Studio.

 

It is clear that the question here is not specific to AVR.

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moved back to Studio forum by popular demand. I will no longer do any thread moving unless someone specifically asks for a thread to be moved to a specific forum.

 

And let's ger rid of the the ARM studio forum, questions can be asked here.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
let's ger rid of the the ARM studio forum

I agree that the division into "communities" is stupid and does not serve the purpose at all.

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
unless someone specifically asks for a thread to be moved

No worries there ... !

 

wink

 

Quote:
to a specific forum

Another stupid forum feature: A user can only request that a thread be moved; there is no way for a user to specify - or even suggest - where it should be moved to!

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

there is no way for a user to specify - or even suggest - where it should be moved to!

Yes there is, I asked before that a post be made in the forum that needs to be moved.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly