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 interrupt

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



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

TIMER1 interrupt
PostPosted: Thu Sep 23, 2010 7:47 pm     Reply with quote

I am using a PIC18F23k20 and whatever I write to set_timer1(nnnn) the time between two interrupts is not changing. Looking at the .lst shows that the registers are written as expected. Changing the clock divider does change the time. What do I do wrong?
Same problem using timer0.

Thanks.

Code:


#int_TIMER1
void  TIMER1_isr(void)     //  tick
{
output_toggle(PIN_C6);
   time_count1++;
   Intcount++;
   if(Intcount > 14)    //increment lapsed time
      {
      time_count2++;
      Intcount = 0;
   flag = TRUE;
//output_toggle(PIN_C6);
      }
set_timer1(0x125);
}

void main()
{
unsigned long VCC =0.0, VMon =0.0;
unsigned long i=0, l = 0;
int1 done;
unsigned int j = 0;

   setup_adc_ports(sAN0|sAN1|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   
   set_timer1(0x125);

   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);

   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
//setup_oscillator(OSC_8MHZ|OSC_TIMER1);
   clear_interrupt(INT_TIMER1);

   enable_interrupts(INT_TIMER1);
    ext_int_edge(H_TO_L);      // init interrupt triggering for button press
//  enable_interrupts(INT_EXT2);// turn on interrupts
   enable_interrupts(GLOBAL);
   
      set_tris_a(0xfb);
    set_tris_c(0x3f);
      set_tris_b(0xff);
   read_adc(ADC_START_ONLY);

   LED_RED_OFF;
   LED_GREEN_OFF;     
delay_ms(10);
output_b(0xff);
 

while(1)   delay_ms(300);   // 30 sec;

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 23, 2010 8:36 pm     Reply with quote

1. Post a compilable program. Add the #include for the PIC, #fuses,
#use delay, etc.

2. Delete about 90% of your code, so the remaining code concentrates
on the Timer1 problem. Delete anything that isn't related to the problem.

3. Post a description of your external hardware, if it's related to the
test program.

4. Post your compiler version.

5. Describe your test environment. Is it Proteus ? Or is it a hardware
board ?
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

Timer1 interrupt
PostPosted: Thu Sep 23, 2010 10:19 pm     Reply with quote

Thanks for the prompt reply PCM.

The compiler version is CCS PCH C Compiler, Version 4.106, 49215

I did paste what was in the main.h in the main.c for simplicity.
Whatever I write in set_timer1() I always get the same interval.

Code:

#include <18F23k20.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODELAYINTOSC         

#use delay(clock=3900000)

//#use rs232(baud=9600,parity=N,xmit=PIN_C6,bits=8)

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)


int16 Intcount=0,time_count1=0, time_count2=0;
int1 flag = 0;


#int_TIMER1
void  TIMER1_isr(void)     // 65.4ms tick
{

output_toggle(PIN_C6);
   time_count1++;
   Intcount++;
   if(Intcount > 14)    //increment ca every ...
      {
      time_count2++;
      Intcount = 0;
   flag = TRUE;
//output_toggle(PIN_C6);
      }
set_timer1(250);
}

void main()
{

   setup_adc_ports(sAN0|sAN1|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   
   set_timer1(0x250);

   setup_timer_0(RTCC_INTERNAL);
    setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
   setup_timer_2(T2_DISABLED,0,1);

   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   clear_interrupt(INT_TIMER1);

   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   
 set_tris_c(0x3f);

while(1) ;
}

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

timer1 interrupt
PostPosted: Thu Sep 23, 2010 11:03 pm     Reply with quote

Sorry, I forgot to mention that I run it on the real hardware and not simulation. For the sake of this problem the only relevant hardware is a darlington driving the LED.

PCM programmer wrote:
1. Post a compilable program. Add the #include for the PIC, #fuses,
#use delay, etc.

2. Delete about 90% of your code, so the remaining code concentrates
on the Timer1 problem. Delete anything that isn't related to the problem.

3. Post a description of your external hardware, if it's related to the
test program.

4. Post your compiler version.

5. Describe your test environment. Is it Proteus ? Or is it a hardware
board ?

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Sep 24, 2010 2:30 am     Reply with quote

A few comments:
You are selecting INTRC_IO.
Your clock statement, selects 3.9MHz.
Is this an allowable combination for the chip?...

This will stop a lot of things from working as 'expected'.

How are you detecting the 'change' in output?.
The counter counts in 4*instruction time steps.
By the time you reach the interrupt, at least 30 instruction times _will_ have passed.
You then increment two 16bit variables (probably another dozen instructions), and at the 'minimum', do a value comparison as well (a few more). Probably something like 60 instruction times to the point where you set the timer. So the timer will have incremented by then, by perhaps 15 counts.
Given the total count is to 65536, the change between not setting the timer, and setting it to 250, will be about 65536/65301. 0.35% If you are using something like a scope, this will be completely in detectable.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Fri Sep 24, 2010 3:08 am     Reply with quote

A minor comment:
Quote:
set_timer1(250);
and
Quote:
set_timer1(0x250);
Be consistent in using decimal or hexadecimal.

Code:
   setup_spi(SPI_SS_DISABLED);
This is a CCS setup wizard error. The parameter for disabling SPI should be changed to FALSE.
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

Re: timer1 interrupt
PostPosted: Fri Sep 24, 2010 11:31 am     Reply with quote

ckielstra: thank you for the note on the setup_spi.
As for the timer I tried 8 and 16 bit and I didn't correct before sending.
INTRC is what I use and I tried INTRC_IO to see if it would make a difference but it doesn't.

tthelma: I measure the time on C6. it is toggled when I enter the interrupt and that is what I measure with a scope. The second C6 toggle is commented out.

I need to generate a timer interrupt every 5ms and be able to change it to 250ms and back to 5ms. I tried using timer0 but it shows the same problem. I am pretty sure I do something wrong but what?

Any further suggestion?

Thanks.

Rocky wrote:
Sorry, I forgot to mention that I run it on the real hardware and not simulation. For the sake of this problem the only relevant hardware is a darlington driving the LED.

PCM programmer wrote:
1. Post a compilable program. Add the #include for the PIC, #fuses,
#use delay, etc.

2. Delete about 90% of your code, so the remaining code concentrates
on the Timer1 problem. Delete anything that isn't related to the problem.

3. Post a description of your external hardware, if it's related to the
test program.

4. Post your compiler version.

5. Describe your test environment. Is it Proteus ? Or is it a hardware
board ?

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

timer1 interrupt
PostPosted: Fri Sep 24, 2010 12:45 pm     Reply with quote

ckielstra, I took your timer0 rtc example in another post and I had the same problem. Then instead of using set_timer0(250) I tried set_timer0(65000) and that changed the time as it should. It still was not wokring in 8bit mode and finally I found in the CCS help that 16 bit is the default mode and 8 bit must be set:
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1 | RTCC_8_BIT);
Yes, I did read the manual but missed that. :(
The reason I didn't see changes in 16 bit was that the value 250 was too low to see a significant change from 0.

Thanks to all who answered.

ckielstra wrote:
A minor comment:
Quote:
set_timer1(250);
and
Quote:
set_timer1(0x250);
Be consistent in using decimal or hexadecimal.

Code:
   setup_spi(SPI_SS_DISABLED);
This is a CCS setup wizard error. The parameter for disabling SPI should be changed to FALSE.

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Sep 24, 2010 2:16 pm     Reply with quote

Be aware that your timings will still not be right, if you do something like a 'delay' statement, unless you have fixed the clock statement. Your chip is _not_ running at 3.9MHz.

Best Wishes
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

TIMER1 interrupt
PostPosted: Fri Sep 24, 2010 6:38 pm     Reply with quote

Yes, I did, thank you.

Ttelmah wrote:
Be aware that your timings will still not be right, if you do something like a 'delay' statement, unless you have fixed the clock statement. Your chip is _not_ running at 3.9MHz.

Best Wishes

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
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