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 can I use Timer0 on PIC16F877A ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Hello friends,

Does anyone know on how can I use Timer0 on PIC16F877A ?
I'm using Hi-Tech C.

Thanks
 

IMHO, you are asking a general question.

Which one do you want to know first?
- special function registers
- block diagram/operation
- timer mode/counter mode
- use of prescalers
- interrupt operation
- calculating interrupt rate
- using preset values
- skip timer technique for multitasking

It is always nice to read the datasheet first, just 3-4 pages i think. Understand how TMR0 works by referring to the block diagram
 

for example I want to create 1 second delay for timer 0.....
do you guys have an example code ?
For a example I blink LED on portd bit 0, every 1 second....
so RD0 = ~RD0 every 1 second, how can I set the timer0 for it ?

Thanks

I got this one from Hi Tech C example but I don't know yet on how to use it...
Code:
timer0_isr(void)
{
	if(reload == 0){
		// effect a change on PORTB whenever our desired period is reached.
		// Note this timing will contain a margin of error.
		reload = RELOADS + 1;
		seconds++;
		PORTD++;	// effect a change on PORTB
	}
	reload--;
	T0IF = 0;
}

main()
{
	// initialize timer 0; 
	
	//OPTION = 0b0111;	// prescale by 256
	T0CS = 0;			// select internal clock
	T0IE = 1;			// enable timer interrupt
	GIE = 1;			// enable global interrupts
	TRISD = 0;			// output changes on LED
    PORTD = ~PORTD;
   
	for(;;)
		continue;		// let interrupt do its job
    PORTD = 1;
}
 

PIC16F877A, Fosc = 4 MHz, 10 ms * 100 = 1000 ms 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
unsigned char time = 0, timerflag = 0;
 
//Timer0
//Prescaler 1:64; TMR0 Preload = 97; Actual Interrupt Time : 9.986 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG     = 0x85;
  TMR0       = 97;
  INTCON     = 0xA0;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit   = 0;
    TMR0     = 97;
    TMR0IE_bit = 0;
    //Enter your code here
    time++;
    if(time == 100){
    timerflag = 1;
    }
 
    
  }
}
 
void main(){
        TRISD = 0x00;
        PORTD = 0x00;
        LATD = 0x00;
 
        InitTimer0()
 
    while(1){
 
        if(timerflag == 1){
            timerflag = 0;
            PORTD = ~PORTD;
        }
    }
 
}

 

PIC16F877A, Fosc = 4 MHz, 10 ms * 100 = 1000 ms 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
unsigned char time = 0, timerflag = 0;
 
//Timer0
//Prescaler 1:64; TMR0 Preload = 97; Actual Interrupt Time : 9.986 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG     = 0x85;
  TMR0       = 97;
  INTCON     = 0xA0;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit   = 0;
    TMR0     = 97;
    TMR0IE_bit = 0;
    //Enter your code here
    time++;
    if(time == 100){
    timerflag = 1;
    }
 
    
  }
}
 
void main(){
        TRISD = 0x00;
        PORTD = 0x00;
        LATD = 0x00;
 
        InitTimer0()
 
    while(1){
 
        if(timerflag == 1){
            timerflag = 0;
            PORTD = ~PORTD;
        }
    }
 
}

Thank you for the info I'll make it now and see what happens, I'll get back soon

- - - Updated - - -

PIC16F877A, Fosc = 4 MHz, 10 ms * 100 = 1000 ms 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
unsigned char time = 0, timerflag = 0;
 
//Timer0
//Prescaler 1:64; TMR0 Preload = 97; Actual Interrupt Time : 9.986 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG     = 0x85;
  TMR0       = 97;
  INTCON     = 0xA0;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit   = 0;
    TMR0     = 97;
    TMR0IE_bit = 0;
    //Enter your code here
    time++;
    if(time == 100){
    timerflag = 1;
    }
 
    
  }
}
 
void main(){
        TRISD = 0x00;
        PORTD = 0x00;
        LATD = 0x00;
 
        InitTimer0()
 
    while(1){
 
        if(timerflag == 1){
            timerflag = 0;
            PORTD = ~PORTD;
        }
    }
 
}


do you have the calculation or description of it ?
thanks

- - - Updated - - -

I got these errors with the code above
Code:
Error   [192] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 16.5 undefined identifier "TMR0IF_bit"
Error   [192] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 19.1 undefined identifier "TMR0IE_bit"
Error   [192] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 33.1 undefined identifier "LATD"
Error   [312] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 37.1 ";" expected
Error   [285] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 43.1 no identifier in declaration
Warning [374] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 43.1 missing basic type; int assumed
Error   [314] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 43.1 ";" expected
Error   [285] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 45.1 no identifier in declaration
Warning [374] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 45.1 missing basic type; int assumed
Error   [314] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 45.1 ";" expected

- - - Updated - - -

I defined as char
Code:
char TMR0IF_bit,TMR0IE_bit;
but get :
Code:
Error   [192] C:\Users\Antonius\Documents\MPLAB\LCD16X2\LCD_with_timer.c; 35.1 undefined identifier "LATD"
for the time being, I commented out LATD

- - - Updated - - -

I can't see a proper result for the timer, anyone can help me ? thanks
 

Change TMR0IF_bit and TMR0IE_bit to TMR0IF and TMR0IE. If it doesn't work then use INTCONbits.TMR0IF and INTCONbits.TMR0IE. Put ; semi-colon after InitTimer0() function call. Remove LATD line, you don't need it.
 

Change TMR0IF_bit and TMR0IE_bit to TMR0IF and TMR0IE. If it doesn't work then use INTCONbits.TMR0IF and INTCONbits.TMR0IE. Put ; semi-colon after InitTimer0() function call. Remove LATD line, you don't need it.
Ok, I'll try, I'll let you know the progress..
thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top