'---------------Title-------------- ' File......pwm_sn754410_1.pbp ' Started....1/10/09 ' Microcontroller used: Microchip Technology 16F88 ' microchip.com ' PicBasic Pro Code: micro-Engineering Labs, Inc. ' melabs.com '--------Program Desciption-------- ' Program uses the command PAUSEUS to generate its own ' PWM signals to drive the SN574410 H-bridge motor driver. ' FOR..NEXT loop changes the motor speed. '---------Related Programs--------- ' Robotic application used in proportional1.pbp at: ' http://www.cornerstonerobotics.org/picbasic_robots1.php '---Review PicBasic Pro Command---- ' The PicBasic Pro Compiler Manual is on line at: ' http://www.microengineeringlabs.com/resources/index.htm#Manuals ' ' PAUSEUS Period ' Pause the program for Period in microseconds ' Look around page 113 in the PicBasic Pro Compiler Manual '---------PIC Connections---------- ' 16F88 Pin Wiring ' --------- ---------- ' RB0 PWM Motor 2 ' RB1 Direction Motor 2 ' RB2 PWM Motor 1 ' RB3 Direction Motor 1 ' RB4 LED1 through 150 ohm resistor ' RB5 LED2 through 150 ohm resistor ' ' See schematic at: ' http://www.cornerstonerobotics.org/schematics/pic_programming_pwm_sn754410.pdf '------------Variables------------- c0 var word ' WORD to store counter, c0 pulse_width var word ' Word to store pulse_width '----------Initialization---------- ANSEL = 0 ' Configure all pins to digital ' operation since not using ADC ' (Analog to Digital Converter) OSCCON = $60 ' Sets the internal oscillator in the ' 16F88 to 4 MHz TRISB = %00000000 ' Sets all pins in PORTB as outputs PORTB = %00000000 ' Sets all PORTB pins to LOW '-------------Main Code------------ start: high 4: high 5: pause 500: low 4: low 5 ' Blinks LEDs connected to RB4 ' and RB5 for 500 ms. gosub forward ' Jump to subroutine forward high 4: pause 100: low 4 ' Blinks LED connected to RB4 ' for 100 ms. gosub backup ' Jump to subroutine backup high 5: pause 100: low 5 ' Blinks the LED connected to RB5 ' for 100 ms. gosub turn ' Jump to subroutine turn goto start ' Jump to start label end forward: ' Subroutine forward for c0 = 5000 to 20000 step 50 ' FOR..NEXT loop counts from 5000 ' to 20000 in steps of 50. pulse_width = c0 ' Assigns the current value of c0 to ' the variable pulse_width. high 1 : high 3 ' Set the direction of Motors 2 and 1 ' to forward high 0 : high 2 ' Leading edge of pulse into PWM input ' pins of SN754410 H-bridge for ' Motors 2 and 1. pauseus pulse_width ' Length of pulse_width in microseconds ' HIGH for pulse_width in microseconds. low 0 : low 2 ' Falling edge of pulse pauseus 20000-pulse_width ' LOW for 20 ms period - pulse_width (in ' microseconds) next ' Loop back to FOR command for next c0 return ' Return to next line in the main program backup: ' Subroutine backup for c0 = 5000 to 20000 step 50 ' FOR..NEXT loop counts from 5000 ' to 20000 in steps of 50. pulse_width = c0 ' Assigns the current value of c0 to ' the variable pulse_width. low 1 : low 3 ' Set the direction of Motors 2 and 1 ' to reverse high 0 : high 2 ' Leading edge of pulse into PWM input ' pins of SN754410 H-bridge for ' Motors 2 and 1. pauseus pulse_width ' Length of pulse_width in microseconds ' HIGH for pulse_width in microseconds. low 0 : low 2 ' Falling edge of pulse pauseus 20000-pulse_width ' LOW for 20 ms period - pulse_width (in ' microseconds) next ' Loop back to FOR command for next c0 Return ' Return to next line in the main program turn: ' Subroutine turn for c0 = 5000 to 20000 step 50 ' FOR..NEXT loop counts from 5000 ' to 20000 in steps of 50. pulse_width = c0 ' Assigns the current value of c0 to ' the variable pulse_width. low 1 : high 3 ' Set the direction of Motors 2 to reverse ' and Motor 1 to forward high 0 : high 2 ' Leading edge of pulse into PWM input ' pins of SN754410 H-bridge for ' Motors 2 and 1. pauseus pulse_width ' Length of pulse_width in microseconds ' HIGH for pulse_width in microseconds. low 0 : low 2 ' Falling edge of pulse pauseus 20000-pulse_width ' LOW for 20 ms period - pulse_width (in ' microseconds) next ' Loop back to FOR command for next c0 Return ' Return to next line in the main program