View previous topic :: View next topic |
Author |
Message |
mototest2
Joined: 24 Dec 2006 Posts: 53
|
Posted: Fri Jul 11, 2014 10:54 am Post subject: pwm with smoothly frequency and duty cycle with timer0 |
|
|
Hi All
Please help me, I need make PWM with timer0 with adjusted frequency 0-250Hz and duty cycles 0-100%
with Timer1 all work good, but I must use timer0 and it don't work , can you help me ?
here is with TIMER1 :
Code: |
Dim Duty_cycle_count As Long
Dim Frequency As Single
Dim Period As Single
Dim Timer_count As Long
Dim Duty_cycle As Byte
Const Timer_tick = 1000000000 / _xtal * 2
Tccr1a = &B01100011
Tccr1b = &B00010001
Config Portb.2 = output
Config Portb.1 = input
' in timer1 pinb.2 and b.1 are connected together
frequency = 180
duty_cycle = 50
do
call pwm_parameters
loop
sub pwm_parameters
Period = 1000000000 / Frequency
Timer_count = Period / Timer_tick
Tmp = Timer_count / 256
Ocr1ah = Tmp
Tmp = Timer_count Mod 256
Ocr1al = Tmp
Period = Timer_count * Duty_cycle
Duty_cycle_count = Period / 100
Compare1b = Duty_cycle_count
end sub
|
Thanks
Bob[/code]
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Jul 11, 2014 8:25 pm Post subject: |
|
|
Used micro is important thing i this case |
|
Back to top |
|
|
mototest2
Joined: 24 Dec 2006 Posts: 53
|
Posted: Fri Jul 11, 2014 8:29 pm Post subject: |
|
|
Atmega48 |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Wed Jul 16, 2014 4:16 pm Post subject: |
|
|
Sorry for long time for answear. My holidays with mor of % ....This is even for Timer2 so if You don`t have done it yet I can help. |
|
Back to top |
|
|
mototest2
Joined: 24 Dec 2006 Posts: 53
|
Posted: Wed Jul 16, 2014 4:22 pm Post subject: |
|
|
I tried different method and don't work, must be timer0 not timer2 ,because I have free only pind.5 or pind.6 |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Wed Jul 16, 2014 8:48 pm Post subject: |
|
|
If I understand datasheet enough.. This config means : Mode 11,PWM Phase Correct, No prescale.
If Im not wrong then in Timer0 CompareB You can use only in CTC mode.
When for Timer1 ByteH and ByteL is used then for 8Bit timer propably..all must be recalculated. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Jul 17, 2014 9:17 am Post subject: |
|
|
I`m not profi with programming and steel learning but this is my little idea.
Code: | Output_pin Alias Portb.5
Dim Freq As Word
Dim Freq_copy As Word
Dim Duty_cycle As Word
Dim Duty_copy As Word
Dim Timer_load As Byte
Dim Change_flag As Bit
' Timer0 in CTC mode, Freq and Duty must be calculated first
' for crystal
Do
If Output_pin = 1 Then
Select Case Freq _copy
Case 0
Change_flag = 1
Duty_copy = Duty_cycle
If Duty_copy > 255 Then
Timer_load = 255
Duty_copy = Duty_copy - 255
Else
Timer_load = Duty_copy
Duty_copy = 0
End If
Case Is < 255
Timer_load = Freq_copy
Freq_copy = 0
Case Is > 255
Timer_load = 255
Freq_copy = Freq_copy - 255
End Select
End If
If Output_pin = 0 Then
Select Case Duty_copy
Case 0
Change_flag = 1
Freq_copy = Freq
If Freq_copy > 255 Then
Timer_load = 255
Freq_copy = Freq_copy - 255
Else
Timer_load = Freq_copy
Freq_copy = 0
Case Is < 255
Timer_load = Duty_copy
Duty_copy = 0
Case Is > 255
Timer_load = 255
Duty_copy = Duty_copy - 255
End Select
End If
Loop
End
'-------------------------------------
Timer0_isr:
Compare0a = Timer_load
If Change_flag = 1 Then
Change_flag = 0
Toggle Output_pin
End If
Return |
I dont know is this fast enough and whats happend when value for freq or duty be close to 0. Today don`t have time for testing.
I forgot that Cases must be 0 - <255 - >255 and I fix it now. |
|
Back to top |
|
|
|