Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 16
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Vaxian (17)


Next birthdays
05/21 Dalus (34)
05/21 Kizmo (37)
05/22 Skynet (32)
Contact
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.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

PIC help please

Move Thread LAN_403
Part Scavenger
Mon Aug 07 2006, 01:11AM Print
Part Scavenger 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.

__CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF



;***** VARIABLE DEFINITIONS (examples)

; 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

bcf STATUS, RP0 ;select bank 0


Loop



movlw 0x00
movwf ones
movlw 0x00
m ovwf tens
call Delay

movlw b'000001'
movwf ones
movlw b'100001'
movfw tens
call Delay

movlw b'000010'
movwf ones
movlw b'010010'
movfw tens
call Delay

movlw b'000100'
movwf ones
movlw b'001100'
movfw tens
call Delay

movlw b'001000'
movwf ones
movlw b'010010'
movfw tens
call Delay

movlw b'010000'
movwf ones
movlw b'100001'
movfw tens
call Delay

movlw b'100000'
movwf ones
movlw b'000000'
movfw tens
call Delay
goto Loop

do_tens
bsf LEDPORT2, DRV1
movfw tens
movwf LEDPORT
retlw 0x00

do_on es
bcf LEDPORT2, DRV1
movfw ones
movwf LEDPORT
retlw 0x00

Delay
movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
retlw 0x00

end



BTW, if you can't tell already, I'm new at this so it's likely to be something stupid.

Thanks
Back to top
Steve Conner
Mon Aug 07 2006, 09:53AM
Steve Conner 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:

banksel PIE1
bsf PIE1,TMR2IE ; Enable TMR2 interrupt
banksel 0

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.
Back to top
Part Scavenger
Mon Aug 07 2006, 10:05AM
Part Scavenger Registered Member #79 Joined: Thu Feb 09 2006, 11:35AM
Location: Arkansas
Posts: 673
Thanks! I'll try that.

I also noticed I've got code inbetween the interrupt and main code, and that it's skipped over. angry

Thanks
Back to top
Bjørn
Mon Aug 07 2006, 01:49PM
Bjørn 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.
Back to top
Part Scavenger
Fri Sept 22 2006, 02:25PM
Part Scavenger 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. angry
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
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.