Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] How to measure the frequency count of AC supply(50Hz) using T0CKI of pic16f877a

Status
Not open for further replies.

karthims

Newbie level 5
Joined
Sep 15, 2014
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
chennai
Activity points
74
I have enclosed my code with this thread.As well i seen few of threads in our forum.but even though i can not able to get the result. In that i given snippet for Timer1 for to produce exact 1 second delay to count the frequency.
Used controller-PIC16f877a
IDE -MPLABx
compiler -HItech c
 

Attachments

  • test1.txt
    1 KB · Views: 54

The logic is you have to use TMR0 as a counter (8 bit counter) and TMR1 as a Timer.

Set Timer1 to interrupt every 500 ms. Use a counter variable and increment it in TMR1 ISR. If counter value is 2 then clear counter and stop TMR1 and TMR0 and set a flag.

Test if this flag is set in while(1) loop. If it is set then convert the TMR0 result to frequency and then convert it to string and display it and clear the flag and enable TMR1 and TMR0 (reinitialize).

TMR1 and TMR0 should be started at the same time. TMR0 and TMR1 should be stopped after 1 sec.

In TMR0 ISR use a counter and increment it once on TMR0 interrupt. This way if TMR0 value overflows after reaching 255 (8 bit) then counter will hold 1. This is used with TMR0 value to make the frequency calculation.
 
Actually i followed your method,even though i did not get the output, i am using timer0 in polling method and timer 1 ISR.I have attached the edited code belowView attachment edabrd.txt
 
Last edited:

I see that you have not disabled Comparators and ADC. Configure CMCON and ADCON1 regsiters and try.
 
Actually i configured analog pins as digital in LCD initialization as well i disabled comparators .Even though it is not working.I have attached my required snippet to help.View attachment edabrd.txt
 

hello,


i suppose you are using Q=20Mhz
it is not a good way to get 1000 interrupts to define a time of 1 second !
lets the timer1 do the job , it can count up to 100 msec , in 10 interrupts
init TMR1 with 3035 value => 100mS

so more simple ...

pay attention : TMR0 8 bits ..limit 255

and with 1 sec ... resolution is +-1Hz
Code:
int PointOneSec;    // compte number of .1 sec
unsigned char Counts;

void TIMER0_INIT()
 {
    Counter_DIR;
    CMCONbits.CM0=1;//disable comparators
    CMCONbits.CM1=1;
    CMCONbits.CM2=1;
    OPTION_REG = 0b00101000;
 }


void interrupt isr()
{
  if(( TMR1IE==1)&& (TMR1IF==1))
   {
      PointOneSec++;
      TMR1H = 0x0B; //  3035      for 0.1 sec
      TMR1L = 0xDB
      if(PointOneSec>9)
       {
          TMR1IE=0;
          TMR1ON=0;
         fg_freq_count=1;
       }
    TMR1IF=0;
    }
}

void TIMER1_INIT()
 {
  T1CONbits.T1CKPS0=1;//8 prescale
  T1CONbits.T1CKPS1=1;
  T1CONbits.T1OSCEN=0;//
  T1CONbits.TMR1CS =0;
   T1CONbits.T1SYNC =0;
  TMR1H = 0x0B; //  3035     was    0xFE;//64910 in decimal for 1ms
  TMR1L = 0xDB; //                 was   0xC8;
  TMR1IF=0;
  GIE=1;
  PEIE=1;
  TMR1IE=1;
  TMR1ON=1;
 PointOneSec=0;
 }



void main()
 {    
   Counts=0;
    fg_freq_count=0;
    TIMER0_INIT();
    TIMER1_INIT();
    TMR0=0;
    while(1)
     {
        if(fg_freq_count==1)   // one sec elapsed 
         {
           Counts=TMR0;
          LCD_CMD(0x01);
          CONV_INT2CHAR(Counts);     // you get the nb of count per sec ..so in Hz
          fg_freq_count=1;
          PointOneSec=0;
           TMR1H = 0x0B; //  3035      for 0.1 sec
          TMR1L = 0xDB
           TMR1IE=1;
           TMR1ON=1;
            TMR0=0;
         }
     }
 }
 
Last edited:

This is the Hi-Tech PICC project I made as you are using Hi-Tech PICC Compiler. The attached .rar file includes Proteus 8.2 SP2 format file and simulation screenshot. It is working in Proteus but not completely working in hardware. In hardware LCD display is working fine but it is not displaying frequency value on 2nd line of LCD.

I have used EasyPIC v7 development board for testing.
 

Attachments

  • Mains Frequency Counter PIC16F877A 10 MHz.rar
    285.3 KB · Views: 69

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top