View previous topic :: View next topic |
Author |
Message |
Tiny
Joined: 10 Nov 2010 Posts: 101 Location: The Netherlands
|
Posted: Tue Mar 08, 2016 9:01 pm Post subject: pwm frequentie |
|
|
Hello,
I'am ussing the atmega1284p running at 20Mhz for driving a h-bridge motor driver.
i control the speed with changing the duty cycly.
if a config timer1 as pwm generator
Code: |
Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 1
|
Then i get a pwm frequenty of +/- 39Khz
i use PWM1B to control the speed
But i need a higer frequenty say about 96Khz
Can some tell me how to config timer1 for this and that i stil can control the speed with PWM1B
Thankx,
Tiny
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
olhexy
Joined: 03 Apr 2011 Posts: 192 Location: Tilburg, Netherlands
|
Posted: Tue Mar 08, 2016 11:21 pm Post subject: |
|
|
If you want 96kHz from 20Mhz that means 208 cycles for 1 PWM cycle, and that is less than 8 bits.
I don't know how to set this in Bascom, so I would use direct registers.
I am looking now at page 133 of http://www.atmel.com/images/doc8059.pdf
There is the table with Waveform Generation modes.
The phase/frequency correct modes are dual slope, that makes that you can adjust in steps of 2 cycles.
If maximum is 208 cycles you may want the finest step, 1 cycle.
That is possible with Fast PWM mode 15: set bit 3 and 4 in TCCR1B, and set bit 0 and 1 in TCCR1A.
You can set TOP with OCR1A at 208. See figure 14-7 (page 124).
You set duty cycle with OCR1BL (and OCR1BH, but that stays &H00 as you have maximum 208).
Please read yourself through the details...
Last edited by olhexy on Tue Mar 08, 2016 11:36 pm; edited 2 times in total |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Tue Mar 08, 2016 11:26 pm Post subject: |
|
|
"Normally" Bascom set PWM to Phase Correct Mode. You can change one bit and Timer should be in Fast PWM Mode
Code: | Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 1
TCCR1B.WGM12 = 1 'valid for m644p |
20MHz/256(8BIT)=78125Hz=~78kHz (Phase correct divide this/2 and result is 39062Hz=39kHz) |
|
Back to top |
|
|
Tiny
Joined: 10 Nov 2010 Posts: 101 Location: The Netherlands
|
Posted: Fri Mar 11, 2016 7:02 am Post subject: |
|
|
Tanks for the tips, i wil try it this weekend. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Fri Mar 11, 2016 9:38 am Post subject: |
|
|
olhexy wrote: | If you want 96kHz from 20Mhz that means 208 cycles |
That's ok.
Quote: | You can set TOP with OCR1A at 208. See figure 14-7 (page 124). |
Re-read the data sheet, exactly the formula given for Fast-PWM, you'll notice that for getting 208 cycles the correct value for TOP is 207. |
|
Back to top |
|
|
|