You are on page 1of 2

{ int i; status=AT91C_BASE_TC0 ->TC_SR; //Read status register AT91C_BASE_AIC ->AIC_ICCR = 0x1000 ; //Clear the interrupt bit by writing 1 to AIC_ICCR

if ((status&0x01)==0x01) { overflow_counter++; //If COVFS bit is set increment the overflow counter }else if ((status&0x20)==0x20) { reg_a = AT91C_BASE_TC0->TC_RA; //If register A is loaded reser overflow-counter to 0 overflow_counter=0; }else if ((status&0x40)==0x40) { reg_b = AT91C_BASE_TC0->TC_RB; if(window_counter<N_SAMPLES){ sample_values[window_counter] = (overflow_counter*65536) + reg_b-reg_a; //If register B is loaded calculate the count and add it in the array window_counter++; //Increment the counter }else{ window_counter =0; for(i=0;i<N_SAMPLES;i++){ window_sum=window_sum+sample_values[i]; //add the accumulated samples } // window_average=window_sum>>= N_SHIFT; (Division with the number of samples) window_average=(window_sum>>= N_SHIFT); //Divide by left shift window_average=(window_average<<=2)+window_average; //Multiply by 5 factor acheived my shifting left by 2 and adding the same number new_Value_Flag=1; // Flag set when a new sample is created sample_values[window_counter] = (overflow_counter*65536) + reg_b-reg_a; //Accumulate the new sample value } } AT91C_BASE_AIC->AIC_EOICR = 0 ; //Write a value to the end of interrupt register } void Timer0_Init(){ AT91C_BASE_PMC->PMC_PCER = (AT91C_BASE_PMC->PMC_PCER |0x1000); //Enable peripheral clock to TIOA0 AT91C_BASE_PIOB->PIO_PDR = 0x1; //Disable the Paralell IO

AT91C_BASE_PIOB->PIO_ASR = 0x1; //Select the A peripheral AT91C_BASE_TC0-> TC_CMR = 0x000A0600; //Set the TC_CMR to detect falling edges AT91C_BASE_TC0-> TC_IER = 0x61; //Enable interrupt to detect LDRBS, LDRAS and COVFS AT91C_BASE_TC0->TC_CCR = 0x01 ; //Enable the clock printf("\n\r TI"); } void Aic_Init(){ AT91C_BASE_AIC ->AIC_SMR[12] =0x60; //Set AIC_SMR to Positive edge triggered and priority 0 AT91C_BASE_AIC ->AIC_SVR[12] = (unsigned int)timer0_c_irq_handler; //Set the AIC_SVR to interrupt service routine AT91C_BASE_AIC ->AIC_ICCR = 0x1000 ; //Clear any interrupts AT91C_BASE_AIC ->AIC_IECR = 0x1000; //Enable the interrupt printf("\n\r AI"); }

You might also like