TMR2 not working..


Results 1 to 18 of 18

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    Got it working, and I only found one problem.

    The 194 would be for timer 0. Where it counts from that number until it overflows at 256, which takes 62 counts.

    Timer 2 works a little different.
    It counts from 0 up to the number in PR2, then resets to 0 again.
    So 62 is the number for PR2 @ ~1ms. It's actually 62.5, so we may need to drop the prescaler to 4 and use 250 for PR2 if you want more accuracy.

    I do not see any problems with the interrupt portions, and it all works fine here. Here's the breadboard I just put together.

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1902&stc=1&d=118607629 9">


    And here's the program I'm using ...
    Code:
    <font color="#0000FF"><b><i>; http://www.picbasic.co.uk/forum/showthread.php?t=6818
    
    </i></b></font><font color="#000080">@ ERRORLEVEL -306   </font><font color="#0000FF"><b><i>; turn off crossing page boundary message
    </i></b></font><font color="#000080">@  __CONFIG    _CONFIG1, _CP_OFF  &amp; _DEBUG_OFF &amp; _WRT_PROTECT_OFF &amp; _CPD_OFF &amp; _LVP_OFF &amp; _BODEN_OFF &amp; _MCLR_OFF &amp; _PWRTE_ON &amp; _WDT_OFF &amp; _XT_OSC
    
    </font><font color="#008000"><b>DEFINE </b></font><b>LCD_DREG PORTB  </b><font color="#0000FF"><b><i>' LCD data port 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>LCD_DBIT </b><font color="#800000"><b>0      </b></font><font color="#0000FF"><b><i>' LCD data starting bit 0 or 4 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>LCD_RSREG PORTB </b><font color="#0000FF"><b><i>' LCD register select port 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>LCD_RSBIT </b><font color="#800000"><b>4     </b></font><font color="#0000FF"><b><i>' LCD register select bit 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>LCD_EREG PORTB  </b><font color="#0000FF"><b><i>' LCD enable port 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>LCD_EBIT </b><font color="#800000"><b>5      </b></font><font color="#0000FF"><b><i>' LCD enable bit 
    </i></b></font><font color="#008000"><b>PAUSE </b></font><font color="#800000"><b>500 </b></font>: <font color="#008000"><b>LCDOUT </b></font><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>1 </b></font>: <font color="#008000"><b>PAUSE </b></font><font color="#800000"><b>250 </b></font><font color="#0000FF"><b><i>; Initialize LCD
    
    </i></b></font><b>LED   </b><font color="#008000"><b>VAR </b></font><b>PORTA</b>.<font color="#800000"><b>4
    
    </b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;DT_INTS-14.bas&quot;     </font><font color="#0000FF"><b><i>' Base Interrupt System
    </i></b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;ReEnterPBP.bas&quot;     </font><font color="#0000FF"><b><i>' Include if using PBP interrupts
    
    </i></b></font><font color="#008000"><b>ASM
    </b></font><font color="#0000FF"><b><i>;T0IF = TMR0IF
    ;T0IE = TMR0IE
    </i></b></font><font color="#000080">INT_LIST  macro    </font><font color="#0000FF"><b><i>; IntSource,        Label,  Type, ResetFlag?
            </i></b></font><font color="#000080">INT_Handler   TMR2_INT,  _tick,   PBP,  yes
        endm
        INT_CREATE               </font><font color="#0000FF"><b><i>; Creates the interrupt processor
    </i></b></font><font color="#008000"><b>ENDASM
    </b></font><font color="#000080">@    INT_ENABLE  TMR2_INT     </font><font color="#0000FF"><b><i>; Enable Timer 1 Interrupts  
    
    '------- initial -------------------- 
    </i></b></font><b>TMR2_clock   </b><font color="#008000"><b>VAR   WORD
    </b></font><b>OPTION_REG </b>= <font color="#800000"><b>%00000000 
    </b></font><b>TRISB </b>= <font color="#800000"><b>%00000000 </b></font><font color="#0000FF"><b><i>' portb all output
    </i></b></font><b>TRISA </b>= <font color="#800000"><b>%00111111 </b></font><font color="#0000FF"><b><i>' some analog inputs
    </i></b></font><b>ANSEL </b>= <font color="#800000"><b>%00001111 </b></font><font color="#0000FF"><b><i>' some analog inputs
    </i></b></font><b>CMCON </b>= <font color="#800000"><b>%00000111 </b></font><font color="#0000FF"><b><i>' turn of comparators
    </i></b></font><b>ADCON1 </b>= <font color="#800000"><b>%10000000</b></font><font color="#0000FF"><b><i>' right justified
    </i></b></font><b>T2CON </b>= <font color="#800000"><b>%00000011 </b></font><font color="#0000FF"><b><i>' TMR2 prescale 1:16 
    '--------------------------------------------------- 
    </i></b></font><b>TMR2_clock </b>= <font color="#800000"><b>0
        
    </b></font><font color="#008000"><b>LCDOUT </b></font><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>' clear screen
        
    </i></b></font><b>PR2 </b>= <font color="#800000"><b>62 </b></font><font color="#0000FF"><b><i>' load tmr2 PR2 to reset every 1ms
    </i></b></font><b>T2CON</b>.<font color="#800000"><b>2 </b></font>= <font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>' turn TMR2 on
    
    </i></b></font><b>MAIN</b>:
        <font color="#008000"><b>LCDOUT </b></font><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>$80</b></font>, <font color="#008000"><b>DEC </b></font><b>TMR2_clock</b>,<font color="#FF0000">&quot; &quot; </font><font color="#0000FF"><b><i>;---&gt; CHANGED
    </i></b></font><font color="#008000"><b>GOTO </b></font><b>MAIN
    
       
    tick</b>:
        <b>TMR2_clock </b>= <b>TMR2_clock </b>+ <font color="#800000"><b>1
        </b></font><font color="#008000"><b>IF </b></font><b>TMR2_clock </b>= <font color="#800000"><b>500 </b></font><font color="#008000"><b>THEN 
            </b></font><b>TMR2_clock </b>= <font color="#800000"><b>0
            </b></font><font color="#008000"><b>TOGGLE  </b></font><b>LED
             </b><font color="#0000FF"><b><i>' Set some flag..
        </i></b></font><font color="#008000"><b>ENDIF
    </b></font><font color="#000080">@ INT_RETURN
    </font>
    The LED flashes at almost 1 hz, and the numbers on the LCD are counting away.

    I say "almost" due to the Timer 2 prescaler. For better accuracy, use ...
    Code:
    T2CON = %00000001 ' TMR2 prescale 1:4
    PR2 = 250         ' load tmr2 PR2 to reset every 1ms
    Attached Images Attached Images  
    DT

Similar Threads

  1. 2x16 lcd not working with pic16f72
    By vu2iia in forum Schematics
    Replies: 4
    Last Post: - 16th February 2011, 14:59
  2. Blink.Bas on 18f45k20 Newbie seeks working example.
    By DiscoEd in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th December 2009, 03:36
  3. Pic 16 F628A not working
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th March 2009, 12:26
  4. Troubled with Instant Ints and TMR2
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 7th October 2007, 06:23
  5. Hserin not working...
    By robert0 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd August 2005, 12:25

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