Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Minor increases in duty cycle values

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Mon May 20, 2019 10:54 am    Post subject: Minor increases in duty cycle values Reply with quote

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
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon May 20, 2019 5:08 pm    Post subject: Re: Minor increases in duty cycle values Reply with quote

atmega64 wrote:
to have minor increases in duty cycle values?
The code I'm using is as follows:
Your code is nonsense.

atmega64 wrote:
Code:
Pwm2b = 40                                                  '40KHZ
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
View user's profile
atmega64

Bascom Member



Joined: 23 Feb 2005
Posts: 298
Location: ITALY

italy.gif
PostPosted: Tue May 21, 2019 8:16 am    Post subject: Reply with quote

Sorry, you are right...

this is the correct code with fixed frequency 40Khz:

Code:

Tccr2a = &B00100011
Tccr2b = &B00001010                                         'Prescaler 8
Ocr2a = 49                                                  '40KHZ
Pwm2b = Pwr_pwm_tmp
 


and setting PWR_PWM_TMP = 24, I have this signal:



I therefore have 48 steps ...

Quote:

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.


would be perfect ... how can I do?
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue May 21, 2019 1:06 pm    Post subject: Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Wed May 22, 2019 2:59 pm    Post subject: Reply with quote

Please take a look for something I use and it working fine Very Happy
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewforum&f=8

You use Mega that have two 16 bit timers... thing again.
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed May 22, 2019 5:39 pm    Post subject: Reply with quote

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. Evil or Very Mad
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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