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] Problem with frequency counter based on PIC16f877a

Status
Not open for further replies.

RAJPUT VIDISHA

Member level 1
Joined
Jul 14, 2015
Messages
37
Helped
0
Reputation
0
Reaction score
1
Trophy points
6
Activity points
344
hi...
guys really need help in making frequency counter with pic16f877a...i want to display frequency via UART but its not working ...this is my code
Code:
/*
 * SIMPLE SERIAL FREQUENCY METER
 *
 * this program writes one time per second to the RS232 communication line,
 * the frequency of the input signal on the RB0 pin
 *
 * PIC16F877A
 * 4 Mhz crystal, XT clock
 *
 * PORTB.0, in : counter input
 * PORTc.6, out : RS232 tx
 * PORTc.7, in : RS232 rx

 */

sbit BUZZER at RC1_bit;                 // Buzzer connections
sbit BUZZER_Direction at TRISC1_bit;



/*
 * RAM variables
 */
unsigned long   cntr ;          // number of RB0 transition
unsigned int    ovrflw ;        // number of timer0 overflows
unsigned char   str[10] ;       // display result string

/*
 * constant strings
 */
const unsigned char welcome[] = "\r\rRS232 Frequency Meter Ready" ;
const unsigned char unit[] = " Hz\r" ;

/*
 * write the s ram string to RS232
 */
void    Comm_Write(unsigned char *s)
        {
        while(*s)
                {
                Soft_Uart_Write(*s) ;
                s++ ;
                }
        }

/*
 * write the s constant string to RS232
 */
void    Comm_WriteConst(const unsigned char *s)
        {
        while(*s)
                {
                Soft_Uart_Write(*s) ;
                s++ ;
                }
        }

/*
 * convert the cnrt long value to string
 */
void    Long2str(void)
        {
        unsigned char   i, j ;

        if(cntr == 0)
                {
                str[0] = '0' ;
                str[1] = 0 ;
                }
        else
                {
                str[0] = 0 ;
                i = 0 ;
                while(cntr > 0)
                        {
                        for(j = i + 1 ; j > 0 ; j--)
                                {
                                str[j] = str[j - 1] ;
                                }
                        str[0] = cntr % 10 ;
                        str[0] += '0' ;
                        i++ ;
                        cntr /= 10 ;
                        }
                }
        }

/*
 * interrupt routine called 4000000/256 times by seconds :
 * the timer TMR0 is increased each 4 clock cycles (quartz frequency is 16 Mhz),
 * and overflows when reseting from 255 to 0,
 * calling the interrupt procedure with bit T0IF set
 *
 * also called on each RBO transition, with bit INTF set
 */
void    interrupt(void)
        {
        if(INTCON.INTF)
                {
                /*
                 * RB0 interrupt
                 */
                cntr++ ;                // inc. transition counter
                INTCON.INTF = 0 ;       // clear interrupt flag to enable next call
                }
        else if(INTCON.T0IF)
                {
                /*
                 * TIMER 0 overflow
                 */
                ovrflw++ ;              // inc. overflow counter
                INTCON.T0IF = 0 ;       // clear interrupt flag to enable next call on overflow
                }
        }

/*
 * entry point
 */
main()
 {

        Sound_Init(&PORTC,1);
        Sound_Play(1000,100);



        Soft_Uart_Init(PORTC, 7, 6, 9600, 0) ;    // RS232 on PORTC, bits 7 & 6, 38400 bauds
        Comm_WriteConst(welcome) ;              // write welcome message

        TRISB0_bit = 1 ;                  // RB0 interrupt pin as input

   // T0CON = 0b11001000;

        OPTION_REG = 0b01001000 ;       // no prescaler

        /*
         * main loop
         */
        for(;;)
                {
                cntr = 0 ;              // clear counters
                ovrflw = 0 ;

                INTCON = 0b10000110;           // T0IF, INTF and GIE enabled

                while(ovrflw < 3906) ;         // wait 1 second : 15626 = 16 000 000 / 4 / 256, rounded up

                INTCON.GIE = 0 ;                // stop all interrupts

                Long2Str() ;                    // convert counter to string
                Comm_Write(str) ;               // write string
                Comm_WriteConst(unit) ;         // write unit
                }
  }

//
 
Last edited by a moderator:

hello



What is your problem ?
Does "Welcome" message appears on your terminal ?
or probelme on interrupts..

what about T0IE_bit and TMR0IE_bit ?
 

HI...
i am getting 0x00 0x00 thats it...sorry i am new to pic device..plz help me
 

why do you not use

Code:
unsigned long jj = 3700000;
char txt[11];
...
LongWordToStr(jj, txt);
// txt is "   3700000" (three blanks here)

and UART Hardware ...

Try firts to display a long value on terminal..
and further treat Frequency probleme..
 

Hi ,
If you are not getting welcome msg , then there is a error related to baudrate setting. where are you selecting fuse bits,are you enabling external clock by a separate programmer exclusively ??.
 

Hi...
my baud rate is 9600 and i'm selecting it in UART terminal and not enabling external clock...
 

Hi

I am attaching two versions of Frequency Counters. See if this solves your problem. One is PIC16F877A version which runs at 20 MHz clock and another is PIC18F46K22 version which runs at 4 MHz.

I have only tested in Proteus. Please test it in hardware.
 

Attachments

  • FC 16F877A RS232.rar
    90.8 KB · Views: 75
  • PIC18F46K22 Based Frequency Counter.rar
    157.9 KB · Views: 69
  • 500 KHz.png
    500 KHz.png
    33.8 KB · Views: 102
  • Frequency Counter 500 MHz.png
    Frequency Counter 500 MHz.png
    45.4 KB · Views: 96

Hello RAJPUT VIDISHA,

Try with this code (and also take in count precedent advices):

Code:
/*
 * RAM variables
 */
[COLOR="#FF0000"]volatile[/COLOR] unsigned long   cntr ;          // number of RB0 transition
[COLOR="#FF0000"]volatile[/COLOR] unsigned int    ovrflw ;        // number of timer0 overflows

Regards.
JY
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top