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.

PIC16F877A Timer1 Help

Status
Not open for further replies.

CE_PICaPart11

Newbie level 3
Joined
Jul 13, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,313
Hello, I can't seem to be getting my timer 1 to work. I have a 20 Mhz clock for the PIC. I am pretty new at using PICs. I do have a question about the clock and timer1. I have timer1 hooked up to the internal oscillator... but there is no internal oscillator. Am I configuring timer1 correctly? Also, can I put my interrupt service routine inside timer.c?

My main.c file is:
Code:
#include "htc.h"
#include "../include/timer.h"

#define _XTAL_FREQ 20000000

__CONFIG(WDTDIS & PWRTDIS & BORDIS & DEBUGDIS & UNPROTECT & HS);

void interrupt ISR(void)
{
	if(TMR1IF)
	{
		counter++;
		if(counter == 20) /* wait some time */
		{
			RB7 = ~RB7;
			counter = 0;
		}
		TMR1L = 0x00;       /* Low byte */
		TMR1H = 0x00;       /* High byte */
		TMR1IF = 0; /* Clear interrupt flag */
	}	
}

void io_init(void)
{
	TRISA = 0; /* PORTA is 6-bit wide - make all outputs */
	TRISB = 0; /* PORTB is 8-bit wide - make all outputs */
	PORTA = 0; /* Initial output is 0 */
	PORTB = 0b01010101; 
}	

void main(void)
{
	io_init();
	timer_init();
	RB7 = 0;
	RB6 = 1;
	while(1)
	{
	}	
}

My timer.c file is:
Code:
#include "../include/timer.h"

void timer_init(void)
{
	T1CKPS1 = 1;    /* Prescaler of 8 */
	T1CKPS0 = 1;
	TMR1ON = 1;     /* Enable timer1 */          
	TMR1IE = 1;     /* Enable timer1 interrupts */
	TMR1L = 0x00;   /* Low byte */
	TMR1H = 0x00;   /* High byte */
}

Thanks for any help at all.
 

i think your code is correct (not sure), i have just roughly go through it.

but i suggest you to use debugger toll if you are using MPLAB there is timer and you can see the update of all the inbuilt hardware and registers also.
 

I don't use HT C but the code looks basically OK to me. I would suggest two things though.

1. Load TMR1H before TMR1L so there is less chance of the timer rolling over again too quickly.

2. Check HTC really is setting the bits in timer_init().
I always use a construction like this to set bits: T1CON |= (1 << TMR1ON);
and to reset a bit I use T1CON &= ~(1 << TMR1ON);

so the register as well as the bit is explicitly named.

Brian.
 

I am using the "internal oscillator" for timer1. However, PIC16F877A does not have an internal oscillator, so does that mean that I have to change it to use the external and wire my 20 MHz to the T1OSI and T1OSO pins and change TMR1CS = 1 (external)?

I didn't do that before because i just thought the "internal" would really be the external oscillator... meaning that timer1 would have a different clock source than the CPU.

Added after 1 hours 43 minutes:

I figured it out. I didn't enable global and peripheral interrupts. Thanks for the help.
 

CE_PICaPart11 said:
I am using the "internal oscillator" for timer1.
Actually, you are using the "internal clock source" which is derived from the main external oscillator.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top