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 CCS Technical Support

Setup Timer1 on PIC16F687

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



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

Setup Timer1 on PIC16F687
PostPosted: Mon Dec 08, 2008 7:55 pm     Reply with quote

Hi,
I want to have a increment of 1 from 0 to 9 every 500ms and display the number on a 7_segment_led, but increment is never happened. The below is my code could you help me to find out what's wrong with it?

Thanks!

Code:

#include <16F687.h>

#use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN

volatile unsigned int8 i=0;

#INT_TIMER1
void TIMER1_ISR()
{
    i++;
    if(i > 9)
      i = 0;
}

void main()
{

  setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_16);   //Setup SPI interface, mode 0


  set_timer1(0x1F00);      //1 interrupt every 500ms
  setup_timer_1(T1_EXTERNAL | T1_CLK_OUT | T1_DIV_BY_8);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(global);
 
  while(1)
  {
    Display_i_to_led();
  }
 
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 8:40 pm     Reply with quote

Quote:
setup_timer_1(T1_EXTERNAL | T1_CLK_OUT | T1_DIV_BY_8);

Use T1_INTERNAL instead of the parameters in bold.
boulder



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:01 pm     Reply with quote

Yes, it works with the internal clock, 8MHz. Why it does not work with external clock? In my PCB, this chip is connected to an external clock, 3866400 Hz, on purpose for UART baudrate 115200. Are there any methods to make timer1 work with this external clock?

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:16 pm     Reply with quote

Quote:
#use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN

You are not using the internal oscillator at 8 MHz. You are using the
crystal oscillator (XT) at 3.6864 MHz.
boulder



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:22 pm     Reply with quote

Yes, I am using internal clock. I already changed clocked after your first reply from
Code:

#use delay(clock=3686400, crystal)

to
Code:

#use delay(clock=8000000)


I don't understand why external clock does not work. Do you have any explanation for it?

Thanks!!!
boulder



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:40 pm     Reply with quote

I realize that if I use external clock
Code:

#use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN

But Timer1 setup use INTERNAL
Code:

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);

it works too. Why?

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:54 pm     Reply with quote

T1_INTERNAL uses the crystal frequency, divided by 4.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 9:57 pm     Reply with quote

You can't run the Timer1 oscillator at 3.6864 MHz. Look in the 16F687
data sheet, on page 246 (page 248 in the Acrobat reader):
http://ww1.microchip.com/downloads/en/DeviceDoc/41262E.pdf
It shows that the typical frequency for the Timer1 oscillator is only
32.768 KHz. An older revision of the data sheet lists 200 KHz as the
maximum allowable frequency. You can't run it with a 3.6864 MHz
crystal.
boulder



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 10:30 pm     Reply with quote

Is it Okay for me to use both 3686400Hz and T1_INTERNAL and in my project? Because I need this external crystal for baudrate 115200, also it works fine for Timer1 with T1_INTERNAL.

Thanks!!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 08, 2008 10:34 pm     Reply with quote

That is the standard way of doing it.
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