interfacing with led

Upload: junbucayu

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Interfacing With LED

    1/17

    Interfacing with LED

    To interface an LED, the cathode should be connected towards the port of the computer and anodetowards the positive five volts external supply.

    A series resistor, typically 330 ohms, must be present to limit the current passing through the LED.

    or

    The ground of the external power supply must also be connected to the ground of the parallel port.

    With this configuration, the LED will only light if 0V or low logic is present in the output port. This is so

    because there will be a potential difference between the external supply and computer port. This willeventually cause an electric current to flow, a current enough to lit up the LED.

    LED Blinking Program

    Consider this problem:

    An LED is interfaced to D0 of the parallel printer port. Write a program that will control it to blink five times.

  • 8/10/2019 Interfacing With LED

    2/17

  • 8/10/2019 Interfacing With LED

    3/17

    Therefore, our Turbo C statement will be

    Executing this statement will cause the LED to light.

    Next we will solve the statement that will turn off the LED. We can do this by sending bit 1to D0.

    We still send 0s to D7-D1 so that the whole byte can be easi ly converted.

    Next we will convert the number.

    Therefore, our Turbo C statements that will turn off the LED should be:

    So we can now write our Turbo C program that will control the LED to blink 5 times.

  • 8/10/2019 Interfacing With LED

    4/17

  • 8/10/2019 Interfacing With LED

    5/17

    Parallel Port Tutorial

    This tutorial will help you get a taste of controlling your machine using the printer port.Though the parallel port isn't being used for many applications ,it is a boon for ushobbyists. This tutorial's main aim is to get you working ,so that you can send signalsfrom the port like control a motor. Taking inputs from the port will be covered in asubsequent tutorial.

    Parallel Port Anatomy:

    Following are the pinouts:

    Picture Courtesy ::Ian Harries

    8 Output pins [D0 to D7] 5 Status pins [S4 to S7 and S3] 4 Control pins [C0 to C3] 8 ground pins [18 to 25]

    The Pins having a bar over them ,means that the signal is inverted by the parallel port'shardware.If a 1 were to appear on the 11 pin [S7], the PC would see a 0. The Statuspins are mainly used by the PC to know the status of the printer ,like if there is paper inthe printer, end of paper etc.

    Only the Data Port will be covered in this segment.

    Formatted:Font: (Default) Arial, 12

    http://www.doc.ic.ac.uk/~ih/doc/par/http://www.doc.ic.ac.uk/~ih/doc/par/http://www.doc.ic.ac.uk/~ih/doc/par/
  • 8/10/2019 Interfacing With LED

    6/17

    Parallel Port Female Connector

    The Data Port

    Sending commands involves only the data pins [D0 to D7].Though it is possible to usethe some other pins as input, we'll stick to the basics.

    Please remember that the Data pins are from pin 2 to pin 9 and not from pin 1.

    If you have a good eyesight, check your parallel port connectors. Both the connectors[male/female], have numbers etched next to their pins, so people like us don't screw upour ports, connecting them the wrong way.The word "Parallel" denotes sending anentire set of 8 bits at once to the PC [That's why term Parallel Port].However we canuse the individual pins of the port ; sending either a 1 or a 0 to a peripheral like a motoror LED.

    Sending Commands to the Port:

    This part is easy.Just a single line of code does the trick.

    Open up your C compiler.

    Type the following program:

    #include{stdio.h}#include {dos.h}[Please replace the {} bracket to ]

    void main(void)

    { outportb(0x378,0xFF); //da line}

    That's it ,you just set all your data pins to 1.

    Formatted:Font: (Default) Arial, 10

  • 8/10/2019 Interfacing With LED

    7/17

    If you take an LED and put one terminal at pin2 and the other to pin 18,it wouldglow.[Use a 2K resistor in series with the LED, otherwise u'll end up ruining your LED,or source too much current from the port pin]

    if you wish to switch it off. Type this:

    outportb(0x378,0x00);

    instead of the above line.(da line)

    What did you do?:

    Formatted:Font: (Default) Arial, 12

    Formatted:Font: (Default) Arial, 12

  • 8/10/2019 Interfacing With LED

    8/17

    0x378 is the parallel port address . Usually this is the default address.Sometimes it is

    0x278 too

    0x00 is the command appearing at the output pins. The Format is in Hexadecimal

    So if u want to make pin no2 high, that's the first pin you type

    0x01 which would mean 0000 0001 for the data port.

    0x04 would mean 0000 0100

    0x55 would mean 0101 0101

    0x0A would mean 0000 1010

    see the table below for reference

    0000-00001-10010-20011-30100-40101-50110-60111-71000-81001-91010-A

    1011-B1100-C1101-D1110-E1111-F

    That finishes your basics so that you can run your motor.

    Material to control a Motor via a parallel port:

    1 parallel port Male connector 1 DC Motor 1 Motor Driver [L293D] 1 5V regulator [7805]

    Before trying out anything ,please remember that your parallel port is not meant ordesigned to handle more than 5Volts.If possible , trying accessing your parallel port

  • 8/10/2019 Interfacing With LED

    9/17

    using Windows 98.Windows XP does not allow access to the parallel port. You'll need

    special drivers for that.

    Steps to Control a Motor:

    Use the Voltage regulator 7805,to get a constant DC 5V voltage from your DCpower supply.

    Connect your motor to your Motor Driver L293D

    .

    Connect your parallel port pins to your Female connector [on your PC],through the male

    connector as follows

    Short all Ground pins i.e from 18 to 25.

    Commands for the motor

  • 8/10/2019 Interfacing With LED

    10/17

    o outportb(0x378,0x00); ---------STOP MOTORo outportb(0x378,0x03);---------MOVE MOTOR(Break!))o outportb(0x378,0x01);---------MOVE MOTOR(CCW)o outportb(0x378,0x02);---------MOVE MOTOR(CW) .

    C program for the motor

    #include{stdio.h}#include{conio.h}#include{dos.h}[Please replace the {} bracket to ]

    main(){outportb(0x378,0x00); ---------STOP MOTORsleep(2);outportb(0x378,0x01);---------MOVE MOTOR(CCW)sleep(2);

    outportb(0x378,0x02);---------MOVE MOTOR(CW)sleep(2);outportb(0x378,0x03);---------MOVE MOTOR(Break!)sleep(2);

    Formatted:Font: (Default) Arial, 12

  • 8/10/2019 Interfacing With LED

    11/17

    return 0;

    }

    The Sleep(n) function tells the port to hold [Latch] the command for (n) seconds.eg: sleep(2)------------------delay or sleep for 2 secondsIf you want to work in milliseconds ,use the delay(n) commandeg: delay(500) --------------delay for 500 milliseconds

    That's it, you can now control a motor using the parallel port.Use 2 motors and you have a moving machine.You can actually control the motors using the arrow keys using the Bioskey()function. Check "C" help for this.

    Robotic assembly refers to the process of utilizing different types of robots in the production ormanufacturing process in order to effectively carry out the assembly of specified items. The process

    requires the utilization of any number of a vast array of robotic technology, ranging from simple

    mechanical machines to more fully developed and complex robots. This application of robots to the

    assembly process also ranges from very simple tasks to more intricate ones that require more expertise

    and a higher level of programming. Most manufacturing companies use some form of robotic assembly

    somewhere in the production process. Regardless of the advancement in the robotic assembly process,

    some form of human input is usually required in order to ensure that procedures go according to plan.

    Programming is usually the final step involved in building a robot. If you followed the lessons,so far you have chosen the actuators, electronics, sensors and more, and have assembled the

    robot so it hopefully looks something like what you had initially set out to build. Withoutprogramming though, the robot is a very nice looking and expensive paperweight.

    It would take much more than one lesson to teach you how to program a robot, so instead, thislesson will help you with how to get started and where (and what) to learn. The practicalexample will use Processing, a popular hobbyist programming language intendedto be used

    with theArduino microcontrollerchosen in previous lessons. We will also assume that you willbe programming amicrocontrollerrather than software for a full-fledged computer.

    http://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/arduino-2.html
  • 8/10/2019 Interfacing With LED

    12/17

    What Language to Choose?

    There are many programming languages which can be used to programmicrocontrollers, the most common of which are:

    Assembly; its just one step away from machine code and as such it is very tedious to use.

    Assembly should only be used when you need absolute instruction-level control of your

    code. Basic;one of the first widely used programming languages, it is still used by

    some microcontrollers (Basic Micro,BasicX,Parallax)for educational robots.

    C/C++;one of the most popular languages, C provides high-level functionality while

    keeping a good low-level control.

    Java; it is more modern than C and provides lots of safety features to the detriment of

    low-level control. Some manufacturers likeParallaxmake microcontrollers specificallyfor use with Java.

    .NET/C#; Microsofts proprietary language used to develop applications in VisualStudio. Examples includeNetduino,FEZ Rhinoandothers).

    Processing(Arduino); a variant of C++ that includes some simplifications in order to

    make the programming for easier.

    Python, one of the most popular scripting languages. It is very simple to learn and can beused to put programs together very fast and efficiently.

    Inlesson 4,you chose a microcontroller based on the features you needed (number of I/O, usercommunity, special features, etc). Often times, a microcontroller is intended to be programmed

    in a specific language. For example:

    Arduinomicrocontrollers useArduino softwareand are re-programmed inProcessing. Basic Stampmicrocontrollers use PBasic

    Basic Atommicrocontrollers use Basic Micro

    Javelin Stampfrom Parallax is programmed in Java

    If you have chosen a hobbyist microcontroller from a known or popularmanufacturer,there is

    likely a large book available so you can learn to program in their chosen programming language.If you instead chose a microcontroller from a smaller, lesser known manufacturer (e.g. since it

    had many features which you thought would be useful for your project), its important to seewhat language the controller is intended to be programmed in (C in many cases) and whatdevelopment tools are there available (usually from the chip manufacturer).

    http://www.robotshop.com/blog/en/files/2006wMindstormsProgramRobot.jpghttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/basicx-microcontroller.htmlhttp://www.robotshop.com/basicx-microcontroller.htmlhttp://www.robotshop.com/basicx-microcontroller.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/productinfo.aspx?pc=RB-Sec-01&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Sec-01&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Sec-01&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Ghi-09&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Ghi-09&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Ghi-09&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Lin-36&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Lin-36&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Lin-36&lang=en-UShttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/blog/en/?p=3700http://www.robotshop.com/blog/en/?p=3700http://www.robotshop.com/blog/en/?p=3700http://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://arduino.cc/http://arduino.cc/http://arduino.cc/http://processing.org/http://processing.org/http://processing.org/http://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/suppliers.htmlhttp://www.robotshop.com/suppliers.htmlhttp://www.robotshop.com/suppliers.htmlhttp://www.robotshop.com/blog/en/files/2006wMindstormsProgramRobot.jpghttp://www.robotshop.com/suppliers.htmlhttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/basic-atom-microcontroller.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://processing.org/http://arduino.cc/http://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/blog/en/?p=3700http://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/productinfo.aspx?pc=RB-Lin-36&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Ghi-09&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Sec-01&lang=en-UShttp://www.robotshop.com/javelin-stamp-microcontroller.htmlhttp://www.robotshop.com/basic-stamp-microcontrollers.htmlhttp://www.robotshop.com/basicx-microcontroller.htmlhttp://www.robotshop.com/basic-atom-microcontroller.html
  • 8/10/2019 Interfacing With LED

    13/17

    Getting Started

    The first program you will likely write is Hello World(referred to assuch for historicreasons). This is one of the simplest programs that can be made in a computer

    and is intended to print a line of text (e.g. Hello World) on the computer monitor or LCDscreen. In the case of a microcontroller, another very basic program you can do that has an effecton the outside world (rather than just on-board computations) is toggling an IO pin. Connecting

    an LED to and I/O pin then setting the I/O pin to ON and OFF will make the LEDblink. Although the simple act of turning on an LED may seem basic, the function can allow for

    some complex programs (you can use it to light up multi-segment LEDs, to display text andnumbers, operate relays, servos and more).

    Step 1: Ensure you have all components needed to program the microcontroller

    Not all microcontrollers come with everything you need to program them, and mostmicrocontrollers need to be connected to a computer via USB plug. If your microcontroller doesnot have a USB or DB9 connector, then you will need a separate USB to serial adapter, and wire

    it correctly. Fortunately many hobbyist microcontrollers are programmable either via an RS-232port or by USB, and include the USB connector on-board which is used not only for two-way

    communication, but also to power the microcontroller board.

    Step 2: Connect the microcontroller to the computer and verify which COM port it is connectedto. Not all microcontrollers will be picked up by the computer and you should read the gettingstarted guide in the manual to know exactly what to do to have your computer recognize it and

    be able to communicate with it. You often need to download drivers (specific to each operatingsystem) to allow your computer to understand how to communicate with the microcontroller

    and/or the USB to serial converter chip.

    Step 3: Check products user guide for sample code andcommunication method / protocol

    Dont reinvent the wheel if you dont have to. Most manufacturers provide some code (or pseudo

    code) explaining how to get their product working. The sample code may not be in theprogramming language of your choice, but dont despair; do a search on the Internet to see ifother people have created the necessary code.

    Check product manuals / user guides

    Check the manufacturers forum

    Check the internet for the product + code Read the manual to understand how to write the code

    http://en.wikipedia.org/wiki/Hello_world_programhttp://en.wikipedia.org/wiki/Hello_world_programhttp://www.robotshop.com/blog/en/files/1280px-PSP-Homebrew.jpeghttp://www.robotshop.com/blog/en/files/1280px-PSP-Homebrew.jpeghttp://www.robotshop.com/blog/en/files/1280px-PSP-Homebrew.jpeghttp://en.wikipedia.org/wiki/Hello_world_program
  • 8/10/2019 Interfacing With LED

    14/17

    Useful Tips

    1. Create manageable chunks of functional code: By creating segments of code specific toeach product, you gradually build up a library. Develop a file system on your computer to

    easily look up the necessary code.2. Document everything within the code using comments: Documenting everything is

    necessary in almost all jobs, especially robotics. As you become more and more

    advanced, you may add comments to general sections of code, though as you start, youshould add a comment to (almost) every line.

    3. Save different versions of the codedo not always overwrite the same file: if you findone day that your 200+ lines of code do not compile, you wont be stuck going through itline by line; instead you can revert to a previously saved (and functional) version and add

    / modify it as needed. Code does not take up much space o a hard drive, so you shouldnot feel pressured to only save a few copies.

    4. Raise the robot off the table or floor when debugging (so its wheels/legs/tracksdontaccidentally launch it off the edge), and have the power switch close by in case the robottries to destroy itself. An example of this is if you try to send a servo motor to a 400us

    signal when it only accepts a 500 (corresponding to 0 degrees) to 2500us (correspondingto 180 degrees) signal. The servo would try to move to a location which it cannotphysically go to (-9 degrees) and ultimately burn out.

    5. If code does something that does not seem to be working correctly after a few seconds,turn off the powerits highly unlikely the problem will fix itself and in the meantime,you may be destroying part of the mechanics.

    6. Subroutines may be a bit difficult to understand at first, but they greatly simplify yourcode. If a segment of code is repeated many times within the code, it is a good candidate

    to be replaced with a subroutine.

    Practical Example

    We have chosen an Arduino microcontroller to be the brain of ourrobot. To get started, we can take a look at theArduino 5 Minute Tutorials.These tutorials will

    help you use and understand the basic functionality of the Arduino programming language. Onceyou have finished these tutorials, take a look at the example below.

    For the robot we have made, we will create code to have it move around (left, right, forward,reverse), move the two servos (pan/tilt) and communicate with the distance sensor. We choseArduino because of the large user community, abundance of sample code and ease of integration

    with other products.

    http://www.robotshop.com/hexapod-development-platforms-1.htmlhttp://www.robotshop.com/hexapod-development-platforms-1.htmlhttp://www.robotshop.com/tracked-development-platforms-1.htmlhttp://www.robotshop.com/tracked-development-platforms-1.htmlhttp://www.robotshop.com/tracked-development-platforms-1.htmlhttp://www.robotshop.com/blog/en/files/arduino-uno.jpghttp://www.robotshop.com/blog/en/?p=3640http://www.robotshop.com/blog/en/?p=3640http://www.robotshop.com/blog/en/?p=3640http://www.robotshop.com/blog/en/files/arduino-uno.jpghttp://www.robotshop.com/blog/en/?p=3640http://www.robotshop.com/tracked-development-platforms-1.htmlhttp://www.robotshop.com/hexapod-development-platforms-1.html
  • 8/10/2019 Interfacing With LED

    15/17

    Distance sensor

    Fortunately in the Arduino code, there is an example for getting values from an analog sensor.For this, we go to File -> Examples -> Analog -> AnalogInOutSerial (so we can see the values)

    Pan/Tilt

    Again, we are fortunate to have sample code to operate servos from an Arduino. File ->Examples -> Servo -> Sweep

    Note that text after two slashes // are comments and not part of the

    compiled code

    #include // This loads the servo script, allowing you to use specific functionsbelow

    Servo myservo; // create servo object to control a servoint pos = 0; // variable to store the servo position

    void setup() // required in all Arduino code

    {myservo.attach(9); // attaches the servo on pin 9 to the servo object}

    void loop() // required in all Arduino code

    {for(pos = 0; pos < 180; pos += 1) // variable pos goes from 0 degrees to 180 degrees in steps of1 degree

    {myservo.write(pos); // tell servo to go to position in variable posdelay(15); // waits 15ms for the servo to reach the position

    http://www.robotshop.com/blog/en/files/sharp-gp2d120-ir-range-sensor-4-30-cm-B.jpghttp://www.robotshop.com/blog/en/files/sharp-gp2d120-ir-range-sensor-4-30-cm-B.jpghttp://www.robotshop.com/blog/en/files/servocity-spt200-pan-tilt.jpghttp://www.robotshop.com/blog/en/files/sharp-gp2d120-ir-range-sensor-4-30-cm-B.jpghttp://www.robotshop.com/blog/en/files/servocity-spt200-pan-tilt.jpghttp://www.robotshop.com/blog/en/files/sharp-gp2d120-ir-range-sensor-4-30-cm-B.jpg
  • 8/10/2019 Interfacing With LED

    16/17

    }

    for(pos = 180; pos>=1; pos-=1) // variable pos goes from 180 degrees to 0 degrees{myservo.write(pos); // tell servo to go to position in variable posdelay(15); // waits 15ms at each degree

    }}

    Motor Controller

    Here is where it gets a bit harder, since no sample code is availablespecifically for the Arduino. The controller is connected to the Tx (serial) pin of the Arduino and

    waits for a specific start byte before taking any action. Themanualdoes indicate thecommunication protocol required; a string with specific structure:

    080 (start byte)

    000 (specific to this motor controller; if it receives anything else it will not take action)

    motor # and direction (motor one or two and direction explained in the manual)

    motor speed (hexadecimal from 0 to 127)

    In order to do this, we create a character with each of these as bytes within the character:

    unsigned char buff[6];

    buff[0]=080; //start byte specific to Pololu motor controller

    buff[1]=0; //Device type byte specific to this Pololu controllerbuff[2]=1; //Motor number and direction byte; motor one =00,01buff[3]=127; //Motor speed 0 to 128 (ex 100 is 64 in hex)

    Serial.write(buff);

    Therefore when this is sent via the serial pin, it will be sent in the correct order.

    Putting all the code together makes the robot move forward and sweep the servo while reading

    distance values.

    You can see the full robot and theuser manual.

    http://www.robotshop.com/blog/en/files/pololu-low-voltage-serial-controller.jpghttp://www.robotshop.com/PDF/smc05a-low-voltage-dual-serial-motor-controller-guide.pdfhttp://www.robotshop.com/PDF/smc05a-low-voltage-dual-serial-motor-controller-guide.pdfhttp://www.robotshop.com/PDF/smc05a-low-voltage-dual-serial-motor-controller-guide.pdfhttp://www.robotshop.com/content/PDF/robotshop-rover-development-manual.pdfhttp://www.robotshop.com/content/PDF/robotshop-rover-development-manual.pdfhttp://www.robotshop.com/content/PDF/robotshop-rover-development-manual.pdfhttp://www.robotshop.com/blog/en/files/pololu-low-voltage-serial-controller.jpghttp://www.robotshop.com/content/PDF/robotshop-rover-development-manual.pdfhttp://www.robotshop.com/PDF/smc05a-low-voltage-dual-serial-motor-controller-guide.pdf
  • 8/10/2019 Interfacing With LED

    17/17

    ProgrammingProgramming is the process of creating a sequence of instructions that tell a computational

    device, such as the Microcontroller on a VEX robot, how to perform a task. There are severaloptions for creating and downloading programs to your VEX robot that range from very simpleto highly sophisticated. VEX Microcontrollers are pre-programmed with Default Code that

    allows you a simple way to get started without writing and downloading code. However, morecomplex robot configurations and behavior are possible through programming.

    Autonomous Code:Autonomous code allows a robot to perform behaviors without input from the radio controltransmitter. The robot follow pre-programmed routines responding only to sensor inputs.

    Radio Control Code:

    Radio control code allows you to configure the way in which the radio control transmittercontrols the robot, allowing a human operator to provide input to the robot.

    Mixed Autonomous and Radio Control Code:

    Autonomous code can be integrated with radio control code to achieve even better robot

    performance for complex tasks.