If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Registered Member #79
Joined: Thu Feb 09 2006, 11:35AM
Location: Arkansas
Posts: 673
I'm trying to multiplex stuff by using the Timer2 on the PIC16F684. However, it doesn't ever seem to interrupt. I'm trying to run a two strings of LED's alternatively with alternating displays. I think the code is pretty self-explainatory.
I've double-checked all of the information I can find on interrupts/TMR2, etc, and my settings appear correct.
What did I do wrong in my code?
list p=16F684 ; list directive to define processor #include <p16F684.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
; '__CONFIG' directive is used to embed configuration word within .asm file. ; The lables following the directive are located in the respective .inc file. ; See data sheet for additional information on configuration word settings.
; example of using Shared Uninitialized Data Section INT_VAR UDATA_SHR 0x71 w_temp RES 1 ; variable used for context saving status_temp RES 1 ; variable used for context saving
LEDPORT equ PORTC LEDPORT2 equ PORTA DRV1 equ 0
;
**************************************************
******************** RESET_VECTOR CODE 0x000 ; processor reset vector goto main ; go to beginning of program
cblock 0x20 count1 counta countb ones tens end
c
INT_VECTOR CODE 0x004 ; interrupt vector location movwf w_temp ; save off current W register contents movf STATUS,w ; move status register into W register movwf status_temp ; save off contents of STATUS register
btfss PIR1,TMR2IF ; Flag set if TMR2 interrupt goto INTX ; Jump if not timed out
; Timer (TMR2) timeout every 16 milliseconds
bcf PIR1,TMR2IF ; Clear the calling flag
; isr code can go here or be located as a call subroutine elsewhere btfss LEDPORT2, DRV1 call do_tens call do_ones
INTX movf status_temp,W ; retrieve copy of STATUS register movwf STATUS ; restore pre-isr STATUS register contents swapf w_temp,F swapf w_temp,W ; restore pre-isr W register contents retfie ; return from interrupt
main ; remaining code goes here movlw 07h ;turn off the comparators movwf CMCON0 movwf CMCON1
bsf STAT
US, RP0 ;setup ports as all outputs movlw 0x00 movfw TRISA movfw TRISC bc
f STATUS, RP0
; Set up Timer 2.
movlw b'01111110' ; Post scale /16, pre scale /16, TMR2 ON ;movlw b'00010110' ; Post scale /4, pre scale /16, TMR2 ON movwf T2CON
bsf STATUS, RP0 ;select bank 1
movlw .249 ; Set up comparator movwf PR2
bsf PIE1,TMR2IE ; Enable TMR2 interrupt
bcf STATUS, RP0 ;select bank 0
; Global interrupt enable
bsf INTCON,PEIE ; Enable all peripheral interrupts bsf INTCON,GIE ; Global interrupt enable
Registered Member #30
Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
The PIE1 register is in memory bank 1, so you have to use the bank select bits to access it. (and remember to put them back afterwards.) You didn't use them, so it never gets written and your interrupt stays disabled. I like to use the banksel macro:
The banked architecture is one of the PIC's pains in the ass. You have to remember which registers can't be accessed from bank 0. It still catches me out after programming PICs in assembler for about 7 years.
Registered Member #27
Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
The banks alone is a reason to not use a PIC. Not all PICs have the registers in the same banks so it is almost impossible to get it right all the time.
The banksel method that Steve uses is the best method to make readable code that will work on different PIC models.
Don't use interrupts unless there is a reason to because they are an extra source of problems. Both timing and your simple mental model of the PIC goes out the window when you use interrupts.
Use interrupts when you need to save power or minimise response time.
Registered Member #79
Joined: Thu Feb 09 2006, 11:35AM
Location: Arkansas
Posts: 673
If anyone is interested, my biggest problem was that I was using the "template file" from MPLAB, and it's interrupt variables were overlapping my own variables.
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.