#include /* include peripheral declarations */ #include void main(void) { byte pw; byte ad_1, ad_2; 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) ad_2 = ADR; /* 5 <= TCH1L <= 230 */ for(;;) { while(!PTA_PTA1); ad_1 = ADR; pw = (ad_1>>1)+(ad_2>>1); while(PTA_PTA1); TCH1L = pw; ad_2 = ad_1; } }