| Extra GP2D05s to BrainStem Example Last Modified: 2006-11-02 | | |
| Acroname Robotics | |||
| Introduction The Sharp GP2D02 and Sharp GP2D05 rangers have a 2-wire interface with a clock input and data output. The BrainStem GP 1.0 has a built-in port for one GP2D02 ranger. It is possible to interface additional GP2D02 and GP2D05 sensors with the digital IO pins on the BrainStem GP controller.
Source Code - TEA File for Additional GP2D0X Sensor The first program demonstrates routines for reading a GP2D02 or GP2D05 sensor. Each device has its own clock line and data line. A diode placed with its anode connected to the clock input and cathode connected to the controller digital IO pin ensures proper operation. (In a schematic, the "arrow" of the diode points into the controller.) The functions in the program allow the user to specify any two digital IO pins as clock and data lines for a GP2D02 or GP2D05 sensor. Note the quick_pulse routine. The GP2D02 requires a short clock pulse whose width is close to the average time it takes to execute one instruction in a TEA program. It was necessary to create a routine that uses two consecutive 1-byte VM opcodes to generate a pulse. This makes the pulse as short as possible. /* more0205.tea */
/* GP2D05 and GP2D02 routines for digital IO pins */
#include <aCore.tea>
#include <aDig.tea>
#include <aPrint.tea>
void quick_pulse(char x)
{
asm
{
/* configure stack for quick write of 1 then 0 */
pushlb 0
pushsb 4
pushlb aPortDigitalBlockSize
multb
pushls aPortDigital + aOffsetDigitalIO
adds
pushlb 1
pushss 3
/* write 1 then write 0 */
/* (GP2D02 needs a very quick clock pulse) */
popbmx
popbmx
}
}
void config_GP2D05(char clock, char data)
{
/* configure clock and set it high */
aDig_Config(data, ADIG_INPUT);
aDig_Config(clock, ADIG_OUTPUT);
aDig_Write(clock, 1);
}
char read_GP2D05(char clock, char data)
{
char reading;
aDig_Write(clock, 0);
aCore_Sleep(600);
reading = aDig_ReadChar(data);
aDig_Write(clock, 1);
aCore_Sleep(20);
return reading;
}
void config_GP2D02(char clock, char data)
{
/* configure clock and set it high */
aDig_Config(data, ADIG_INPUT);
aDig_Config(clock, ADIG_OUTPUT);
aDig_Write(clock, 1);
}
int read_GP2D02(char clock, char data)
{
char k;
int reading = 0;
aDig_Write(clock, 0);
aCore_Sleep(700);
for (k = 0; k != 8; k++) {
/* pulse the clock line */
quick_pulse(clock);
/* shift in the new bit */
reading = (reading<<1) | aDig_ReadChar(data);
}
/* delay ensures module gets reset properly */
aDig_Write(clock, 1);
aCore_Sleep(20);
return reading;
}
void main()
{
int r;
char b;
config_GP2D05(3,4);
config_GP2D02(1,2);
while (1)
{
r = read_GP2D02(1,2);
b = read_GP2D05(3,4);
aPrint_IntDec(r);
aPrint_Char(',');
aPrint_CharDec(b);
aPrint_Char('\n');
}
}
Source Code - TEA File for Several GP2D02 Sensors The following program shows how one clock line can be shared among several GP2D02 sensors. Each clock line will still need a diode, but the cathodes of all the diodes will be connected to one clock line. The data lines of each sensor will go to a different IO pin. Since there are several sensors, the readings must be stored in global variables. The routine that reads the sensors is very similar to the routine in the previous program, but shifts in readings from multiples sensors. The choice of three sensors is arbitrary. With minor modifications, the routine could handle up to four GP2D02 sensors on a GP module. /* many02.tea */
/* Read three GP2D02 sensors on digital IO lines */
/* Read three GP2D02 sensors on digital IO lines */
/* Uses common clock for all three sensors */
#include <aCore.tea>
#include <aDig.tea>
#include <aPrint.tea>
#define GP2D02_CLOCK 1
#define GP2D02_INPUT_A 2
#define GP2D02_INPUT_B 3
#define GP2D02_INPUT_C 4
int rangeA = 0;
int rangeB = 0;
int rangeC = 0;
void quick_pulse(char x)
{
asm
{
/* configure stack for quick write of 1 then 0 */
pushlb 0
pushsb 4
pushlb aPortDigitalBlockSize
multb
pushls aPortDigital + aOffsetDigitalIO
adds
pushlb 1
pushss 3
/* write 1 then write 0 */
/* (GP2D02 needs a very quick clock pulse) */
popbmx
popbmx
}
}
void read_all_GP2D02()
{
char k;
/* clear the global range variables */
rangeA = 0;
rangeB = 0;
rangeC = 0;
/* initialize all the modules */
aDig_Write(GP2D02_CLOCK, 0);
aCore_Sleep(700);
for (k = 0; k != 8; k++) {
/* pulse the clock line */
quick_pulse(GP2D02_CLOCK);
/* shift in the new bits of each reading */
rangeA = (rangeA << 1) | aDig_ReadChar(GP2D02_INPUT_A);
rangeB = (rangeB << 1) | aDig_ReadChar(GP2D02_INPUT_B);
rangeC = (rangeC << 1) | aDig_ReadChar(GP2D02_INPUT_C);
}
/* delay ensures modules get reset properly */
aDig_Write(GP2D02_CLOCK, 1);
aCore_Sleep(20);
}
void main()
{
/* configure the GP2D02s */
aDig_Config(GP2D02_INPUT_A, ADIG_INPUT);
aDig_Config(GP2D02_INPUT_B, ADIG_INPUT);
aDig_Config(GP2D02_INPUT_C, ADIG_INPUT);
aDig_Config(GP2D02_CLOCK, ADIG_OUTPUT);
aDig_Write(GP2D02_CLOCK, 1);
while (1)
{
read_all_GP2D02();
aPrint_IntDec(rangeA);
aPrint_Char(',');
aPrint_IntDec(rangeB);
aPrint_Char(',');
aPrint_IntDec(rangeC);
aPrint_Char('\n');
}
}
Revision History:
| ||||
Related Examples: Robot Program with Two TEA Programs with Different Behaviors Example | ||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved. |