PDA

View Full Version : PIC18F4550 timer1 questions



GilB
- 2nd August 2005, 16:17
Hi

Q1: if I need the counter to count 1 second. Do I need to check for overflow, thus configuring TMR1 to 0xFFFF-....?

Q2: I wrote the following code, but it seems it only works once and I need the timer to restart. What am I doing wrong?

void timer1(unsigned short val)
{
TMR1ON = 0;
TMR1IF = 0;
TMR1 = val;
T1CON = 0xC8;//0b11001000
TMR1ON = 1;

while(!TMR1IF)
;
}

void main(void)
{
TRISD = 0x00;//output
PORTD = 0x00;

while(1)
{
PORTD = 0x01;
timer1(0xFFFF-0x8000);
PORTD = 0x08;
timer1(0xFFFF-0x8000);
}
}

rhino
- 2nd August 2005, 16:24
Well... to start, it looks like your programming with a "C" compiler. I'm not very fluent with C. This forum is for the Picbasic compilers from Melabs. There maybe someone here that can help out, but really you should look for the forums dedicated to your compiler. Sorry!

Bruce
- 3rd August 2005, 00:46
This isn't exactly the best place for questions on the C18 compiler, but,
since I dabble with this myself, and it's a freebie, here's a quick example;

Assumes 20MHz osc. Give 1S pulses on RD0/RD1.

#include p18f452.h
#include timers.h

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

unsigned int Loops = 0;

void Timer1(unsigned int val)
{
PIR1bits.TMR1IF = 0;
WriteTimer1(val);

while(!PIR1bits.TMR1IF)
PIR1bits.TMR1IF=0;
Loops += 1;
}

void main(void)
{
LATD = 0x00;
TRISD = 0x00;
T1CON = 0b10110101;
WDTCON = 0;

while(1)
{
LATD = 0x01;
while(Loops<10)
{
Timer1(0x0bdc);
}
LATD = 0x08;
while(Loops<20)
{
Timer1(0x0bdc);
}
Loops = 0;
}
}


I'll let you figure out how & why it works with the 18F datasheet & C18
manuals.

Microchip's forum http://forum.microchip.com/tt.asp?forumid=3 would be
your best bet for getting quick answers on this compiler.

GilB
- 4th August 2005, 05:36
10x Bruce
I'll look into your example and next direct all my questions to the link you gave me.

10x again.