HOME            BACK

 

Slave(E128) module

 

This module provides functions to control the robot to play the game.

 

The slave module is for E128 and controls the two motors to move the robot.

 

Here is code for this module. Please click on the menu for detail.

 

- Pseudo Code

 

- Source Code

 

 

 

 

 

HOME            BACK             TOP

 

 

Pseudo Code

 

 

/***** MAIN FUNCTION *****/

 

void main(void) {

          Initialize the slave system

          Enable interrupts

          Wait forever

}

 

//**************************************************************************************************

// Function : void Initialize(void);

// Initialize PWM, SPI, and motor

//**************************************************************************************************

void Initialize(void) {

          Initialize PWM

          Initialize Motor

          Initialize SPI

}

 

//**************************************************************************************************

// Function : void InitMotor(void);

// initialize wheel motors

//**************************************************************************************************

void InitMotor(void) {                                   

Set MotorLbit and MotorRbit of Port U as output

Stop the motors

}                   

 

//**************************************************************************************************

// Function : void InitPWM(void);

// initialize PWM sub-system

//**************************************************************************************************

void InitPWM(void) {

Enable the PWM subsytem on channel 0 and 1

Map PWM to PORT T

Choose clock SA

Set polarity

Set pre-scaler for clock A

Set scaler for clock A to make 10000 Hz frequency with 1% resolution

Set initial period of channel 0 and 1 to be 100 ( = 10000Hz)

Set initial duty cycle of channel 0 and 1 to be 0%

}

           

//**************************************************************************************************

// Function : void InitSPI(void);

// initialize SPI

//**************************************************************************************************

void InitSPI(void) {

          Enable SPI

          Select Slave mode

          Set clock polarity and phase - mode 3

          Set Baud rate = 24 MHz / {(0+1)*2^(7+1)} = 93.75 kHz */

Set SS control

Enable SPI interrupt

}

 

//**************************************************************************************************

// Function : void MoveStraight(void);

// move the robot straight forward

//**************************************************************************************************

void MoveStraight(void) {

          Move the robot straight forward at high speed

}

 

//**************************************************************************************************

// Function : void TurnCW(void);

// rotate the robot CW

//**************************************************************************************************

void TurnCW(void) {

          rotate the robot CW

(turn motors in opposite direction, same speed)

}

 

//**************************************************************************************************

// Function : void TurnCCW(void);

// rotate the robot CCW

//**************************************************************************************************

void TurnCCW(void) {

          rotate the robot CCW

(turn motors in opposite direction, same speed)

}                                      

 

//**************************************************************************************************

// Function : void MoveCW(void);

// Turn the robot CW with bigger radius 

//**************************************************************************************************

void MoveCW(void) {

          Turn the robot CW with bigger radius

(turn motors in same direction, different speed)

}

 

//**************************************************************************************************

// Function : void MoveCCW(void);

// Turn the robot CCW with bigger radius 

//**************************************************************************************************

void MoveCCW(void) {

          Turn the robot CCW with bigger radius

(turn motors in same direction, different speed)

}

 

//**************************************************************************************************

// Function : void Stop(void);

// stop the robot

//**************************************************************************************************

void Stop(void) {

          stop the robot

}

 

//**************************************************************************************************

// Function : void MoveBack(void);

// Drive the robot backward

//**************************************************************************************************

void MoveBack(void) {

          Drive the robot backward

}       

 

//**************************************************************************************************

// Function : void SetPWM (signed char order);

// Control each motor PWM separately according to the input  

//**************************************************************************************************

void SetPWM (signed char order) {

Control each motor PWM separately according to the input

If order is for right wheel, control right wheel

          Set PWM of right motor as order

ELSE If order is for left wheel, control left wheel     

          Set PWM of left motor as order

END

}

            

//**************************************************************************************************

// Interrupt for SPI : void interrupt _Vec_SPI ControlMotors(void) ;

// read data from master and follow the order

//**************************************************************************************************

void interrupt _Vec_spi ControlMotors(void) {

          Read transferred data (order) from the master

Move according to the order

}

 

 

 

 

HOME            BACK             TOP

 

 

Source Code

 

 

#include <hidef.h>

#include <mc9s12e128.h>

#include <S12E128bits.h>

#include <bitdefs.h>

#include <stdio.h>

#include "S12eVec.h"

 

#pragma LINK_INFO DERIVATIVE "SampleS12"

 

 

 

/***** MODULE DEFINITIONS *****/

 

#define MotorRbit BIT6HI

#define MotorLbit BIT7HI

 

#define ForwardSpeed 40

#define HiSpeed 40 

#define LoSpeed 10

#define TurnSpeed 25                               

 

#define moveStr 102

#define turnCW  103

#define turnCCW 104

#define moveCW 105

#define moveCCW 106

#define stop 107

#define moveBack 108

 

 

/***** MODULE FUNCTIONS *****/

 

void Initialize(void);

void InitMotor(void);

void InitPWM(void);

void InitSPI(void);

void MoveStraight(void);

void TurnCW(void);

void TurnCCW(void);

void MoveCW(void);

void MoveCCW(void);

void Stop(void);

void MoveBack(void);

void SetPWM (signed char order);

 

/***** MAIN FUNCTION *****/

 

void main(void) {

  Initialize();

            EnableInterrupts;

  for(;;) {} /* wait forever */

}

 

 

 

/***** MODULE FUNCTION IMPLEMENTATION *****/

 

//**************************************************************************************************

// Function : void Initialize(void);

// Initialize PWM, SPI, and motor

//**************************************************************************************************

void Initialize(void) {

            InitPWM();

            InitMotor();

            InitSPI();

}

 

//**************************************************************************************************

// Function : void InitMotor(void);

// initialize wheel motors

//**************************************************************************************************

void InitMotor(void) {                                               

  // Set MotorLbit and MotorRbit of Port U as output

  DDRU |= (MotorLbit|MotorRbit);

  Stop();

}                        

 

//**************************************************************************************************

// Function : void InitPWM(void);

// initialize PWM sub-system

//**************************************************************************************************

void InitPWM(void) {

  /* enable the PWM subsytem on channel 0 and 1*/

  PWME  |= (_S12_PWME0|_S12_PWME1);

  /* map PWM to PORT T */

  MODRR |= (_S12_MODRR0|_S12_MODRR1);

  /* choose clock SA */

  PWMCLK |= (_S12_PCLK0|_S12_PCLK1);

  /* set polarity */

  PWMPOL = (_S12_PPOL0|_S12_PPOL1);

 

  /* set pre-scaler for clock A */

  PWMPRCLK &= ~(_S12_PCKA0 | _S12_PCKA1 | _S12_PCKA2);

  PWMPRCLK |= _S12_PCKA2;   // /16

  /* set scaler for clock A to make 10000 Hz frequency with 1% resolution */

  PWMSCLA = 0x96 >> 1;

 

  /* set initial period of channel 0 and 1 to be 100 ( = 10000Hz) */

  PWMPER0 = 0x64;

  PWMPER1 = 0x64;

 

  /* set initial duty cycle of channel 0 and 1 to be 0% */

  PWMDTY0 = 0;

  PWMDTY1 = 0;

}

             

//**************************************************************************************************

// Function : void InitSPI(void);

// initialize SPI

//**************************************************************************************************

void InitSPI(void) {

            SPICR1 |= _S12_SPE;       /* SPI enable */

            SPICR1 &= ~(_S12_MSTR);         /* Slave mode */

            SPICR1 |= (_S12_CPOL | _S12_CPHA);           /* clock polarity and phase - mode 3 */

           

            /* Bit rate = 24 MHz / {(0+1)*2^(7+1)} =93.75 kHz */

            SPIBR |= _S12_SPR0 | _S12_SPR1 | _S12_SPR2;               /* SPR = 7 */

 

  /* ss control */

            SPICR1 |= _S12_SSOE;

            SPICR2 |= _S12_MODFEN;

 

  /* enable SPI transmit interrupt */

  SPICR1 |= _S12_SPIE;

}

 

 

//**************************************************************************************************

// Function : void MoveStraight(void);

// move the robot straight forward

//**************************************************************************************************

void MoveStraight(void) {

  PTU |= MotorRbit;

  PWMDTY0 = (100-ForwardSpeed);

  PTU |= MotorLbit;

  PWMDTY1 = (100-ForwardSpeed);

 

}

 

//**************************************************************************************************

// Function : void TurnCCW(void);

// rotate the robot CCW

//**************************************************************************************************

void TurnCCW(void) {

            // turn motors in opposite direction, same speed

            PTU |= MotorRbit;

            PWMDTY0 = (100-TurnSpeed);

            PTU &= ~(MotorLbit);

            PWMDTY1 = TurnSpeed;

 

}

 

//**************************************************************************************************

// Function : void TurnCW(void);

// rotate the robot CW

//**************************************************************************************************

void TurnCW(void) {

            // turn motors in opposite direction, same speed

            PTU &= ~(MotorRbit);

            PWMDTY0 = TurnSpeed;

            PTU |= MotorLbit;

            PWMDTY1 = (100-TurnSpeed);

 

}                                                

 

//**************************************************************************************************

// Function : void MoveCW(void);

// Turn the robot CW with bigger radius 

//**************************************************************************************************

void MoveCCW(void) {

            // turn motors in same direction, different speed

            PTU |= (MotorRbit);

            PWMDTY0 = (100-HiSpeed);

            PTU |= (MotorLbit);

            PWMDTY1 = (100-LoSpeed);

 

}

 

//**************************************************************************************************

// Function : void MoveCCW(void);

// Turn the robot CCW with bigger radius 

//**************************************************************************************************

void MoveCW(void) {

            // turn motors in same direction, different speed

            PTU |= (MotorRbit);

            PWMDTY0 = (100-LoSpeed);

            PTU |= (MotorLbit);

            PWMDTY1 = (100-HiSpeed);

 

}

 

//**************************************************************************************************

// Function : void Stop(void);

// stop the robot

//**************************************************************************************************

void Stop(void) {

            PTU &= ~(MotorRbit);

            PWMDTY0 = 0;

            PTU &= ~(MotorLbit);

            PWMDTY1 = 0;

 

}

 

//**************************************************************************************************

// Function : void MoveBack(void);

// Drive the robot backward

//**************************************************************************************************

void MoveBack(void) {

            PTU &= ~(MotorRbit);

            PWMDTY0 = ForwardSpeed;

            PTU &= ~(MotorLbit);

            PWMDTY1 = ForwardSpeed;

 

}

 

//**************************************************************************************************

// Function : void SetPWM (signed char order);

// Control each motor PWM separately according to the input  

//**************************************************************************************************

void SetPWM (signed char order) {

  /* control right wheel */

  if (order >= 0) {

    order -= 51;

    order = order*2;

 

    if (order >= 0) {

      PTU |= MotorRbit;

      PWMDTY0 = 100-order;

    } else {

      PTU &= ~(MotorRbit);  // MotorLbit --> controls RIGHT MOTOR

      PWMDTY0 = -1*order;

    }

  }

    /* control left wheel */

    else {

    order += 51;

    order = 2*order;

 

    if (order >= 0) {

      PTU |= MotorLbit;

      PWMDTY1 = 100-order;

    } else {

      PTU &= ~(MotorLbit);

      PWMDTY1 = -1*order;

    }

  }                                                                        

}

    

//**************************************************************************************************

// Interrupt for SPI : void interrupt _Vec_SPI ControlMotors(void) ;

// read data from master and follow the order

//**************************************************************************************************

void interrupt _Vec_spi ControlMotors(void) {

            signed char order, flag;

           

            // Read transferred data (order) from the master

            flag = SPISR;

            order = SPIDR;

            // Move according to the order

            if (order == moveStr) {

                        MoveStraight();

            } else if (order == turnCW) {

                        TurnCW();

            } else if (order == turnCCW) {

                        TurnCCW();

            } else if (order == moveCW) {

                        MoveCW();

            } else if (order == moveCCW) {

                        MoveCCW();

            } else if (order == stop) {

                        Stop();

            } else if (order == moveBack) {

                        MoveBack();

            } else {

              SetPWM(order);

            }

}

    

 

HOME            BACK             TOP