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

Using Timer1 with a 32,768khz cristal

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







Using Timer1 with a 32,768khz cristal
PostPosted: Fri Jun 21, 2002 10:12 pm     Reply with quote

Hello everyone,

I am trying to use this code, connecting a 32,768khz cristal to T1oso and T1osi, but it doesnŽt work.
The idea is to put the 16F627 to sleep, and wake it up after 2 minutes (i.e. when Timer1 overflows). And on, and on....
Can anyone help me to fix the bug, cause I can not find it ?

#include <16F627.H>
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,NOLVP,MCLR
#use delay(clock=4000000)

#int_timer1
void isr()
{
output_high(PIN_B1);
}


void main()
{
delay_ms(4000);
setup_timer_1( T1_EXTERNAL_SYNC | T1_DIV_BY_1 );
set_timer1(0xffff);
enable_interrupts( INT_TIMER1 );
enable_interrupts(GLOBAL);

sleep();
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 5124
johnpcunningham
Guest







Re: Using Timer1 with a 32,768khz cristal
PostPosted: Sat Jun 22, 2002 1:02 am     Reply with quote

:=Hello everyone,
:=
:= I am trying to use this code, connecting a 32,768khz cristal to T1oso and T1osi, but it doesnŽt work.
:= The idea is to put the 16F627 to sleep, and wake it up after 2 minutes (i.e. when Timer1 overflows). And on, and on....
:= Can anyone help me to fix the bug, cause I can not find it ?
:=
:=#include <16F627.H>
:=#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,NOLVP,MCLR
:=#use delay(clock=4000000)
:=
:=#int_timer1
:=void isr()
:={
:= output_high(PIN_B1);
:=}
:=
:=
:=void main()
:={
:= delay_ms(4000);
:= setup_timer_1( T1_EXTERNAL_SYNC | T1_DIV_BY_1 );
:= set_timer1(0xffff);
:= enable_interrupts( INT_TIMER1 );
:= enable_interrupts(GLOBAL);
:=
:= sleep();
:=}
********************************************************
Since your using an external clock, it looks like the setup_timer_1() command is not turning on the external oscillator (bit 3 of T1CON register) when I compiled your code.

Also, are you setting up PORTB B1 to a default value when the code first runs. I did not see it in the code. I think default is HIGH Z so you may see erratic behavior?

You need to put a loop around your sleep command so that it repeats, at least for your test.

I haven't tested the code below but it should get you going in the right direction. If all works as planned, port pin B1 should come on in the interrupt, the delay(200ms) will run, and then B1 will go low again and the go back to sleep while waiting for the next interrupt. Make sure all of th other interupt flags are cleared (EEPROM, ANALOG, etc) or the SLEEP command will be treated as a NOP and will never be executed if there is another interrupt pending.

The TIMER1 should time out every 2^16/32768 = 2 seconds so the 200ms delay should not cause any problems.

/////////////////
#byte T1CON = 0x0010
#define b_T1CKPS1 5
#define b_T1CKPS0 4
#define b_T1OSCEN 3
#define b_T1SYNC 2
#define b_TMR1CS 1
#define b_TMR1ON 0

#use STANDARD_IO (B)
//////////////////

#int_timer1
void isr()
{
output_high(PIN_B1);
}


void main()
{

//default on startup
output_low(PIN_B1);

delay_ms(4000);

//1:1 prescale, OSC on, SYNC on, TIMER1 not enabled (yet)
T1CON = 0x0A;

//clear the 16 bit TIMER1 count register
set_timer1(0x0000);

enable_interrupts( INT_TIMER1 );
enable_interrupts(GLOBAL);

//start the TIMER1 counting
bit_set(T1CON, b_TMR1ON);

while(1)
{
sleep();
delay_ms(200);

//clear the pin after interrupt occurs
output_low(PIN_B1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 5126
Gabriel Caffese
Guest







Re: Using Timer1 with a 32,768khz cristal
PostPosted: Sat Jun 22, 2002 7:56 am     Reply with quote

Thanks, John, IŽll try your code today and IŽll tell you.

Bye.-

:=:=Hello everyone,
:=:=
:=:= I am trying to use this code, connecting a 32,768khz cristal to T1oso and T1osi, but it doesnŽt work.
:=:= The idea is to put the 16F627 to sleep, and wake it up after 2 minutes (i.e. when Timer1 overflows). And on, and on....
:=:= Can anyone help me to fix the bug, cause I can not find it ?
:=:=
:=:=#include <16F627.H>
:=:=#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,NOLVP,MCLR
:=:=#use delay(clock=4000000)
:=:=
:=:=#int_timer1
:=:=void isr()
:=:={
:=:= output_high(PIN_B1);
:=:=}
:=:=
:=:=
:=:=void main()
:=:={
:=:= delay_ms(4000);
:=:= setup_timer_1( T1_EXTERNAL_SYNC | T1_DIV_BY_1 );
:=:= set_timer1(0xffff);
:=:= enable_interrupts( INT_TIMER1 );
:=:= enable_interrupts(GLOBAL);
:=:=
:=:= sleep();
:=:=}
:=********************************************************
:=Since your using an external clock, it looks like the setup_timer_1() command is not turning on the external oscillator (bit 3 of T1CON register) when I compiled your code.
:=
:=Also, are you setting up PORTB B1 to a default value when the code first runs. I did not see it in the code. I think default is HIGH Z so you may see erratic behavior?
:=
:=You need to put a loop around your sleep command so that it repeats, at least for your test.
:=
:=I haven't tested the code below but it should get you going in the right direction. If all works as planned, port pin B1 should come on in the interrupt, the delay(200ms) will run, and then B1 will go low again and the go back to sleep while waiting for the next interrupt. Make sure all of th other interupt flags are cleared (EEPROM, ANALOG, etc) or the SLEEP command will be treated as a NOP and will never be executed if there is another interrupt pending.
:=
:=The TIMER1 should time out every 2^16/32768 = 2 seconds so the 200ms delay should not cause any problems.
:=
:=/////////////////
:=#byte T1CON = 0x0010
:=#define b_T1CKPS1 5
:=#define b_T1CKPS0 4
:=#define b_T1OSCEN 3
:=#define b_T1SYNC 2
:=#define b_TMR1CS 1
:=#define b_TMR1ON 0
:=
:= #use STANDARD_IO (B)
:=//////////////////
:=
:=#int_timer1
:=void isr()
:={
:= output_high(PIN_B1);
:=}
:=
:=
:=void main()
:={
:=
:=//default on startup
:= output_low(PIN_B1);
:=
:=delay_ms(4000);
:=
:=//1:1 prescale, OSC on, SYNC on, TIMER1 not enabled (yet)
:=T1CON = 0x0A;
:=
:=//clear the 16 bit TIMER1 count register
:=set_timer1(0x0000);
:=
:=enable_interrupts( INT_TIMER1 );
:=enable_interrupts(GLOBAL);
:=
:=//start the TIMER1 counting
:=bit_set(T1CON, b_TMR1ON);
:=
:=while(1)
:={
:=sleep();
:=delay_ms(200);
:=
:=//clear the pin after interrupt occurs
:=output_low(PIN_B1);
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 5128
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