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

Problem with Timer1 [SOLVED]

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



Joined: 10 Mar 2020
Posts: 6

View user's profile Send private message

Problem with Timer1 [SOLVED]
PostPosted: Wed Jul 01, 2020 2:11 pm     Reply with quote

Hello everyone, i tried to use timer1 module of pic, but isn't work.

I tried to make a blink led with 1Hz of frequency:

t = (4 * 8 * (65536-65411)) / 4.10^6 = 1ms

Code:

#include <16f676.h>

#FUSES NOMCLR
#FUSES NOWDT
#FUSES NOPROTECT
#FUSES NOBROWNOUT
#FUSES NOCPD
#FUSES XT

#USE delay(clock=4MHz)

int ticks =0;

#INT_TIMER1
void TIMER1_tick(){
set_timer1(65411);
ticks ++;
 if (ticks >= 1000){
 OUTPUT_toggle(PIN_A0);
 ticks = 0;
 }
//clear_interrupt(INT_TIMER1);
}

void main(){
set_tris_A(0);
set_tris_C(0);
output_a(0);
set_timer1(65411);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
 while(true){
 }
}

Hardware:
Led => Pin A0
Crystal 4MHz and 33pF caps => OSC1-2

1) Would it be a problem related to fuses?
2) "clear_interrupt()" is necessary on timer1_isr? (same as "T1IF = 0"?)

Thanks!


Last edited by lcs on Wed Jul 01, 2020 2:33 pm; edited 1 time in total
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 01, 2020 2:21 pm     Reply with quote

One issue I can immediately see is that you are using int for your ticks, which is an 8 bit value. That only will go up to 255 and then roll over to zero. Therefore you never reach 1000. You need to use int16 which will allow you to reach 65535 (unsigned).

I suggest looking at the type definitions in stdint.h which makes your types very explicit and easy to keep track of.
lcs



Joined: 10 Mar 2020
Posts: 6

View user's profile Send private message

PostPosted: Wed Jul 01, 2020 2:33 pm     Reply with quote

dluu13 wrote:
One issue I can immediately see is that you are using int for your ticks, which is an 8 bit value. That only will go up to 255 and then roll over to zero. Therefore you never reach 1000. You need to use int16 which will allow you to reach 65535 (unsigned).

I suggest looking at the type definitions in stdint.h which makes your types very explicit and easy to keep track of.


AArrrgh
I forgot that "int" is int8 due to the use of other compilers.
I focused on other details and forgot the basics.
Solved. Thanks!
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