View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
How to use #INT_EXT pin as pulse counter |
Posted: Fri May 25, 2018 4:03 am |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Fri May 25, 2018 5:14 am |
|
|
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
|
|
Posted: Fri May 25, 2018 5:22 am |
|
|
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: 19548
|
|
Posted: Fri May 25, 2018 6:19 am |
|
|
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
|
|
Posted: Sat May 26, 2018 12:30 am |
|
|
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: 19548
|
|
Posted: Sat May 26, 2018 12:49 am |
|
|
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.
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
|
|
Posted: Mon May 28, 2018 4:22 am |
|
|
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.
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"
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
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Mon May 28, 2018 6:21 am |
|
|
Well done. |
|
|
|