/*This project measures the period of any frequency applied to the Microcounteroller.Pin INT(external interrupt) is used to detect each rising edge of the wave.Timer1 is used the calculate the time of the wave period(Ton + Toff).This is how it works:1 - First rising edge is detected using INT(external interrupt).2 - Timer1 will start counting.3 - Second rising edge is detected using INT(external interrupt).4 - Timer1 will stop counting because from first rising edge to the second rising edge is a complete wave which means the period(Ton + Toff).5 - Calculate period.6 - Display results on LCD.Author: Sameer Semmoor*/ // LCD module connectionssbit LCD_RS at RB2_bit;sbit LCD_EN at RB3_bit;sbit LCD_D4 at RB4_bit;sbit LCD_D5 at RB5_bit;sbit LCD_D6 at RB6_bit;sbit LCD_D7 at RB7_bit;sbit LCD_RS_Direction at TRISB2_bit;sbit LCD_EN_Direction at TRISB3_bit;sbit LCD_D4_Direction at TRISB4_bit;sbit LCD_D5_Direction at TRISB5_bit;sbit LCD_D6_Direction at TRISB6_bit;sbit LCD_D7_Direction at TRISB7_bit;// End LCD module connectionsunsigned int timer1_register; //stores timer1 register valueunsigned int overflow_number=0; //stores timer1 overflow numberunsigned long counter=0; //stores coutner valuechar counter_text[11]; //sotres counter value converted to stringfloat period; //stores the periodic timechar period_text[20]; //stores the periodic time converted to stringunsigned short edge_counter=0; //edge counter, will be used to determine fisrt & second rising edges of the pulsebit flag; //will be set if two edges are detected, so freguency & period can be calculatedvoid CONFIGURE_CAPTURE(){ trisb.b0 = 1; //B0(INT0) as input INTEDG_bit = 1; //Interrupt on rising edge of RB0/INT pin INTE_bit = 1; //enable INT0 external interrupt //Timer1 configurations--------------------------------------------------------- //prescaler 1:8 T1CKPS1_bit = 1; T1CKPS0_bit = 1; TMR1IE_bit = 1; //enable timer1 overflow interrupt TMR1ON_bit = 0; //stop timer1 at the beginning TMR1H = 0; TMR1L = 0; //reset high & low bytes of timer1//------------------------------------------------------------------------------ //enable all interrupts PEIE_bit = 1; GIE_bit = 1;}void get_print_timer1(){ timer1_register = (TMR1H<<8) + TMR1L; //get high & low bytes of timer1 //get counter value + number of overflow of timer1, so we can store values greater than 65536, because timer1 can only count up to 65536. counter = ( overflow_number * 65536 ) + timer1_register; //calculate period period = (float)(counter + 1); period = (float)period * (4); period = (float)period * 1/20000000; period = (float)period * 8; //convert counter & period values to string LongWordToStr(counter, counter_text); FloatToStr(period,period_text); //display timer1 & period values lcd_out(2,1,"counter: ");lcd_out(2,9, counter_text); lcd_out(4,1, "T: ");lcd_out(4,5, period_text); lcd_out_cp("S"); //reset Timer & counters TMR1H = 0; TMR1L = 0; overflow_number = 0;}void interrupt(){ if(TMR1IF_bit == 1) //if Timer1 overflowed { TMR1IF_bit = 0; //reset timer1 interrupt flag overflow_number++; //increment every timer1 overflow } if(INTF_bit == 1) //if INT0 interrupt occured (rising edge detected) { INTF_bit = 0; //reset INT0 interrupt flag TMR1ON_bit = 0; //stop timer1 edge_counter++; //increment by 1 if(edge_counter == 1) //if cnt incremented by 1 , this means first rising edge is detected { TMR1ON_bit = 1; //timer1 start counting } if(edge_counter == 2) //if cnt incremented by 1 again menas 2, this means second rising edge is detected { //disable all interrupts PEIE_bit = 0; GIE_bit = 0; TMR1ON_bit = 0; //stop timer1 flag=1; //set this flag, to indicate that timem of the pulse is counted } }}void main() { Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // CLEAR display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off CONFIGURE_CAPTURE(); lcd_out(1,1, " CAPTURE TIMER1"); while(1) { if(flag == 1) //if this flag is set { get_print_timer1(); edge_counter=0; //reset rising edge counter flag = 0; //reset this flag //reenable all interrupts PEIE_bit = 1; GIE_bit = 1; } }}
การแปล กรุณารอสักครู่..