PIC18F4550 timer1 questions


Closed Thread
Results 1 to 4 of 4
  1. #1
    GilB's Avatar
    GilB Guest

    Default PIC18F4550 timer1 questions

    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);
    }
    }

  2. #2
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Lightbulb Is this in C?

    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!

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    #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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    GilB's Avatar
    GilB Guest


    Did you find this post helpful? Yes | No

    Default 10x

    10x Bruce
    I'll look into your example and next direct all my questions to the link you gave me.

    10x again.

Similar Threads

  1. Software PWM using Timer1
    By muqasim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2009, 11:49
  2. RPM With Timer1
    By mel4853 in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 29th June 2009, 21:50
  3. Help with TIMER1 + SLEEP
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2009, 15:07
  4. Time Period of Timer1
    By arnol34 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th May 2007, 00:31
  5. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts