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

Timer1 external clock problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Timer1 external clock problem
PostPosted: Mon Jan 11, 2010 2:26 pm     Reply with quote

I couldn't use timer1 external clock. I want to use 32.768Khz crystal for my counter. I have read a sample code for this operation:
Code:

#include <16F877.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=4000000)       
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

int a;

void main()
{
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); //32768Hz external Timer1 oscillator

while(1)
 {
  set_timer1(0);
  delay_ms(1000);
  a=get_timer1();
  printf("%d",a);
  delay_ms(1000);
 }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 11, 2010 2:52 pm     Reply with quote

Quote:
int a;

a=get_timer1();
printf("%d",a);


Timer1 is 16 bits wide. You should declare 'a' as 'int16'.

To display a 16-bit unsigned value, you should use "%lu" in the
printf statement. Also, you should add "\n\r", so it goes to a newline
after displaying the timer value.

Example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=29957&start=4

If that doesn't help, then post a description of the crystal circuit
that is connected to the Timer1 oscillator pins. These are different
pins than the normal main oscillator pins.

Also post your compiler version.
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 8:57 am     Reply with quote

I have changed my code but i couldn't succeed and my schematic is;

My code is;
Code:
#include <16F877.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
unsigned int16 a;
void main()
{
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); //32768Hz external Timer1 oscillator   
while(1)
{
set_timer1(0);
delay_ms(500);
a=get_timer1();
printf("%lu",a);
delay_ms(2000);
}
}

I think the problem can be related with my simulation program,Proteus.Or is there any different reason?
mkuang



Joined: 14 Dec 2007
Posts: 257

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

PostPosted: Tue Jan 12, 2010 9:09 am     Reply with quote

Instead of this:

Code:

setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);


Try this:

Code:

setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1| 0x8);


This manually enables bit 7 (T1OSCEN) of the T1CON register instead of using the T1_CLK_OUT (which I am not sure whether it actually enables the oscillator).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 12:01 pm     Reply with quote

Quote:
I have changed my code but I couldn't succeed.

Post your compiler version.
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 1:05 pm     Reply with quote

My compiler verison is 4.068.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 1:15 pm     Reply with quote

I installed vs. 4.068 and compiled the program shown in your post
with the schematic. I programmed it into a 16F877 on my PicDem2-Plus
board. It works. I did add a "\n\r" in the printf statement, so it would
go to a new line after each value is displayed in the TeraTerm window
on my PC.

Here's the output. It's correct. You have a delay of 500 ms after you
clear the timer. During that time, it will count up to about 16384.
Then you read the timer every 2 seconds. During that time, it will do
65536 counts, which puts it right back at about 16384 again. That's
what we see below, so it's working.
Code:

16385
16384
16384
16385
16384
16384
16385
16384
16385
16385
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 1:51 pm     Reply with quote

Ok thanks for your help I will try this code in real hardware.

Thanks for considering...
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jan 12, 2010 5:03 pm     Reply with quote

Just a thought.... Is this your first program in Proteus?

What I remember from playing with Proteus is that it doesn't look at the connected crystal, this part of the PIC processor is not simulated. What you have to do is right-click on the PIC processor and enter the desired clock frequency in one of the property fields.
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Wed Jan 13, 2010 12:45 am     Reply with quote

By clicking right on the PIC processor you can change only the processor clock frequency. You can't change the external clock which is connected to RC0- RC1 oscillator frequency. I have already connected 4Mhz crystal to OSC1-OSC2 pins and this determines the processor clock frequency. The main problem in Proteus adding second external oscillator for Timer1.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jan 13, 2010 2:07 pm     Reply with quote

I just checked, for the main oscillator you have to configure the clock using the properties dialog (press ctrl-E when mouse is over the processor), at least in my ISIS version 6.9.

I'm no Proteus expert, so I don't know about the second oscillator support.
princez



Joined: 01 Aug 2011
Posts: 3

View user's profile Send private message

external oscillator problem
PostPosted: Mon Aug 01, 2011 1:31 pm     Reply with quote

hay, I am having an external oscillator problem on Proteus. I am using Isis version 7, but I am unable to simulate my code on Proteus with an external oscillator. The hex file I generated was on CCS.
Here's my code,

Code:

#include<18f4550.h>
#fuses HS,NOWDT
#use delay (oscillator=20M)
#use rs232 (baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7,stream=com1,bits=8)

void main(void)
{
int i;

while(true)
  {
   fprintf(com1,"hello");
   putc(13);
   i=getenv("CLOCK");
   fprintf(com1,"%d",i);
   delay_ms(1000);
  }

}

Can anyone please tell me if I have written a right code ?? Or there's anything missing in it. Also on actual hardware, is there any way to determine what's the baud rate of my controller or its clock speed?
temtronic



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

View user's profile Send private message

PostPosted: Mon Aug 01, 2011 2:31 pm     Reply with quote

Last question first..you should be able to read the SFR associated with baud rate and figure out the 'speed'.....
First question last... Get RID of Proteus ! As you've just found out it's full of bugs, quirks and bad DRCs....in no way will it ever 'simulate' the real world !
princez



Joined: 01 Aug 2011
Posts: 3

View user's profile Send private message

PostPosted: Wed Aug 03, 2011 9:30 am     Reply with quote

temtronic wrote:
..you should be able to read the SFR associated with baud rate and figure out the 'speed'.....


how to do this ?? will it tell the circuit baud rate ? Confused
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Aug 05, 2011 9:53 am     Reply with quote

the best thing you can do is try with real circuits not this shaky simulator.

LOSE proteus - it is a great way to waste time
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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