View previous topic :: View next topic |
Author |
Message |
phamngockienbp
Joined: 14 Aug 2019 Posts: 9
|
Timer 1 as counter - PIC 16F887 |
Posted: Wed Aug 14, 2019 9:43 am |
|
|
Hi everyone.
I would like to use timer 1 as counter mode on pic 16F887.
My code is as below:
Code: |
Void main()
{
SETUP_TIMER_1(T1_EXTERNAL| T1_DIV_BY_2);
SET_TIMER1(0);
lcd_init();
while(true)
{
count=GET_TIMER1();
lcd_gotoxy(1,1);
printf (lcd_putc,"Count = %4lu",count);
}
|
When I try to simulate on proteus, Pic didn't count the first pulse. The second pulse will be counted as 1 and the third pulse will be 2...
Please kindly advise me if there is anything missing on my code. Thank you in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Wed Aug 14, 2019 10:02 am |
|
|
You have DIV_BY_2, so the counter will advance on every second rising
edge (the timer doesn't actually count 'pulses', but rising edges).
The third pulse should also be '2', not '3'.
It should go:
EDGE COUNT
1 0
2 1
3 1
4 2
5 2
6 3.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Aug 14, 2019 3:17 pm |
|
|
and...
with the understanding that Proteus CANNOT be trusted to simulate a PIC 100% !
.. While some operations may work, several don't and even more are, well, random events that kinda look like it might work or YOUR code is faulty, however it's really Proteus that is busted ! |
|
|
phamngockienbp
Joined: 14 Aug 2019 Posts: 9
|
TIMER 1 AS COUNTER - PIC 16F887 |
Posted: Thu Aug 15, 2019 12:42 am |
|
|
Hi
Thank you for your help.
I had adjusted my code to:
SETUP_TIMER_1(T1_EXTERNAL| T1_DIV_BY_1).
and then testing this code with the real pcb.
T1CKI pin had been connected with input square signal for testing.
What showing on the LCD is as below:
Signal_______count value
0 Volt_________0
5 Volt_________0 ( At the first rising edge, the read value is still 0 )
0 Volt_________0
5 Volt_________1 ( At the second rising edge, pic will count as 1 )
0 Volt_________1
5 Volt_________2
0 Volt_________2
5 Volt_________3
0 Volt_________3
5 Volt_________4
0 Volt_________4
......._________ n
When I use Timer 0 as counter, pic worked perfectly.
But when I use Timer 1 as counter, the number showing on the LCD is always less than the actual value 1 unit.
Please kindly help me once again. Thank you in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Thu Aug 15, 2019 5:45 am |
|
|
Now, seriously, I've just stuck this into a chip:
Code: |
#include <16F887.h>
#device ADC=10
#use delay(crystal=20000000)
//Setup serial for output
#use rs232 (UART1, Baud=9600, ERRORS)
void main()
{
int16 count=0;
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1);
set_timer1(0);
while(TRUE)
{
while (get_timer1()==count)
;
count=get_timer1();
printf("Count=%4lu\n\r",count);
}
}
|
Stuck on the bench with a serial connection, and fired a 10 pulse burst into
it with a programmable pulse generator at 1KHz.
It printed three times:
Count= 1
Count= 9
Count= 10
Exactly what it should. It started printing immediately it saw the first
edge, then took just under 9mSec to load the characters to print.
Came back and saw the count had gone up to 9, started printing this
then returned and there had been one more count.
One thing that might be catching you out, is _signal levels_. The T1CKI
pin is a Schmitt trigger input. Has to go up to 4v, on a 5v PIC to be seen
as 'high'. However T0CKI is also Schmitt so the same would apply to this. |
|
|
phamngockienbp
Joined: 14 Aug 2019 Posts: 9
|
|
Posted: Thu Aug 15, 2019 8:50 am |
|
|
Hi
Thank you very much for your kindly help.
I had tried with your code but there is nothing change.
I had used 1 more countable value " K " and change the previous code a little bit.
if(input(pin_c0)==1) k=1;
count = get_timer1()+k ;
And then the number showing on the Lcd is now exactly the actual value.
Thank you! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Thu Aug 15, 2019 10:02 am |
|
|
As posted, your code just adds one al the time. Once k is 1 it stays 1... |
|
|
|