Atmega 1284 OC0A

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

Hello,

 

I have the following code :

 

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

int global = 0;
ISR(TIMER0_COMPA_vect)
{
    global++;
}

int main(void)
{
    TCCR0A = (1 << COM0A0);
    TCCR0B =
    DDRB = 0xFF;
    TIMSK0 = (1 << OCIE0A);
    OCR0A =  50;
    TCCR0B = (1 << WGM02) | (1 << CS02) | (1 << CS00);
    sei();

    while (1)
    {

    }
}

shouldn't this code toggle OC0A

what am i doing wrong here ?

 

Thanks,

Lorant

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

DajkaLorant wrote:
int global = 0;

Variables used in code and in ISRs should be volatile.

DajkaLorant wrote:
TCCR0B =
???

 

Comments in the code would be useful.

 

From datasheet:

WGM02 set and the others 0 (Mode 4) is a reserved mode.

 

(For posting code, use the button that looks like "<>")

David (aka frog_jr)

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

When you fix the TCCR0B value to the mode for fast PWM toggling, then the PD6 pin should toggle. 

 

But how can you tell if it is?  By my quick calculations [assuming 8MHz clock, with timer0 prescaler set at sysClk/1024, OCR0A == 50], there will be a toggle every 6 milliseconds (or about 170 Hz if the pin were connected to a speaker through a 47 ohm resistor).  A simple LED would appear to be on dimly at this setting.  To detect this pin toggling at this speed, you need an oscilloscope (or a speaker to will beep if you are in audio range ~100-5KHz,  Yes, I know that audio range is higher, but I'm talking about detecting OC0A toggling with a tiny speaker here.)

 

Your code isn't really code, it's a collection of C language statements that appear to configure registers for some CPU using the capitalized menomics for those registers.  That would be code if it were commented before it was posted in public forum, but it's not, so it's just a bunch of code lines.

 

Is this some kind of assignment for some kind of school? If so, then tell us the name of the school and the name of the class.   Bozo State University, Bozoistan Republic,  Dept of Engineering; freshman class: EE105 Introduction to Microcontrollers;  lab homework assignment #3,  ...etc...

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

Hi,

 

I just buoyed myself an atmel-ICE and an Atmega-1284.

It is not a school project, i'm just playing around with it's features.

 

Regards,

Lorant

Last Edited: Thu. Oct 12, 2017 - 06:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What's your goal? Even playing around you should know what you want to achieve.
.
MG

I don't know why I'm still doing this hobby

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    TCCR0B =
    DDRB = 0xFF;

As frog pointed out that is a bit curious. In C whitespace does not matter so the line is really:

    TCCR0B = DDRB = 0xFF;

which is setting both those registers to 0xFF. I know you later:

TCCR0B = (1 << WGM02) | (1 << CS02) | (1 << CS00);

which then corrects things but this is a dangerous habit to get into!

 

Actually I say it corrects things but as frog also said, just setting WGM02 means you are selecting mode 4 that is "reserved" I imagine you actually want mode 3 for starters which has WGM02=0 and both WGM01 and WGM00 set. The latter two bits are not in TCCR0B, they are in TCCR0A.

 

Putting all that together gives:

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

volatile int global = 0;
ISR(TIMER0_COMPA_vect)
{
    global++;
}

int main(void)
{
    // Toggle output on compare, Mode 3 - Fast PWM (Top = 0xFF)
    TCCR0A = (1 << COM0A0) | (1 << WGM01) | (1 << WGM00);
    // Following could be just (1 << 7) as OC0A = PB7 as output
    DDRB = 0xFF; 
    // enable the TIMER0_COMPA interrupt
    TIMSK0 = (1 << OCIE0A);
    // initital duty is 50:205 = 19.6 %
    OCR0A =  50;
    // Start the timer with F_CPU/64 prescaling
    TCCR0B = (1 << CS02) | (1 << CS00);
    sei();

    while (1)
    {

    }
}

 

Last Edited: Fri. Oct 13, 2017 - 08:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

Thank you for your answer clawson.

I guess in the debugger i cannot see the port toggling only if i use an oscilloscope.

 

Thanks,

Lorant 

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

I believe the simulator is good enough to show this. Be warned though that time pases very slowly in the simulator so it may be a few thousand clicks around your while(1) loop before you might expect to see anything change. For simulation you may want to increase the timer speed from \64 to \1

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

Hi clawson,

 

What does the Update of OCR0x means in table from page 143 (http://www.atmel.com/Images/Atme...)

 

Thanks,

Lorant

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

Can't access your link - can't you just post a screen grab of the bit you are asking about?

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

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

In some modes you can write a new OCR value but it won't come into effect immediately but will be buffered and only as the timer passes TOP or BOTTOM does the buffered value finally come into effect. Effectively one change per cycle. In other modes the change is immediate.

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

Hi,

 

Thanks for your fast response.

 

BR,

Lorant

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

hello,

 

What would be the difference between Mode 3 and mode 7.

Since on mode 3 i can sense a difference with the multi-meter (averaging of voltage)

 

Best Regards,

Lorant

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

Read the table. Mode 3 has TOP fixed at 0xFF so the frequency cannot be changed (only the duty) but in this mode channel / output A is available. In mode 7 top (and hence frequency) is variable but to do this it uses the OCRA register which means that in this mode out / channel A cannot be used for PWM. In mode 7 the only channel / output you can use is B not A.

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

DajkaLorant wrote:
What does the Update of OCR0x means in table from page 143

From the data sheet, page 132:

The OCR0x Registers are double buffered when using any of the Pulse Width Modulation (PWM) modes. When double buffering is enabled, the CPU has access to the OCR0x Buffer Register. The double buffering synchronizes the update of the OCR0x Compare Registers to either top or bottom of the counting sequence. The synchronization prevents the occurrence of odd-length, non-symmetrical PWM pulses, thereby making the output glitch-free.

 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"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

Hi,

 

I've seen in the simulator that if used in mode 7 the counter (TCNT0) counts to OCRA than it resets to zero.

In mode 3 it counts up to 0xFF.

If no output on OC0A is produced for mode 7 what would be the use case for this mode ?

 

BR,

Lorant

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

DajkaLorant wrote:
I've seen in the simulator that if used in mode 7 the counter (TCNT0) counts to OCRA than it resets to zero. In mode 3 it counts up to 0xFF.

Just as was pointed out earlier, in the table in the datasheet.

 

 

DajkaLorant wrote:
If no output on OC0A is produced for mode 7 what would be the use case for this mode ?

Well, there >>is<< an output if you have made the pin an output and set the COM bits.  But not the waveform you expect.

 

When e.g. OCR0A is used as TOP, generally that is used to produce a desired waveform on another timer channel -- OC0B, OC0C, ...

 

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:
But not the waveform you expect.
I believe that would be better said as "But not a PWM waveform."

Operation depends on WGM mode, for example:

  • In Fast PWM (Mode 3), OC0A can be set low or high on match.
  • In Fast PWM (mode 7) OC0A can be toggled, set low or set high on match.

These can be useful for certain requirements...

David (aka frog_jr)

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

Hi,

 

Does anyone have a good documentation with examples (besides datasheet) what kind of wave forms does these settings generate ?

 

Thanks,

Lorant

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

It's in the data sheet! 

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

clawson wrote:
It's in the data sheet!

But that's not good enough. :(

 

I guess OP needs to tell u s what in particular is lacking in the datasheet for a particular mode and other setting combinations.

 

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

Hi,

 

What waveform should i expect from mode 3 and mode 7

 

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

Hi

 

with mode 7 and COM0A set to 2 i get constant 5 volts

on mode 3 and COM0A set to 2 i get a voltage which depends on OCR0A (on multi meter i get an average voltage)

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

Looking at the datasheet:

 

In Mode 3 with COM0A set to 2, then OC0A will be cleared at match and then go high when the counter wraps to 0 (BOTTOM).

In Mode 7, the special case described in the note occurs. So (if COM0A is set to 2),  the OC0A will be set and stay there till some overt action sets it low.

 

These are just as you observe.

 

David (aka frog_jr)

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

I wonder if you're missing the point? Don't use A, use B. In that case both 3 and 7 will generate a square wave with variable on/off time with the ratio set by OCRB. The difference is that in mode 7 you can use OCRA to vary the frequency (while in mode 3 it is fixed). 

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

hi

 

with OCR0B set i managed to generate a waveform (with my multi meter average voltage) in both mode 3 and 7.

Is this documented that it does not work with OCR0A ?

 

Thanks,

Lorant

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

Have you read the manual or listened to anything I've said so far in this thread? Both of us tell you that in mode 7 OCR0A is used to set the frequency, so it can't be used to set duty so output A is not controllable for PWM if there's nothing to set the duty. (OCR0A is being used to set frequency (ie TOP) in this mode. Don't pick mode 7 if you want to use A for PWM.

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

DajkaLorant wrote:
Is this documented that it does not work with OCR0A ?

Seems only screaming will help..

 

YES! IN THE D-A-T-A-S-H-E-E-T!

 

We understand that reading the datasheet might seem to be a daunting endeavor when new to microcontrollers, but there is no way around it. If you - on first read - spend five minutes on each page of the timer chapter to get acquainted with it then that's a few hours of reading. After that you should have a rough to fair  understanding of the capabilities and functionality of the timer. You then return to read in detail about things you really need to master.

 

We all do this all the time. There is no way around that. There is no free lunch. There is no easy way around it. Face it. Read.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"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

Ok :)

i'll try harder