Learning pics, trouble with interrupts.

Discussion in 'Embedded Systems and Microcontrollers' started by jdraughn, Jul 13, 2009.

  1. jdraughn

    Thread Starter Active Member

    Jan 30, 2009
    33
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2.  
    3. Start: ;Start of main program
    4.  BCF T0CON,T0CS ;clear the source edge bit (timer mode)
    5.  BSF T0CON,PSA ;dont assign a prescaler
    6.  BSF INTCON2,TMR0IP ;make it high priority
    7.  BSF RCON,IPEN ;enable priority lvls on all interupts
    8.  BSF INTCON,GIE ;turn on interupts
    9.  BSF INTCON,TMR0IE ;turn on interups for timer0
    10.  BSF T0CON,TMR0ON ;turn on the timer
    11.  GOTO MainLoop
    12.  
    13. MainLoop:
    14.  
    15.  GOTO MainLoop
    16.  
    17.  
    Am I wrong in assuming that when animating in debug mode, once the timer bit overflows the green arrow should move up to:
    Code ( (Unknown Language)):
    1.  
    2. HighInt:
    3. ; *** high priority interrupt code goes here ***
    4.   BCF INTCON,T0IF ;clear the interupt overflowed flag
    5.  
    6.   retfie FAST
    7.  
    Which was set further up via:
    Code ( (Unknown Language)):
    1.  
    2. HI_INT_VECTOR CODE 0x0008 ;High priority interrupt vector
    3.   bra HighInt  ;go to high priority interrupt routine
    4.  
    5.  
    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.
     
  2. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    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.


    Code ( (Unknown Language)):
    1. ;;;;;;; Vectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    2.  
    3.         org  0x0000             ;Reset vector
    4.         nop
    5.         goto  Mainline
    6.  
    7.         org  0x0008             ;High priority interrupt vector
    8.         goto  HiPriISR          
    9.  
    10.         org  0x0018             ;Low priority interrupt vector
    11.         goto  $                 ;Trap
    12.  
    13.  
    14. Mainline
    15.         rcall Initial  
    16. Loop
    17.         goto Loop
    18.  
    19. Initial
    20.         ;Initialize all timers and interrupts here like you did before
    21.         return
    22.  
    23. ;;;;;;; HiPriISR interrupt service routine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    24.  
    25. HiPriISR                        ;High-priority interrupt service routine
    26.         ;Do stuff here and clear interrupt flag
    27.         retfie  FAST            ;Return and restore STATUS, WREG, and BSR
    28.                                 ; from shadow registers
    29.  
    30.  
     
  3. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    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.

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

    Thread Starter Active Member

    Jan 30, 2009
    33
    0
    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).
     
  5. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5

    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.
     
  6. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    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.
     
Loading...