/******************* * Universidade Federal de Campina Grande * Disciplina: * * * Hugo Raniere * Michael Angelo * */ #include /* include peripheral declarations */ #include void main(void) { int soma = 0; int i = 0; int modulo = 0; 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 */ for(;;) { soma = 0; i = 0; while (i < 20) { // 1 milisegundo while(!PTA_PTA1); modulo = (ADR < 124) ? (124 - ADR) : (ADR - 124); soma += modulo; while(PTA_PTA1); i++; } if (soma > 400) { if (TCH1L == 5) TCH1L = 230; else TCH1L = 5; // espera 1 segundo for (i = 0; i < 20000; i++) { while(!PTA_PTA1); while(PTA_PTA1); } } } }