'---------------Title-------------- ' File......pwm1.pbp ' Started....4/25/06 ' Microcontroller used: Microchip Technology 16F88 ' microchip.com ' PicBasic Pro Code: micro-Engineering Labs, Inc. ' melabs.com '--------Program Desciption-------- ' pwm1.pbp drives a dc motor at different speeds '----------Related Lesson---------- ' pwm1.pbp is used in the lesson MOTOR CONTROL WITH PWM at: ' http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii21_motor_control_pwm.pdf '------------Comments-------------- ' Rather than reducing the voltage to a motor through ' a potentiometer for example, PWM cuts the time a ' motor receives voltage by turning pulses on and off ' very quickly. This pulse can feed a transistor switch ' which then drives the motor at different speeds. '-----New PicBasic Pro Commands---- ' The PicBasic Pro Compiler Manual is on line at: ' http://www.microengineeringlabs.com/resources/index.htm#Manuals ' ' PWM Pin, Duty, Cycle ' Pin is the output pin for the pulse. ' Duty adjusts the amount of time the pulse is on and off. ' Duty ranges from 0 (0% on time and 100% off time) ' to 255 (100% on time and 0% off time). ' Look around page 122 in the PicBasic Pro Compiler Manual ' The Jameco motors that we tested would not turn with ' duty values less than 140 (a 55% duty cycle). See the ' Jameco gear head motor charts listed in the table. ' Cycle is the number of cycles the pulse is sent. '---------Revision History--------- ' 11/25/07 Change MCU from 16F84A to 16F88 ' 11/25/08 Convert from PIC16F84A to PIC16F88, ' add PIC16F88 oscillator and ANSEL = 0 initializations. '----------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 '-------------Main Code------------ start: pwm 0,255,200 ' Pulse sent to PORTB.0 at a duty ' value of 255(100% duty cycle) for ' 200 cycles. At 4MHz, each cycle ' is about 5 ms long, so the total ' time for 200 cyclesis: ' Total Time = 200*5 ms = 1000 ms or 1 sec. pwm 0,190,200 ' Pulse sent to PORTB.0 at a duty ' value of 190(75% duty cycle) for ' 200 cycles. Motor rotational speed ' approximately 50% of maximum rpm ' for the Jameco motors used. pwm 0,140,200 ' Pulse sent to PORTB.0 at a duty ' value of 140(55% duty cycle) for ' 200 cycles. Motor rotational speed ' approximately 20-24% of maximum rpm ' for the Jameco motors used. goto start ' Jump to start label end