Difference between revisions of "CSC231 Lab 6"

From dftwiki3
Jump to: navigation, search
(INPUTTING INFORMATION)
(LedOff)
Line 192: Line 192:
  
 
Ok, you figured it out.  Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it.  Use it to turn OFF the LED.
 
Ok, you figured it out.  Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it.  Use it to turn OFF the LED.
 +
 +
Remember to check the [http://www.arduino.cc/en/Reference/HomePage Arduino Reference Page] if you need to debug your sketch...
  
 
====LedOnOff====
 
====LedOnOff====

Revision as of 09:52, 31 October 2008

Back to CSC231 Weekly Schedule


CSC 231 Lab # 6

© D. Thiebaut, 2008

In this lab we are going to make the Arduino keep on listening to commands it gets from the assembly language on the PC, and execute each command as it receives them.

Getting the SimpleMessageSystem Library for the Arduino

ArduinoCGI.jpg

You need a new library for the Arduino called SimpleMessageSystem (Note: there is a newer version of it, Messenger, but I haven't been able to play with it enough to make it the version to use for the project.)

  • Boot the Ubuntu CD-Rom
  • Open a Terminal window and type
  sudo su
  cd 
  cd arduino-0010/hardware/libraries
  • download the library file from the Arduino Playground:
  wget http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip  
  • unzip the file
  unzip SimpleMessageSystem.zip  
  • Remove the archive file
  rm SimpleMessageSystem.zip  
  • Set the ownership of the new library the same ownership as that of the arduino software
  chown -R 1000:1000 SimpleMessageSystem
  • Open the arduino GUI
  cd ../..
  .arduino
  • Create this new sketch.
/* 
 ---- SimpleMessageSystem Example 1 ----
 Control Arduino board functions with the following messages:
 
 r a -> read analog pins
 r d -> read digital pins
 w d [pin] [value] -> write digital pin
 w a [pin] [value] -> write analog pin 
 
 Base: Thomas Ouellet Fredericks 
 Additions: Alexandre Quessy  
 */

// Include de SimpleMessageSystem library
// REMOVE THE FOLLOWING LINE IF USING WIRING
#include <SimpleMessageSystem.h> 

void setup() {
  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!! 
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!
}

void loop() {
  if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
    switch (messageGetChar()) { // Gets the first word as a character
    case 'r': // Read pins (analog or digital)
      readpins(); // Call the readpins function
      break; // Break from the switch
    case 'w': // Write pin
      writepin(); // Call the writepin function
    }
  }
}

void readpins(){ // Read pins (analog or digital)
  switch (messageGetChar()) { // Gets the next word as a character
    case 'd': // READ digital pins
      messageSendChar('d');  // Echo what is being read
      for (char i=2;i<14;i++) {
         messageSendInt(digitalRead(i)); // Read pins 2 to 13
      }
      messageEnd(); // Terminate the message being sent
      break; // Break from the switch
  case 'a': // READ analog pins
      messageSendChar('a');  // Echo what is being read
      for (char i=0;i<6;i++) {
        messageSendInt(analogRead(i)); // Read pins 0 to 5
      }
      messageEnd(); // Terminate the message being sent
   }
}

void writepin() { // Write pin
  int pin;
  int state;

  switch (messageGetChar()) { // Gets the next word as a character
  case 'a' : // WRITE an analog pin
    pin = messageGetInt(); // Gets the next word as an integer
    state = messageGetInt(); // Gets the next word as an integer
    pinMode(pin, OUTPUT); //Sets the state of the pin to an output
    analogWrite(pin, state); //Sets the PWM of the pin 
    break;  // Break from the switch
    
  case 'd' : // WRITE a digital pin
    pin = messageGetInt();  // Gets the next word as an integer
    state = messageGetInt();  // Gets the next word as an integer
    pinMode(pin,OUTPUT);  //Sets the state of the pin to an output
    digitalWrite(pin,state);  //Sets the state of the pin HIGH (1) or LOW (0)
  }
}


  • Save your sketch in the /root/sketches folder, and give it the name loop. I found that saving is not always straightforward, and it works best for me if I use the right arrow in the tab area of the Arduino GUI, click it, and use the rename option.
  • IMPORTANT
Note: When you boot your PC in Ubuntu mode from the live CD, you are working with a Linux computer that exists only on the CD and in the RAM. Whatever you create or copy to your ubuntu or root account exists only in RAM. As soon as you close the system, all your changes disappear. All the files you created disappear. For this reason, you will need a USB key to save all the files you create, including sketches and libraries, such as the Messenger library you created above.
Thus, at this point your /root directory should contain two folders: arduino-0010 and scketchbook, and sketchbook should have a new sketch called loop. You may want to drag the whole root folder to your USB memory stick and save all these new modifications.


CSC231 Ubuntu Root Folder.png

  • Use the Tools menu to set the board as the Diecimila, and the port as the USB port.
  • Compile. You may get warnings, but make sure you get a compile confirmation:
 Binary sketch size: 3402 bytes (of a 14336 byte maximum)
  • Upload to the Arduino. Check that the Receive and Transmit LED on the board light up to indicate data transfer.


UbuntuPinguin.png





Assembly Part on the PC

OUTPUTTING INFORMATION

LedOn

You are going to create a program called ledOn that turns on the LED connected to Pin 13.

  • Open a new Terminal window under Ubuntu and type
  sudo su
  cd
  • Highlight the contents of the file driver.c and copy it to the clipboard
  • In the terminal window type
  cat > driver.c
and then click the middle button of your mouse to paste the code you had highlighted
(You may want to copy these to your memory stick as well before you forget!!!)
  • Take a look at LedOn.asm. We'll look over the main features in class. It is important to note where the message containing the command is, and what its format is.
Note 2: Make sure the variable USB in ledOn.asm contains the same string identifying the USB port in your Arduino GUI.
  • Assemble/compile the files:
 nasm -f elf -F stabs asm_io.asm
 nasm -f elf -F stabs ledOn.asm
 gcc -o ledOn driver.c ledOn.o asm_io.o
  • Run the program
  ./ledOn
Does the LED on Pin 13 turn ON? (If your Pin 13 is damaged from previous labs, please put a resistor plus LED connected to Pin 11).

LedOff

Ok, you figured it out. Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it. Use it to turn OFF the LED.

Remember to check the Arduino Reference Page if you need to debug your sketch...

LedOnOff

Same scenario, but now this time make your assembly program send an ON message, followed by an OFF message in an endless loop.

Measure the frequency of the LED turning ON and OFF. This will give us an idea of how fast we can activate devices attached to the Arduino.

For those interested creating sound, the human ear can hear sounds between about 300 and 20,000 Hz.


Open Problems

Things your instructor doesn't know, but that you might be able to discover while playing with the Arduino, and in which case you are asked to share your discoveries!

  • How can the serial speed of communication be increased so that the PC and the Arduino talk faster? I have tried changing the speed in the sketch from 9600 to a higher frequency, such as 115200, but that doesn't seem to work. The Java machine supporting the arduino crashes...