/* ** ################################################################### ** ** Filename : CruzCruz2.C ** ** Project : CruzCruz2 ** ** Processor : MC68HC908QT4CP ** ** Version : Driver 01.00 ** ** Compiler : Metrowerks HC08 C Compiler V-5.0.16 ** ** Date/Time : 24/4/2004, 16:34 ** ** Abstract : ** ** Main module. ** Here is to be placed user's code. ** ** Settings : ** ** ** Contents : ** ** No public methods ** ** ** (c) Copyright UNIS, spol. s r.o. 1997-2002 ** ** UNIS, spol. s r.o. ** Jundrovska 33 ** 624 00 Brno ** Czech Republic ** ** http : www.processorexpert.com ** mail : info@processorexpert.com ** ** ################################################################### */ /* MODULE CruzCruz2 */ //#pragma MESSAGE DISABLE C1420 /* WARNING C1420: Result of function-call is ignored. You can uncomment this line to disable this compiler warning */ /*Including used modules for compilling procedure*/ #include "Cpu.h" #include "Events.h" /*Include shared modules, which are used for whole project*/ #include "PE_Types.h" #include "PE_Error.h" #include "PE_Const.h" #include "IO_Map.h" #define abs(A) (A) < 0 ? -(A) : (A) float nosso_sqrt( int value ) { float result = value/2; float divisor; do { divisor = result; result = (value/divisor+divisor)/2; } while(abs(result) < 0.001); return result; } void main(void) { int soma = 0; int i = 0; int modulo = 0; int conta = 0; // count how many times the signal crosses the zero int adr, adr_; // ADC data register... will be used to calculate the exactly time the signal crosses the zero int p1,p2,p3; float p; PTA = 0; /* clear/initialize Port A */ /* do PWM */ TSC_TSTOP = 1; // stop counter TSC_PS = 0; // prescaler divide por 1 TSC_TRST = 1; // reset counter TMODH = 0; TMODL = 250; // PWM frequency 20kHz TCH1H = 0; TCH1L = 50; // pulse width TSC0_MS0B = 0; // PWM TSC1_MS1A = 1; // output compare TSC1_ELS1B = 1; TSC1_ELS1A = 0; // clear on compare TSC1_TOV1 = 1; // toggle on overflow TSC_TSTOP = 0; // start counter /* do AD */ ADSCR_CH = 2; // entrada pino AD2=PTA4 ADSCR_ADCO = 1; // convert continously ADICLK_ADIV = 2; // AD clock = 5MHz/4 (about 1MHz) TCH1L = 5; /* 5 <= TCH1L <= 230 */ adr_ = ADR-0x80; // Initializing the former sample p1 = p2 = p3 = 0; for(;;) { soma = 0; i = 0; conta = 0; while (i < 2000) { // 100 milisegundo while(!PTA_PTA1); modulo = (ADR < 124) ? (124 - ADR) : (ADR - 124); soma += modulo; adr = ADR-0x80; // Initializing the first sample if( ( adr<=0 && adr_>0 ) || ( adr>0 && adr_<=0 ) ) conta++; // counting // Calculo do Coeficiente de Correlação Linear p1 = ( adr * adr_ ) + p1; if ( i < 1999 ) p2 = ( adr * adr ) + p2; if ( i > 0 ) p3 = ( adr * adr ) + p3; // Calculo do Coeficiente de Correlação Linear adr_ = adr; while(PTA_PTA1); i++; } // p = p1 / nosso_sqrt( p2 * p3 ); if ( soma > 10000 ) { if ( ( conta < 500 ) && ( p < 0.3 ) ) TCH1L = 230; else TCH1L = 5; // espera 1 segundo for (i = 0; i < 20000; i++) { while(!PTA_PTA1); while(PTA_PTA1); } } } } /* END CruzCruz2 */ /* ** ################################################################### ** ** This file was created by UNIS Processor Expert 02.96 for ** the Motorola HC08 series of microcontrollers. ** ** ################################################################### */