3 channel frequency counter

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
priyal101
Posts: 1
Joined: 21 Jun 2018 07:37

3 channel frequency counter

#1 Post by priyal101 » 21 Jun 2018 07:42

Hi, I am required to make a 3 channel frequency counter for my project. I am using a PIC18F4550. I am a begineer at PIC and I found a single channel code in one of the books I was reading. I am having problem with the timers(falling short on them). Can anyone help me modify it to 3 channel?


This is the single channel code

Code: Select all

//LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//end LCD module connections

#define PULSE PORTA.RA4

unsigned int Overflow;
unsigned char Cnt;
//
//Timer interupt service routine jumps here at every 10ms
//
void interrupt(void)
{
 if(INTCON.TMR0IF==1)            //If TIMER0 INTERRUPT
            {
                  Overflow++;     //Increment Overflow flag
                  INTCON.TMR0IF=0; //Clear timer0 interrupt flag
            }
 if(PIR1.TMR1IF==1)                //If timer1 interrupt
            {
                   TMR1H=0x0B;     //Reload timer register
                   TMR1L=0xDC;
                   Cnt++;          //Increment cnt
                   PIR1.TMR1IF=0;  //Clear Timer1 interrupt flag
            }
}



void main()
{
 unsigned char Txt[11];
 unsigned long Elapsed;
 unsigned char L_Byte, H_Byte;

 ANSELA = 0; // Configure PORTA as digital
 ANSELB = 0;
 TRISA.RA4 = 1; // RA4 is input
 Lcd_Init(); // Inialize LCD
 Lcd_Cmd(_LCD_CURSOR_OFF); // Disable cursor
//
 // Configure TIMER0 for 16-bit mode, no prescaler, clock provided by external
 // signal into pin T0CKI. Timer is not started here
 //
 T0CON = 0x28; // TIMER0 16-bit,no prescaler,T0CKI clk
//
// Configure Timer1 for 250 ms Overflow. Timer1 is not started here
//
 T1CON = 0x36;

 PIE1 = 0x01; // Enable TIMER1 interrupts
 PIR1.TMR1IF = 0; // Clear TIMER1 interrupt flag
 INTCON = 0xE0; // Enable TIMER0 and TIMER1 interrupts

 for(;;) // Wait for interrupts
 {
 TMR0H = 0; // Clear Timer0 registers
 TMR0L = 0;
 TMR1H = 0x0B; // Load Timer1 registers
 TMR1L = 0xDC;
 //
 Overflow = 0; // Clear Overflow count
 Cnt = 0; // Clear Cnt
 //
  // Start TIMER0. It will increment each me an external pulse is detected.
 // TIMER0 increments on the rising edge of the external clock
 //
 T0CON.TMR0ON = 1;
 //
 // Start Timer1 to count 4 × 250 ms = 1 s
 //
 T1CON.TMR1ON = 1;
 while(Cnt != 4); // Wait unl 1 s has elapsed
 //
 // 1 s has elapsed. Stop both mers
 //
 T0CON.TMR0ON = 0; // Stop TIMER0
 T1CON.TMR1ON = 0; // Stop Timer1
 // Get TIMER0 count
 L_Byte = TMR0L;
 H_Byte = TMR0H;
 //
 // Store TIMER0 count is variable Elapsed
 //
 Elapsed = (unsigned long)256 * H_Byte + L_Byte;
 Elapsed = 65536 * Overflow + Elapsed;
 //
 // Convert into string and display
 //
 LongWordToStr(Elapsed, Txt); // Long to string conversion
 Lcd_Cmd(_LCD_CLEAR); // Clear LCD
 Lcd_Out(1,1,"Frequency (HZ)"); // Display heading
 Lcd_Out(2,1,Txt); // Display measured frequency

 Delay_ms(1000); // Wait 1 s and repeat
 }
}

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: 3 channel frequency counter

#2 Post by jayanthd » 26 Jun 2018 10:17

My project is here. You can refer it. I am pic.programmer there.

https://www.edaboard.com/showthread.php ... Tachometer

Code: Select all

Elapsed = (unsigned long)256 * H_Byte + L_Byte;
will be slower to compute.

Try

Code: Select all

Elapsed = (unsigned long)H_Byte << 8 + L_Byte;
Edit:

Another thing...

In Lcd sbit defines you should replace

Code: Select all

RB4_bit;
and other similar with LATx registers as you are using PIC18F otherwise you will get garbage or no display.
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

Post Reply

Return to “User Projects”