View previous topic :: View next topic |
Author |
Message |
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sat Jan 07, 2017 4:24 am Post subject: ADC_PWM @ 16khz |
|
|
Hello everyone!
I made a buck converter circuit it takes an input voltage of C.C. And reduces it in an amount controllable with the switch action of the mosfet channel PWM 16Khz.
The code that works very well but I want to replace with an ADC potentiometer as I do?
Code: | $regfile = "m8adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
'-----------------------
'-----------------------[Timer1 Configuration]
Config Timer1 = Pwm , Prescale = 1 , Compare_a_pwm = Clear_down , Compare_b_pwm = Clear_up
'WGM10 WGM11 WGM12 WGM13
Tccr1a.0 = 1 : Tccr1a.1 = 0 : Tccr1b.3 = 1 : Tccr1b.4 = 0 'Phase Correct PWM / 08-bit
'Tccr1a.0 = 0 : Tccr1a.1 = 1 : Tccr1b.3 = 0 : Tccr1b.4 = 0 'Phase Correct PWM / 09-bit
'Tccr1a.0 = 1 : Tccr1a.1 = 1 : Tccr1b.3 = 0 : Tccr1b.4 = 0 'Phase Correct PWM / 10-bit
'-----------------------
'-----------------------[LCD Configuration]
Config Lcd = 16 * 2
Config Lcdbus = 4
Config Lcdpin = Pin , E = Portc.1 , Rs = Portc.0
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3
Config Lcdpin = Pin , Db6 = Portc.4 , Db7 = Portc.5
'-----------------------
'-----------------------[GPIO Configuration]
Config Pind.2 = Input : Incr_key Alias Pind.2 : Portd.2 = 1
Config Pind.3 = Input : Decr_key Alias Pind.3 : Portd.3 = 1
'-----------------------
'-----------------------[Single Configuration]
Config Single = Scientific , Digits = 2
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'-----------------------[GPIO Configuration]
Dim Duty_cycle As Single , Vout As Single
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'--->[Main Program]
Cursor Off :
Ocr1a = 0 : Gosub Calculation
Do
If Incr_key = 0 Then
If Ocr1a < 255 Then Incr Ocr1a '511 | 1023
Gosub Calculation
End If
If Decr_key = 0 Then
If Ocr1a > 0 Then Decr Ocr1a
Gosub Calculation
End If
Loop
End
'---<[End Main]
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'--->[Sub-routine]
' Duty: 0 ~ 255 [0 = 0% / 255 = 100%]
Calculation:
Duty_cycle = Ocr1a / 255 : Duty_cycle = Duty_cycle * 100 '511 | 1023
Locate 1 , 1 : Lcd "Duty = " ; Duty_cycle ; " % "
Vout = Duty_cycle * 5 : Vout = Vout / 100
Locate 2 , 1 : Lcd "Vout = " ; Vout ; " V "
Return
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
(BASCOM-AVR version : 2.0.7.5 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat Jan 07, 2017 1:10 pm Post subject: |
|
|
Hello.
You must know that in Mega8 ADC is on PORTC pins so LCD take them all in your design
I decide reconect RS and EN pins of LCD to pins where switches was connected before but you can do whatever you like exept that you must have acces into one of ADC input pin.
Rest of code is simple. Shift 2 divide ADC value to fit it into 8Bit.
Code: | $regfile = "m8adef.dat"
$crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32
'-----------------------
'-----------------------[Timer1 Configuration]
Config Timer1 = Pwm , Prescale = 1 , Compare_a_pwm = Clear_down , Compare_b_pwm = Clear_up
'WGM10 WGM11 WGM12 WGM13
Tccr1a.0 = 1 : Tccr1a.1 = 0 : Tccr1b.3 = 1 : Tccr1b.4 = 0 'Phase Correct PWM / 08-bit
'Tccr1a.0 = 0 : Tccr1a.1 = 1 : Tccr1b.3 = 0 : Tccr1b.4 = 0 'Phase Correct PWM / 09-bit
'Tccr1a.0 = 1 : Tccr1a.1 = 1 : Tccr1b.3 = 0 : Tccr1b.4 = 0 'Phase Correct PWM / 10-bit
'-----------------------
'-----------------------[LCD Configuration]
Config Lcd = 16 * 2
Config Lcdbus = 4
Config Lcdpin = Pin , E = Portd.2 , Rs = Portd.3 'unused switches used here
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3
Config Lcdpin = Pin , Db6 = Portc.4 , Db7 = Portc.5
'-----------------------
'-----------------------[ADC Configuration]
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Dim Adc_read As Word , Old_read As Word
'-----------------------
'-----------------------[Single Configuration]
Config Single = Scientific , Digits = 2
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'-----------------------[Variables]
Dim Duty_cycle As Single , Vout As Single
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'--->[Main Program]
Cursor Off :
Ocr1a = 0 : Gosub Calculation
Do
Adc_read = Getadc(0)
Shift Adc_read , Right , 2 'divide/4 so 1023 become 254
If Old_read <> Adc_read Then 'only if potentiometer was moved
Old_read = Adc_read
Ocr1a = Adc_read
Gosub Calculation 'LCD will be updated only if value was changed
End If
Loop
End
'---<[End Main]
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'--->[Sub-routine]
' Duty: 0 ~ 255 [0 = 0% / 255 = 100%]
Calculation:
Duty_cycle = Adc_read / 255 : Duty_cycle = Duty_cycle * 100 '511 | 1023
Locate 1 , 1 : Lcd "Duty = " ; Duty_cycle ; " % "
Vout = Duty_cycle * 5 : Vout = Vout / 100
Locate 2 , 1 : Lcd "Vout = " ; Vout ; " V "
Return
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sat Jan 07, 2017 9:34 pm Post subject: |
|
|
EDC Thank you very much !! |
|
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
|
|