You are on page 1of 43

Paper 143.

478

Mechatronics Sensors, Interfacing & Software Driver


Dr. Peter Xu Senior Lecturer Institute of Technology & Engineering Massey University
Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

! ! ! ! ! ! !

Sensors, Interfacing and Software Driver

Introduction - Sensing, Interfacing and Programming Photoresistor IR Proximity Sensor Pyroelectric Sensor Ultrasonic Ranging Sensor Shaft Encoders Microswitch

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Interface Between Sensors and Micro Sensor-interface electronics Sensor-driver routines Software Drivers Once a set of sensors has been selected and the proper interface circuitry has been designed to connect your sensors to a microprocessor, the microprocessor has to be programmed to read the sensors. These pieces of code are often written in assembly language and are known as software drivers. Software drivers provide a well-defined interface between a hardware device (intelligent machine) and a program that needs to use the device. Software drivers deal with the hardware-software interface

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

RS 276-1657 Photoresistor and Interfacing to a MC6811


Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Photoresistors
I. Principle and Interfacing Total resistance: RT=R+RL Current : I=V/RT The voltage on PE1: VPE1=I RL => VRL/(R+RL) MC6811 8-bit A/D converter maps the variable voltage, VPE1, into the range 0 to 255. A good compromise between sensitivity and range will be achieved if the resistance R is set to the same value as the resistance exhibited by the photoresistor when exposed to the light level in the middle of the range of light level in which the machine must operate.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

II. Software Driver MC6811 port E - 8-bit input port or 8-channel analog-to-digital converter. - Internally only A/D circuit for entire port, only four registers to store results from the eight channels. - Voltage reference pins VRH, VRL - Two control registers ADCTL, OPTION, are used to configure the mode of conversion.
Conversion sequences can be chosen that repeat on a single channel for four times or on four channels, once each. For later mode, two banks of four PE0-PE3 and PE4-PE7. Conversions are complete 34clock cycles after the ADCTL register is written. After each conversion, results are posted in registers ADR1, ADR2, ADR3, ADR4.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Configuring ADCTL

When SCAN=0, four conversions are performed, once each When SCAN=1, conversions continue in a round-robin fashion. When MULT=0, four conversions are repeated on a single channel. The selected channel is set by the lower four bits of ADCTL. When MULT=1, one bank of four channels is converted.
(if bits 2 and 3 are set 0, channels PE0-PE3 are converted. If bits 2 and 3 are set 1, channels PE4-PE7 are converted.)

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Assembly Code
ph-right ph-left option adctl adr1 adr2 equ $10 equ $11 equ $1039 equ $1030 equ $1031 equ $1032

update-photo bset option #%10000000 bset adctl #%00010000 check-result brclr adctl #%10000000 check-result ldaa adr1 staa ph-right ldaa adr2 staa ph-left rts

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

C Code
int ph_right=0; int ph_left=0; void update_photo() { poke(option, 0b10000000); poke(adctl, 0b00010000); while ( (peek(adctl) & 0b10000000)==0) {} ph_left=peek(adr1); ph_right=peek(adr2);}

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Near-Infrared Proximity Detectors


I. Principle and Interfacing - The sensor uses an infrared LED and infrared phototransistor. - Near-infrared proximity detectors are often called IRs for short. - They are sensitive in the range just below visible light (around 880 nm wavelength) - The output of the transistor interfaced to a control circuit producing a go/no-go output. - Sensitivity can be adjusted by changing the value of R2; reduce the value to increase sensitivity.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Sharp G1U52X IR Proximity Detector


Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Hamamatsu S3599 IR Proximity Detector


Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

- The detector responds to a carrier frequency of 40 kHz. A frequency 40 kHz means the LED is blinked on and off with a period of 25 us. This signal is then modulated at a lower frequency so that the blinking is on for 600us and then off for 600us. - The LEDs blink only when pins PD2 and PD3 of port D are asserted, which can be done by writing a program. - The detector is a digital signal, either 0 or 5V. Pin PE4 of MC6811 can be used in the normal digital input mode. The A/D converter capacity is not necessary here.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

II. Software Driver C Code


int ir_status=0; void ir_detect() { int val_off, val_on; bit_set(port_D,0b00000100); sleep(0.000600); val_on=peek(port_E); bit_clear(port_D,0b00000100); sleep(0.000600); val_off=peek(port_E); if ((val_off & ~val_on & 0b00000100 ==0b00000100) ir_status=1; else ir_status=0; }

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Pyroelectric Sensors
I. Principle and Interfacing - Essential component in certain types of motion detecting burglar alarms. A lithium tantalate crystal. - The output changes when small changes in the temperature of the sensor occur over time. - Charge is induced as the crystal is heated. Detect radiation in the 810um range (the range of infrared energy emitted by humans). - The Eltec 442-3 sensor incorporates two lithium tantalate crystals. The amplified difference of the voltage across the crystals is the output of the sensor. In the case that both crystals are at the same temperature, the sensor produces an output signal that remains steady at about 2.5V (assuming a 5 V power supply). If a person walks in front of the sensor moving from left to right, the signal will arise above 2.5V by about one volt and then fall below it, finally returning to the steady-state value. Should a person walk in front of the sensor mvoing from right to left, the reverse will happen.
Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

II. Software Driver


- By taking advantage of the MC6811s A/D port, we can implement the interface with a minimum of components. The same software driver used before can gather pyroelectric data. - A program to notice when the readings go above or below a preset threshold can trigger some machine behavior. More sophisticated software could look for trends and try to determine which way the person is moving and attempt to follow.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Microswitches

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Path Following Using Infrared Sensors

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Ultrasonic Ranging Sensors


I. Principle and Interfacing - A sonar transducer can provide distance information. It measures the time of flight between the initiation of a ping and the return of its echo. - Polaroid 6500 ranging module The ranging board begins by sending a brief 400 volt signal to the transducer, creating an ultrasonic chirp. After transmitting the chirp, the ranging board monitors the transducer for a returning echo. When an echo is detected, the range boards asserts (sets to high) its output line. By measuring the time between initiation of the chirp and return of the echo, the microprocessor determines the distance to the object responsible for the echo.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

- MC6811 Interfacing Two input capture registers to record the time when the sonar ping begins its flight and the time when the echo is detected. The input lines PA1 and PA2 are associated with the timer. PD2 and PD3 used to control its infrared emitters. Using the logical AND (HC08 chip) of signals from PD2 and PD3 to initiate a sonar ping. When PD2 and PD3 are asserted, INIT and PA2 are forced high, the former initiates a sonar ping. Upon detection of a returning echo, the ranging board asserts Echo, driving PA1 high. The gate AND acts as a buffer to drive the LED, when Echo goes high, the LED lights, giving a visual indication that an echo has been detected.

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

II. Software Driver


/* Enable input capture on rising edge on lines PA1 and PA2 void init_sonar() { bit_set(tctl2, 0b010100); bit_clear(tctl2,0b101000); } /* Initiate a sonar ping */ void ping() { poke(tflg1,0b10); bit_set(portd,0b001100); sleep(0.030); bit_clear(portd,0b001100); } /*Determine if an echo was received, if so compute the range */ float range() { if ( (peek(tflg1) & 0b10)==0) return -1.0; else return ((float) ((peekword(tic2)-peeword(tic1)) >>1 0*0.000569); }
Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Shaft Encoders
I. Principle & Interfacing The disk attaches to the motor shaft and spins with it. A nearinfrared LED is placed on one side of the disks slots and a phototransistor on the other. As the disk spins, the light passing through the disk is interrupted by the moving slots, and a signal in the form of a pulse train is produced at the output of the phototransistor. By using a microprocessor to count these pulses, the robot can tell how far its wheels have rotated. The interface between shaft encoders and MC6811 port A pins PA7 and PA0. Shaft encoder data from the left wheel are counted by the pulse accumulator hardware associated with PA7. For the right wheel, interrupts are triggered by the input capture hardwarre (IC3) connected to PA0. Shaft-encoder pulses are counted in software in an interrup-handler routine.
Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

8-bit counter register (pulse accumulator)

pulse accumulator control register

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

TMSK1 register

TFLG1 register

TCTL2 register

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

III. Shaft-Encoder Input Capture Software Driver


Interrupt-Handler Routine for IC3
TFLG1 EQU $1023 ORG MAIN_START subroutine_initialize_module ldd #IC3_interrupt_handler std $FFEA cli rts variable_right_clicks fdb 0 IC3_interrupt_handler ldd variable_right_clicks addd #1 std variable_right_clicks ldaa #%00000001 staa TFLG1 rti
Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

Supervising Program
int TCTL2= 0x1021; int TMSK1=0x1022; int TFLG1=0x1023; int PACTL=0x1026; int PACNT=0x1027; void init_velocity() { poke(PACTL, 0b01010000); poke(PANCT,0); bit_clear(TCTL2, 0b00000010); bit_set(TCTL2, 0b00000001); bit_set(TMSK1, 0b00000001); } float get_left_vel() { float vel; vel = (float) peek(PACNT); poke(PACNT,0); return (vel); } float get_right_vel() { float vel; vel = (float) right_clicks; right_clicks=0; return (vel); }

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

II. Shaft-Encoder Pulse-Accumulator Software Driver


int PACTL=0x1026; int PACNT=0x1027; void init_velocity() { poke(PACTK, 0b01010000); poke(PACNT, 0); } float get_left_vel() { float vel; vel=(float) peek(PACNT); poke(PACNT,0); return(vel); }

Dr. Peter Xu, Copyright, ITE, Massey University, New Zealand

You might also like