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.

How to use Timer in PIC16F877A

Status
Not open for further replies.

Phyllis

Newbie level 1
Joined
Apr 26, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
9
Hello, I am beginner in microcontroller. I am using pic 16f877a.
How to turn on led after 1 minute without using delay?
 

Here I am attaching a project. It blinks LED connected to RD0 once every one second. Timer1 is used for delay. It is mikroC PRO PIC code.
 

Attachments

  • LED Timer.rar
    29.9 KB · Views: 55

Basics:

1. set port pin(s) to output mode so you can control the LED
2. configure a timer, TMR1 would be best choice in your application because it has 16 bits so can be used to create longer delays.
3. configure the source to the timer, internal clock or external clock and set the pre-scaler to a suitable divisor.
4. timers count UP and it's easiest to test them for rolling over to zero after maximum count so work out a value to load for the number of counts before roll over.
5. either poll the timer, looking for zero or better still, enable the timer interrupts so it jumps by itself to your interrupt code when it reaches zero.

Long delays like one minute may not be possible using a single roll over, you may need to use a software counter to count the roll overs until sufficient time has elapsed. For example, use 600 * 100mS counts to make 60 seconds.

Brian.
 

Sorry, my mistake. I had given code for 1 sec timer. Here is the code for 1 minute timer.


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
42
43
44
45
46
47
48
49
50
51
52
53
char counter = 0;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer1() {
  T1CON        = 0x31;
  TMR1IF_bit = 0;
  TMR1H        = 0x0B;
  TMR1L        = 0xDC;
  TMR1IE_bit = 1;
  INTCON = 0xC0;
}
 
void Interrupt(){
  if(TMR1IF_bit) { 
    TMR1IF_bit = 0;
    TMR1H         = 0x0B;
    TMR1L         = 0xDC;
    //Enter your code here
    if(++counter == 120) {
           PORTD.F0 = ~PORTD.F0;
           counter = 0;
    }
  }
} 
 
 
void main() {
 
     CMCON = 0x07;
     
     ADCON1 = 0x87;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0x00;
     TRISD = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     
     InitTimer1();
     
     while(1) {
     
     
     
     }
}

 

Milan.rajik, your code only works if the clock is 4MHz. Phyllis hasn't told us what clock speed they are using.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top