View previous topic :: View next topic |
Author |
Message |
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Fri May 24, 2013 7:24 pm Post subject: Adc and pwm |
|
|
Hi to all
I am trying to generate a frequency of 31.5 kHz for a buck converter, I will also use ADC (10 bit) to change the pulse width from minimum to maxim. I am comfortable with the other part of the code but find it difficult to set registers for generating the PWM. the ADC is 10bit so I would wish to have the pwm resolution to be 10bit too.
Regards
(BASCOM-AVR version : 2.0.7.3 , Latest : 2.0.7.6 ) |
|
Back to top |
|
|
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Sat May 25, 2013 6:19 pm Post subject: |
|
|
Hi to all Forum members
As I said I want to generate the frequency of 31.25 Khz and have write the following code, I have not used the ADC yet as I want to fixup the PWm thing first, but I am getting the frequency of 3.911 kHz what could be the problem.
'------------------------------------------------------------------------------
'name : Buck Regulator.bas
'copyright : (c) 2013, NaseerAK
'purpose : Buck Converter
'micro : Mega8
'suited for demo : ?
'commercial addon needed : no
'------------------------------------------------------------------------------
$regfile = "m8def.dat" ' we use the M8
$crystal = 8000000
$baud = 19200
$hwstack = 32
$swstack = 8
$framesize = 24
$regfile = "m8.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40
Config Adc = Single , Prescaler = Auto , Reference = Avcc
'Now give power to the chip
Start Adc
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 9
Dim W As Word , Channel As Byte
Do
Pwm1a = 512 'this should result in 50% duty cycle
Pwm1b = 256 'this should result in 25% duty cycle
Loop
End
Thanks and regards |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat May 25, 2013 6:23 pm Post subject: |
|
|
Error in code !!
your code show 2 x MCU setup
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 9 _________________ / Kim |
|
Back to top |
|
|
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Sat May 25, 2013 7:30 pm Post subject: |
|
|
Sorry it is like this
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1
but still the same result |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat May 25, 2013 9:31 pm Post subject: |
|
|
this is how to calculate freq 8 or 10 bit
8Bit : (PWM = 8 )
(Xtal_in_Hz / Prescaler) / (256*2) = freq_in_hz
10Bit : (PWM = 10)
(Xtal_in_Hz / Prescaler) / (1024*2) = freq_in_hz
and you use 10bit ( PWM = 10)
(8000000 / 1) / (1024 * 2) = 3.90625 khz _________________ / Kim |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Sun May 26, 2013 10:52 am Post subject: |
|
|
Try this
Code: | $regfile = "m8def.dat"
$crystal = 8000000
$hwstack = 128
$swstack = 64
$framesize = 64
' fast pwm, mode 14, noninverted output, prescaler 1
' set frequency with register ICR1, duty cycle with OCR1A(Pwm1a), OCR1B(Pwm1b)
Tccr1a = Bits(wgm11 , Com1a1 , Com1b1)
Tccr1b = Bits(wgm12 , Wgm13 , Cs10)
' ICR1(Capture1) = (F_CPU[Hz] / (freq[Hz] * prescaler)) - 1 (freq=31250)
Capture1 = 255
Config Adc = Single , Prescaler = Auto
Dim Adc_value As Word
Ddrb = Bits(1 , 2) ' output pins OC1A , OC1B
'--------------------------------------
Do
Adc_value = Getadc(0)
Pwm1a = Adc_value / 4 ' use upper 8 bits of adc_value
Pwm1b = 127 ' duty cycle 50%
Loop
End
'--------------------------------------
|
|
|
Back to top |
|
|
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Sun May 26, 2013 7:32 pm Post subject: |
|
|
Hi
This is why I call it this forum a great place with every member willing to help.
Thanks particularly to Visovian and Kimmi.
Cheers
naseerAk |
|
Back to top |
|
|
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Sun May 26, 2013 7:35 pm Post subject: |
|
|
And it means I have to work with 8 bit pwm If I wish the frequency to be this much high? |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Sun May 26, 2013 8:48 pm Post subject: |
|
|
Yes.
8-bit pwm means 256 duty cycle values.
It is surely enough for most cases. |
|
Back to top |
|
|
naseerak
Joined: 08 Nov 2008 Posts: 138
|
Posted: Mon May 27, 2013 4:24 pm Post subject: |
|
|
Peace to all
what if I want inverted pwm to drive P-channel mosfet for a buck converter to be used in a MPPT controller. |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Mon May 27, 2013 6:02 pm Post subject: |
|
|
In the datasheet search for table "Compare Output Mode, Fast PWM". |
|
Back to top |
|
|
|