EE 308: Microcontrollers Review Part I Aly El-Osery Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA February 15, 2018 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 1 / 19
Harvard Architecture overview data bus data bus Code memory control bus CPU control bus Data memory address bus Harvard architecture address bus Separate buses for accessing code and data Faster Less delays Requires more hardware Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 2 / 19
Data, address and control lines Data lines carry information in and out of the CPU. More lines results in faster data transfer but more complex and more expensive. Address lines identifies the devices and memory to be connected to the CPU Control lines control devices signals for read/write as directed by the CPU Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 3 / 19
Memory Register file: embedded in the CPU for fast operations Code memory: ROM typically flash Data memory: RAM typically SRAM Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 4 / 19
Addressing modes Immediate (single-register): operand is a register and may have a constant value as a second operand, e.g., NEG R18 ;negate the content of R18 LDI R19,0 x06 ;load 0x06 into R19 Register: two registers hold the data to be manipulated ADD R20, R23 ;add R23 to R20 and store the result in R20 Direct: operand is a memory location LDS R19,0 x560 ;load R19 with the content of mem loc 0x560 STS 0x40, R19 ;store R19 to data space location 0x40 Register indirect: operand is a register but in points to the memory location LDI XL,0 x30 ;load R26 (low byte of X) with 0x30 LDI XH,0 x01 ;load R27 (high byte of X) with 0x01 LD R18,X ;load R18 with content of memory 0x130 Auto-increment/auto-decrement: content of register is pre- or post- incremented/decremented after/before memory access LD Rn,X+ ;load Rn with content pointed to by X then inc X LD Rn,-X ;decrement X then load Rn by contented pointed to by new X Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 5 / 19
Arithmetic Logic Unit (ALU) A[n 1 : 0] B[n 1 : 0] Takes two inputs Status Performs arithmetic and logic operations Op Code ALU Status Result Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 6 / 19
Memory map Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 7 / 19
I/O space I/Os and peripherals are placed in I/O space I/O addresses are offset from memory address by 0x20 I/O registers in the range 0x00 to 0x1F are directly bit accessible IN and OUT commands must use addresses in the range 0x00 to 0x3F When using LD and ST commands I/O registers are addressed as data space and therefore 0x20 must be added to their addresses Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 8 / 19
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. include " m1284def. inc " ldi r16, hi8 ( RAMEND ) out SPH, r16 ldi r16, lo8 ( RAMEND ) 5 out SPL, r16 ;initialize stack pointer sbi DDRC, 0 ;set bit 0 of ddrc here : sbi PORTC, 0 10 call delay ;call delay subroutine cbi PORTC, 0 call delay rjmp here 15 delay : ldi r20, 255 dl1 : dec r20 brne dl1 ret Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 9 / 19
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. include " m1284def. inc " ldi r16, hi8 ( RAMEND ) out SPH, r16 ldi r16, lo8 ( RAMEND ) 5 out SPL, r16 ;initialize stack pointer sbi DDRC, 0 ;set bit 0 of ddrc here : sbi PORTC, 0 10 call delay ;call delay subroutine cbi PORTC, 0 call delay rjmp here 15 delay : ldi r20, 255 dl1 : dec r20 brne dl1 ret Directive Label Comment Instruction Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 9 / 19
Manipulating the stack PUSH instruction: stores onto the stack POP instruction: removes content from the stack Data is stored where the SP is pointing and then it is decremented Last-in-first-out Some microcontrollers increment the SP rather than decrement Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 10 / 19
Initializing the stack pointer When the program starts the stack pointer is set to zero and will need to be initialized so that it points to the end of the RAM. As different microcontrollers have different size RAM we use RAMEND to represent the address of the last RAM location Stack pointer is initialized as follows LDI r16, hi8 ( RAMEND ) ;load SPH OUT SPH, r16 LDI r16, lo8 ( RAMEND ) ;load SPL OUT SPL, r16 SP could be decremented up to the point above 0x100 for the ATmega1284 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 11 / 19
Program memory constant addressing 0x0001 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 12 / 19
Program memory constant addressing 2-bytes 0x0001 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 12 / 19
Program memory constant addressing 2-bytes 0x0000 0x0001 0x0001 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 12 / 19
Program memory constant addressing 2-bytes 0x0000 0x0010 0x0001 0x0011 0x0001 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 12 / 19
Program memory constant addressing 2-bytes 0x0000 0x0010 0x0001 0x0011 0x0001 The byte address of each location is the program address multiplied by 2 To access the high byte the Z register showed have a zero in the LSB To access the low byte the Z register showed have a one in the LSB GNU Assembler handles this natively In Atmel ASM2 you will have to multiply the desired address by 2 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 12 / 19
Branching Write an assembly program that implements the following if (T >= 90) F = H ; else if (T > 40) F = N ; else F = C ; Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 13 / 19
Branching Write an assembly program that implements the following if (T >= 90) F = H ; else if (T > 40) F = N ; else F = C ; Assuming that the value of T is loaded into r20 and F is r21 cpi r20,90 brlt elseif ldi r21, H rjmp end elseif : cpi r20,40 brlt else ldi r21, N rjmp end else : ldi r21, C end : Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 13 / 19
Branching Write an assembly program that implements the following for (i =0; i <10; i ++) y = i /2; Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 14 / 19
Branching Write an assembly program that implements the following for (i =0; i <10; i ++) y = i /2; Assuming that the value of i is loaded into r20 and y is r21 for : cpi r20, 10 brge end mov r21, r20 asr r21 inc r20 jmp for end : Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 14 / 19
General digital I/O details Typically there are three main registers that control digital I/O ports Data Direction Register (DDRx): Controls whether each pin of a port x is an output (value of 1) or input (value of 0). Port Register (PORTx): Setting the bit in this register corresponds to sending a high value to the pin. Clearing a bit corresponds to setting the pin to low. Reading the PORTx provides the value that was written to it. Port Input Register (PINx): Generally used for reading the value of a pin that was set as an input. At reset all port are configured as input, i.e., DDRx is set to zero Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 15 / 19
Pull-up resistors What will happen if an input pin is connected to a switch that is connected to ground? Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 16 / 19
Pull-up resistors What will happen if an input pin is connected to a switch that is connected to ground? If the switch is not pressed the input value to the pin is floating Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 16 / 19
Pull-up resistors What will happen if an input pin is connected to a switch that is connected to ground? If the switch is not pressed the input value to the pin is floating If the switch is pressed the pin is shorted and must provide lots of current Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 16 / 19
Pull-up resistors What will happen if an input pin is connected to a switch that is connected to ground? If the switch is not pressed the input value to the pin is floating If the switch is pressed the pin is shorted and must provide lots of current Need a pull-up resistor Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 16 / 19
Using I/O port example Given and alarm system with an indicator if it is armed and indicator if a door is open. If the system is armed turn on an LED. If the system is armed and a door opens sound the alarm. Only pins 0, 1, 6 and 7 of port A are available. Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 17 / 19
Using I/O port example - solution Make pins 0 and 7 an input to represent the status of the system and the door, respectively. Make pins 1 and 6 of port A an output to control the LED and the siren, respectively. Don t change any of the other pins. Keep checking pin 0, if it goes high send a high on pins 1 and check pin 7. If pin 7 goes high output a high on pin 6. Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 18 / 19
Using I/O port example - solution Make pins 0 and 7 an input to represent the status of the system and the door, respectively. Make pins 1 and 6 of port A an output to control the LED and the siren, respectively. Don t change any of the other pins. Keep checking pin 0, if it goes high send a high on pins 1 and check pin 7. If pin 7 goes high output a high on pin 6. in r16, DDRA in r17, PORTA ori r16, (1<< PA1 ) (1 < < PA6 ) andi r16, ~((1 < < PA0 ) (1 < < PA7 )) andi r17, ~((1 < < PA1 ) (1 < < PA6 )) out PORTA, r17 out DDRA, r16 l1: sbis PINA, 0 rjmp l1 sbi PORTA,1 l2: sbis PINA, 7 rjmp l2 sbi PORTA,6 l3: rjmp l3 Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 18 / 19
Generating a square wave example. include " m1284def. inc " ldi r16, hi8 ( RAMEND ) out SPH, r16 ldi r16, lo8 ( RAMEND ) 5 out SPL, r16 ;initialize stack pointer sbi DDRC, 0 ;set bit 0 of ddrc here : sbi PORTC, 0 10 call delay ;call delay subroutine cbi PORTC, 0 call delay rjmp here 15 delay : ldi r20, 255 dl1 : dec r20 brne dl1 ret Aly El-Osery (NMT) EE 308: Microcontrollers February 15, 2018 19 / 19