{ ***************************************************************************** * Name : MONGOOSE.BAS * * Author : William Richardson * * Notice : Copyright (c) 2007 blueroomelectronics * * : All Rights Reserved * * Date : 12/23/2007 * * Version : 0.9 beta Notes : Routines designed for 8 MHz Clock : 976.5625KHz PWM Motor module : MotorLR (LeftMotor +/-127, RightMotor +/-127) ***************************************************************************** } Module Mongoose Device = 18F2525 { ***************************************************************************** * Name : MotorLR (LeftMotor, RightMotor) * * Purpose : Sets PWM direction and speed for left & Right motors * * Range +127 / -127 (positive forward, 0 stop, negative reverse) * ***************************************************************************** } Public Sub MotorLR(LeftMotor, RightMotor As ShortInt) LATB = 0 ' clear the direction If LeftMotor < 0 Then LeftMotor = -LeftMotor // abs() High(PORTB.3) // Reverse LeftMotor Else High(PORTB.1) // Forward LeftMotor EndIf If RightMotor < 0 Then RightMotor = -RightMotor // abs() High(PORTB.2) // Reverse RightMotor Else High(PORTB.0) // Forward RightMotor EndIf CCPR2L = LeftMotor // 0 - $7F CCPR1L = RightMotor // 0 - $7F End Sub // Initialize PWM module & I/O for 1KHz PWM operation (Private) Inline Sub InitMongoose() OSCCON = $72 ' enable 8MHz clock CMCON = 7 ' comparators off PR2 = $7F ' 9bit PWM mode CCP1CON = $0C ' CCP1 enable PWM mode CCP2CON = $0C ' CCP2 enable PWM mode CCPR1L = 0 ' zero duty register Left Motor CCPR2L = 0 ' zero duty register Right Motor T2CON = %00000101 ' 1:4 prescaler, TMR2 on (1953.125KHz PWM) TRISB = %11110000 ' motor direction TRISC = %11111001 ' PWM outputs RC1, RC2 End Sub // Initialize the Mongoose Config OSC = INTIO67, LVP = OFF, PBADEN = OFF #option LCD_DATA = PORTA.4 #option LCD_RS = PORTC.3 // #option LCD_RW = PORTC.4 // Assign the LCD connections #option LCD_EN = PORTC.5 // 4bit mode InitMongoose