Вы находитесь на странице: 1из 6

Project 5 Wind Direction In this project you will create a device to sense the wind direction.

. General Specification Your device needs to sense which one of eight directions the wind is coming from. Essentially wind direction is analog in nature. Meaning it can come from any direction. Your device will digitize the wind direction in one of eight numbers that will represent the direction the wind is coming from. 1. How many binary digits are needed to keep track of the eight directions? To digitize the wind direction you will build an Optical Encoder. An Optical Encoder can be used to sense the position of a shaft. In our case the shaft will be connected to a wind vane. To get an idea of what you will be building go to wikipedia.org and read the article on rotary encoders. Your Optical Encoder will be built using Omron Electronics EE-SX1018 Slotted Optical Switches. These devices are essentially a switch, meaning they are either on or off. When an opaque object blocks the slot the switch is off. When the slot is clear the switch is on. You will need to use three of the Omron Electronics EE-SX1018 Slotted Optical Switches to detect which one of the eight directions your wind vane is pointing to. You will need to assign numbers (encode) to the eight possible wind directions. One possible encoding scheme is: Wind Direction N NE 001 E SE S SW W NW Binary Number 000 010 011 100 101 110 111

Looking closely at the vertical columns of the binary numbers one will see that the left binary digit (the Most Significant Bit (MSB)) is a one for half of the directions and off for the other half. Fig. 1 shows what an interrupter for the MSB would look like. The middle binary digit is off for one quarter of the numbers, on for one quarter of the numbers, off for one quarter and back on for the final one quarter of the numbers. Fig. 2 shows what an interrupter for the middle binary digit would look like. The right most binary number (the Least Significant Bit) is on and off for one eighth of the numbers. 2. Draw an interrupter for the LSB (Least Significant Bit).
Fig.2 Fig.1

Hardware Design 3. Is an Optical Encoder an input or an output device? 4. Is the Optical Encoder an analog or a digital device? Examine one of the Slotted Optical Switches to see how it functions. Connect the following circuit on your protoboard. Note that the pinout shown on the data sheet is for the bottom view.

5. Measure the voltage at pins 1 and 3 of U$1 with the slot open and with the slot obscured. V pin 1 Slot Open Slot Obscured 6. Are the voltage levels of Pin 3 sufficient to drive Vih and Vil of an ATtiny261? 7. Using Ohms law (V = I X R) calculate the current flowing through the LED. V pin 3

You will use PORTB 2,3 & 4 to read the state of your Optical Encoder. 8. On the ATtiny261 what pins are PORTB 2,3 & 4? 9. Draw a schematic of the Optical Encoder circuit. (Here are schematic symbols for the Photo Interrupters, the ATtiny261 and Resistors)

Translate this schematic to a protoboard layout

Program Specification

This program will read from three photo interrupters and light one of the LEDs in a bank of eight LEDs to indicate the position of a shaft. Wind Direction N NE 001 E SE S SW W NW Optical Encoder Reading 000 1 010 011 100 101 110 111 Lit LED 0 2 3 4 5 6 7

To accomplish this the program will initialize the Input Port and the Output Port. The program will then read from the photo interrupters, translate the 3 bit binary number read from the photo interrupters into a bit position and light the LED that corresponds to the number read. Target microcontroller is AVRs ATtiny261. Program Flowchart 6. Create a flow chart for the Program Specification

Testing Now test your software and circuitry to see that it functions properly. Does the correct LED light up when you block the slot(s) on the associated Photo Interrupter(s).

Extra Credit Wire up & solder the Optical Encoder subassembly onto perfboard. I suggest you use a connector between the Photo Interrupter devices and the ATtiny and resistors. This will allow for the optical encoder subassembly to be mounted remotely from the microcontroller board. Program /****************************************************************** * WindDir.c * * Programmer: Thomas Trickel * * Creation Date: 11/17/8 * Last Modification Date: 11/17/8 * * Description: This program will read from three photo interrupters * and light one of the LEDs in a bank of eight LEDs * to indicate the position of a shaft * * Input: No user input. Three photo interrupters will be read via PortB pins 2,3 & 4 * * Conversion: The three bit binary number read from PortB pins 2,3 & 4 will * be translated to a bit position for display on a bank of eight LEDs * * Output: Wind direction will be displayed on a bank of eight LEDs. * To indicate North no LEDs will be lit. A direction of NE will light the 0 bit LED, * a direction of E will light the 1 bit LED and so on until NW which will light the * 7 or MSB LED. * ******************************************************************/ #include <avr/io.h> char GetEncoderPosition (void) { char RawOpticalEncoderReading; char EncoderPosition;

//the number read from PortB pins 2,3 &4 //the Optical Encoder number on bits 0,1 & 2 //read PortB pins //mask off all but bits 2,3 & 4

RawOpticalEncoderReading = PINB;

EncoderPosition = RawOpticalEncoderReading & 0x1C; EncoderPosition >>= 2; return (EncoderPosition); } char TranslateEncoderPosition(char EncoderPosition)

//Shift the Raw Encoder Position right by two places // thereby moving bits 2,3 & 4 to bits 0,1 & 2

{ char TranslatedEncoderPosition; //Encoder Position number translated into a bit position TranslatedEncoderPosition = 0b00000001 << EncoderPosition; return(TranslatedEncoderPosition); } int main (void) { //start of the Main function //number read from the Optical Encoder //binary bit corresponding to current wind position //N = bit 0 or 00000001 //NE = bit 1 or 00000010 //E = bit 2 or 00000100 //SE = bit 3 or 00001000 // ... //W = bit 6 or 01000000 //NW = bit 7 or 10000000 //setup PortA for all output //setup PortB to be all input

char Encoder_Position; char WindDirectionBitPosition;

DDRA = 0xFF; DDRB = 0x00; PORTB = 0xFF;

while (1) //loop forever { Encoder_Position = GetEncoderPosition(); //Get an Optical Encoder reading //translate the encoder position number // to the Wind Direction bit position WindDirectionBitPosition = TranslateEncoderPosition(Encoder_Position); PORTA = WindDirectionBitPosition; } return (0); } //return from the Main function //display Wind Direction on the LED bank //end of the forever while

Вам также может понравиться