231a-af Lab 7 Report

From CSclasswiki
Jump to: navigation, search

Lab 7 Report

This lab interfaces the Arduino with the pc through the use of arduino-serial.c and arduino_loop.pde.

Input/Output PC to Arduino: Part 1

We enter commands at the linux prompt to set the Arduino LED 13 on/off, or to read the status of various pins.

The Arduino program: arduino_loop.pde

  • Cut and paste arduino_loop.pde in the arduino GUI on the Ubuntu machine.
  • Compile it.
  • Upload it to the Arduino.

The C Program running on Ubuntu: arduino-serial.c

gcc -o arduino-serial arduino-serial.c
  • Run it as follows:
./arduino-serial -b 9600 -p /dev/ttyUSB0  -s "w d 13 1"
  • Try to read the digital pins too:
 ./arduino-serial -b 9600 -p /dev/ttyUSB0 -s "r d" -r
  • Exercise 1: Modify the command line so that you will turn Pin 13 ON, then OFF, then ON, then OFF again.
./arduino-serial -b 9600 -p /dev/ttyUSB0 -s "w d 13 1" -s "w d 13 0" -s "w d 13 1" -s "w d 13 0"
  • Exercise 2: Look at the code of serial-arduino.c and figure out how you can make the program wait for a few ms or seconds between commands. Once you have figure this out, repeat Exercise 1 but make the LED stay ON for a second before being turned OFF, and similarly make it stay OFF a second before being turnd ON again.
./arduino-serial -b 9600 -p /dev/ttyUSB0 -s "w d 13 1" -d 1000 -s "w d 13 0" -d 1000  -s "w d 13 1" -d 1000 -s "w d 13 0"
  • Exercise 3:
    • Take a wire and connect it between Pin 3 of the Arduino connector and Ground.
    • Use serial-arduino to request a list of the values currently on the digital pins.
    • Identify Pin 3 in the list of 0s and 1s (you will need to see how arduino_loop.pde generates the string of 1s and 0s).
    • Now connect Pin 3 to Vcc using the same wire. Now Pin 3 sees a high voltage.
    • Use serial-arduino to read the digital pins
    • Verify that you can read the changing value of Pin 3 this way

The Assembly program

Step 1: C + asm = new program

You will have probably noticed the two commented lines in serial-arduino containing asm_main in them, one to indicate that asm_main is extern, i.e. defined somewhere else, and one to call it at the end of the loop that processes the command line arguments.

  • Uncomment the two asm_main statements.
  • Save talkToArduino.asm on the computer:
;;; talkToArduino.asm
;;; D. Thiebaut
;;; 
;;;  nasm -f elf talkToArduino.asm
;;;  gcc -o talkToArduino  arduino-serial.c  talkToArduino.o
;;;

;;; ----------------------- EXTERN LABELS -----------------------
extern serialport_writebyte	; int function
extern serialport_write		; int function
extern serialport_read_until	; int function
extern displayBuffer		; int function
	
extern buf	
extern byte		
        
        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
msg1	db	"w d 13 1", 0
msg1len	equ	$-msg1
	
	
        
        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:
	
	;; turn Pin 13 On 
  	mov	eax, msg1
  	mov	ecx, msg1len
  	call	copyMsg                     ;create copy of msg1 in buf, in C prog
  	call	serialport_write           ;call function in C prog

	
        ;; return to C program
	
        ret

;;; ----------------------------------------------------------------
;;; copyMsg1: puts array whose address in eax  in external buffer
;;; 	      number of bytes shoudl be in ecx.
;;; ----------------------------------------------------------------
copyMsg:
	pushad
	mov	esi, eax	; source buffer
	mov	edi, buf	; destination buffer in C program
.for	mov	al, [esi]
	mov	[edi], al
	inc	esi
	inc	edi
	loop	.for
	popad
	ret
  • Assemble the code and link it with the C program:
nasm -f elf talkToArduino.asm
gcc -o arduino-serial  arduino-serial.c  talkToArduino.o
  • Make sure Pin 13 is off. Then run the program, but without specifying any options relating to the pins. This time we want the assembly program to do the work!
./arduino-serial -b 9600 -p /dev/ttyUSB0

Step 2: More assembly

  • Exercise 4: Modify the assembly language program and add another message, msg2, that can be used to turn Pin 13 Off. Modify the body of the asm_main function so that it will turn Pin 13 ON and OFF a few times.
;;; talkToArduino.asm
;;; D. Thiebaut
;;; 
;;;  nasm -f elf talkToArduino.asm
;;;  gcc -o talkToArduino  arduino-serial.c  talkToArduino.o
;;;

;;; ----------------------- EXTERN LABELS -----------------------
extern serialport_writebyte	; int function
extern serialport_write		; int function
extern serialport_read_until	; int function
extern displayBuffer		; int function
	
extern buf	
extern byte		
        
        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
msg1	db	"w d 13 1", 0
msg1len	equ	$-msg1
msg2	db	"w d 13 0", 0
msg2len	equ	$-msg2
	
	
        
        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:
	
	;; turn Pin 13 Off 
  	mov	eax, msg2
  	mov	ecx, msg2len
  	call	copyMsg                     ;create copy of msg1 in buf, in C prog
  	call	serialport_write           ;call function in C prog
	
	;; 	turn Pin 13 On
	mov	eax, msg1
	mov	ecx, msg1len
	call	copyMsg
	call	serialport_write

	
        ;; return to C program
	
        ret

	
;;; ----------------------------------------------------------------
;;; copyMsg1: puts array whose address in eax  in external buffer
;;; 	      number of bytes shoudl be in ecx.
;;; ----------------------------------------------------------------
copyMsg:
	pushad
	mov	esi, eax	; source buffer
	mov	edi, buf	; destination buffer in C program
.for	mov	al, [esi]
	mov	[edi], al
	inc	esi
	inc	edi
	loop	.for
	popad
	ret
  • Exercise 5: Add the delay function to your assembly program and make Pin 13 blink with a 0.1 second delay between transitions.
;;; talkToArduino.asm
;;; D. Thiebaut
;;; 
;;;  nasm -f elf talkToArduino.asm
;;;  gcc -o talkToArduino  arduino-serial.c  talkToArduino.o
;;;

;;; ----------------------- EXTERN LABELS -----------------------
extern serialport_writebyte	; int function
extern serialport_write		; int function
extern serialport_read_until	; int function
extern displayBuffer		; int function
	
extern buf	
extern byte		
        
        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
msg1	db	"w d 13 1", 0
msg1len	equ	$-msg1
msg2	db	"w d 13 0", 0
msg2len	equ	$-msg2
	
	
        
        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:
	
	;; turn Pin 13 Off 
  	mov	eax, msg2
  	mov	ecx, msg2len
  	call	copyMsg                     ;create copy of msg1 in buf, in C prog
  	call	serialport_write           ;call function in C prog

	call	delay
	
	;; 	turn Pin 13 On
	mov	eax, msg1
	mov	ecx, msg1len
	call	copyMsg

	call	serialport_write

	call delay

	
        ;; return to C program
	
        ret

;;; ---------------------------------
;;; delay: creates a 0.1sec delay
;;; ---------------------------------
delay:	
	pushad
	mov	ecx, 100000000	; 2 x 100,000,000 cycles (assuming 2GHz)
.for	add	eax, 1		; 1 cycle
	loop	.for		; 1 cycle
	popad
	ret


	
;;; ----------------------------------------------------------------
;;; copyMsg1: puts array whose address in eax  in external buffer
;;; 	      number of bytes shoudl be in ecx.
;;; ----------------------------------------------------------------
copyMsg:
	pushad
	mov	esi, eax	; source buffer
	mov	edi, buf	; destination buffer in C program
.for	mov	al, [esi]
	mov	[edi], al
	inc	esi
	inc	edi
	loop	.for
	popad
	ret

Step 3: Reading the status of a pin

We are now going to do the same experiment of Exercise 3, but this time the assembly program will

  1. Pick a pin, say Pin 3, and connect a wire to it. Then connect this wire to GND, or to +5V.
  2. send a command to the Arduino to request the status of all the pins
  3. read a string from the Arduino
  4. figure out where Pin 3 appears in the string
  5. get the '0' or the '1' corresponding to Pin 3
  6. put this '0' or this '1' character at the 7th position of a string that contains "w d 13 ?"
  7. send this string to the Arduino to turn Pin 13 ON or OFF, depending on what Pin 3 is connected to.
  • Exercise 6: Go ahead and make your assembly program implement all the steps required!
    • The solution to this exercise was put up here.

Navigation

Return to Main Page