Menu

Counting Interrupts

Dirk
2014-02-06
2014-02-08
  • Dirk

    Dirk - 2014-02-06

    Here is my first real project after playing around with LEDs
    I have a DC motor with hall sensor and a L297 based stepper driver.
    The motor speed can vary from 100 to 3000 RPM.
    Calculating the actual speed would be nice but is not necessary.
    What I´m trying to do is getting far beyond my present knowledge.

    Thanks for any help or hints..

    ;Chip Settings
    #chip 18F452,20
    
    ;Defines (Constants)
    #define LCD_NO_RW 'Needed when RW is grounded
    #define LCD_IO 4
    #define LCD_RS PORTE.0
    #define LCD_Enable PORTE.1
    #define LCD_DB4 PORTD.4
    #define LCD_DB5 PORTD.5
    #define LCD_DB6 PORTD.6
    #define LCD_DB7 PORTD.7
    #define Stepper PORTC.1
    #define Enable PORTB.0
    #define Direction PORTB.1
    
    ;Variables
    Dim Pulse As word
    
    ;Interrupt Handlers
    On Interrupt CCP1 Call Freq
    
    dir Stepper out
    dir Enable out
    dir Direction out
    initCCP1
    start:
    Locate 1,0
    Print (Pulse)
    Repeat 5
            set Stepper on
    Wait 2 ms
        set Stepper off
        Wait 2 ms
    end repeat
    goto start
    
    Sub initCCP1
        dir PortC.2 in ;pulse input Pin RC2
        T1CON = b'00110001'
        CCP1CON = b'00000101' ;start pulse capture on rising edge
        StartTimer 1
    End Sub
    
    Sub Freq
    TMR1L =0 ;reset timer 1
    TMR1H =0
    Pulse_H = CCPR1H ;read pulse width and store
    Pulse_L = CCPR1L
    CCP1IF = 0 ;clear capture flag
    End Sub
    
     

    Last edit: Dirk 2014-02-07
  • kent_twt4

    kent_twt4 - 2014-02-07

    You are updating a continuous rotation servo every 20ms? If it is a straight DC motor then I would us HPWM.

    For others, here is the link that discusses using a CCPX module to get a frequency http://sourceforge.net/p/gcbasic/discussion/579126/thread/1c752e92/?limit=25#1ac9
    Using select case or math could be faster than using table values.

    Here's a thought on using CCPX for rpm:
    100rpm is going to be 1.667Hz or a 600ms period. At best with a 20Mhz osc and prescale of 8, you will get a period of 0.10486 sec or 572 rpm. With a 4Mhz osc you get 114rpm, so that would really help.

    Here's some code updating a servo with the Timer1Overflow interrupt every 20ms.

    'For a servo you want to update every 20ms so set up Timer1
    'to overflow by presetting the register values TMR1H and TMR1L
    'for 8 Mhz clk overflow happens every (65535 / 8000000) x 4 = 0.032768 sec
    'solve for 20 ms: (0.02 x 8000000) / 4 = 40000
    'TMR counts down from top so; 65535 - 40000 = 25535 or 0x636F
    
    #chip 12F683, 8
    #config osc = int
    #define LED GPIO.4
    
    Dir LED Out
    On Interrupt Timer1Overflow Call UpDateServo
    InitTimer1 Osc, PS1_1
    TMR1L = 0x6f
    TMR1H = 0x63
    startTimer 1
    
    Main:
    If Update = True Then
       Update = False
       PulseOut led, 1500 us
    end if
    goto Main
    
    Sub UpdateServo
    Update = True
    TMR1L = 0x6f
    TMR1H = 0x63
    End Sub
    
     
  • mmotte

    mmotte - 2014-02-07

    First thing that strikes me is that a L297 is a stepper controller. You still need a L298 bridge driver for the motor or transistors.

    Does your hall sensor pulse once per revolution?

    So if you aren't interested in speed then you must be interested in position? You could run the sensor into a counter input to the PIC. But if you reverse the motor you still would count up. Then you need a second sensor and do some logic to determine direction or I guess you are telling the L297 direction so now i am stuck on how to count down in a PIC counter.

    Have fun.
    M

     
    • Dirk

      Dirk - 2014-02-08

      Hi mmotte,

      we are talking abaut two different motors.
      The first one is a brushed 12v 200W dc-motor rotating clockwise only
      with its own PWM driver board. Speed can be adjusted with a pot.
      Perhaps, in future I´ll use a TC44xx and a suitable H-Bridge to control the motor.
      The second motor is a NEMA17 bipolar stepper.
      Logic circuit L297 and two L6203 drivers.
      For each revolution of the DC-Motor, max speed is 3000RPM, the stepper
      has to do 5 steps and of course reverse after a specific count.
      If my math is right, we have 20ms to perform 5 steps in case the DC-motor is running at max speed.

      Thanks a lot and have a nice weekend.

      D

       

Log in to post a comment.