231a-ab Lab 6

From CSclasswiki
Jump to: navigation, search

Objective

The objective of this lab is to investigate how we can use the Assembly Language to communicate with the Arduino.

SimpleMessageSystem Library for the Arduino

For this lab, it is necessary that the Arduino has the "SimpleMessageSystem" Library. Instructions on how to download this library can be found here. Follow the instructions until creating and compiling the loop sketch.
Make sure that you save this library and the loop sketch to a secondary memory if you are currently working in a temporary memory.

Necessary Files

Copy these files to your working folder:

Assemble and compile 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

Make sure that you save these files to a secondary memory if you are currently working in a temporary memory.

USB variable in ledOn.asm

Make sure that the USB variable in the ledOn.asm file is the same USB port that the Arduino uses. In the Arduino GUI, go to Tools > USB and check to see which USB port you are using to connect your Arduino to your computer.

Turning the pin 13 LED on

After assembling and compiling the necessary files, run ledOn.asm:

./ledOn

After running the file, my Arduino pin 13 turned on. If your pin 13 LED does not turn on, it might have been damaged. Try putting a resistor and LED to pin 11.

Turning the pin 13 LED off

To turn the LED off, you only need to edit section .data of the ledOn.asm. Make sure that the section looks like this:

          section        .data
errorMsg  db     "Error accessing file!",0x0a, 0
errorMsg1 db     "Error opening file!",0x0a, 0
usb       db     "/dev/ttyUSB0",0
handle    dd     0
noRead    dd     0 
msg       db     "w d 13 0", 0x0d 
msgLen    equ    $-msg

Basically, instead of sending the message "w d 13 1" which means to write to the digital pin 13 the input 1 (which turns the LED on), we change it to "w d 13 0" which means to write to digital pin 13 the input 0 (which turns the LED off).

After editing ledOn.asm, save it to file ledOff.asm.

Turning the pin 13 LED on and off

To make the LED blink, you need to edit both the section .data and section .text of the ledOn.asm. Make sure that the sections are edited to:

          section        .data
errorMsg  db     "Error accessing file!",0x0a, 0
errorMsg1 db     "Error opening file!",0x0a, 0
usb       db     "/dev/ttyUSB0",0
handle    dd     0
noRead    dd     0
msg       db     "w d 13 0", 0x0d
msgLen    equ    $-msg
msg2	  db     "w d 13 1", 0x0d
msgLen2   equ    $-msg2
        section .text

global  asm_main
asm_main:

;;; open file, write string, close file...

        mov     ebx, usb
        call    openFile
        mov     dword[handle], eax
        mov     ebx, [handle]

blink:
	mov     ecx, msg
        mov     edx, msgLen
        call    writeFile
	mov	ebx, dword[handle]
	call	delay
	mov	ecx, msg2
	mov	edx, msgLen2
	call	writeFile
	mov     ebx, dword[handle]
	call	delay
	jmp 	blink
	call	closeFile
	
delay:	
	mov	ecx, 12900000
for:	dec	ecx
	loop	for
	ret
       
;;; we're done!
        ret
Frequency = 20.31 Hz
The time the LED is on is proportional to the time the LED is off.

Now, we have two messages being written to the file: msg turns the LED off, msg2 turns the LED on. We have to write both messages to the file. However, to make the LED blink, we need to have a delay in between. In this program, we used the section delay. The rest of the section .text is the same from the exit section to the doneWriteFile section.

With the current delay section, the frequency of the Arduino turning on and off is 20.31 Hz. To get a higher frequency, reduce the number of repetitions of the loop inside delay or reduce the starting value for the loop or both.

One of the problems that I encountered during the lab is that the time the LED is on is not proportional to the time the LED is off. That is, the image on the left is not symmetrical. The initial mistake is that the delay was not placed correctly. Thus, make sure that there is a delay after the LED is turned on and after the LED is turned off.