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.

[SOLVED] TMR0 Problem Regarding Time Calculations

Status
Not open for further replies.

thir13enth

Member level 1
Joined
Feb 23, 2011
Messages
38
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
Quezon City, Philippines
Activity points
1,545
Microcontroller = PIC16F877A
Oscillator = 20MHz XT
Compiler = MikroC
----------------------------

Hello, I am trying to work my Timer 0 with my PIC. Unfortunately, I cannot make it work the way I wanted it to. There is no error when I compile my program, but when I try to simulate it in Proteus, it gave fast outputs. Here is my code:

Code:
unsigned count;

void main() {

 OPTION_REG = 0xE4; // gie = peie = tmr0ie = tmr0if = 1
 TRISD = 0x00;
 PORTD = 0x00;
 TMR0 = 0;
 INTCON = 0x07; // PS0 = PS1 = PS2 = 1 set to 1:256

 do {
    count++;
    if (count == 76) {
      PORTD = ~PORTD;      // Toggle PORTB LEDs
      count = 0;             // Reset cnt
    }
  } while(1);
}

What I need is to lengthen its duration. Say for example, 2 seconds. Anyone who has a tutorial on how to do do the calculations especially the preload value? I want to learn how, of course based with my PIC which is PIC16F877A.

Regards,
Gello Mark C. Vito
 

Code:
int Counter=0;

void interrupt()
{
	 INTCON=0x20;						//Clear the GIE and the T0IE
	 TMR0=0;                            //Re-initialize TMR0 register
	 ++Counter;                         //Increment the dummy variable
}

void main()
{
 	 TrisB=0;                           //Configure PortB as Output
 	 TrisA=255;                         //Configure PortA as Input
 	 PortB=0;                           //Initialize portB
	 INTCON=0xA0;                       //Enable the GIE abd T0IE
	 OPTION_REG=0x85;                   //Set the prescale to 64
	 TMR0=0;                          //Initialize the Timer0 register
	 /* These values of the prescaler and the TMR0 as well as the dummy variable will ajust the
	  interrupt to fire every 1 second
	 */

	 while(1)
	 {
			 if (Counter==64)
			 {
			    portb=~portb;         //Toggle portb status
			    Counter=0;            //Reset the dummy variable
		     }
	 }
}

The oscillator is 4MHz.

Hope this helps.
 
I really appreciate the effort. Though I would greatly appreciate if you can explain to me how to come up with the values.

Thanks.
Gello Mark C. Vito
 

You can try this pic timer calculator

**broken link removed**

Alex
 
I appreciate the efforts though for the second time, I would want to insist knowing the calculations. This is our final year project, so it is very critical and essential for me to know the calculations details by details.

Thanks,
Gello Mark C. Vito
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top