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

timer1 setup!!

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



Joined: 28 Jan 2006
Posts: 8
Location: Brighton

View user's profile Send private message

timer1 setup!!
PostPosted: Mon Jan 30, 2006 9:25 am     Reply with quote

Hi everyone this is my first post in the forum so please be patient... Smile
I have made this simple code to operate timer1 with external clock and ISR activated. The program seems to work fine however when I count the time with a stopwatch I found that the ISR is served at approx 9.8 seconds. My target is to set the timer for a 15 sec delay... I haven't initialized any value because I want to see that timer overflow operates efficiently. Moreover the wizard said that by using the 32.768 external crystal and div by 8 I will get a 16 sec delay.... Here is my code:
Code:

#include "C:\Projects\test.h"
#int_TIMER1
TIMER1_isr()
{

      OUTPUT_C(0x0C);
      delay_ms(1000);
       set_timer1(0x0000);

}



void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8|T1_CLK_OUT);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);


   set_timer1(0x0000);
   SET_TRIS_C(0xFF);

   while(1)
   {
      OUTPUT_C(0x04);
      delay_ms(500);
      OUTPUT_C(0x08);
      delay_ms(500);
   }

}
[/quote]


This is my header:
Code:

#include <18F242.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, NOPUT, NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB


If anyone has a suggestion to make, I would be delighted to hear it. Thank you all.
Agios
KenMacfarlane



Joined: 20 Sep 2005
Posts: 23
Location: Glasgow, Scotland, UK

View user's profile Send private message

Delay and Interrupts
PostPosted: Mon Jan 30, 2006 9:54 am     Reply with quote

Delay_ms switches the GIE global interrupt enable bit off (under PCWH with my 6621).
Agios



Joined: 28 Jan 2006
Posts: 8
Location: Brighton

View user's profile Send private message

PostPosted: Mon Jan 30, 2006 12:32 pm     Reply with quote

Thank you for your reply KenMacfarlane. Unfortunately by making some adjustments and by removing the //delay_ms// I didn't notice any improvement. 10sec delay is the maximum I get from timer1!
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Jan 30, 2006 2:13 pm     Reply with quote

Not to be insulting, but your code is interesting to say the least.

Quote:

#int_TIMER1
TIMER1_isr()
{

OUTPUT_C(0x0C);
delay_ms(1000);
set_timer1(0x0000);

}


ISR's should be short and sweet. The interrupt, well...., interrupts the normal flowing program to do something important. You should get in and out as quickly as possible so the main body can be doing it's thing. You have a 1 second delay inside of your ISR. This is not a good practice. You should have the ISR set the output and then, possible, have a different timer keep track how long it's been and then change the output when you want to.

Quote:
Moreover the wizard said that by using the 32.768 external crystal and div by 8 I will get a 16 sec delay


Do you mean that you are using a 32.768Mhz crystal? Your code states that you have a main clock of 4Mhz. If this is not correct then this will definately affect your delay times.

You also have delay times in your main body that will be affected by the ISR output command. It's possible that the output is changing so quickly that you aren't able to notice it properly. I, personally, hate using the delay() command. I use timers to handle the delays as it lets the main body do it's thing instead of having the whole show just sit there and twiddle it's thumbs.

Ronald
Agios



Joined: 28 Jan 2006
Posts: 8
Location: Brighton

View user's profile Send private message

PostPosted: Mon Jan 30, 2006 3:39 pm     Reply with quote

Roland, thank you for your comments. I have recently started using CCS compiler and for that reason I am trying to make some template codes to conrol peripherals and other microcontroller functions. My intention on the above code is to lit up some leds, verifying that timer1 is operting as wanted. Yes, I am using a 4Mhz crystal to clock the instructions and additionally I use 35.768Khz external clock to handle timer1. Unfortunately when I set timer1 without initial value I get a delay of 9seconds. Hence assuming that timer1 is in 16 bit mode, and by taking into account the hole configuration the delay should be greater than just 9 seconds.. That is where my problem begins. Has anyone got any suggestion to make?? That will be extremely helpfull. Thank you in advance.

Agios Mr. Green
jspencer



Joined: 22 Dec 2003
Posts: 57
Location: Boise, ID USA

View user's profile Send private message

PostPosted: Mon Jan 30, 2006 4:55 pm     Reply with quote

Quote:
#use delay(clock=4000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, NOPUT, NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB


If you are using the 32.768kHz external crystal for timer1, then you need to use the INTRC instead of the HS in your fuses. This is what I'm using for the 16F688 using the 4MHz internal clock and a 32.768kHz external crystal for timer1. Works like a champ.

Code:
#include <16F688.h>
#device adc=8
#use delay(clock=4000000)

// Will need to change to code protect
#fuses INTRC
#fuses NOWDT
#fuses NOPROTECT
#fuses NOMCLR
#fuses NOCPD
#fuses NOBROWNOUT
#fuses PUT
#fuses NOIESO
#fuses NOFCMEN
Agios



Joined: 28 Jan 2006
Posts: 8
Location: Brighton

View user's profile Send private message

PostPosted: Sun Feb 05, 2006 5:07 pm     Reply with quote

Thank you for your reply jspencer. Unfortunatelly PIC18F242 does not support #fuses INTRC therefore I was unable to configure my PIC to work as a champ(Unfortunately)!! Laughing Laughing . Moreover I just got picdem2 plus and the posibility of an erroneus connection does not exist anymore. Has anyone faced the same problem in the past??? It DOES count but it seems that counts with the main 4Mhz crystal and NOT with the external 32.768Khz crystal. According to my calculations, without set timer1 function the timer it should give me: (65535 * 4 * 8)/ 32768 = 64 sec approx. My code is the following:

Code:

#include "C:\Projects\test.h"

#int_TIMER1
TIMER1_isr()
{

      OUTPUT_B(0x0F);
      delay_ms(150);


}



void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_OFF);
   set_timer1(0xFFFF);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8|T1_CLK_OUT);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);

   while(1)
   {
      OUTPUT_B(0x01);
      delay_ms(300);
      OUTPUT_B(0x08);
      delay_ms(300);

   }

}


And my fuse settings are:
Code:

#include <18F242.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, NOPUT, NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB


Any suggestion is much appreciated.

Thank you all

Agios
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 05, 2006 8:34 pm     Reply with quote

My first comment is that you have way too much code to test this problem.
Notice how small my program is. Smaller is better, when investigating
a problem.

The following program works OK on the PicDem2-Plus board.
It blinks LED "RB0" at a rate of 2 seconds on, and then 2 seconds off.
This is correct, because with a 32.768 KHz crystal, it will take 2 seconds
to make Timer1 count from 0x0000 to 0xFFFF and then overflow to
0x0000 to create the interrupt.

Also make sure that you have jumper J6 installed. This jumper
enables the LEDs.

Code:
#include <18F452.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)

#int_timer1
void timer1_isr(void)
{
 output_toggle(PIN_B0);
}

//==================================
void main()
{
output_low(PIN_B0);

setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);

enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

while(1);
}
Agios



Joined: 28 Jan 2006
Posts: 8
Location: Brighton

View user's profile Send private message

PostPosted: Mon Feb 06, 2006 8:22 am     Reply with quote

Thank you PCM programmer for your reply. From what you have written in the post I realized that my formula should be modified for the TIMER 1 with the external 32768KHz clock attached. The delay calculation formula will therefore be: Delay = [65536(2 to the power of 16) * 1 (instruction cycle) * 1 (prescaler) ] / 32768. Now it makes sense. Thank you all for your help.

Regards

Agios
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