Thread: Problem with TIMER0 Interrupt

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    3

    Problem with TIMER0 Interrupt

    Hi,
    I execute the following lines of code and
    expect me that the program will deliver a 1Hz
    pulse train on PIN RA2. the problem is that
    I only get 6 pulses from RA2 (LED). I Send you
    an image of the simulation. Can you see anything
    wrong?

    Thank you!Problem with TIMER0 Interrupt-6seconds_execution-jpg
    Code:
    #include <pic.h>
    #include <htc.h>
    
    
    void Initialization ();
    
    __CONFIG(FOSC_LP & WDTE_ON & PWRTE_OFF & CP_OFF);
    #define _XTAL_FREQ 32768              
    
    #define LED RA2
    
    int S = 0; 
    
    void main() 
    {
        Initialization ();
    	while (1)
    	{
    		if (S)
    		{
    			S=0;
    			LED = 1;
    			__delay_us (1);
    			LED = 0;
    			
    		}
    	}   
    }
    
    void Initialization(void)
    {
    	TRISA = 0b00000000;              	
    	TRISB = 0b00000001;          	
    	OPTION_REG = 0b00000100;
    	INTCON = 0b10100000;        	
    	TMR0 = 0; 						
    	LED = 0;
     }
    
    void interrupt tc_int(void)
    {    
    	if (T0IF) 
    	{        
    		T0IE = 0;
    		S = S+1;
    		T0IF = 0;
    		T0IE = 1;
    		TMR0 = 0;
    	}
    }
    Attached Files Attached Files
    Last edited by Salem; 01-11-2021 at 10:13 PM. Reason: Inlined the code

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Looks like Microchip C code; no idea which MCU.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    3
    Problem with TIMER0 Interrupt-6seconds_execution-png

  4. #4
    Registered User
    Join Date
    Jan 2021
    Posts
    3
    The old one: PIC16F84

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Looking at your code; I question this line.

    Code:
    S=0;
    Would not
    Code:
    S--;
    Be more correct?

    And, is the "if" statement correct?
    Edit: Looks like the "if" statement will not make a difference from my idea of using an while since the if is inside of a for ever while loop.

    Tim S.
    Last edited by stahta01; 01-11-2021 at 04:46 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The next thing to try would be commenting out this line
    Code:
    TMR0 = 0;
    In the interrupt tc_int.

    Been over 10 years since I did PIC programming.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PIC16F73 - Timer0 Interrupt
    By Sadlercomfort in forum C Programming
    Replies: 2
    Last Post: 09-11-2015, 10:36 AM
  2. Problem!!!!! pic16f690 Interrupt PORTB
    By soguel in forum C Programming
    Replies: 1
    Last Post: 11-13-2014, 10:46 AM
  3. Timer0 RTC
    By Alex Margetts in forum C Programming
    Replies: 0
    Last Post: 03-15-2014, 11:29 AM
  4. Problem coding Banner sensor interrupt...
    By processing in forum C Programming
    Replies: 1
    Last Post: 02-14-2005, 04:41 PM
  5. interrupt
    By juandy in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2002, 08:24 AM

Tags for this Thread