Pulsin within subroutine


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    5

    Unhappy Pulsin within subroutine

    How do you get PBP to actively read the Pulsin variable within a subroutine? This code needs to constantly check the Pulsin variable in order to actively change the motors speed when the toggle on the remote is moved, and I can't get it to work. What am I doing wrong? Help!

    DEFINE OSC 20
    OSCCON.4 = 1
    OSCCON.5 = 1
    OSCCON.6 = 1

    'Define I/O pin names
    motor_dir VAR PORTB.5 'Motor H-Bridge direction line
    motor_pwm VAR PORTB.6 'Motor H-bridge pulse-width-Mod
    stop_button VAR PORTB.0 'Stop button on PORTB.0
    brake1 VAR PORTB.2 'Brake on pin B.2

    'Declare Variables
    motor_speed VAR BYTE 'Motor speed as percentage of maximum
    motion_dir VAR BIT 'Motor direction
    on_time VAR WORD 'PWM ON pulse width
    off_time VAR WORD 'PWM OFF pulse width
    pwm_cycles VAR BYTE '# of PWM pulses sent
    widthx VAR BYTE 'Name to store pulse value

    'Define Constants
    pwm_period CON 50 'Period of each motor PWM signal cycle
    CW CON 1 'Rotate the motor clockwise
    CCW CON 0 'Rotate the motor counter clockwise
    Pulse CON 50

    'Initialize I/O and Variables
    TRISB.6 = 0 'Configure H-bridge DIR pin as an output
    TRISB.5 = 0 'Configure H-bridge PWM pin as an output
    TRISB.1 = 1 'Configure input pulse pin as an input
    TRISB.2 = 0 'Configure brake pin as an output
    motion_dir = CW 'Starting motor direction: CW (forward)
    motor_speed = 50 'Starting motor speed = 50% duty cycle
    Low motor_pwm 'Make sure the motor is off to begin with

    start:
    pulsin PORTB.1,1,widthx
    IF widthx >= 50 && widthx < 70 Then run_motor1
    IF widthx >70 && widthx <90 Then Goto run_motor2
    IF widthx >90 Then
    Goto run_motor3
    Else
    Goto brake
    Endif
    Return

    'Subroutine to run motor at the desired speed and direction
    run_motor1:
    Low Brake1
    'Set the motor direction
    motor_dir = CW
    motor_speed = 50
    'Output PWM signal
    Gosub pwm_periods 'Calculate the on and off pulse widths
    While (widthx >= 50 && widthx < 70)
    Gosub pwm_pulse 'Send out a full PWM pulse
    Wend
    Goto start

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    I can't see all your subroutines, but I certainly hope you are using the hardware PWM routines, not the ones built in to PBP!
    Charles Linquist

  3. #3
    Join Date
    Jan 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Pulsin within subroutine

    Here's the entire code, it it helps...

    DEFINE OSC 20
    OSCCON.4 = 1
    OSCCON.5 = 1
    OSCCON.6 = 1

    'Define I/O pin names
    motor_dir VAR PORTB.5 'Motor H-Bridge direction line
    motor_pwm VAR PORTB.6 'Motor H-bridge pulse-width-modulation line
    stop_button VAR PORTB.0 'Stop button on PORTB.0
    brake1 VAR PORTB.2 'Brake on pin B.2

    'Declare Variables
    motor_speed VAR BYTE 'Motor speed as percentage of maximum (0 to 100)
    motion_dir VAR BIT 'Motor direction (1:CW/Forward 0:CCW/Reverse)
    on_time VAR WORD 'PWM ON pulse width
    off_time VAR WORD 'PWM OFF pulse width
    pwm_cycles VAR BYTE '# of PWM pulses sent during the position control loop
    widthx VAR BYTE 'Name to store pulse value
    run1 VAR BYTE

    'Define Constants
    pwm_period CON 50 'Period of each motor PWM signal cycle (in microsec)
    CW CON 1 'Rotate the motor clockwise
    CCW CON 0 'Rotate the motor counter clockwise
    Pulse CON 50

    'Initialize I/O and Variables
    TRISB.6 = 0 'Configure H-bridge DIR pin as an output
    TRISB.5 = 0 'Configure H-bridge PWM pin as an output
    TRISB.1 = 1 'Configure input pulse pin as an input
    TRISB.2 = 0 'Configure brake pin as an output
    motion_dir = CW 'Starting motor direction: CW (forward)
    motor_speed = 50 'Starting motor speed = 50% duty cycle
    Low motor_pwm 'Make sure the motor is off to begin with

    start:
    pulsin PORTB.1,1,widthx
    IF widthx >= 50 && widthx < 70 Then run_motor1
    IF widthx >70 && widthx <90 Then Goto run_motor2
    IF widthx >90 Then
    Goto run_motor3
    Else
    Goto brake
    Endif
    Return

    'Subroutine to run motor at the desired speed and direction
    run_motor1:
    Low Brake1
    'Set the motor direction
    motor_dir = CW
    motor_speed = 50
    'Output PWM signal
    Gosub pwm_periods 'Calculate the on and off pulse widths
    While (widthx >= 50 && widthx < 70)
    Gosub pwm_pulse 'Send out a full PWM pulse
    Wend
    Goto start

    run_motor2:
    Low Brake1
    'Set the motor direction
    motor_dir = CCW
    motor_speed = 70
    'Output PWM signal
    Gosub pwm_periods 'Calculate the on and off pulse widths
    While (stop_button == 0) 'Until the stop button is pressed
    Gosub pwm_pulse 'Send out a full PWM pulse
    Wend
    Goto start

    run_motor3:
    Low Brake1
    'Set the motor direction
    motor_dir = CW
    motor_speed = 100
    'Output PWM signal
    Gosub pwm_periods 'Calculate the on and off pulse widths
    While (stop_button == 0) 'Until stop button is pressed
    Gosub pwm_pulse 'Send out a full PWM pulse
    Wend
    Goto start

    'Subroutine to calculate the PWM on and off pulse widths based on the desired
    'motor speed
    pwm_periods:
    'Be careful to avoid integer arithmetic
    IF (pwm_period >= 655) Then
    on_time = pwm_period/100 * motor_speed
    off_time = pwm_period/100 * (100 - motor_speed)/100
    Else
    on_time = pwm_period*motor_speed/100
    off_time = pwm_period*(100 - motor_speed)/100
    Endif
    Return

    'Subroutine to output a full PWM pulse based on the data from pwm_periods
    pwm_pulse:
    'Send the ON pulse
    High motor_pwm
    Pauseus on_time

    'Send the OFF pulse
    Low motor_pwm
    Pauseus off_time
    Return

    brake:
    'brake motor
    High brake1
    Goto start

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Is this supposed to run the motor continuously? Again - why aren't you using the hardware PWM facilities provided by most PICs?
    Charles Linquist

  5. #5
    Join Date
    Jan 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Pulsin within subroutine

    The program is supposed to run the motor until the remote control lever isn't pushed the right distance, creating a different pulse width. The PWM is just fine, that isn't the problem. So far, when the lever is pushed over to the area of speed two or three, it will run the motor until the stop button is pushed. I need it to run the motor until the lever is no longer creating the right pulse. This is why I've attempted to change run_motor1. Since this program needs to be controlled by remote control only, I nee the run_motor1 subroutine to recognize the widthx value, and return to main when the lever is in a different position. I haven't had luck with the hardware PWM or HPWM command. Calculating the pulse on and off time is more reliable. I just can't make it recognize the pulse when in the subroutine, in order to return to the main program.

  6. #6
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    I don't see why you have a RETURN at the position shown below. I can imagine that the RETURN is popping a strange address off the stack.


    start:
    pulsin PORTB.1,1,widthx
    IF widthx >= 50 && widthx < 70 Then run_motor1
    IF widthx >70 && widthx <90 Then Goto run_motor2
    IF widthx >90 Then
    Goto run_motor3
    Else
    Goto brake
    Endif
    Return <--- why this?
    Charles Linquist

  7. #7
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Oversight or typo?

    Although I didn't try to completely understand the routine, I did notice something off...
    You have RETURN at the end of the start loop but don't see where it was GOSUB'd from. Is it suppose to be GOTO?
    Code:
    start:
    pulsin PORTB.1,1,widthx
    IF widthx >= 50 && widthx < 70 Then run_motor1 
    IF widthx >70 && widthx <90 Then Goto run_motor2
    IF widthx >90 Then
    Goto run_motor3
    Else
    Goto brake
    Endif
    Return
    Just a thought and not sure if it would even make a difference.
    Louie

Similar Threads

  1. Replies: 17
    Last Post: - 12th April 2014, 02:17
  2. Better understanding PULSIN
    By Wirecut in forum mel PIC BASIC
    Replies: 12
    Last Post: - 29th June 2008, 10:17
  3. Funny PULSIN values: what is going on???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th April 2008, 08:02
  4. Pulsin 16F819 problem
    By rekcahlaer in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th April 2007, 13:52
  5. PULSIN and RCTIME
    By Dwayne in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th November 2004, 14:45

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts