I'm about to tackle writing a module for a small robot kit..

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

I'm about to tackle writing a module for a small robot kit..

Post by blueroomelectronics » Fri Dec 21, 2007 6:25 pm

I'm using the SE version of Swordfish and will tackle adding / modifying two modules posted on the Swordfish site (PWM for the motors & ISROnChange for the rotation sensors). Looks pretty straight forward; are there any gotchas I should be aware of?
Image
The Mongoose Mechatronics Chassis

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Mon Dec 31, 2007 12:11 am

Well here's the beta version, and it works great. Now I'd like to add the interrupt on change for the rotation sensors.

Code: Select all

{
*****************************************************************************
*  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
InitMongoose

Post Reply