Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

1 second interrupt using timer0

Status
Not open for further replies.

sachin95

Newbie level 3
Joined
Oct 13, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
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:cool:


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 a moderator:

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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top