Simple TMR1 application example needed


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938

    Default Simple TMR1 application example needed

    Hello,

    I want to count, as an example, how much time (or instruction cycles) a FOR/NEXT loop takes to do its count using TMR1 in Timer mode.

    So I made this piece of code but I can never read any result in the PIC's memory (I have the PIC connected to my PICKit2 so I can readout the memory easely).

    What am I doing wrong?

    Code:
    <html><head></head><body><!--StartFragment--><pre><code><font color="#000080"><i>' Fuses PIC16F88
    </i></font><font color="#008000">@ DEVICE INTRC_OSC_NOCLKOUT
    @ DEVICE PROTECT_OFF
    @ DEVICE WDT_ON
    @ DEVICE PWRT_OFF
    @ DEVICE MCLR_OFF
    @ DEVICE BOD_OFF
    @ DEVICE LVP_OFF
    @ DEVICE CPD_OFF
    @ DEVICE DEBUG_OFF
    @ DEVICE CCPMX_OFF
    @ DEVICE2 IESO_OFF
    @ DEVICE2 FCMEN_OFF
    
    </font><font color="#000080"><i>'____________________________________________________________________________
    ' Registers   76543210
    </i></font>OPTION_REG = %10000000 <font color="#000080"><i>' PORTB Pull-Ups disabled
    </i></font>ANSEL      = %00000000 <font color="#000080"><i>' Disable analog inputs
    </i></font>ADCON0     = %00000000 <font color="#000080"><i>' A/D Module is OFF
    </i></font>CMCON      = %00000111 <font color="#000080"><i>' Comparator Module is OFF
    </i></font>INTCON     = %00000000 <font color="#000080"><i>' All Interrupts are disabled
    </i></font>TRISA      = %00000000 <font color="#000080"><i>' Set Input/Output
    </i></font>PORTA      = %00000000 <font color="#000080"><i>' Ports High/Low
    </i></font>TRISB      = %00000000 <font color="#000080"><i>' Set Input/Output
    </i></font>PORTB      = %00000000 <font color="#000080"><i>' Ports High/Low
    </i></font>T1CON      = %00000000 <font color="#000080"><i>' TMR1 settings
    
    '____________________________________________________________________________
    ' Variables
    </i></font>Ctr_A   <b>VAR BYTE       </b><font color="#000080"><i>' counter
    </i></font>Ctr_B   <b>VAR WORD       </b><font color="#000080"><i>' counter
    </i></font>Mem_Loc <b>VAR WORD       </b><font color="#000080"><i>' memory Location Pointer
    </i></font>Result  <b>VAR WORD</b>(10)   <font color="#000080"><i>' holds the TMR1 value
    
    </i></font>Ctr_A   = 0
    Ctr_B   = 0
    Mem_Loc = $FA8
    Result  = 0
    
    <font color="#000080"><i>'____________________________________________________________________________
    ' Program
    </i></font><b>FOR </b>Ctr_A = 0 <b>TO </b>9                    <font color="#000080"><i>' 10 count loop
    
        </i></font>TMR1H = 0                         <font color="#000080"><i>' reset Timer 
        </i></font>TMR1L = 0                         <font color="#000080"><i>' reset Timer 
        </i></font>T1CON.0 = 1                       <font color="#000080"><i>' start Timer
    
        </i></font><b>FOR </b>Ctr_B = 0 <b>TO </b>100 : <b>NEXT </b>Ctr_B <font color="#000080"><i>' let TMR1 increment a little
    
        </i></font>T1CON.0 = 0                       <font color="#000080"><i>' stop Timer 
        </i></font>Result.LowByte  = TMR1L           <font color="#000080"><i>' store LOW TMR1's value to Result
        </i></font>Result.HighByte = TMR1H           <font color="#000080"><i>' store HIGH TMR1's value to Result
        </i></font>Result[Ctr_A] = Ctr_A             <font color="#000080"><i>' assign Result's index
    
    </i></font><b>NEXT </b>Ctr_A                            <font color="#000080"><i>' loop
    
    </i></font><b>ERASECODE </b>Mem_Loc                     <font color="#000080"><i>' free-up 32 mem locations
    
    </i></font><b>FOR </b>Ctr_A = 0 <b>TO </b>9                    <font color="#000080"><i>' 10 count loop
       </i></font><b>WRITECODE </b>Mem_Loc, Result[Ctr_A]   <font color="#000080"><i>' store Result(x) var
          </i></font>Mem_Loc = Mem_Loc + 1           <font color="#000080"><i>' increment mem location
    </i></font><b>NEXT </b>Ctr_A                            <font color="#000080"><i>' loop
    
    </i></font><b>END</b></code></pre><!--EndFragment--></body></html>
    Roger

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Roger,

    It's working..

    But the 16F88 defaults to 31.25khz internal oscillator.
    So it takes a while to complete.

    If you read it back too quick, you don't see anything.

    Adding ...
    &nbsp; &nbsp; OSCCON = %01100010 ; Set Internal OSC to 4mhz.
    it takes a fraction of a second to complete.

    And this line overwrites the Timer results, so you might want to comment it...
    Result[Ctr_A] = Ctr_A ' assign Result's index

    hth,

    Added:
    F88's Flash memory is written in blocks of 4, so you'll need to change the length of the data.
    Last edited by Darrel Taylor; - 4th February 2010 at 08:48. Reason: added
    DT

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Xtal issue now

    Thanks Darrel,

    After I posted my message, I added a blinky and noticed the "very slow motion".

    Now I use a 20MHz crystal so I corrected the configuration fuse and added the DEFINE information.

    But new problems come up...

    To get the program starting, I need to approach my finger close to the xtal (?!) - it looks like some capacitance issue. I added a 10Mohms resistor in parallel with the xtal and now, the program starts everytime I power the PIC.

    On the other hand, my PICKit2 programmer won't recognize my PIC when I have a 20MHz xtal intalled on the breadboard. I don't know what this comes from. Maybe one of you does?
    Roger

Similar Threads

  1. TMR1 external LP xtal setup check
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th October 2009, 18:11
  2. Replies: 2
    Last Post: - 7th October 2009, 00:06
  3. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 01:47
  4. Circuit needed to allow a PIC to turn something on or off
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th October 2005, 21:17
  5. PIC18 USB simple example needed
    By harryweb in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th September 2005, 22:20

Members who have read this thread : 0

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