Difference between revisions of "CSC231 Lab 7"
(→The Assembly Program) |
|||
Line 84: | Line 84: | ||
===The Assembly Program=== | ===The Assembly Program=== | ||
− | You will have probably noticed the two commented lines in serial-arduino containing asm_main in them. | + | 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, please. | ||
+ | |||
+ | * On the Ubuntu PC, open emacs and create a file called '''talkToArduino.asm''' | ||
+ | |||
+ | <code><pre> | ||
+ | ;;; 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 Off | ||
+ | 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 |
Revision as of 07:14, 7 November 2008
Back to CSC231 Weekly Schedule
Contents
CSC 231 Lab # 7
© D. Thiebaut, 2008
Before you start, you will need to setup the Ubuntu the same way we did in Lab #6, so that you are in the root folder of ubuntu, logged in as root, and you have copied your environment from the USB stick to your Ubuntu root account.
Input/Output PC to Arduino: Part I
In this first part you will enter commands at the Linux prompt to set the Arduino LED 13 On and Off, or to read the status of various pins.
Don't miss the presentation that will be done in class. We'll go over the 3 programs involved in today's lab.
The Arduino program: arduino_loop.pde
- First take a close look at the program that runs on the arduino: arduino_loop.pde, whose code is available here. Notice how the loop function continuously store incoming characters in the buffer until a \n character is found, in which case the contents of the buffer is analyzed, the command identified, and executed.
- Cut and paste arduino_loop.pde in the arduino GUI on your Ubuntu machine.
- Compile it.
- Upload it to the Arduino.
The C-Program running on the Ubuntu: arduino-serial.c
- Take a close look at arduino-serial.c . It is the program running on the Ubuntu PC/laptop. You should copy/paste it into an emacs window and save it to your
- compile the program as follows:
gcc -o arduino-serial arduino-serial.c
- run it as follows:
./arduino-serial -b 9600 -p /dev/ttyUSB0 -s "w d 13 1"
- where -b is the switch used to set the communication speed (9600 baud--very slow), -p is used to set the port associated with the USB connection, and -s means Send a message to the arduino. Here the message is Write a 1 on Digital Pin 13 of the arduino.
- Try to read the digital pins, too:
./arduino-serial -b 9600 -p /dev/ttyUSB0 -s "r d" -r
- In this case you make the program send a string first (the request to read), and receive next. In receive mode, the arduino-serial program simply wait and fills a buffer with characters received until it gets a '\n' character. Then it displays what it has received.
EXERCISE #1 Modify the command line so that you will turn Pin 13 ON, then OFF, then ON, then OFF again. |
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. |
EXERCISE #3
|
The Assembly 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, please.
- On the Ubuntu PC, open emacs and create a file called talkToArduino.asm
;;; 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 Off
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