CTC on atmega32.

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

I need a CTC wave output 

 

Last Edited: Fri. Aug 3, 2018 - 10:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Disclosure - we have already had a bit of a dialog in PM:

 

vcoder 1:42 pm

vcoder's picture

I am using atmel atmega 2560.

 

I need to write a pwm code to give a constant output at exactly 32KHz at any duty cycle.

I am extremely new to this and desperately need help. I have read the datasheet and tried to understand the most I could.

I think we need to use a 16 bit timer in fast pwm mode.

 

Could you please help me out with this?

You 2:11 pm

clawson's picture

Suggest you start a thread so you get help from everyone but this all starts with your choice of clock for the 2560. How fast do you plan to run it? That may then dictate how feasible 32kHZ PWM is. Also how accurate does this 32k have to be anyway. If only 31.25k were achievable would that work for example?

 

Anyway the 16 bit timer has FAST PWM modes where the frequency us variable (either using ICR or OCR1A). That register varies the frequency while OCR1B (and maybe OCR1C) will vary the duty ratio.

vcoder 2:24 pm

vcoder's picture

It has to be exactly 32KHz. I planned on running it at 16MHz with prescaler one. If we set top as 499 the frequency comes out to be exactly 32KHz in fast pwm. 

 

I need the frequency to be fixed for variable duty cycles. Can I not use OCR1A then?

You 3:15 pm

clawson's picture

OK well 16,000,000 / 32,000 is exactly 500 so a timer with a 0 to 499 counting range run at 16,000,000 / 1 will overflow (reach TOP) at exactly the 32kHz rate. So this sounds "do able". The timer will have to run at /1 as none of the other divisions give a full integer divide of 500. For example is you set the CS1n bits to run timer 1 at /8 then you would need TOP at 500/8 but that is 62.5 which is not representable in integers - you could not program ICR/OCR with 62.5

 

But, yeah, 16MHz and /1 will work.

 

As the manual shows:

 

 

You will want to set your WGM bits to select either mode 14 or mode 15 depending on whether you want to use ICR1 or OCR1A to set TOP (to 499). If you use mode 15 then you cannot use the OC1A pin for PWm output as the controlling register is already tied up for setting frequency, not duty., For this reason most people would choose mode 14.

As the timer will count 0..499 (500 steps) then that is the granularity of your duty control. So 0.2% steps.

vcoder 3:22 pm

vcoder's picture

Yes. Thank you. 

Another thing... what do we do with TCNT1 do we increment it in the while loop using TCNT1++ or what? 

How do we use TOVn flag?

You 3:46 pm

clawson's picture

No the whole point of a timer is that it runs independently. As soon as you set the CS10 bit the timer will start to run and it will "tick" up automatically at the agreed rate. If you had set /8 for example then TCNT1 would increment 2 million times per second. With /1 then it will run at the same rate as F_CPU. So 16MHz. So it will reach 500 in 500/16000000 s, that is 31.25us which is the 32kHz rate.

 

Once it has started (and I'm assuming you will set some COM bits so the output pin is driven (once you set it to output in the DDR register)) then the timer will simply count in a cycle from 0 to 499 (the value you put in the register that defines TOP). As it rolls over from TOP to BOTTOM the output pins will likely be set one way, then when the TCNT1 matches the duty value you put in OCR1[A/B/C] the pin will change state the other way - this creates the variable duty (by you varying the OCR value) as the timer runs. So that is the only thing you really need to do. Once started everything runs automatically and the only interaction you make is to vary the duty by rewriting the OCR register. If you want to stop the timer just set all the CS bits back to 0.

vcoder 4:09 pm

vcoder's picture

Ive attached my current code. Evidently its full of mistakes. 

Could you please help me with them. 

Attachment(s): 

Image icon WhatsApp Image 2018-08-02 at 8.33.26 PM.jpeg

You 4:19 pm

clawson's picture

Don't just type random bit/register names at your C editor and expect something to happen! Design this from the outset and then the implementation will be obvious. There are plenty of articles in the tutorial forum on this site about using timers and PWM etc.

 

Hint: YOU don't do TCNT==OCR comparisons in your code because that is exactly what's built into the timer to take the load of doing something like that from your software. As I say, PWM is "fire and forget". Once you start the timer and it begins PWM the only interaction is to vary the OCR register to vary the duty.

 

As I said at the start, if you posted this in a forum you would be getting advice from 50 other people too, not just one.

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

.

Last Edited: Fri. Aug 3, 2018 - 10:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
already had a bit of a dialog in PM

I just noticed that I did as well.

 

vcoder wrote:
It has to be exactly 32KHz.

How much would you like to wager that te requirement is >>really<< 32.768Hz?  OP should expand on this.  And define "exactly" -- even cooked crystals are off a bit.

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

From further PMs I received it seems OP has gone off in a bit of a huff because I published our PM dialog. But what else could I do, without it folks would like waste time re-exploring a lot of what had already been said.

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

clawson wrote:

From further PMs I received it seems OP has gone off in a bit of a huff because I published our PM dialog. But what else could I do, without it folks would like waste time re-exploring a lot of what had already been said.

How odd.  That PM dialog shows nothing but OP needing help, and you going to a lot of effort to provide that help.

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

I simply don't answer PM help unless I "know" the person.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Smells like a student who knows his professor monitors forums for cheaters.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

This is crazy.     A student that has made an effort to solve her project is free to ask technical questions on a Public Forum.

 

The Professor may not like it but it seems perfectly reasonable to me.

 

The student who uses Private Messages in the hope that the Professor or fellow students do not find out is WRONG.

 

David.