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

TIMER0 Interrupt Problem

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



Joined: 25 Mar 2010
Posts: 8

View user's profile Send private message Send e-mail

TIMER0 Interrupt Problem
PostPosted: Sun May 09, 2010 12:25 pm     Reply with quote

Hi for all
First of all excuse me about my English.
I want to use timer0 interrupt but it isn't work well!
where is the problem?
Code:

#include <16f877a.h>             // Define PIC Microcontroller Header
#fuses   HS                      // Set Oscillator On "High Speed" Mode
#use  delay(clock=20000000)   // Set 20MHz Oscillator For Microcontroller

#include <LCD.c>                 // Define LCD Header
#define  use_portd_lcd TRUE      // Set PORTD For LCD Input Pin

#int_timer0
void Timer0_isr()
{
 int i=0;
 
 lcd_init();
 while(i<11){
 printf(lcd_putc,"\fCOUNT=%u",i);
 delay_ms(100);
 i++;}
 return;
}

//----------------     PIN CONFIG     ----------------
#byte PORTA=0x05                 // Call PORTA Register From BANK0
#byte PORTB=0x06                 // Call PORTB Register From BANK0
#byte PORTC=0x07                 // Call PORTC Register From BANK0
#byte PORTD=0x08                 // Call PORTD Register From BANK0
#byte PORTE=0x09                 // Call PORTE Register From BANK0
#byte TRISA=0x85                 // Call TRISA Register From BANK0
#byte TRISB=0x86                 // Call TRISB Register From BANK0
#byte TRISC=0x87                 // Call TRISC Register From BANK0
#byte TRISD=0x88                 // Call TRISD Register From BANK0
#byte TRISE=0x89                 // Call TRISE Register From BANK0
//----------------------------------------------------
#bit  TMR0IF=0x0B.2



void main()
 {
   TRISA=0;
   TRISB=0;
   TRISC=0;
   TRISE=0;
   PORTC=0;
   setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_256);
   interrupt_active(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

 for(;;)
  {
   
   
   PORTB=0b00000001;
   delay_ms(1000);
   
   if(get_timer0()==0){set_timer0(0);};
   
   PORTB=0b00000000;
   delay_ms(1000);

   
   

  }
 }


This link is CODE and SCH:
http://www.4shared.com/file/bTwzl32U/TIMER0INTRRUPT.html
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 09, 2010 1:25 pm     Reply with quote

Tell us the purpose of your project. What do you want to do ?
saeidjabbari



Joined: 25 Mar 2010
Posts: 8

View user's profile Send private message Send e-mail

PostPosted: Sun May 09, 2010 10:42 pm     Reply with quote

Hi my friend
It's easy! I want when timer0 is overflow, jump to interrupt loop and count 0 to 10, and go back to main loop and turn off led. But it's not done correctly.
If download schematic and code you can understand my opinion.

Thanks For Advice
Ttelmah



Joined: 11 Mar 2010
Posts: 19349

View user's profile Send private message

PostPosted: Mon May 10, 2010 1:55 am     Reply with quote

Code:

int1 timer_overflow=false;

#int_timer0
void Timer0_isr() {
   timer_overflow=true;
}

void main()  {
   int i;
   output_b(0);
   output_c(0);
   output_d(0);
   output_e(0);
   setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_256);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   for(;;) {
     if (timer_overflow) {
        timer_overflow=false;
        output_high(PIN_B0); 
        lcd_init();
        for (i=0;i<11;i++){
           printf(lcd_putc,"\fCOUNT=%u",i);
           delay_ms(100);
        }
        output_low(PIN_B0);
     }
  }
}

As shown, this will wait for the timer to overflow, turn on pin B0, count for 0 to 10 on the LCD, with 100mSec between each count, then turn off the LED. Repeating whenever the timer overflows.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Mon May 10, 2010 3:26 am     Reply with quote

I still don't get the goal of the project.
At the current settings the timer will overflow in 13ms. Keeping this in mind the 100ms delay makes no sense.
Ttelmah



Joined: 11 Mar 2010
Posts: 19349

View user's profile Send private message

PostPosted: Mon May 10, 2010 3:37 am     Reply with quote

Yes.
I'm so used to 18F chips, where Timer0, is 16bit by default, that the 'old' 8bit version didn't occur to me. 13.1mSec.....

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19349

View user's profile Send private message

PostPosted: Mon May 10, 2010 3:38 am     Reply with quote

and (of course), in the original code, it'll never get out of the interrupt routine, for the same reason.

Best Wishes
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