ias0430 microprocessor systems€¦ · demonstrate writing text to lcd/led screen or serial output...

28
IAS0430 MICROPROCESSOR SYSTEMS Fall 2018 Arduino and assembly language Martin Jaanus U02-308 [email protected] 620 2110, 56 91 31 93 Learning environment : http://isc.ttu.ee Materials : http:// isc.ttu.ee/martin

Upload: others

Post on 17-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

IAS0430

MICROPROCESSOR

SYSTEMS

Fall 2018

Arduino and assembly language

Martin Jaanus U02-308

[email protected] 620 2110, 56 91 31 93

Learning environment : http://isc.ttu.ee

Materials : http://isc.ttu.ee/martin

Page 2: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Topics

The digital electronics in analogue world

. Write assembly code to initialize an AVR ATmega 328P and

run code on it

. Understand how it works line by line

. Trying this in using the Arduino environment

. Upload the code to Arduino Uno

Page 3: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Homeworks (can be done also in LAB)

• The aim is to demonstrate basic skills of programming in assemby language

• You need to prepare your code and demonstrate its working ability to teacher (teacher may ask you to make some modifications )

• Before defending you need to upload your code in learning environment using pdf format.

• You can book a Arduino board with multi-functional shield (like Homelabkit) but you can use your own board and extensions (even with different microprocessor)

30 sets avaiable for booking

From the number of completed tasks depends

your grade.

Page 4: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Homeworks

• 5 tasks with different difficulty

1. Demonstrate usage of hardware output of MCU (ex. Write some led blinnking program) – 500 points

2. Demonsrate usage of hardware input of MCU (ex. You press buttons and something happen) – 500 points

3. Demonstrate writing text to LCD/LED screen or serial output of MCU – 600 points

4. Demonstrate usage of MCU’s analogue input. -600 points

5. Demonstrate usage of MCU’s timer(s) and/or interrupt(s). -600 points

All tasks (or at least the major part of code ) must be written in assembler !

Page 5: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

The assembly example of led blinker

• Example – Atmega328 (Arduino)

• Three universal ports

• Arduino – development environment

• Needs skill of using datasheet!

Here and next AVR pictures :

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2545-8-bit-AVR-Microcontroller-ATmega48-88-168_Datasheet.pdf

Page 6: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Open Datasheet — Prepare a solution

• You must read the datasheet !

• Find the Register Summary and you’ll find all the records

and object of our studies. Here you find the three main

registers of atmel avr i/o ports: PORTx, DDRx and PINx

Page 7: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Open Datasheet — Prepare a solution

• Here’s a drawing of their basic functionality: as you can see, there’s

an internal pull-up for every pin. It can be activated by setting the

DDR bit of the pin to 0 and the Port bit to 1. A cleared DDR bit means

that the pin is an input pin. So the pin is disconnected from the Port

register (see the driver in the drawing?) and the pin is floating. In this

case the Port bit controls the pull-up.

• We highlight the configuration we will take in our project: we put the

DDRx to 1 and we will switch PORTx between the high and low states

to blink the LED.

• For writing code only some

instructions can be used.

Page 8: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Install and run Arduino environment

• Install Arduino environment and makse sure it is

connected and working ! (check the connection) !

• You can also test the LED blinking sketch from examples !

https://www.arduino.cc/

Page 9: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Using Assembly code in C projects

• One possibility is to do like this:

void somefunction(void) __attribute__((naked))

{

asm volatile ("

; your assembly code here

");

}

It is often used when the main program is written in C but it is needed to

use ASM operations (usually for percise timing – you cannot calculate

processor cycles in C),

Page 10: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Full Assembly in Arduino Environment

1. Make empty project/sketch File > New , name as you want.

2. Modify the default code in in this way:

extern "C" {

// function prototypes

void start();

;

}

void setup() {

start();

}

void loop() {

}

The name of exetutable routine

Page 11: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Full Assembly in Arduino Environment (2)

Add your assembly project file:

1.Click the upside down triangle button at the top right of

the editor window.

2.New Tab > Name for new file: > blink.S - actually you can

give it any name you like as long as it has the .S extension.

It has to be an upper case S.

Page 12: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Full Assembly in Arduino Environment (3)

• You need to include definitions and set program start

offset:

#define __SFR_OFFSET 0

#include "avr/io.h"

.global start

start:

; the assembly code to execute

sbi DDRB,5 ; Set PB5 as output

.......

This must be same in main .ino file

Page 13: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

#include "avr/io.h"

• Gives the names for registers

• It gives possibility to use predefined names

• You need to use definations

written in datasheet!

The assember does not accept

Arduino definations !

Page 14: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Init your code

The simpest possible program: .ORG 0x000 ; the next instruction has to be written to add 0x0000

; this is required, when native ASM compiler is used

; infinite loop

START: ; this is a label “START”

rjmp START ; Relative JuMP to START

The compiler translates labels into addresses respect to starting

address

When the instruction is executed, the CPU (ALU—Arithmetic Logic

Unit) will jump to START. The jump will be repeated over and over,

resulting in an infinite loop.

Starting address

Page 15: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Do the math

• Assuming that our Arduino AVR is running at 16 MHz (16

Million clock cycles per second), how long does all this

take?

• T = 1/ F then T = 1 / 16E6

• T = 0,0000000625 seconds, not precisely 0,06 us

• That’s pretty fast! We can not see at this frequency the

blinking of the LED. So coming to the answer it has been

observed experimentally that human eye can’t make out a

difference in the picture frame if it appears for less than

16ms to 13ms (0,016-0,013s).

Page 16: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Do the math (2)

• That’s pretty fast! We can not see at this frequency the blinking of the LED. So coming to the answer it has been observed experimentally that human eye can’t make out a difference in the picture frame if it appears for less than 16ms to 13ms (0,016-0,013s). Hence purely based on this we can say sampling frequency is 60 Hz to 80 Hz (about 0,06 & 0,08 MHz).

• So we will need to know how many times we will loop to in terms of a cycle of say half a second off and half a second on so that the LED flashes every half a second at a frequency of 16 MHz we need x cycles. Here’s how:

• if 1 cycle takes -----------0,0000000625 seconds

• x cycles will take -----------0,5 seconds

• this makes 0,5 / 0,0000000625 s = 8 000 0000 cycles

• How to achieve all this cycles if I can count only up to 255 (remember, we have an 8-bit chip ) ?

Page 17: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Do the math (3)

• For this we will have to implement two loops: inner and

outloop.

Calculations - The inner loop is treated like one BIG

instruction needing 262145 clock cycles.

• The registers can be used in pairs, allowing to work with

values from 0 to 65 535. That’s a word.

The following piece of code clears registers 24 and 25

and increments them in a loop until they overflow to zero

again.

When that condition occurs, the loop doesn’t go around

again.

Page 18: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Math to asm

• clr r24 ; clr needs 1 cycle

• clr r25 ; clr needs 1 cycle

• DELAY_05: ; we need .5s delay

• adiw r24, 1 ; adiw needs 2 cycles and

• brne DELAY_05 ; brne needs 2 cycles if the branch is done

• ; and 1 otherwise

Every time the registers don’t overflow the loop takes adiw(2) + brne(2)

= 4 cycles.

This is done 0xFFFF (65 535) times before the overflow occurs. The

next time the loop only needs 3 cycles, because no branch is done.

Page 19: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Math to asm

• The “outer” loop will be down-counting from 31 to zero using R16.

• ldi r16, 31

• OUTER_LOOP: ; outer loop label

• ldi r24, 0 ; clear register 24

• ldi r25, 0 ; clear register 25

• DELAY_05: ; the loop label

• adiw r24, 1 ; “add immediate to word”: r24:r25 are

• ; incremented

• brne DELAY_05

• dec r16 ; decrement r16

• brne OUTER_LOOP ; load r16 with 8

Page 20: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Math to asm

• The overall loop needs: 262 145 (inner loop) + 1 (dec) + 2 (brne) = 262 148 * 31 = 8 126 588 cycles.

• This is more like what we want, but 126 588 cycles too long.

• This is where the fine-tuning comes in — we need to change the initial value of r24:r25.

• The outer loop is executed 31 times and includes the “big-inner-loop-instruction”.

• We have to subtract some cycles from the inner loop: 126 588 / 31 = 4 083 cycles per inner loop.

• This is what the inner loop has to be shorter. Every iteration of the inner loop takes 4 cycles (the last one takes 3 but that’s not so important, if needed NOP can be added), so let’s divide those 4 083 by 4.

• That’s 1 020.8 or 1 021 less iterations.

• This is our new initial value for r24:r25

• he result is 8 000 000 clock cycles

Page 21: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Main code.ORG 0x0000 ; the next instruction has to be written to

; address 0x0000

rjmp START ; the reset vector: jump to "main"

START:

out SPH, r16ldi r16, 0xFF ; load register 16 with 0xFF (all bits 1)

out DDRB, r16 ; write the value in r16 (0xFF) to Data

; Direction Register BLOOP:

sbi PortB, 5 ; switch off the LEDrcall DELAY_05 ; wait for half a second

cbi PortB, 5 ; switch it on

rcall DELAY_05 ; wait for half a seconrjmp LOOP ; jump to loop

DELAY_05: ; the subroutine:

ldi r16, 31 ; load r16 with 31

OUTER_LOOP: ; outer loop label

ldi r24, 0

ldi r25, 0

DELAY_LOOP: ; "add immediate to word": r24:r25 are; incremented

adiw r24, 1 ; if no overflow ("branch if not equal"), go

; back to "delay_loop"

brne DELAY_LOOP

dec r16 ; decrement r16

brne OUTER_LOOP ; and loop if outer loop not finished

ret ; return from subroutine

Arduino example (for comparision):

void setup() { // initialize digital pin 13 as an output.

pinMode(13, OUTPUT); }

void loop() {

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Delay – function from library

Remember – if you using another Arduino board with different processor,

this Assembly code may not work, C code may work ! Refer to datasheet!!!!!

Page 22: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Send code to Arduino Uno• And save those for

future reference/

codes !

• The next task (code)

can be written by

using this „template“

Page 23: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Uno schematics

www.arduino.cc

Page 24: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Uno pinouts

• The yellow background shows the processor’s pin name

www.arduino.cc

Page 25: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Multifuncional Shield

• The complete circuit

http://blog.jeronimus.net/2017/04/arduino-multi-function-

shield.html

Page 26: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Multifuncional Shield

LED

• Notice that they are connected between Vcc and I/O pins !

• Active state is LOW, Arduino pins are 10,11,12,13

Buzzer is connected to pin 3

Page 27: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Multifuncional Shield

Buttons

• External pullup resistors

• Connected to Arduino pins A1,A2,A3

• Pressed state is LOW

Analogue input is connected to pin A0

Page 28: IAS0430 MICROPROCESSOR SYSTEMS€¦ · Demonstrate writing text to LCD/LED screen or serial output of MCU –600 points 4. Demonstrate usage of MCU’s analogue input. -600 points

Arduino Multifuncional Shield

• 7 segment LED array

2 of 74HC595 Shift registres are used as driver !

It has 3 pins to connect Arduino

RCLK – 4 (output clock)

SRCLK- 7 (input clock)

SER – 8 (data)

You need to refer to datasheet !http://www.ti.com/lit/ds/symlink/sn74hc595.pdf