K22 processor

Committer:
bosko001
Date:
Tue Mar 30 17:05:48 2021 +0200
Revision:
11:f46528c56b8f
Parent:
7:4aa3dac73b66
sredjeni pinovi u pinovi_h

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