CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to use #INT_EXT pin as pulse counter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

How to use #INT_EXT pin as pulse counter
PostPosted: Fri May 25, 2018 4:03 am     Reply with quote

Hi,

Please advise me how to use BIN B0 / INT pin to count external pulses
of 2.097152 MHz and make interrupt every time when timer 1 overflows ?

Is it possible to do so ?

(My purpose is count those interrupts to make one second pulse for my digital lock).

best regards

arto
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri May 25, 2018 5:14 am     Reply with quote

You should tell us which PIC you're using. How the xtal gets 'counted' depends on the PIC.

Jay
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Fri May 25, 2018 5:22 am     Reply with quote

temtronic wrote:
You should tell us which PIC you're using. How the xtal gets 'counted' depends on the PIC.

Jay


Thank for you answer Jay,
I'm using PIC18F4525 chip and it's clock frequency is 16 MHz.
2.097152 Pulses come from 74HCT ic ... it forms oscillator and it's power supply voltage is 5 V as PIC ic too have 5 V power supply.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri May 25, 2018 6:19 am     Reply with quote

Don't.

Connect the signal to the T0CKI pin instead.

The PIC can't cope with an interrupt at 2.097152MHz on B0. Not even close to being able to handle this (by several orders of magnitude). However Timer0, is quite happy to count a clock at this frequency.
Then INT_TIMER0 will trigger at the overflow. Use a rolling counter in this to give an accurate 1/second. So:
Code:

#INT_TIMER0
void count_from_clock(void)
{
    static int8 count=0;
    count++; //how many pulses received
    if (count>=8)
    {
        output_toggle(PIN_xx); //change the pin you want to signal with
        count-=8;
    }
}


This will change the pin twice per second (so give 1Hz).
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Sat May 26, 2018 12:30 am     Reply with quote

Ttelmah wrote:
Don't.

Connect the signal to the T0CKI pin instead.

The PIC can't cope with an interrupt at 2.097152MHz on B0. Not even close to being able to handle this (by several orders of magnitude). However Timer0, is quite happy to count a clock at this frequency.
Then INT_TIMER0 will trigger at the overflow. Use a rolling counter in this to give an accurate 1/second. So:
Code:

#INT_TIMER0
void count_from_clock(void)
{
    static int8 count=0;
    count++; //how many pulses received
    if (count>=8)
    {
        output_toggle(PIN_xx); //change the pin you want to signal with
        count-=8;
    }
}


This will change the pin twice per second (so give 1Hz).



Thank's for your good advice...

If I use timer0 for this is it possible to use wdt same time with pic18f4525 ?

maybe it is not good to null count every overflow : static int8 count=0;

all the best

-arto-
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat May 26, 2018 12:49 am     Reply with quote

It doesn't null every overflow.
A _static_ variable is initialised once at the start of execution. So this keeps the count merrily for you. Smile

Read the data sheet. The watchdog and the timer have no relation at all on a PIC18. On a PIC16, they share the prescaler, but not on the 18 chips.

You can use any of the timers who's pin connection suits you. So T0CKI, or T13CKI (though this is shared between T1 and T3, so might be a waste).
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Mon May 28, 2018 4:22 am     Reply with quote

Ttelmah wrote:
It doesn't null every overflow.
A _static_ variable is initialised once at the start of execution. So this keeps the count merrily for you. Smile

Read the data sheet. The watchdog and the timer have no relation at all on a PIC18. On a PIC16, they share the prescaler, but not on the 18 chips.

You can use any of the timers who's pin connection suits you. So T0CKI, or T13CKI (though this is shared between T1 and T3, so might be a waste).


Hello "Ttelmah" Smile

Lot of thanks for your good advice again !

I make my own version counting pulses from A4 pin ...

Thanks to advise me to use pin A4 instead of pin B.1

This my version ... it works fine ... I checked it with my oscilloscope and frequency counter and it works fine:
Code:

 setup_timer_0 (RTCC_EXT_L_TO_H | RTCC_DIV_32) ;   
 enable_interrupts(INT_TIMER0);
 enable_interrupts(GLOBAL);
.
.
.
.
#INT_TIMER0
void count_from_clock(void)
{
   
    //incoming pulse 2097152 Hz
   
    //REMOVE THIS LATER:
    output_toggle(PIN_A2); //change the pin you want to signal with
   


all the best Smile
-arto-
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 28, 2018 6:21 am     Reply with quote

Well done. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group