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

Timer 1 as counter - PIC 16F887

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



Joined: 14 Aug 2019
Posts: 9

View user's profile Send private message

Timer 1 as counter - PIC 16F887
PostPosted: Wed Aug 14, 2019 9:43 am     Reply with quote

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: 19219

View user's profile Send private message

PostPosted: Wed Aug 14, 2019 10:02 am     Reply with quote

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: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 14, 2019 3:17 pm     Reply with quote

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

View user's profile Send private message

TIMER 1 AS COUNTER - PIC 16F887
PostPosted: Thu Aug 15, 2019 12:42 am     Reply with quote

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: 19219

View user's profile Send private message

PostPosted: Thu Aug 15, 2019 5:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 15, 2019 8:50 am     Reply with quote

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: 19219

View user's profile Send private message

PostPosted: Thu Aug 15, 2019 10:02 am     Reply with quote

As posted, your code just adds one al the time. Once k is 1 it stays 1...
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