IO plocana FRDM K64

ZeroCrossing/ZeroCrossing.cpp

Committer:
bosko001
Date:
2021-03-30
Revision:
11:f46528c56b8f
Parent:
7:4aa3dac73b66

File content as of revision 11:f46528c56b8f:

#include "ZeroCrossing.h"
#include "mbed.h"


#include "Pinovi.h"

// extern DigitalOut ledB;
// void ledOFF(void ){ ledB=1;};  void ledON(void) { ledB=0;}



InterruptIn zc( ZC_PIN );



#define Dout(n)   dout##n(PIN_OUT_##n, 1)

DigitalOut Dout(6);
DigitalOut Dout(5);
DigitalOut Dout(4);
DigitalOut Dout(3);
DigitalOut Dout(2);
DigitalOut Dout(1);


#define Din(n)   din##n(PIN_IN_##n, PullUp)

DigitalIn Din(6);
DigitalIn Din(5);
DigitalIn Din(4);
DigitalIn Din(3);
DigitalIn Din(2);
DigitalIn Din(1);



/************************************************************************************************************************/

#define IB(b)   b?0:1   //invert bita
#define IB1(b)  ~b


void S_bitPortIn::portIn_refresh(  )    { d0 = din1; d1 = din2; d2 = din3; d3 = din4; d4 = din5; d5 = din6; d6 = d7 = 0;}

void S_bitPortOut::portOut_refresh( )    { dout1 = IB(d0); dout2 = IB(d1); dout3 = IB(d2); dout4 = IB(d3); dout5 = IB(d4); dout6 = IB(d5); }


/************************************************************************************************************************/


#define ZCtimeMeasurement( mtime )   t.stop(); unsigned int frct = t.elapsed_time().count(); if( frct > frc ) mtime = frct - frc; frc = frct; t.start();

// aktivacija postpone funkcije, mora da postoji pospone vreme, da bi se timer aktivirao
#define ATimer( atimer, atimerfun, atime)   \
    if( atime != 0 )   \
    atimer.attach_us([this](){ atimerfun();}, atime ); 


/* iniciranje vremena izvrsenja postponovane funkcije. Ako je bilo vreme 0, stavla je timeONOFF/2 - po defaultu, u suprotnom
   stavice se vreme postone_toutONOFFtime, koje su int atributi klase, i inicijalno su 0   */
#define POSTPONtime_initiate( ONOFF) \
{                                  \
    if( postpone_tout##ONOFF##time == 0 )   postpone_tout##ONOFF( time##ONOFF/2 );  \
    else postpone_tout##ONOFF( postpone_tout##ONOFF##time );  \
}


C_zeroCrossing::C_zeroCrossing( C_data *pc_data)
{
    this->pc_data = pc_data;

int *p;   

p = (int*)0x4004B0C8; // digital filter width register PTC
*p = *p | (10);   //  filter clocks number max 32

p = (int*)0x4004B0C4; // digital filter clock register PTC
*p = *p | (0<<12);   // 0 - bus clock 1 - lpo clock


p = (int*)0x4004B0C0; // digital filter enable register of PTC
*p = *p | (1<<12);   // filter enabled on pin PTC12

//   pasive filter jedino radi 

p = (int*)0x4004B030; // adresa control registra od PTC12
*p = *p | 0x10;   // pasive filter enabled

    /* handleri uzlazne i silazne ivice interrupt-a zero crossinga */
        zc.rise([this](){  ZCtimeMeasurement( timeOFF) ontime_toutONfun(); POSTPONtime_initiate(ON)  inc_zcCounter();});
        zc.fall([this](){  ZCtimeMeasurement( timeON)  ontime_toutOFFfun(); POSTPONtime_initiate(OFF) inc_zcCounter(); });

    // set_ontimeONfun( [](){ledOFF();} );
    // set_ontimeOFFfun(  [](){ledON();} );
    // set_postponeONfun( ([]() { ledON();   }) );
    // set_postponeOFFfun( []() { ledOFF();  } );

}

    void C_zeroCrossing::ontime_toutONfun( void) {  if( p_ontime_toutONfun != nullptr) p_ontime_toutONfun(); else {/*ledOFF();*/} }
    void C_zeroCrossing::ontime_toutOFFfun(void) {  if( p_ontime_toutOFFfun != nullptr) p_ontime_toutOFFfun(); else {/*ledON();*/} }

//  mora da postoji postone funkcija i postpone vreme da bi se funkcija izvrslia 
    void C_zeroCrossing::postpone_toutONfun( void) { if( (p_postpone_toutONfun !=nullptr) ) p_postpone_toutONfun(); /*else*//*{   ATimer( toutON, ledOFF, 10000 );}*/}
    void C_zeroCrossing::postpone_toutOFFfun( void) { if( (p_postpone_toutOFFfun !=nullptr) )p_postpone_toutOFFfun(); /*else*//*{ ATimer( toutOFF, ledON, 100000 );}*/}

    void C_zeroCrossing::postpone_toutON( unsigned int tim ) {  ATimer(toutON, postpone_toutONfun, tim )    }
    void C_zeroCrossing::postpone_toutOFF( unsigned int tim  ) {  ATimer(toutOFF, postpone_toutOFFfun, tim ) }