Learning pics, trouble with interrupts.

Thread Starter

jdraughn

Joined Jan 30, 2009
33
Hi, I am learning how to program pics and am having trouble with making interupts happen on a PIC18F6720.

I set all the bits that I think I need to enable functionality and then go into a endless loop and watch my variable values with some watches. I can see TMR0IF being set to a 1 when my timer overflows, but once it overflows it will not automatically branch to either my high or low priority address where it would clear the TMR0IF bit and execute the code that happens on a timer overflow.

Here is the meat of my code:
Rich (BB code):
Start: ;Start of main program
 BCF T0CON,T0CS ;clear the source edge bit (timer mode)
 BSF T0CON,PSA ;dont assign a prescaler
 BSF INTCON2,TMR0IP ;make it high priority
 BSF RCON,IPEN ;enable priority lvls on all interupts
 BSF INTCON,GIE ;turn on interupts
 BSF INTCON,TMR0IE ;turn on interups for timer0
 BSF T0CON,TMR0ON ;turn on the timer
 GOTO MainLoop
 
MainLoop:
 
 GOTO MainLoop
Am I wrong in assuming that when animating in debug mode, once the timer bit overflows the green arrow should move up to:
Rich (BB code):
HighInt:
; *** high priority interrupt code goes here ***
  BCF INTCON,T0IF ;clear the interupt overflowed flag
 
  retfie FAST
Which was set further up via:
Rich (BB code):
HI_INT_VECTOR CODE 0x0008 ;High priority interrupt vector
  bra HighInt  ;go to high priority interrupt routine
I am brand new to this and have been following the tutorial at http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_A_6.pdf to the best of my ability. There are so many differences though that sometimes I have some trouble converting the code for his PIC16F to PIC18F code, plus I have a later version of MPLAB (8.33) so there are differences in syntax which I have to try and covert. Just doing the best I can.
 

Vaughanabe13

Joined May 4, 2009
102
I think you are generally doing everything right. You might try some of these changes and see if this works. This is generally how I program the 18F pics in assembly. I think there may be a problem with your interrupt vector setup and program flow, which is causing the PC to be loaded incorrectly on an interrupt (timer overflow in your case). *Edit* Now that I think about, check your datasheet and make sure you are setting/clearing the correct bits for TMR0 overflow. I think Timer 0 can have several different types of interrupts associated with it, so make sure you explicitly set it to interrupt on overflow.


Rich (BB code):
;;;;;;; Vectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        org  0x0000             ;Reset vector
        nop
        goto  Mainline

        org  0x0008             ;High priority interrupt vector
        goto  HiPriISR          

        org  0x0018             ;Low priority interrupt vector
        goto  $                 ;Trap


Mainline
        rcall Initial   
Loop
        goto Loop

Initial
        ;Initialize all timers and interrupts here like you did before
        return

;;;;;;; HiPriISR interrupt service routine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

HiPriISR                        ;High-priority interrupt service routine
        ;Do stuff here and clear interrupt flag
        retfie  FAST            ;Return and restore STATUS, WREG, and BSR
                                ; from shadow registers
 

Vaughanabe13

Joined May 4, 2009
102
Also, another thing: Did you include code to set the configuration bits on the PIC? I realize you may have left that out for simplicity but make sure you have it in there. Here is an example of setting up the configuration bits of the PIC18F452. Change to suit your PIC, and research if you don't know what it means/does.

Rich (BB code):
       list  P=PIC18F452, F=INHX32, C=160, N=0, ST=OFF, MM=OFF, R=DEC, X=ON
        #include P18F452.inc
        __CONFIG  _CONFIG1H, _HS_OSC_1H  ;HS oscillator
        __CONFIG  _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_42_2L  ;Reset
        __CONFIG  _CONFIG2H, _WDT_OFF_2H  ;Watchdog timer disabled
        __CONFIG  _CONFIG3H, _CCP2MX_ON_3H  ;CCP2 to RC1 (rather than to RB3)
        __CONFIG  _CONFIG4L, _LVP_OFF_4L  ;RB5 enabled for I/O
        errorlevel -314, -315          ;Ignore lfsr messages
 

Thread Starter

jdraughn

Joined Jan 30, 2009
33
Thanks for the reply, I will take another look and make sure it's set to interupt on timer overflow. I did set configuration bits, and that is for example, where some differences reside between the tutorials on the net and the new 8.33 IDE. Your setting configs with __config, but it's done differently now.

I have decided I had enough with programming in assembly and am going to go to c code, but have been struggling to get it all installed properly. I don't know why microchip has to make everything so complicated. With visual studio I install it and go, with MP Lab I install it, install some other stuff, find out I don't have a license, do research, install some other stuff, still can't find the files I need to compile, do research, see lots of other people having the same trouble I am, download some more files, ect... and im on cellular dial up so this isin't exactly happening quickly.

Sorry for the delayed response too, I forgot which forums I asked this on and it took forever to find it. I had done so much pic related research over the last few days I couldint find this website out of all the hundreds I had listed in my browser history.

I was going to look around for the answer to this question, but is it possibly to swap between c code and assembly on the fly in the same file? I am so used to doing some things in assembly that I would like to continue to at least do some of my programming in assembly to keep the syntax and all fresh in my mind (just for my own personal learning purposes).
 

Vaughanabe13

Joined May 4, 2009
102
Thanks for the reply, I will take another look and make sure it's set to interupt on timer overflow. I did set configuration bits, and that is for example, where some differences reside between the tutorials on the net and the new 8.33 IDE. Your setting configs with __config, but it's done differently now.

I have decided I had enough with programming in assembly and am going to go to c code, but have been struggling to get it all installed properly. I don't know why microchip has to make everything so complicated. With visual studio I install it and go, with MP Lab I install it, install some other stuff, find out I don't have a license, do research, install some other stuff, still can't find the files I need to compile, do research, see lots of other people having the same trouble I am, download some more files, ect... and im on cellular dial up so this isin't exactly happening quickly.

Sorry for the delayed response too, I forgot which forums I asked this on and it took forever to find it. I had done so much pic related research over the last few days I couldint find this website out of all the hundreds I had listed in my browser history.

I was going to look around for the answer to this question, but is it possibly to swap between c code and assembly on the fly in the same file? I am so used to doing some things in assembly that I would like to continue to at least do some of my programming in assembly to keep the syntax and all fresh in my mind (just for my own personal learning purposes).

I will agree with you that getting a C compiler set up and working correctly is not fun in MPLAB if you are using a 3rd party compiler. However, once you get it working it is "set and forget" and you don't really need to set it up again or change it. I try to stick to PIC18F chips for that reason because I like Microchip's C18 compiler which will only work for 18F chips. For chips in the midrange line (16F, etc.) I usually try to stick to assembly, but that is all personal preference.

Now about your 'switching between C and assembly' question, the answer is yes and no. What you can do is most C compilers provide an assembly option for you to write small blocks in assembly code. The C18 compiler, for example, lets you write something like this:

_asm movlw 0b10101010
movwf PORTB,0 _endasm

The _asm _endasm tags will tell the compiler that everything in between them is assembly code, so you can effectively program in assembly. However, this is not encouraged in most cases, and can get you very turned around and confused if you use it a lot. I would highly recommend spending time learning C and try to stick to one language or the other at a time. It will be much less confusing in the end.

About __config, this method does work. There are different ways of writing it but you are basically doing the same thing. As long as you have it working for you you can ignore what I wrote, I was just making sure you had it in your program.
 

Vaughanabe13

Joined May 4, 2009
102
I reread your original post and realized you are using an 18F chip, so I would highly recommend downloading the "student" version of the C18 compiler from Microchip and using that. It will work for x number of days in the full retail mode, and after the trial has expired it will still work well enough for a beginner. All it does is disable some extra features (that you probably won't need) and disable code optimizing. But for a student trying to learn how to program in C it will work for you just fine.

And I am almost sure your timer problem has to do with the way you set it up. You definitely need to read the section of the datasheet for your timer and there will be some bits you need to set to tell the program that you want the timer to send an interrupt on overflow. It should be as simple as that, but I don't have the time to look it up for you right now.
 
Top