K22 processor

Committer:
bosko001
Date:
Sun Mar 21 18:20:15 2021 +0100
Revision:
7:4aa3dac73b66
Child:
11:f46528c56b8f
novi pocetak

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 7:4aa3dac73b66 1 #include "ZeroCrossing.h"
bosko001 7:4aa3dac73b66 2 #include "mbed.h"
bosko001 7:4aa3dac73b66 3
bosko001 7:4aa3dac73b66 4
bosko001 7:4aa3dac73b66 5 #include "Pinovi.h"
bosko001 7:4aa3dac73b66 6
bosko001 7:4aa3dac73b66 7 // extern DigitalOut ledB;
bosko001 7:4aa3dac73b66 8 // void ledOFF(void ){ ledB=1;}; void ledON(void) { ledB=0;}
bosko001 7:4aa3dac73b66 9
bosko001 7:4aa3dac73b66 10
bosko001 7:4aa3dac73b66 11
bosko001 7:4aa3dac73b66 12 InterruptIn zc( ZC_PIN );
bosko001 7:4aa3dac73b66 13
bosko001 7:4aa3dac73b66 14 DigitalOut dout6(PTB2, 1 );
bosko001 7:4aa3dac73b66 15 DigitalOut dout5(PTB3, 1 );
bosko001 7:4aa3dac73b66 16 DigitalOut dout4(PTB10, 1 );
bosko001 7:4aa3dac73b66 17 DigitalOut dout3(PTB11, 1 );
bosko001 7:4aa3dac73b66 18 DigitalOut dout2(PTC11, 1 );
bosko001 7:4aa3dac73b66 19 DigitalOut dout1(PTC10, 1 );
bosko001 7:4aa3dac73b66 20
bosko001 7:4aa3dac73b66 21 // DigitalIn din1( PTC1, PullUp);
bosko001 7:4aa3dac73b66 22 // DigitalIn din2( PTC2, PullUp);
bosko001 7:4aa3dac73b66 23 // DigitalIn din3( PTC3, PullDown);
bosko001 7:4aa3dac73b66 24 // DigitalIn din4( PTC4, PullUp); // PROBLEM!!!!!
bosko001 7:4aa3dac73b66 25 // DigitalIn din5( PTC8, PullDown);
bosko001 7:4aa3dac73b66 26 // DigitalIn din6( PTC9, PullDown);
bosko001 7:4aa3dac73b66 27
bosko001 7:4aa3dac73b66 28 DigitalIn din6( PTC5, PullUp);
bosko001 7:4aa3dac73b66 29 DigitalIn din5( PTC7, PullUp);
bosko001 7:4aa3dac73b66 30 DigitalIn din4( PTC0, PullUp);
bosko001 7:4aa3dac73b66 31 DigitalIn din3( PTC9, PullUp);
bosko001 7:4aa3dac73b66 32 DigitalIn din2( PTC8, PullUp);
bosko001 7:4aa3dac73b66 33 DigitalIn din1( PTC1, PullUp);
bosko001 7:4aa3dac73b66 34
bosko001 7:4aa3dac73b66 35
bosko001 7:4aa3dac73b66 36 /************************************************************************************************************************/
bosko001 7:4aa3dac73b66 37
bosko001 7:4aa3dac73b66 38 #define IB(b) b?0:1 //invert bita
bosko001 7:4aa3dac73b66 39 #define IB1(b) ~b
bosko001 7:4aa3dac73b66 40
bosko001 7:4aa3dac73b66 41
bosko001 7:4aa3dac73b66 42 void S_bitPortIn::portIn_refresh( ) { d0 = din1; d1 = din2; d2 = din3; d3 = din4; d4 = din5; d5 = din6; d6 = d7 = 0;}
bosko001 7:4aa3dac73b66 43
bosko001 7:4aa3dac73b66 44 void S_bitPortOut::portOut_refresh( ) { dout1 = IB(d0); dout2 = IB(d1); dout3 = IB(d2); dout4 = IB(d3); dout5 = IB(d4); dout6 = IB(d5); }
bosko001 7:4aa3dac73b66 45
bosko001 7:4aa3dac73b66 46
bosko001 7:4aa3dac73b66 47 /************************************************************************************************************************/
bosko001 7:4aa3dac73b66 48
bosko001 7:4aa3dac73b66 49
bosko001 7:4aa3dac73b66 50 #define ZCtimeMeasurement( mtime ) t.stop(); unsigned int frct = t.elapsed_time().count(); if( frct > frc ) mtime = frct - frc; frc = frct; t.start();
bosko001 7:4aa3dac73b66 51
bosko001 7:4aa3dac73b66 52 // aktivacija postpone funkcije, mora da postoji pospone vreme, da bi se timer aktivirao
bosko001 7:4aa3dac73b66 53 #define ATimer( atimer, atimerfun, atime) \
bosko001 7:4aa3dac73b66 54 if( atime != 0 ) \
bosko001 7:4aa3dac73b66 55 atimer.attach_us([this](){ atimerfun();}, atime );
bosko001 7:4aa3dac73b66 56
bosko001 7:4aa3dac73b66 57
bosko001 7:4aa3dac73b66 58 /* iniciranje vremena izvrsenja postponovane funkcije. Ako je bilo vreme 0, stavla je timeONOFF/2 - po defaultu, u suprotnom
bosko001 7:4aa3dac73b66 59 stavice se vreme postone_toutONOFFtime, koje su int atributi klase, i inicijalno su 0 */
bosko001 7:4aa3dac73b66 60 #define POSTPONtime_initiate( ONOFF) \
bosko001 7:4aa3dac73b66 61 { \
bosko001 7:4aa3dac73b66 62 if( postpone_tout##ONOFF##time == 0 ) postpone_tout##ONOFF( time##ONOFF/2 ); \
bosko001 7:4aa3dac73b66 63 else postpone_tout##ONOFF( postpone_tout##ONOFF##time ); \
bosko001 7:4aa3dac73b66 64 }
bosko001 7:4aa3dac73b66 65
bosko001 7:4aa3dac73b66 66
bosko001 7:4aa3dac73b66 67 C_zeroCrossing::C_zeroCrossing( C_data *pc_data)
bosko001 7:4aa3dac73b66 68 {
bosko001 7:4aa3dac73b66 69 this->pc_data = pc_data;
bosko001 7:4aa3dac73b66 70
bosko001 7:4aa3dac73b66 71 int *p;
bosko001 7:4aa3dac73b66 72
bosko001 7:4aa3dac73b66 73 p = (int*)0x4004B0C8; // digital filter width register PTC
bosko001 7:4aa3dac73b66 74 *p = *p | (10); // filter clocks number max 32
bosko001 7:4aa3dac73b66 75
bosko001 7:4aa3dac73b66 76 p = (int*)0x4004B0C4; // digital filter clock register PTC
bosko001 7:4aa3dac73b66 77 *p = *p | (0<<12); // 0 - bus clock 1 - lpo clock
bosko001 7:4aa3dac73b66 78
bosko001 7:4aa3dac73b66 79
bosko001 7:4aa3dac73b66 80 p = (int*)0x4004B0C0; // digital filter enable register of PTC
bosko001 7:4aa3dac73b66 81 *p = *p | (1<<12); // filter enabled on pin PTC12
bosko001 7:4aa3dac73b66 82
bosko001 7:4aa3dac73b66 83 // pasive filter jedino radi
bosko001 7:4aa3dac73b66 84
bosko001 7:4aa3dac73b66 85 p = (int*)0x4004B030; // adresa control registra od PTC12
bosko001 7:4aa3dac73b66 86 *p = *p | 0x10; // pasive filter enabled
bosko001 7:4aa3dac73b66 87
bosko001 7:4aa3dac73b66 88 /* handleri uzlazne i silazne ivice interrupt-a zero crossinga */
bosko001 7:4aa3dac73b66 89 zc.rise([this](){ ZCtimeMeasurement( timeOFF) ontime_toutONfun(); POSTPONtime_initiate(ON) inc_zcCounter();});
bosko001 7:4aa3dac73b66 90 zc.fall([this](){ ZCtimeMeasurement( timeON) ontime_toutOFFfun(); POSTPONtime_initiate(OFF) inc_zcCounter(); });
bosko001 7:4aa3dac73b66 91
bosko001 7:4aa3dac73b66 92 // set_ontimeONfun( [](){ledOFF();} );
bosko001 7:4aa3dac73b66 93 // set_ontimeOFFfun( [](){ledON();} );
bosko001 7:4aa3dac73b66 94 // set_postponeONfun( ([]() { ledON(); }) );
bosko001 7:4aa3dac73b66 95 // set_postponeOFFfun( []() { ledOFF(); } );
bosko001 7:4aa3dac73b66 96
bosko001 7:4aa3dac73b66 97 }
bosko001 7:4aa3dac73b66 98
bosko001 7:4aa3dac73b66 99 void C_zeroCrossing::ontime_toutONfun( void) { if( p_ontime_toutONfun != nullptr) p_ontime_toutONfun(); else {/*ledOFF();*/} }
bosko001 7:4aa3dac73b66 100 void C_zeroCrossing::ontime_toutOFFfun(void) { if( p_ontime_toutOFFfun != nullptr) p_ontime_toutOFFfun(); else {/*ledON();*/} }
bosko001 7:4aa3dac73b66 101
bosko001 7:4aa3dac73b66 102 // mora da postoji postone funkcija i postpone vreme da bi se funkcija izvrslia
bosko001 7:4aa3dac73b66 103 void C_zeroCrossing::postpone_toutONfun( void) { if( (p_postpone_toutONfun !=nullptr) ) p_postpone_toutONfun(); /*else*//*{ ATimer( toutON, ledOFF, 10000 );}*/}
bosko001 7:4aa3dac73b66 104 void C_zeroCrossing::postpone_toutOFFfun( void) { if( (p_postpone_toutOFFfun !=nullptr) )p_postpone_toutOFFfun(); /*else*//*{ ATimer( toutOFF, ledON, 100000 );}*/}
bosko001 7:4aa3dac73b66 105
bosko001 7:4aa3dac73b66 106 void C_zeroCrossing::postpone_toutON( unsigned int tim ) { ATimer(toutON, postpone_toutONfun, tim ) }
bosko001 7:4aa3dac73b66 107 void C_zeroCrossing::postpone_toutOFF( unsigned int tim ) { ATimer(toutOFF, postpone_toutOFFfun, tim ) }
bosko001 7:4aa3dac73b66 108
bosko001 7:4aa3dac73b66 109
bosko001 7:4aa3dac73b66 110