SysSetPWMOut, set PWM output

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

La modulazione di larghezza di impulso (o PWM, acronimo di Pulse Width Modulation), è una modulazione che permette di ottenere un valore medio dipendente dal rapporto tra la durata dell’impulso positivo e di quello negativo (duty cycle).

Questo blocco funzione esegue la gestione del circuito PWM hardware presente su alcuni moduli (Utilizzabile solo sui moduli che hanno la gestione PWM hardware), non è da confondere con il FB PWMOut, PWM output management che invece realizza un PWM software (Molto meno performante in frequenza) ma che può essere utilizzato con una qualsiasi uscita logica. Per abilitare le uscite opzionali occorre richiedere il codice di protezione, vedi protezione funzioni e blocchi funzione. E’ comunque possibile utilizzarli liberamente in modo test per 15 Min.

Il FB permette la definizione del valore di frequenza Frequency e di duty cycle Duty del generatore PWM  sul modulo e canale indicato. Il range dei valori dipendono dal modulo utilizzato ed in generale avremo.

  • Frequency:=0, il generatore PWM termina il periodo in corso ed azzera l’uscita.
  • Duty:=0, il generatore PWM termina il periodo in corso e disattiva l’uscita.
  • Duty:=100, il generatore PWM termina il periodo in corso ed attiva l’uscita.
  • Duty:=50, il generatore PWM cerca di mantenere al 50% il duty cycle a tutti i valori di frequenza, generatore di fequenza.
Moduli con PWM output

Questo FB opera solo sui moduli che implementano una gestione hardware del PWM, di seguito una tabella riassuntiva.

Module codePWM channelsNote
MPS050-PCB1311OUT0, range frequenza 5Hz÷3kHz
MPS053-PCB1351OUT0, range frequenza 5Hz÷3kHz
MPS054-PCB1372OUT0÷1, range frequenza 5Hz÷3kHz
MPS056-PCB1412OUT0÷1, range frequenza 5Hz÷3kHz
PCB124*0104 (Di cui 3 opzione)OUT0÷3, range frequenza 8Hz÷5kHz
Impostazione duty cycle

I moduli hanno uscite optoisolate quindi si generano ritardi sui fronti di salita e discesa che all’aumentare della frequenza riducono il range impostabile del duty cycle. A causa dei ritardi all’aumentare della frequenza aumenta la granularità delle impostazioni di frequenza e di duty.

Module codeDuty MinDuty MaxDuty Min (20 Hz)Duty Max (20 Hz)
MPS054-PCB137, MPS056-PCB1411%100-(Freq/27.2)1%100-(20/27.2)=99 %
PCB124*010Freq/71.499%20/71.4=1%99%
Information Circle

Blocco funzione

CODESYS: Non disponibile

LogicLab: eLLabXUnified12Lib

Descrizione

Address (USINT) Occorre specificare l’indirizzo di modulo su cui eseguire la gestione del PWM (Range da 0 a 15). Il valore 0 indica il primo modulo di estensione, 1 il secondo e così di seguito. Il valore 255 indica il modulo CPU.
Channel (USINT) Occorre specificare l’indirizzo del canale sul modulo (Range da 0 a 15).
Frequency (REAL) Valore di frequenza in uscita (Fare riferimento al manuale della scheda utilizzata). Il valore è espresso in Hz.
Duty (REAL) Valore di duty cycle del segnale in uscita, impostare 50 se utilizzato come generatore di frequenza. Il valore è espresso in %.
Done (BOOL) Generatore PWM correttamente impostato.
Fault (BOOL) Errore nella esecuzione

Immagine FB SysSetPWMOut

Esempi

Come utilizzare gli esempi.
ST_SysSetPWMOut: Viene impostato il canale 0 (Out 00) del modulo CPU per generare un segnale PWM a 100 Hz con duty cycle 50 %.

ST_PWMOutPulsesCount: Utilizzando un counter connesso all’uscita PWM è possibile dopo un numero prefissato di impulsi bloccare o modificare la frequenza di uscita. Come si vede dallo schema l’usita DO00 del PWM è connessa all’ingresso Di00 del counter.

Sono generati 4 impulsi a 100Hz, seguiti da 3 impulsi a 200Hz ed infine 2 impulsi a 50Hz poi l’uscita si azzera, simulando il comando di motori stepping, avvio, avanzamento, rallentamento ed arresto. La lettura del counter deve essere eseguita entro la durata di 1 impulso (A 200Hz ogni 5mS) quindi il programma và eseguito in task Fast.

Collegamenti per ST_PWMOutPulsesCount
LogicLab (Ptp116, ST_SysSetPWMOut)
PROGRAM ST_SysSetPWMOut
VAR
    PWMOut : SysSetPWMOut; (* PWM output *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysSetPWMOut"
// *****************************************************************************
// This program presets the PWM output channel 0 on CPU module.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // PWM OUTPUT
    // -------------------------------------------------------------------------
    // Preset PWM output.

    IF (SysFirstLoop) THEN
        PWMOut.Address:=255; //Module address
        PWMOut.Channel:=0; //Module channel
        PWMOut.Frequency:=100.0; //Frequency output (Hz)
        PWMOut.Duty:=50.0; //Duty cycle (%)
    END_IF;

    // Manage the PWM output.

    PWMOut(); //PWM output

// [End of file]
LogicLab (Ptp116, ST_PWMOutPulsesCount)
PROGRAM ST_PWMOutPulsesCount
VAR
    Start : BOOL; (* Start command *)
    CaseNr : USINT; (* Program case *)
    PulsesThr : UDINT; (* Pulses threshold *)
    PulsesCtr : UDINT; (* Pulses counter *)
    PWMOut : SysSetPWMOut; (* PWM output *)
    CInp : SysGetCounter; (* Counter acquisition *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_PWMOutPulsesCount"
// *****************************************************************************
// This program shows how to use a counter to count the PWM output pulses. After
// a defined number of pulses the output frequency is changed some times.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // PROGRAM INIT
    // -------------------------------------------------------------------------
    // Executed at first program execution, all variables are initialized.

    IF (SysFirstLoop) THEN
        PWMOut.Address:=255; //Module address
        PWMOut.Channel:=0; //Module channel

        CInp.Address:=255; //Module address
        CInp.Channel:=0; //Module channel
        CInp.Mode:=16#00000000; //Acquisition mode
    END_IF;

    // -------------------------------------------------------------------------
    // PWM OUTPUT
    // -------------------------------------------------------------------------
    // Preset PWM output.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // Wait for the start command.

        0:
        IF NOT(Start) THEN RETURN; END_IF;
        Start:=FALSE; //Start command

        // Set ouput frequency, duty cycle and save counter value.

        CInp(); //Counter acquisition
        PWMOut(Frequency:=100.0, Duty:=50.0); //PWM output
        PulsesThr:=CInp.Value; //Pulses threshold
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Wait for the defined number of pulses (4 pulses at 100 Hz).

        1:
        CInp(); //Counter acquisition
        PulsesCtr:=CInp.Value-PulsesThr; //Pulses counter
        IF (PulsesCtr < 4) THEN RETURN; END_IF;

        // Set ouput frequency, duty cycle and save counter value.

        PWMOut(Frequency:=200.0, Duty:=50.0); //PWM output
        PulsesThr:=CInp.Value; //Pulses threshold
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Wait for the defined number of pulses (3 pulses at 200 Hz).

        2:
        CInp(); //Counter acquisition
        PulsesCtr:=CInp.Value-PulsesThr; //Pulses counter
        IF (PulsesCtr < 3) THEN RETURN; END_IF;

        // Set ouput frequency, duty cycle and save counter value.

        PWMOut(Frequency:=50.0, Duty:=50.0); //PWM output
        PulsesThr:=CInp.Value; //Pulses threshold
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Wait for the defined number of pulses (2 pulses at 50 Hz).

        3:
        CInp(); //Counter acquisition
        PulsesCtr:=CInp.Value-PulsesThr; //Pulses counter
        IF (PulsesCtr < 2) THEN RETURN; END_IF;

        // Stop PWM output with signal low.

        PWMOut(Frequency:=0.0, Duty:=0.0); //PWM output
        CaseNr:=0; //Program case
    END_CASE;

// [End of file]
Was this article helpful?