'---------------Title-------------- ' File......serin2_pwm.pbp ' Started....12/23/08 ' Microcontroller used: Microchip Technology PIC16F88 ' microchip.com ' PicBasic Pro Code: micro-Engineering Labs, Inc. ' melabs.com '--------Program Desciption-------- ' Program uses SERIN2 command to receive command from ' the PC to control the rotational speed of a motor. ' ----Terminal Program Options----- ' HyperTerminal - 9600 baud 8N1, Flow control = None ' ' To download TeraTerm Pro 3.1.3, see: ' http://www.ayera.com/teraterm/download.cfm ' and download TeraTerm Pro Web 3.1.3. ' The terminal program must be the active window for this ' program to work. '----------Related Sites---------- ' See: http://www.melabs.com/resources/samples/pbp/ser2mod.bas '-----------Connections----------- ' 16F88 Pin Function Name Given Wiring ' In Program ' --------- ------------ ---------- ---------- ' ' RB4 PWMout 1K Resistor to Base ' of 2N2222A transistor ' RB2 Receiver Pin PICSI MAX232 Pin 9 ' RB5 Transmit Pin PICSO MAX232 Pin 10 ' ' See the schematic for the PIC power and MCLR connections ' MAX232 Pin Datasheet Function and Wiring ' Designation ' --------- --------- ------------------------------------ ' ' Pin 7 T2OUT Receive Data to Male RS232 DB9 Pin 2 ' Pin 8 R2IN Transmit Data from Male RS232 DB9 Pin 3 ' Pin 9 R2OUT Receive Data to PIC RB2 ' Pin 10 T2IN Transmit Data from PIC RB5 ' ' See schematic at: ' http://cornerstonerobotics.org/schematics/pic_programming_serin2_pwm.pdf '------------Revisions------------ ' 9/21/10 Initiatize RB5 to HIGH '---------Constants/Defines------- define OSC 8 ' Defines oscillator setting at 8 MHz. ' For SEROUT2, an oscillator speed faster ' than 4MHZ may be required for reliable ' operation at 9600 baud and above. '------------Variables------------ InputData var byte ' Variable to receive input data PulseWidth var byte ' Variable for pulse width MODE var WORD ' WORD for MODE value PICSI var PORTB.2 ' Names PORTB.2 name as PICSI PICSO var PORTB.5 ' Names PORTB.5 name as PICSO PWMout var PORTB.4 ' Names PORTB.4 as PWMout '----------Initialization--------- ANSEL = 0 ' Changes analog bits to digital. ' See table below. ' Analog Bit Analog or Digital PIC16F88 Pin ' ------------ ------------------ -------------- ' AN0 Digital RA0 ' AN1 Digital RA1 ' AN2 Digital RA2 ' AN3 Digital RA3 ' AN4 Digital RA4 ' AN5 Digital RB6 ' AN6 Digital RB7 OSCCON = $70 ' Sets the internal oscillator in the ' 16F88 to 8 MHz TRISB = %11101111 ' Sets RB4 to output PORTB = %00100000 ' Sets PIC transmit pin RB5 to HIGH '------------Main Code------------ mode = 84 ' Set RX/TX speed to 84 for 9600 baud ' MODE = 188 (4800 baud) ' MODE = 396 (2400 baud) ' See appendix in manual for other ' MODE examples. pulseWidth = 255 ' Set initial value for PulseWidth ' Instructions sent to terminal program serout2 PICSO, MODE, [" Instructions:",10,13] serout2 PICSO, MODE, [" ",10,13] serout2 PICSO, MODE, ["After the bell tone from the computer speaker,",10,13] serout2 PICSO, MODE, ["you have 2 seconds to type in any number between",10,13] serout2 PICSO, MODE, ["0 and 255. Then hit the enter key.",10,13] serout2 PICSO, MODE, ["Or, after the bell tone, you have 2 seconds to",10,13] serout2 PICSO, MODE, ["type in any letter, then a number between",10,13] serout2 PICSO, MODE, ["0 and 255, then another letter. The",10,13] serout2 PICSO, MODE, ["non-digits before and after the number",10,13] serout2 PICSO, MODE, ["are ignored and the number is assigned to",10,13] serout2 PICSO, MODE, ["the variable InputData",10,13] start: serin2 PICSI, MODE, 2000, nodata, [DEC inputData] ' SERIN2 uses a timeout and a label. ' If we get no input data within 2000 ms, ' we jump to nodata, and use the last ' value we had for PulseWidth. ' Format:SERIN2 Pin,Mode,TimeOut,Label,[Item1] ' Pin = PICSI,(RB2), Declared in variables ' Mode = 84 (9600 baud rate) ' TimeOut = 2000 ms ' Label = nodata ' [Item1] = [DEC inputData] ' The Jameco gearhead motors that we tested ' would not turn with PWM Duty values ' less than 170 (a 67% duty cycle). serout2 PICSO, MODE, [DEC inputData,10,13] ' Format: SEROUT2 Pin, Mode, [Item1] ' Pin = PICSO,(RB5), Declared in Variables ' Mode = 84 (9600 baud rate) ' [Item1] = [DEC InputData, 10, 13] ' Transmits Value of InputData, 10 ' (the ASCII codes for line feed), ' and 13 (the ASCII code for carriage return) ' to the PC. pulseWidth = inputData ' Set pulseWidth = inputData nodata: ' nodata label, program jumps to nodata if ' data is not received within 1000 ms timeout. pwm PWMout,pulseWidth,800 ' Format: PWM Pin,Duty,Cycle ' Pin = PWMout,(RB4), Declared in variables ' Duty = PulseWidth, a variable with ' values between 0(0%) and 255(100%). ' Cycle = 800, PWM signal sent out RB4 ' for 800 cycles. serout2 PICSO, MODE, [7] ' Sends bell tone to computer speakers goto start end