+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Newbie level 3
    Points: 436, Level: 4

    Join Date
    Oct 2012
    Posts
    3
    Helped
    0 / 0
    Points
    436
    Level
    4

    1 second interrupt using timer0

    hi,
    i m new to pic programming.i used an example code in mikroC time calculator to make a 1 second interrupt. the problem is when i simulate it in protues, i can see that led blinking time is more than 1 second.why is that??

    pls help


    thanks.



    this is the code
    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    //Timer interrupt:             25 ms
    //Desired Interrupt Time:      2.5 s
    //cnt value:                   100 (2.5 s / 0.025 s)
    //MCU:                         PIC 16F887
    //Internal module:             Timer0
    //Prescaler 1:256; TMR0 Preload = 61; Actual Interrupt Time = 24.96 ms
     
    unsigned cnt;              // This is the counter variable which will extend desired period
     
    void InitTimer0(){
      OPTION_REG = 0x87;       // Prescaler 256
      TMR0 = 61;
      INTCON = 0xA0;
    }
     
    void Interrupt(){
      if (TMR0IF_bit){
            cnt++;                 // Increment counter, this is the tweak we added in original Timer Calculator code
            TMR0IF_bit = 0;        // Clear TMR0IF
            TMR0       = 61;       // Prepare for next interrupt
      }
    }
     
    void main() {
      InitTimer0();            // Initialize Timer0
      ANSEL  = 0;              // Configure AN pins as digital
      ANSELH = 0;
      C1ON_bit = 0;            // Disable comparators
      C2ON_bit = 0;   
      TRISB = 0;               // PORTB is output
      PORTB = 0xFF;            // Initialize PORTB
     
      cnt = 0;                 // Initialize cnt
     
      do {
            if (cnt >= 40) {      // 1s
              PORTB = ~PORTB;      // Toggle PORTB LEDs
              cnt = 0;             // Reset cnt
            }
      } while(1);
    }
    Last edited by alexan_e; 13th October 2012 at 23:11. Reason: added SYNTAX tags

    •   Alt13th October 2012, 22:49

      advertising

        
       

  2. #2
    Full Member level 5
    Points: 1,942, Level: 10

    Join Date
    Apr 2012
    Location
    bangalore
    Posts
    311
    Helped
    44 / 44
    Points
    1,942
    Level
    10

    Re: 1 second interrupt using timer0

    have u burnt the code on the PIC microcontroller



    •   Alt14th October 2012, 10:47

      advertising

        
       

  3. #3
    Advanced Member level 5
    Points: 38,206, Level: 47
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Silicon Valley, California, USA (from Dhaka, Bangladesh)
    Posts
    4,758
    Helped
    1788 / 1788
    Points
    38,206
    Level
    47
    Blog Entries
    22

    Re: 1 second interrupt using timer0

    The program generates a "1 second interrupt" if the PIC is running at 8MHz. Maybe you have not set the PIC clock frequency as 8MHz in Proteus. If you haven't, set the PIC clock frequency as 8MHz.



--[[ ]]--