Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

PWM: how to set prescale seperately?

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

Bascom Member



Joined: 12 Mar 2019
Posts: 14

germany.gif
PostPosted: Sun Oct 31, 2021 11:50 am    Post subject: PWM: how to set prescale seperately? Reply with quote

Hi all
I want use a 8bit PWM (Timer1) at a ATTiny 4313. Depending on user settings the parameters "Output" and "Prescale should be set.
my variables are
pwm_prescale_ee: set Prescale
pwm_kanal4_ee: activate PWM on Portb.4
pwm_kanal5_ee: activate PWM on Portb.3

at the moment I'm doing this:

Code:
if pwm_kanal4_ee >= 1 and pwm_kanal5_ee = 0 then  'PWM-Ausgänge aktivieren
 select case pwm_prescale_ee  '
   case 10 'A
      Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 1024      '1024 = 15Hz
   case 11 'B
      Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 256    '256 = 61Hz
   case 12 'C
      Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 64      '64 = 244Hz
   case 13 'D
      Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 8       '8 =  1953Hz
   case 14 'E
      Config Timer1 = Pwm , Pwm = 8 , Compare B Pwm = Clear Up , Prescale = 1       '1 = 15625Hz
 end select
end if


if pwm_kanal4_ee = 0 and pwm_kanal5_ee >= 1 then   'PWM-Ausgänge aktivieren
 select case pwm_prescale_ee  '
   case 10 'A
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 1024      '1024 = 15Hz
   case 11 'B
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 256    '256 = 61Hz
   case 12 'C
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 64      '64 = 244Hz
   case 13 'D
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 8       '8 =  1953Hz
   case 14 'E
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 1       '1 = 15625Hz
 end select
end if


if pwm_kanal4_ee >= 1 and pwm_kanal5_ee >= 1 then  'PWM-Ausgänge aktivieren
 select case pwm_prescale_ee  '
   case 10 'A
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 1024      '1024 = 15Hz
   case 11 'B
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 256    '256 = 61Hz
   case 12 'C
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 64      '64 = 244Hz
   case 13 'D
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 8       '8 =  1953Hz
   case 14 'E
      Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 1       '1 = 15625Hz
 end select
end if
 


works well but wastes a lot of flash memory.

My question: how to set the Prescale independly of the hole timer config?

(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.4 )
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Sun Oct 31, 2021 11:53 pm    Post subject: Reply with quote

You can set the 3 bits for the prescaler manually in the TCCR1B register.
For example prescale 64;
Code:

tccr1b.cs10 =1
tccr1b.cs11 =1
tccr1b.cs12 =0

Have look at the datasheet page 114 for the possible values

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
ulfbc

Bascom Member



Joined: 12 Mar 2019
Posts: 14

germany.gif
PostPosted: Mon Nov 01, 2021 10:06 am    Post subject: Reply with quote

found it in the datasheet, but I had no idea to code it in Bascom
thanks a lot
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Tue Nov 02, 2021 10:57 am    Post subject: Reply with quote

Try it. Should work.
Code:

Dim Pwm_kanal4_ee As Eram Byte
Dim Pwm_kanal5_ee As Eram Byte
Dim Pwm_prescale_ee As Eram Byte

Const P1 = &B000_001
Const P8 = &B000_010
Const P64 = &B000_011
Const P256 = &B000_100
Const P1024 = &B000_101

Tccr1a = &B0000_0001                                        'WGM10 for PWM, Phase Correct, 8-bit
If Pwm_kanal4_ee > 0 Then Set Tccr1a.com1a1
If Pwm_kanal5_ee > 0 Then Set Tccr1a.com1b1

 Select Case Pwm_prescale_ee
   Case 10
      Tccr1b = P1024
   case 11 'B
      Tccr1b = P256
   Case 12
      Tccr1b = P64
   case 13 'D
      Tccr1b = P8
   Case 14
      Tccr1b = P1
 End Select


End
 


Back to top
View user's profile Visit poster's website
ulfbc

Bascom Member



Joined: 12 Mar 2019
Posts: 14

germany.gif
PostPosted: Thu Nov 04, 2021 7:59 pm    Post subject: Reply with quote

EDC wrote:
Try it. Should work.




works !
thanks for help

saved round about 15% of my flash Very Happy
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