Вы находитесь на странице: 1из 22

Embedded Systems and Software

Potpourri & Notes on Lab 2

Artist's concept of Mars Exploration Rover. Courtesy NASA

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 1
The AVR Assembler

• We use the AVRASM2 assembler that comes with AVR


studio
• “Assembler” and NOT “compiler” …
• The Assembler performs the conversion from English
mnemonics to opcodes that are programmed into the
microcontroller
• C compilers generate assembly language mnemonics
that are then assembled by an assembler
• Much confusion arises when one does not have a
conceptual understanding of the different assembly
phases

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 2
The assembler’s text substitution allows one to use symbols rather than numbers

.equ PORTB = 0x18 .include "tn45def.inc"


.equ DDRB = 0x17 .cseg

.include "tn45def.inc" sbi DDRB,1

loop:
sbi PORTB,1
cbi PORTB,1
rjmp loop

Phase 1: text substitution

000000 sbi 0x17,1

loop:
000001 sbi 0x17,1
These are program addresses
000002 cbi 0x18,2
000003 cbi 0x18,1
000004 rjmp 0x01

Phase 2: machine instructions,…

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 3
The assembler’s text substitution allows one to use symbols rather than numbers

.include "tn45def.inc" .include "tn45def.inc"


.cseg .cseg

sbi DDRB,1 sbi DDRB,1


sbi DDRB,2
loop:
sbi PORTB,1 loop:
cbi PORTB,1 sbi PORTB,1
rjmp loop cbi PORTB,1
rjmp loop

000000 sbi 0x17,1


000000 sbi 0x17,1
loop: 000001 sbi 0x17,2
000001 sbi 0x17,1
000002 cbi 0x18,2 loop:
000003 cbi 0x18,1 000002 sbi 0x17,1
000004 rjmp 0x01 000003 cbi 0x18,2
000004 cbi 0x18,1
000005 rjmp 0x02

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 4
Assembler Directives

Assembler directives instruct the assembler what to do

The directives are not translated directly into opcodes.

Instead, they are used to adjust the location of the program in memory, define
macros, initialize memory and so on.

This does not generate opcodes.


.include "tn45def.inc" Rather it direct the assembler to
.cseg include replace this line with the
contents of “tn45def.inc”
sbi DDRB,1

loop:
sbi PORTB,1
cbi PORTB,1
rjmp loop

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 5
We have seen this directive
several times thus far

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 6
.INCLUDE – Include another file ASM Directive

The INCLUDE directive tells the Assembler to start reading from a specified file. The
Assembler then assembles the specified file until end of file (EOF) or an EXIT directive is
encountered. An included file may itself contain INCLUDE directives.

Contents of iodefs.asm

Include this in another AM program, and one can use symbols such as sreg
rather than 0x3f:

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 7
.EXIT - Exit this file ASM Directive

The EXIT directive tells the Assembler to stop assembling the file. Normally, the Assembler
runs until end of file (EOF). If an EXIT directive appears in an included file, the Assembler
continues from the line following the INCLUDE directive in the file containing the INCLUDE
directive.

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 8
Assembler Functions

This returns the low byte

ldi r19,low(0x07F3)
This returns the high byte
ldi r20,high(0x07F3)

Text substitution & apply functions

ldi r19,0xF3
ldi r20,0x07
Generate opcodes

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 9
Assembler Functions
LOW and HIGH are often used, others less frequently, and we will not cover these
in this course

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 10
Assembler Preprocessor
The AVRASM2 preprocessor is modeled after the C preprocessor, with some exceptions
(see AVRASM2 documentation).

#define #if #pragma Operators:

#elif #ifdef #undef # (stringification

#else #ifndef #warning ## (concatenation)

#endif #include # (empty directive)

#error #message

.EQU c1 = 0b11011010
.equ c2 = 0b10101010
As with the C preprocessor, the AVRASM2
#define DEBUG preprocessor allows conditional processing.

#ifdef DEBUG For example, one can build a special debug version as
ldi r18,low(c1|c2) shown in the snippet.
rcall delay
#else
ldi r18,low(c1^c2)
#endif

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 11
Microcontroller Fuses
AVR ATtiny45 ® 8-Pin Microcontroller

• Except for GND and VCC all other pins can perform at least 4 functions
• PB5 can be hardware RESET or an ADC input
• PB3 & PB4 can be ADC inputs or where crystal for external oscillator
goes
• How does one configure controller for the external environment?

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 12
Microcontroller Configuration/Fuses
AVR ATtiny45 ® 8-Pin Microcontroller

• Except for GND and VCC all other pins can perform at least 4 functions
• PB5 can be hardware RESET or an ADC input
• PB3 & PB4 can be ADC inputs or where crystal for external oscillator
goes
• How does one configure controller for the external environment?

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 13
Microcontroller Configuration/Fuses

• The Configuration Fuses (Configuration Bits) are the settings


that configure a microcontroller for the external environment
it is expecting to find
• Typical settings include
– Oscillator Type, REST pin usage
– Code Protection
– Brown-Out and Watchdog Timer usage
– Low Voltage Programming
– …
• Different microcontrollers, and members within a device
family has its own fuses

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 14
Enables use of WDT

This chooses if the clock is divided


by 8 or not, start-up time,…

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 15
Vcc
Vcc

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 16
Switch Bounce and Debouncing

Bounce

~ 5 ms

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 17
Switch Bounce and Debouncing

When the switch closes, the


capacitor discharges through
Rb

Question: what is the fall time?


Voltage

Make sure you can calculate this.

t
Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 18
Software Debounce
One idea. Sample n times at regular intervals, say 10 ms apart. Count how many times the
switch is zero . In this is larger than the number of times the switch is high, consider the
switch pressed.

char isPressed(void)
{
char ones=0, zeroes=0, i;

for(i=0;i<=10-1;i++){
if(PINA&0x01){ // read pin == 1
ones++;
} else { // read pin == 0
zeroes++;
}
_delay_ms(10);
}
return (ones > zeroes);
}

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 19
Testing Status of an I/O PIN
...
cbi DDRB,0
cbi DDRB,1
...
SBIS PINB,0
...

This instruction tests a single bit in


an I/O register and skips the next
instruction if the bit is set.

For input use PINB,not PORTB

...
cbi DDRB,0
cbi DDRB,1
...
SBIC PINB,0 Also, the is a companion instruction SBIC (Skip
... next instruction if bit in I/O is Clear)

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 20
Which PIN to Use?
There are 3 available PINs for use
as switch input: PINB0, PINB1,
PINB2.

Which one should you use?

These are used by the


programmer when it downloads
code, so adding a debounce
capacitor may interfere. You may
have to experiment which PIN
works best as an input pin.

Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 21
Embedded Systems and Software, 55:036. The University of Iowa, 2013 Lab 2 Notes Slide 22

Вам также может понравиться