231a-ag Lab7

From CSclasswiki
Jump to: navigation, search

Lab 7: Input/Output PC to Arduino

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

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.
    • " -d --delay=millis Delay for specified milliseconds\n"
  • 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 turned ON again.
./arduino-serial -b 9600 -p /dev/ttyUSB0  -s "w d 13 1" -d 100 -s "w d 13 0" -d 100 -s "w d 13 1" -d 100 -s "w d 13 0"

Exercise #3

  • Use serial-arduino to request a list of the values currently on the digital pins.
    • [d 0 0 0 0 1 0 1 1 1 1 1 1
  • 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).
    • Pin 3 is the second 0 in the list of 0s and 1s.
  • Use serial-arduino to read the digital pins. (After connecting Pin 3 to Vcc using the same wire, and Pin 3 sees a high voltage.)
    • [d 0 1 0 0 0 0 0 0 0 1 1
  • Verify that you can read the changing value of Pin 3 this way.
    • Yes, because the second 0 in the list of 0s and 1s changed to a 1.

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.
        ;; -------------------------
        ;; 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 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

	;; 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                     ; create copy of msg1 in buf, in C prog
  	call	serialport_write           ; call function in C prog

	;; 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

	
        ;; return to C program
	
        ret

Exercise #5

  • Add the delay function to your assembly program and make Pin 13 blink with a 0.1 sec delay between transitions.
        ;; -------------------------
        ;; 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 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

  	call	delay100ms

	;; 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

	
        ;; return to C program
	
        ret

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