Menu

Need interrupt example(s)

Help
fisyon
2013-12-06
2013-12-15
  • fisyon

    fisyon - 2013-12-06

    hi,
    i am using pic16f628a to drive a step motor. portb.4 to portb.7 goes to coils.
    it has 4 switches to control direction, step durations and limiting. (portb.0 to portb.3)
    it works fine with my novice code but i have to press and hold ~5-6 seconds to read status of the switches. because i have to wait for all 4 steps (~1,7 secs per step, used "wait" routines).

    i thought i need to use interrupts but very confused about usage. (only example in the help file was about a timer interrupt)

    i need clear examples (as for dumps as possible :P) if there is a software solution.

     
  • mmotte

    mmotte - 2013-12-06

    This page has helped me before.

    http://gcbasic.sourceforge.net/newfiles/help/oninterrupt.htm

    PORTBChange would be my first look. Put the buttons here on portB and move you outputs.

    GL
    M

     
  • mmotte

    mmotte - 2013-12-07

    16F628 has portb interrupt capable on portB.4 to PortB.7.

    This code operated a motor on my telescope using a h bridge chip
    UDN2993

         'A program to run motor
    
         'Chip model
         #chip 16F886, 4
    
         'Set the pin directions
         #define RAplus PORTB.2 
         #define RAstop PORTB.3
         #define en1 PORTC.2 
         #define dir1 PORTC.3 
         #define en2 PORTC.4 
         #define dir2 PORTC.5 
           #define decup PORTB.4
         #define decdwn PORTB.5 
         dir PORTC out
         dir PORTA in
         dir PORTB in
         #define PWM_Out1 PORTC.2       
         #define PWM_Out2 PORTC.4
    
         MOVLW 0x3c 
         MOVWF IOCB  ' selects pin that will interrupt on portb
    
         On Interrupt PORTBChange Call movefast
         'Main routine
         Start:
    
            wt = ReadAD(AN0)  'interval value
    
            SET dir1 OFF    ' run motor for a couple secs
            'SET en1 ON
            PWMOut 1, 100, 5
            'wait 1 s
            SET dir1 Off
            SET en1 OFF
    
            for ijk=0 to wt   ' wait analog value secs  0-255
    
            wait 10 10ms
    
            NEXT
    
         goto Start
    
         sub movefast
            if RAplus = 0 then
                DO UNTIL RAplus = 1
                SET dir1 OFF    ' run RA motor 
                PWMOut 1, 100, 5
                loop        
            end if
    
            if RAstop = 0 then 
                DO UNTIL RAstop = 1
                SET dir1 ON ' run motor for a couple secs
                PWMOut 1, 100, 5
                loop
            end if
    
                 if decup = 0 then 
                DO UNTIL decup = 1
                SET dir2 OFF    ' run motor for a couple secs
                PWMOut 2, 100, 5
                loop
            end if
            if decdwn = 0 then
                DO UNTIL decdwn = 1
                SET dir2 ON ' run motor for a couple secs
                PWMOut 2, 100, 5
                loop
            end if
    
         End Sub    
    

    It gives an example of interrupt PortBChange.
    RA is ascension Hour angle , dec declination

    I am sorry but I cannot keep the indents in the formatting. yes i read help
    GL
    M

     
  • fisyon

    fisyon - 2013-12-11

    thanks for the replies.
    actually i am using that step motor on a single arm barn door tracker.
    so, as i understand "portbchange" can call a subroutine.

    what if some portB pins changes as output? will that cause a conflict?

    as i understand:
    ...
    on interrupt portbchange call swchange
    ...
    swchange
    ...
    end sub

    is this usage enough?

    if so i really don't get these lines:

    ...
    MOVLW 0x3c
    MOVWF IOCB ' selects pin that will interrupt on portb
    ...

     
  • mmotte

    mmotte - 2013-12-11

    IOCB is a register for Interrupt on change portB in the 16f886 that allows you to select which bit(s) you want to cause the interrupt. On the 16F628A that register(96) is not implemented so I assume portB.4 5 6 or 7 change will cause the interrupt. No register IOCB then you don't need these two lines.

    Also I think you could use portB.0-3 for other purposes like outputs

    73
    M

     
  • mmotte

    mmotte - 2013-12-12

    fisyon,
    I was working on a barndoor to track the stars last month. I used a geared down 4096 stepper from china off of ebay. GCbasic into a 16F886. Finally got the timing pretty good and could take 10 minute exposures. Then it turned cold and windy and cloudy.
    73
    M

     
  • fisyon

    fisyon - 2013-12-13

    your tracker looks nice, congratulations.

    i am using 48 stepper which is very fast for the tracker(the length of the arm=~210mm), then i have to drive very slow (~1750 miliseconds per step).
    i can't see polaris because of buildings then i am using "blind" polar alignment. i have ~5 minutes without star trails which is more than enough for DSLR kit lens (18-55 mm at 55 mm). clouds are always pain in the butt :)

    however, my problem is controlling the pic via switches. it doesn't matter which pin will cause the interrupt (Portb0-3).
    unfortunately i can't find any sample about interrupt with switches.
    of course i am talking about GCBasic examples.

     
  • mmotte

    mmotte - 2013-12-14

    /~~~~
    ::::GCBasic
    'A program to Run a 4096 step geared step motor on a 10-32 threaded rod
    ' for a "barn Door" tracker to hold a camera and track the stars
    'by Mike Otte
    '2013

    'Chip model
    #chip 16F886, 8

    dim Full(5)
    Full(0) = b'00000110'
    Full(1) = b'00000011'
    Full(2) = b'00001001'
    Full(3) = b'00001100'

    dim RFull(5)
    RFull(3) = b'00000110'
    RFull(2) = b'00000011'
    RFull(1) = b'00001001'
    RFull(0) = b'00001100'

    ' my momentary switches
    #define Home PORTC.0
    #define RAstop PORTC.1
    #define RAplus PORTC.2
    #define limit PORTC.3
    'define PORTC upper 4 bits for adjusting the time in additional millisec / step

    'Set the pin directions
    dir PORTB out
    dir PORTC IN
    ' i am not using interrupt because this motor steps fast
    'On Interrupt PORTBChange Call buttons

    wttm = 17 'wait time was 25 msec
    wtmodify= PORTC
    wtmodify = wtmodify AND 0xf0 ' mask off the upper four bits
    rotate wtmodify right simple 'shift them to the lower four bits
    rotate wtmodify right simple ' i put pullups and board withjumper pins to act as switches
    rotate wtmodify right simple
    rotate wtmodify right simple
    wttm = wttm + wtmodify ' add or subtract for tuning delay from portc upper four bits
    ' wttm (wait time) now has the total value
    PortB = 0 'initialize
    myj=0
    wait 1 s
    'Main routine

    Start:
    myStep = myj mod 4
    thisStep = Full(myStep)
    PortB = thisStep
    wait wttm ms
    myj++
    gosub buttons

    goto Start

    sub buttons
    if RAplus = 0 then ' these were going to make it go faster or slower
    DO UNTIL RAplus = 1
    myStep = myj mod 4
    PortB = Full(myStep)
    wait 7 ms
    myj++
    loop
    end if

    if RAstop = 0 then   ' but i don't use these two switches
      DO UNTIL RAstop = 1
        myStep = myj mod 4
            PortB =  RFull(myStep)
             wait  7 ms
        myj++
       loop
    end if
    

    '*
    'I really only use the following two switches
    '"home" switch sends the barn door towards home (closed limit)
    ' the "limit" stops it when it get home
    'and returns it to going forward again
    '
    **
    if home = 0 then
    DO UNTIL limit = 0
    myStep = myj mod 4
    PortB = RFull(myStep)
    wait 2 ms
    myj++
    loop
    end if

    End Sub
    ~~~~/

    this is the code for my barndoor controller.

    If you use interrupts for switches your switches must be on portB inputs portB.4, portB.5, portB.6,and or portB.7

    Your motor must be on some other pins

    73
    M

     
  • fisyon

    fisyon - 2013-12-14

    so, as i understand, i have to change my circuit completely... :(

    it's worthless to do that at this moment. maybe later with a better step motor...

    thanks a lot for your help

     
  • Max

    Max - 2013-12-15

    I have problem too with interrupt .... i am using 16F8768A and interupt on port B doesn' work. the LED is in PORTA.0 and switch is in PORTB.2 and iterrupt is never generated .

    where is the problem ???


    ;Chip Settings
    #chip 16F876A,20
    #config OSC=HS
    
    Dir PORTA.0 out  ' LED
    Dir PORTB.2 in   ' SWITCH
    
    On Interrupt PORTBChange Call LED
    
    inizio:
    goto inizio
    
    Sub LED
        PulseOut PORTA.0, 1 sec
    End Sub
    

    please help :)

     
  • mmotte

    mmotte - 2013-12-15

    Please read the PIC16F87Xa data sheet.

    "Four of the PORTB pins, RB7:RB4, have an interrupt-
    on-change feature. Only pins configured as inputs can
    cause this interrupt to occur (i.e., any RB7:RB4 pin
    configured as an output is excluded from the interrupt-
    on-change comparison).
    "

    Also read,
    "clear the
    interrupt in the following manner:
    a) Any read or write of PORTB. This will end the
    mismatch condition.
    b) Clear flag bit RBIF.
    "
    I am not sure if GCBasic compiler when using this syntax automatically clears RBIF but I think it does?

    73
    M
    This should have been in a new thread.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.