PWM

Allgemeine Themen rund um den Propeller Mikrocontroller und Elektronik
Antworten
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

PWM

Beitrag von PIC18F2550 »

Hallo

ich habe da was im I-net gefunden.

Aber wie erweitere ich das von 100% auf 100,0% oder 100,00%.

Code: Alles auswählen

{A simple pwm object based on code from AN001 - propeller counters
 Author: Jev Kuznetsov
 date  : 16 Oktober 2007

 usage

 OBJ
        pwm : pwmAsm

  ....

  pwm.start( Pin)               ' start pwm
  pwm.SetPeriod( period )       ' set pwm period in clock cycles
  pwm.SetDuty( duty)            ' set duty in %                               
  pwm.Stop

}
VAR
  long  cogon, cog       
  long sDuty                     ' order important (the variables are read from memory in this order)  
  long sPinOut 
  long sCtraVal
  long sPeriod
  

PUB Start( Pin) : okay
'start pwm on Pin @ 80 kHz
  longfill(@sDuty, 0, 4)       
  sDuty := 50                   ' default duty
  sPinOut := |< Pin   
  sCtraVal :=  %00100 << 26 + Pin
  sPeriod := 1000
  
  okay := cogon := (cog := cognew(@entry,@sDuty)) > 0    
  
PUB stop

'' Stop object - frees a cog

  if cogon~
    cogstop(cog)
  longfill(@sDuty, 0, 4) 

PUB SetPeriod(counts)
' set pwm period in clock cycles, frequency = (_clkfreq / period)
   sPeriod := counts


PUB SetDuty(counts)
   if (counts < 0)
     counts := 0
   if (counts > 100)
     counts := 100
   sDuty :=counts*sPeriod/100
DAT
'assembly cog which updates the PWM cycle on APIN
'for audio PWM, fundamental freq which must be out of auditory range (period < 50�S)
        org

entry   mov     t1,par                'get first parameter
        rdlong  value, t1
         
        add     t1,#4                 
        rdlong  pinOut, t1
        or      dira, pinOut         ' set pinOut to output      

        add     t1, #4
        rdlong  ctraval, t1
        mov ctra, ctraval              'establish counter A mode and APIN

        add     t1, #4
        rdlong  period, t1


        mov frqa, #1                   'set counter to increment 1 each cycle

        mov time, cnt                  'record current time
        add time, period               'establish next period

:loop   rdlong value, par              'get an up to date pulse width
        waitcnt time, period           'wait until next period
        neg phsa, value                'back up phsa so that it  trips "value" cycles from now
        jmp #:loop                     'loop for next cycle



period  res 1                    
time    res 1
value   res 1
t1      res 1
pinOut  res 1
ctraval res 1
  
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Benutzeravatar
yeti
Beiträge: 2300
Registriert: Fr 27. Aug 2010, 14:48
Wohnort: Wrong Planet
Kontaktdaten:

Re: PWM

Beitrag von yeti »

Du hast das AN001-PDF und das zugehörige ZIP?

AN001-P8X32ACounters-v2.0_2.pdf
AN001: Propeller P8X32A Counters
(429.75 KiB) 406-mal heruntergeladen
AN001-P8X32ACounters-v2.0_0.zip
AN001: Propeller P8X32A Counters
(5.08 KiB) 368-mal heruntergeladen
𝖂𝖎𝖗 𝖐𝖔̈𝖓𝖓𝖊𝖓 𝖆𝖑𝖑𝖊𝖘 𝖆𝖚𝖘𝖘𝖊𝖗 𝖎𝖓 𝕱𝖗𝖚̈𝖍𝖑𝖎𝖓𝖌, 𝕾𝖔𝖒𝖒𝖊𝖗, 𝕳𝖊𝖗𝖇𝖘𝖙 𝖚𝖓𝖉 𝖂𝖎𝖓𝖙𝖊𝖗! – 𝕯𝖊𝖚𝖙𝖘𝖈𝖍𝖑𝖆𝖓𝖉.
"Du willst hier nicht klicken. Dies interessiert Dich nicht." — Yeti.
"DNA is a four letter word!" — Yeti.
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

Re: PWM

Beitrag von PIC18F2550 »

yeti jetzt schon Danke

wenn ich das richtig verstanden habe muss ich für 100,0%:

sPeriod := 1000 auf 10000 setzen

und in

PUB SetDuty(counts)
if (counts < 0)
counts := 0
if (counts > 100) auf 1000 setzen
counts := 100 auf 1000 setzen
sDuty :=counts*sPeriod/100 auf 1000 setzen

das würde eine PWM mit 8khz ergeben
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

Re: PWM

Beitrag von PIC18F2550 »

Habe mal ein Bisschen rumgetippt.

Code: Alles auswählen

{A simple pwm object based on code from AN001 - propeller counters
 Author: Jev Kuznetsov
 date  : 16 Oktober 2007
 usage
 OBJ
        pwm : pwmAsm
  ....

  pwm.start( Pin)               ' start pwm
  pwm.SetPeriod( period )       ' set pwm period in clock cycles
  pwm.SetDuty( duty)            ' set duty in %                               
  pwm.Stop
}
CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR
  long cogon, cog
  long sDuty                     ' order important (the variables are read from memory in this order)
  long sPinOut
  long sCtraVal
  long sPeriod

PUB TESTEN
  init(0)

PUB Init(Pin) : okay
'start pwm on Pin @ 8 kHz 0..100,00%
  longfill(@sDuty, 0, 4)
  sPinOut := |< Pin
  sCtraVal :=  %00100 << 26 + Pin
  sPeriod := 10000
  sDuty   := 5000
  okay := cogon := (cog := cognew(@entry,@sDuty)) > 0
  
PUB stop
'' Stop object - frees a cog
  if cogon~
    cogstop(cog)
  longfill(@sDuty, 0, 4)

PUB SetPeriod(counts)
' set pwm period in clock cycles, frequency = (_clkfreq / period)
   sPeriod := counts

PUB SetDuty(counts)
   if (counts < 0)
     counts := 0
   if (counts > 10000)
     counts := 10000
   sDuty :=counts

DAT
'assembly cog which updates the PWM cycle on APIN
'for audio PWM, fundamental freq which must be out of auditory range (period < 50ṁS)
        org 0

entry   mov     value,par                'get first parameter
        add     value,#4
        rdlong  pinOut, value
        or      dira, pinOut         ' set pinOut to output

        add     value, #4
        rdlong  ctraval, value            'sCtraVal
        mov ctra, ctraval              'establish counter A mode and APIN

        add     value, #4
        rdlong  period, value             'sPeriod
        mov frqa, #1                   'set counter to increment 1 each cycle
        mov time, cnt                  'record current time
        add time, period               'establish next period

:loop   rdlong value, par              'get an up to date pulse width
        waitcnt time, period           'wait until next period
        neg phsa, value                'back up phsa so that it  trips "value" cycles from now
        jmp #:loop                     'loop for next cycle

value   res 1
pinOut  res 1
ctraval res 1
period  res 1
time    res 1
Jetzt muss ich nur noch testen. :SCHREIEN

Hoffendlich passt alles. :SCHRAUBEN
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

Re: PWM

Beitrag von PIC18F2550 »

Na gut mit einem kanal hab ich es jetzt hinbekommen.

Aber mit zwei in einem COG währe ich zufriedener.
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

Re: PWM

Beitrag von PIC18F2550 »

So fertsch isses.
pwmAsm.spin
(3.15 KiB) 300-mal heruntergeladen
Ein COG 2x PWM mit einer gemeinsamen Periodenzeit.

Beide Kanäle benutzen zwei Ausgäne einmal normal einmal Invertiert.
So kann man einen Motor mit einer Brücke ansteuern.

Code: Alles auswählen

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR
  long cogon, cog

  long sDutyA                     ' order important (the variables are read from memory in this order)
  long sDutyB                     ' order important (the variables are read from memory in this order)
  long sPinAOut1
  long sPinAOut2
  long sPinBOut1
  long sPinBOut2
  long sPeriod
  long sCtraVal
  long sCtrbVal

PUB Init(PinA1,PinA2,PinB1,PinB2) : okay
'start pwm on Pin @ 8 kHz 0..100,00%
  stop
  longfill(@sDutyA, 0, 9)

  sDutyA    := 10000/3
  sDutyB    := 10000/2
  sPinAOut1 := |< PinA1
  sPinAOut2 := |< PinA2
  sPinBOut1 := |< PinB1
  sPinBOut2 := |< PinB2
'  sCtraVal  := %00100 << 26 + PinA1
'  sCtrbVal  := %00100 << 26 + PinB1
  sCtraVal  := %00101 << 26 + PinA1 + PINA2 << 9
  sCtrbVal  := %00101 << 26 + PinB1 + PINB2 << 9
  sPeriod   := 10000

  okay := cogon := (cog := cognew(@entry,@sDutyA)) > 0

PUB stop
'' Stop object - frees a cog
  if cogon~
    cogstop(cog)
  longfill(@sDutyA, 0, 9)

PUB SetPeriod(counts)
' set pwm period in clock cycles, frequency = (_clkfreq / period)
   sPeriod := counts

PUB SetDutyA(counts)
   if (counts < 0)
     counts := 0
   if (counts > 10000)
     counts := 10000
   sDutyA :=counts

PUB SetDutyB(counts)
   if (counts < 0)
     counts := 0
   if (counts > 10000)
     counts := 10000
   sDutyB :=counts

DAT
'assembly cog which updates the PWM cycle on APIN
'for audio PWM, fundamental freq which must be out of auditory range (period < 50?S)
        org 0

entry   mov     T0,par                 'get first parameter

        add     T0,#8
        rdlong  tmp, T0
        or      dira, tmp              ' set pinOut to output
        add     T0,#4
        rdlong  tmp, T0
        or      dira, tmp              ' set pinOut to output
        add     T0,#4
        rdlong  tmp, T0
        or      dira, tmp              ' set pinOut to output
        add     T0,#4
        rdlong  tmp, T0
        or      dira, tmp              ' set pinOut to output

        add     T0, #4
        rdlong  period, T0             'sPeriod
        mov     frqa, #1               'set counter to increment 1 each cycle
        mov     frqb, #1               'set counter to increment 1 each cycle
        mov     time, cnt              'record current time
        add     time, period           'establish next period

        add     T0, #4
        rdlong  tmp, T0
        mov     ctra, tmp               'sCtraVal
        add     T0, #4
        rdlong  tmp, T0
        mov     ctrb, tmp               'sCtraVal

        mov     T0,par                 'get first parameter
        add     T0, #4
:loop   rdlong valueA, par             'get an up to date pulse width
        rdlong valueB, T0              'get an up to date pulse width
        waitcnt time, period           'wait until next period
        neg phsa, valueA               'back up phsa so that it  trips "value" cycles from now
        neg phsb, valueB               'back up phsa so that it  trips "value" cycles from now
        jmp #:loop                     'loop for next cycle


valueA  res 1
valueB  res 1
ctraval res 1
ctrbval res 1
period  res 1

time    res 1
T0      res 1
tmp     res 1
Dateianhänge
Beide Kanäle
Beide Kanäle
Beide Ausgänge eines Kanals
Beide Ausgänge eines Kanals
Zuletzt geändert von PIC18F2550 am So 13. Sep 2020, 19:31, insgesamt 2-mal geändert.
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Benutzeravatar
yeti
Beiträge: 2300
Registriert: Fr 27. Aug 2010, 14:48
Wohnort: Wrong Planet
Kontaktdaten:

Re: PWM

Beitrag von yeti »

Aaaaaach sooooooo...
Deine Hives bekommen Räder!   :DAUMENHOCH
𝖂𝖎𝖗 𝖐𝖔̈𝖓𝖓𝖊𝖓 𝖆𝖑𝖑𝖊𝖘 𝖆𝖚𝖘𝖘𝖊𝖗 𝖎𝖓 𝕱𝖗𝖚̈𝖍𝖑𝖎𝖓𝖌, 𝕾𝖔𝖒𝖒𝖊𝖗, 𝕳𝖊𝖗𝖇𝖘𝖙 𝖚𝖓𝖉 𝖂𝖎𝖓𝖙𝖊𝖗! – 𝕯𝖊𝖚𝖙𝖘𝖈𝖍𝖑𝖆𝖓𝖉.
"Du willst hier nicht klicken. Dies interessiert Dich nicht." — Yeti.
"DNA is a four letter word!" — Yeti.
Benutzeravatar
PIC18F2550
Beiträge: 2831
Registriert: Fr 30. Sep 2011, 13:08

Re: PWM

Beitrag von PIC18F2550 »

Nee Klimabox mit Peltierelement und VCO.
Gruß
PIC18F2550

drone265/278
Barbarus hic ergo sum, quia non intellegor ulli.
Ein Barbar bin ich hier, da ich von keinem verstanden werde.
ʎɐqǝ ıǝq ɹnʇɐʇsɐʇ ǝuıǝ ɹǝpǝıʍ ǝıu ǝɟnɐʞ ɥɔı ´uuɐɯ ɥo
Antworten