Vai al contenuto

Funzioni gestione moduli periferici

Al bus di espansione possono essere connessi moduli periferici, esistono moduli con I/O digitali, I/O analogici, counters, encoders, ecc. Sono disponibili funzioni per la gestione di questi moduli.

ATTENZIONE! Le porte seriali RS232 presenti su alcuni moduli di I/O NON sono gestite da queste librerie.

eGetModulesOnPhrI2cBus

uint8_t eGetModulesOnPhrI2cBus(void);

Ritorna il numero di moduli connessi al bus.

eIsPhrI2cBusExtModuleAv

bool_t eIsPhrI2cBusExtModuleAv(uint8_t Module);

Controlla se il modulo con  indirizzo indicato è connesso al bus. Ritorna: false: Modulo non connesso, true: Modulo connesso.

eGetPhrDI

bool_t eGetPhrDI(uint8_t Address, DI_MODE Mode, uint32_t* Value);

Trasferisce in Value lo stato degli ingressi logici del modulo indicato in Address, specificare in Mode il tipo di acquisizione (Tabella). Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione.

eSetPhrDO

bool_t eSetPhrDO(uint8_t Address, DO_MODE Mode, uint32_t* Value);

Trasferisce Value sulle uscite logiche del modulo indicato in Module, specificare in Mode il tipo di gestione (Tabella). Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione.

eSetAnInpMode

bool_t eSetAnInpMode(uint8_t Address, uint8_t Channel, AI_MODE Mode);

Imposta il modo di acquisizione definito in Mode (Tabella) per il canale definito in Channel sul modulo indicato in Address. Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione. Il modo è impostato in modo permanente, è possibile modificarlo eseguendo una nuova impostazione.

eGetAnInp

bool_t eGetAnInp(uint8_t Address, uint8_t Channel, float32_t* Value);

Esegue l'acquisizione del valore analogico dal canale definito in Channel sul modulo indicato in Address l'acquisizione è eseguita secondo l'impostazione effettuata dall funzione eSetAnInpMode. Il valore acquisito è trasferito nella variabile definita da Value. Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione.

eSetAnOutMode

bool_t eSetAnOutMode(uint8_t Address, uint8_t Channel, AO_MODE Mode);

Imposta il modo di uscita analogica definito in Mode (Tabella) per il canale definito in Channel sul modulo indicato in Address. Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione. Il modo è impostato in modo permanente, è possibile modificarlo eseguendo una nuova impostazione.

eSetAnOut

bool_t eSetAnOut(uint8_t Address, uint8_t Channel, float32_t Value);

Esegue l'impostazione del valore definito in Value in uscita sul canale definito in Channel sul modulo indicato in Address il segnale in uscita dipende dalla impostazione effettuata dall funzione eSetAnOutMode. Viene ritornato l'esito: false: Errore esecuzione, true: Ok esecuzione.

Esempi

Come utilizzare gli esempi.
Sono acquisiti gli I/O sia logici che analogici di un modulo di espansione I/O mixed signal connesso al bus.

CodeLite (Ptp174)
// *****************************************************************************
// PROGRAM "PhrModules.cpp"
// *****************************************************************************
// An example how to manage the peripheral modules.
// Please note that this program is cyclically called by the main.
// -----------------------------------------------------------------------------

#include <stdio.h>
#include <SystemTime.h>
#include <PhrI2cBus.h>
#include <PhrFcts.h>
#include <LastError.h>
using namespace Elsist; //Defines namespace
extern bool FirstLoop; //First execution loop
extern int8_t Kb; //Key pressed

// -----------------------------------------------------------------------------
// PROGRAM EXECUTION
// -----------------------------------------------------------------------------

bool PhrModules(void)
{
    // -------------------------------------------------------------------------
    // LOCAL VARIABLES
    // -------------------------------------------------------------------------
    // Define variables.

    static uint8_t PModules=0; //Peripheral modules
    uint32_t DInputs; //Digital inputs
    float32_t AInp; //Analog input value
    static uint32_t DOutputs; //Digital outputs
    static uint32_t TimeBf; //Time buffer (uS)

    // -------------------------------------------------------------------------
    // MANAGES THE PERIPHERAL MODULES
    // -------------------------------------------------------------------------
    // Read how many modules are attached to the bus, if no modules ends.

    if (FirstLoop)
    {
        PModules=eGetModulesOnPhrI2cBus(); //Peripheral modules
        printf("%d: Module(s) connected\n", PModules);
    }
    if (PModules == 0) return(true);
    
    // -------------------------------------------------------------------------
    // ACQUIRES DIGITAL INPUTS AND MANAGES DIGITAL OUTPUTS
    // -------------------------------------------------------------------------
    // Supposing to have a logic I/O module (With address 0) attached to the bus
    // are reading the inputs and managed the outputs.

    if (!eIsPhrI2cBusExtModuleAv(0)) return(true); //Exit if module not present
    if (!eGetPhrDI(0, DI_MODE::DI_8_LL, &DInputs))
        {printf("eGetPhrDi error: %u\n", eGetLastError()); return(false);}
    
    // Copy Di00 input status on Do00 output.
    
    if (DInputs&eBITPATTERN(0)) eBITSET(DOutputs, 0); else eBITRESET(DOutputs, 0);

    // Blink the Do01 outputs.

    if (TimeBf == 0) TimeBf=eGetSysTime(); //Initialize the reference time
    if ((eGetSysTime()-TimeBf) > eSEC(1))
    {
        TimeBf=eGetSysTime(); //Time buffer (uS)
        if (DOutputs&eBITPATTERN(1)) eBITRESET(DOutputs, 1); else eBITSET(DOutputs, 1);
    }

    // Manage keyboard command and reverses the output status.

    if ((Kb&0xFF) == 'o') {if (DOutputs&eBITPATTERN(2)) eBITRESET(DOutputs, 2); else eBITSET(DOutputs, 2);}

    // Transfer the value on the extension module digital outputs.

    if (!eSetPhrDO(0, DO_MODE::DO_8_LL, &DOutputs))
        {printf("eSetPhrDO error: %u\n", eGetLastError()); return(false);}
    
    // -------------------------------------------------------------------------
    // ACQUIRES ANALOG INPUTS AND MANAGES ANALOG OUTPUTS
    // -------------------------------------------------------------------------
    // Supposing to have a analog I/O module (With address 0) attached to the bus
    // are reading the inputs and managed the outputs.

    if (!eIsPhrI2cBusExtModuleAv(0)) return(true); //Exit if module not present

    if ((Kb&0xFF) == 'a')
    {
        // Acquires the analog input.

        if (!eSetAnInpMode(0, 0, AD_VOLT_0_10_COMMON)) {printf("eSetAnInpMode error\n"); return(false);}
        if (!eGetAnInp(0, 0, &AInp)) {printf("eGetAnInp error\n"); return(false);}
        printf("Analog input value: %f\n", AInp);

        // Set the analog output.

        if (!eSetAnOutMode(0, 0, DA_VOLT_0_10)) {printf("eSetAnOutMode error\n"); return(false);}
        if (!eSetAnOut(0, 0, AInp)) {printf("eSetAnOut error\n"); return(false);}
    }
    return(true);
}

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