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

Can't stop a countdown timer.

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







Can't stop a countdown timer.
PostPosted: Tue May 20, 2003 2:11 am     Reply with quote

I have a basic countdown clock using timer 1 and and external crystal.
How do I stop the timer at 0. I've tried all sorts and haven't had any luck.
Ideas??


#include <16f877.h>
#fuses hs,nowdt,noprotect,put,nowrt,nolvp
#use delay(clock=4000000)
#use RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)

#include <ccslcd2.c>

#define INTS_PER_SECOND 32768

long seconds=60;
long minutes=34;
short one_sec_fl;

#int_timer1
clock_isr() {
one_sec_fl = 1;
set_timer1(0x7fff);
}

main() {

lcd_init();
delay_ms(6);
setup_timer_1( T1_DIV_BY_1 | T1_EXTERNAL | T1_CLK_OUT );
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

printf(lcd_putc,"\f35:00");
lcd_gotoxy(1,2);
printf("\r\npress a key");
Printf(lcd_putc,"Press a key");
getc();

while(true)
{
if (one_sec_fl)
{
--seconds;
if (seconds==-1)
{
--minutes;
seconds=59;
}
one_sec_fl = 0;
lcd_gotoxy(1,1);
printf(lcd_putc,"\%02ld:\%02ld",minutes,seconds);
}
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514573
Tomi
Guest







Re: Can't stop a countdown timer.
PostPosted: Tue May 20, 2003 2:52 am     Reply with quote

<font face="Courier New" size=-1>"seconds" is unsigned by default so your
if (seconds==-1)
never becomes true. Test it for 0xFFFF (if it is a 16-bit long indeed) instead of -1.
To completely stop Timer1 you can use
disable_interrupts(INT_TIMER1);

:=I have a basic countdown clock using timer 1 and and external crystal.
:=How do I stop the timer at 0. I've tried all sorts and haven't had any luck.
:=Ideas??
:=
:=
:=#include <16f877.h>
:=#fuses hs,nowdt,noprotect,put,nowrt,nolvp
:=#use delay(clock=4000000)
:=#use RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)
:=
:=#include <ccslcd2.c>
:=
:=#define INTS_PER_SECOND 32768
:=
:=long seconds=60;
:=long minutes=34;
:=short one_sec_fl;
:=
:=#int_timer1
:=clock_isr() {
:= one_sec_fl = 1;
:= set_timer1(0x7fff);
:= }
:=
:=main() {
:=
:= lcd_init();
:= delay_ms(6);
:= setup_timer_1( T1_DIV_BY_1 | T1_EXTERNAL | T1_CLK_OUT );
:= enable_interrupts(INT_TIMER1);
:= enable_interrupts(GLOBAL);
:=
:= printf(lcd_putc,"\f35:00");
:= lcd_gotoxy(1,2);
:= printf("\r\npress a key");
:= Printf(lcd_putc,"Press a key");
:= getc();
:=
:=while(true)
:= {
:= if (one_sec_fl)
:= {
:= --seconds;
:= if (seconds==-1)
:= {
:= --minutes;
:= seconds=59;
:= }
:= one_sec_fl = 0;
:= lcd_gotoxy(1,1);
:= printf(lcd_putc,"\%02ld:\%02ld",minutes,seconds);
:= }
:= }
:=}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514577
Forest
Guest







Re: Can't stop a countdown timer.
PostPosted: Tue May 20, 2003 3:07 am     Reply with quote

The "=if (seconds==-1)" is only part there to increment minutes. It seems to work OK but I see what u mean.
Where would I insert the =disable_interrupts(INT_TIMER1);??
I've been trying heaps of diffent spots but can't get it right.
I just want the timer to count down and then stop, so that I can go on with other things.
cheers


:=<font face="Courier New" size=-1>"seconds" is unsigned by default so your
:=if (seconds==-1)
:=never becomes true. Test it for 0xFFFF (if it is a 16-bit long indeed) instead of -1.
:=To completely stop Timer1 you can use
:=disable_interrupts(INT_TIMER1);
:=
:=:=I have a basic countdown clock using timer 1 and and external crystal.
:=:=How do I stop the timer at 0. I've tried all sorts and haven't had any luck.
:=:=Ideas??
:=:=
:=:=
:=:=#include <16f877.h>
:=:=#fuses hs,nowdt,noprotect,put,nowrt,nolvp
:=:=#use delay(clock=4000000)
:=:=#use RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)
:=:=
:=:=#include <ccslcd2.c>
:=:=
:=:=#define INTS_PER_SECOND 32768
:=:=
:=:=long seconds=60;
:=:=long minutes=34;
:=:=short one_sec_fl;
:=:=
:=:=#int_timer1
:=:=clock_isr() {
:=:= one_sec_fl = 1;
:=:= set_timer1(0x7fff);
:=:= }
:=:=
:=:=main() {
:=:=
:=:= lcd_init();
:=:= delay_ms(6);
:=:= setup_timer_1( T1_DIV_BY_1 | T1_EXTERNAL | T1_CLK_OUT );
:=:= enable_interrupts(INT_TIMER1);
:=:= enable_interrupts(GLOBAL);
:=:=
:=:= printf(lcd_putc,"\f35:00");
:=:= lcd_gotoxy(1,2);
:=:= printf("\r\npress a key");
:=:= Printf(lcd_putc,"Press a key");
:=:= getc();
:=:=
:=:=while(true)
:=:= {
:=:= if (one_sec_fl)
:=:= {
:=:= --seconds;
:=:= if (seconds==-1)
:=:= {
:=:= --minutes;
:=:= seconds=59;
:=:= }
:=:= one_sec_fl = 0;
:=:= lcd_gotoxy(1,1);
:=:= printf(lcd_putc,"\%02ld:\%02ld",minutes,seconds);
:=:= }
:=:= }
:=:=}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514579
Tomi
Guest







Re: Can't stop a countdown timer.
PostPosted: Tue May 20, 2003 3:35 am     Reply with quote

:=Where would I insert the =disable_interrupts(INT_TIMER1);??

It is depends on what you want Smile
If it is a downcounter then you can test for both minutes and seconds are zero:
if (!(seconds--)) { // test for zero BEFORE decrement
seconds = 59;
if (!(minutes--)) { // if it is true the minutes=0 before decrement and the second is just after zero
disable_interrupts(INT_TIMER1); // disable further ITs
elapsed = TRUE; // set a flag to indicate the end of
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514580
forest
Guest







Re: Can't stop a countdown timer.
PostPosted: Tue May 20, 2003 11:45 pm     Reply with quote

Thanks Tomi - got it working.


:=:=Where would I insert the =disable_interrupts(INT_TIMER1);??
:=
:=It is depends on what you want <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0">
:=If it is a downcounter then you can test for both minutes and seconds are zero:
:=if (!(seconds--)) { // test for zero BEFORE decrement
:= seconds = 59;
:= if (!(minutes--)) { // if it is true the minutes=0 before decrement and the second is just after zero
:= disable_interrupts(INT_TIMER1); // disable further ITs
:= elapsed = TRUE; // set a flag to indicate the end of
:= }
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514615
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