View previous topic :: View next topic |
Author |
Message |
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
Posted: Mon May 20, 2019 10:54 am Post subject: Minor increases in duty cycle values |
|
|
Hi,
what can I do, to have minor increases in duty cycle values? (That is a finer adjustment of the duty cycle ...)
The code I'm using is as follows:
Code: |
$regfile = "m1284Pdef.dat"
$crystal = 16000000
$baud = 19200
$hwstack = 48
$swstack = 24
$framesize = 48
Dim Dutycycle_tmp As Byte
Tccr2a = &B00100011
Tccr2b = &B00001010 'Prescaler 8
Ocr2a = Dutycycle_tmp
Pwm2b = 40 '40KHZ
|
(BASCOM-AVR version : 2.0.8.1 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon May 20, 2019 5:08 pm Post subject: Re: Minor increases in duty cycle values |
|
|
atmega64 wrote: | to have minor increases in duty cycle values?
The code I'm using is as follows: | Your code is nonsense.
Surely not 40kHz.
While I had no fun to disassemble your binary bit-muddle timer2 configuration, I actually had to do for finding out that you use mode 7 of timer2, fast pwm with OCR2A as top.
Using Dutycycle_tmp to control OCR2A alters thus the frequency, and not duty-cycle at constant frequency.
If your code would be nothing else than what you show, then Dutycycle_tmp and OCR2A would be zero. OCR2B has no effect at all, as the timer would always count from 0 to reset at 1, a value of 40 would be never reached.
The formula for this timer mode is: f_OCnx = f_clk_I/O / (prescaler * (1 + OCRnx)) ---> f_OC2B = 16MHz / (8 * (1 + 0)) which calculates to 2MHz.
If for example OCR2A would be set to the value of 40, the resulting frequency would be 48780Hz, and the duty-cycle could be adjusted with OCR2B within the range of 0 to 40. In case this is to coarse, use a 16bit timer or slow down the main clock, as then a prescaler of 1 would be possible while staying in an 8bit value range.
For example, if main clock would be 8MHz, a prescaler of 1 and OCR2A of 199 would result in 40kHz with a 200 step range of duty cycle.
Clockdivision = 2 should do the trick with this controller, however everything runs slower then.
If such is not desirable, use a prescaler of 8 and OCR2A of 49, you have a 50 step range of duty cycle then.
And that's the best you can get then. |
|
Back to top |
|
|
atmega64
Joined: 23 Feb 2005 Posts: 298 Location: ITALY
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue May 21, 2019 1:06 pm Post subject: |
|
|
atmega64 wrote: | would be perfect ... how can I do? |
As I wrote: slow down the main clock and use a prescaler of 1.
Quote: | Clockdivision = 2 should do the trick with this controller, however everything runs slower then. |
This controller sports a CLKPR-register and can slow down the main-clock, with divider of 2 a crystal of 16MHz runs the controller at 8 MHz. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed May 22, 2019 5:39 pm Post subject: |
|
|
EDC wrote: | Please take a look for something I use and it working fine |
The TO already got (imho good) advice in the German forum, where he cross-posted his request.
I dislike cross-posting, because it makes double work for helpers, work for which they would think about twice to do, if they would know the TO goes bargaining around with his lack of knowledge.
For the solution of this thread the result as follows.
Simply use the timer in Bascom's default mode, which is the phase-correct one.
Bascom's default timer mode has for some applications the inherent problem, not to achieve the highest possible frequency possible with fast pwm, means this mode is slower by factor 2.
What sometimes gets a problem, was required here to get a better resolution.
Had the TO done it via Bascom's default timer configuration, he would have finished his problem, without even the requirement to ask.
Not sure why the TO kept quiet and did not post the result here, maybe he feared it becomes obvious that he did employ more forums at the same time without telling.
On the other hand, some users are absolutely self-centered, mine, mine, mine, if only they have their own problem solved, who cares about others. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|