Fast PWM

Sezione dedicata al sistema di sviluppo BASCOM-AVR per i micro AVR
At90s, Attiny, Atmega e Xmega

Fast PWM

Postby tnnbascom » 15 May 2013, 11:24

Salve a tutti,

volevo il vostro prezioso aiuto gentilmente...

Devo impostare il timer di un atmega8 in Fast PWM Mode, operazione fattibile solo da Assembler....

Unico problema che non ho mai usato fin ad ora istruzioni Assembler in Bascom...

ho dato un occhiata al datasheet:

http://www.atmel.com/Images/Atmel-2486- ... asheet.pdf

a pag.88 ma non riesco a capire quali registri settare e come... :oops:

Grazie anticipatamente.
tnnbascom
 
Posts: 87
Joined: 27 Mar 2013, 15:06

Re: Fast PWM

Postby deluca » 15 May 2013, 11:54

@tnnbascom,
Da dove hai appreso la notizia che il fast pwm lo puoi impostare solo in assembly?
Basta impostare nel modo giusto i registri timer dell'avr.... lo puoi fare semplicemente anche in bascom....
Non lo puoi fare con i Config Timer di bascom.... ma scrivendo opportunamente sui registri no-prb.

Se non ricordo male, avevi già aperto un topic a riguardo ?
Ciao
Il mio sito: http://www.delucagiovanni.com ......e la chat: chat/
User avatar
deluca
Site Admin
 
Posts: 1104
Joined: 19 Jun 2011, 10:44
Location: 95123 - Catania (Italy)

Re: Fast PWM

Postby tnnbascom » 15 May 2013, 11:58

bene, mi puoi fare gentilmente un esempio di come impostarli in bascom..., grazie.
tnnbascom
 
Posts: 87
Joined: 27 Mar 2013, 15:06

Re: Fast PWM

Postby deluca » 15 May 2013, 13:14

Quale è la freq del pwm che devi generare, e quale la freq del quarzo?
Ti dico subito che nella modalità fast-pwm avrai una bassa risoluzione del duty-cycle... diciamo che avrai al massimo 3/4bit di risoluzione.

I registri che devi configurare sono quelli del timer1, in particolare Tccr1a e Tccr1b,
setta i bit seguendo la descrizione nel pdf ed assegna il valore ai due registri....

Code: Select all
Tccr1a =                                   
                                                            '1 COM1A1 set ouput to hight level
                                                            '1 COM1AO
                                                            '0 COM1B1
                                                            '0 COM1BO
                                                            '0 FOC1A  ATmega8
                                                            '0 FOC1B  ATmega8
                                                            '1 WGM11
                                                            '0 WGM10
'-----------------------------[ TCCR1B ]----------------------------------------
Tccr1b =                           
                                                            '0 ICNC1 input capture noise canceler
                                                            '0 ICES1 input capture edge select
                                                            '- lasciamolo a 0
                                                            '1 WGM13 waveform generation mode
                                                            '1 WGM12 fast pwm
                                                            '0 CS12  no prescaling
                                                            '0 CS11
                                                            '1 CS10

                                                            'WGM = 1110 = Mode 14, fast PWM
                                                            'CS  =  001 = No prescaling
Ciao
Il mio sito: http://www.delucagiovanni.com ......e la chat: chat/
User avatar
deluca
Site Admin
 
Posts: 1104
Joined: 19 Jun 2011, 10:44
Location: 95123 - Catania (Italy)

Re: Fast PWM

Postby tnnbascom » 15 May 2013, 14:28

grazie gentilissimo...

Ti farò sapere....
tnnbascom
 
Posts: 87
Joined: 27 Mar 2013, 15:06

Re: Fast PWM

Postby tnnbascom » 15 May 2013, 15:58

fatto!!!

Volevo sapere se c'è da fare qualche accorgimento al seguente codice per migliorare l'onda quadra di circa 670 KHz, che io ho bisogno...
Grazie

Code: Select all
regfile = "m8def.dat"
 $crystal = 4000000

 Config Portb.1 = Output                                     'PWM_PIN

 Const Cpwmfreq = 5

 Tccr1a = &B10000010
                      'Bit 7:6 – COM1A1:0: Compare Output Mode for channel A set to 10
                      '        = Clear OC1A on Compare Match, set OC1A at TOP
                      'Bit 5:4 – COM1B1:0: Compare Output Mode for channel B set to 00
                      '        = Normal port operation, OC1B disconnected.
                      'Bit 3 – FOC1A: Force Output Compare for channel A set to 0 - not used in PWM
                      'Bit 2 – FOC1B: Force Output Compare for channel B set to 0 - not used in PWM
                      'Bit 1:0 – WGM11:0: Waveform Generation Mode set to ..10
                      '        = lower bits of Mode 14: fast PWM; TOP = ICR1

 Tccr1b = &B00011001
                      'Bit 7 – ICNC1: Input Capture Noise Canceler set to 0 - not used in PWM
                      'Bit 6 – ICES1: Input Capture Edge Select set to 0 - not used in PWM
                      'Bit 5 – Reserved Bit set to 0
                      'Bit 4:3 – WGM13:2: Waveform Generation Mode set to 11..
                      '        = higher bits of Mode 14: fast PWM; TOP = ICR1
                      'Bit 2:0 – CS12:0: Clock Select set to
                      '        = higher bits of Mode 14: fast PWM; TOP = ICR1
                      'Bit 2:0 – CS12:0: Clock Select set to 010 = clkI/O / 8 (From prescaler)

 Icr1 = Cpwmfreq

 I = Cpwmfreq / 2                                            '50%

 Pwm1a = I

tnnbascom
 
Posts: 87
Joined: 27 Mar 2013, 15:06

Re: Fast PWM

Postby deluca » 15 May 2013, 20:07

La configurazione dei tccr1a e b va bene,
inoltre il calcolo della freq nel caso del fast-pwm utilizzando i tuoi valori è il seguente:

Clock_Xtal / (ICR1+1) = 4MHz / (5+1) = 4MHz / 6 = 666666.6 = 666.6KHz

il prb si ha se si vuole un duty al 50% e purtroppo con un clock basso non avrai il 50% esatto,
per migliorare questo aspetto devi almeno quadruplicare la freq del cristallo e portarlo a 16Mhz.

Tuttavia per semplici applicazioni, penso sia più che accettabile....
Ciao
Il mio sito: http://www.delucagiovanni.com ......e la chat: chat/
User avatar
deluca
Site Admin
 
Posts: 1104
Joined: 19 Jun 2011, 10:44
Location: 95123 - Catania (Italy)


Return to BASCOM-AVR

Who is online

Users browsing this forum: No registered users and 15 guests