Quantcast
Channel: stunning co.de
Viewing all articles
Browse latest Browse all 20

Obstacle detection using MC68HC908AB32.

$
0
0

What the heck is MC68HC908AB32?

The MC68HC908AB32 is a member of the low-cost, high-performance M68HC08 Family of 8-bit microcontroller units (MCUs) designed by Motorola with embedded EEPROM for user data storage. This article presents the source code for Knight 1 Robot which I developed as a part of Microprocessor course at my uni. Special thanks to my friend Łukasz Siwko who helped in the construction of this electronic monster.

Knight 1 Robot

The code is very simple. There was much more work in setting up the electronic elements then in writing the C program. The code handles the reception of the signals from the ultrasonic sensor and the management of two engines (A and B) of the robot that is based on MC68HC908AB32 microcontroller unit. If an obstacle is detected the engines turn the robot right by stoping the right engine (A stops, B full forward).

Two words about pins

PTB_PTB1 to PTB_PTB6 are Port B data register pins from 1 to 6 connected to engine motion control circuit. Pins 1 and 4 enable engines while pins 2,3,5,6 control the movement (10 – forward, 01 – reverse).

PTB_PTB0 (Port B data register pin 0) contains information (logical 0 or 1) from connected ultrasonic sensor receiver circuit about obstacle in view. Sensor sends logical 1 on obstacle detection (0 – no obstacle in view). Output pin of this sensor is connected to Port B data register pin 0.

 
#include <time.h>
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define DELAY 10000
void moveForward(void);
void turnRight(void);
int checkSensors(void);
void main(void) {
    EnableInterrupts; /* enable interrupts */
    for(;;) {
        __RESET_WATCHDOG(); /* feeds the dog */
        if(checkSensors()) {
            turnRight();
        }
        else {
            moveForward();
        }
    } /* loop forever */
    /* please make sure that you never leave main */
}
void turnRight(void) { /* Turns the robot right. */
    int i=0;
    /* Engine A stop */
    PTB_PTB1 = 1; // Enable
    PTB_PTB2 = 1; // Input 1
    PTB_PTB3 = 0; // Input 2
    /* Engine B full forward */
    PTB_PTB4 = 1; // Enable
    PTB_PTB5 = 0; // Input 1
    PTB_PTB6 = 1; // Input 2
    /* sleep(1) */
    /* Sleeping unsupported, using a "for" loop to cause a delay */
    for(i=0;i<DELAY;i++){}
}
void moveForward(void) {
    /* Engine A full forward */
    PTB_PTB1 = 1; // Enable
    PTB_PTB2 = 1; // Input 1
    PTB_PTB3 = 0; // Input 2
    /* Engine B full forward */
    PTB_PTB4 = 1; // Enable
    PTB_PTB5 = 1; // Input 1
    PTB_PTB6 = 0; // Input 2
    /* PTB_PTB1 - 6 are Port B data register pins from 1 to 6 connected
to engine motion control circuit. Pins 1 and 4 enable engines while pins 2,3,5,6
control the movement (10 - forward, 01 - reverse). */
}
int checkSensors(void) { /* Checks whether the obstacle was detected.
Returns 1 on successful detection.*/
    if (PTB_PTB0 == 1) return 1;
    else return 0;
    /* PTB_PTB0 (Port B data register pin 0) contains information (logical 0 or 1)
from connected ultrasonic sensor receiver circuit about obstacle in view. Sensor sends
logical 1 on obstacle detection (0 - no obstacle in view). Output pin of this sensor is
connected to Port B data register pin 0. */
}

This code is available at my GitHub under GPL license. Feel free to download.



Viewing all articles
Browse latest Browse all 20

Trending Articles