490 likes | 702 Vues
ESE 380 Midterm 1 Review. Created by: Mark Roszko. Topics of interest. General I/O terminology and use LEDs in simple microcontroller circuits AVR Address Spaces AVR Instructions and Directives Simple I/O assembly programs. General I/O Terminology and uses .
E N D
ESE 380 Midterm 1 Review Created by: Mark Roszko
(c) 2012 Mark Roszko Topics of interest • General I/O terminology and use • LEDs in simple microcontroller circuits • AVR Address Spaces • AVR Instructions and Directives • Simple I/O assembly programs
(c) 2012 Mark Roszko General I/O Terminology and uses • AVRs have an assortment of pins in groups called PORTs • PORTs have a maximum of 8 pins labeled as PX0, to PX7(where X is the port letter) • Pins can be configured as inputs or outputs independently • Input pins: • Can act as high-impedance entities in the circuit (they don’t output or draw any current) • Have the ability to have a weak-pull up to VDD enabled • Output pins: • Can only output a logic 1 or 0
(c) 2012 Mark Roszko IO Pin Configuration and Usage • All IO ports have three registers associated with them called DDRx, PORTx, PINx (where x is the letter associated with the port) • Each bit in a register represents an individual pin, the bit position corresponds to what pin. (i.e. bit 0 in PORTA means pin A0, bit 0 in DDRA affects the same pin A0) • DDRx • Data Direction Register • 0 = Input, 1 = Output • PORTx • Port Output Register, also enables internal pull-up • If pin is set to output • 0 = Logic 0 output, 1 = Logic 1 output as expected • If pin is set to input • 0 = No internal pull-up, 1 = internal pull-up enabled
(c) 2012 Mark Roszko IO Pin Configuration and Usage • PINx • Port Input Register • Reads the logic level at the pin, logical 0 or 1 returned • Read only register, you can write to it but nothing will happen DON’T MAKE THIS MISTAKE(IT’S COMMON)
(c) 2012 Mark Roszko LEDs - review • LEDs are diodes that create light when enough voltage is applied • LEDs have two specific ends called the anode and cathode • Acurrent-limitingresistoris required to be put in series with the LED on either the anode or cathode side of the LED. (Unless otherwise requested or noted on previous tests for LED connections).
(c) 2012 Mark Roszko LEDs (continued) • LED cathode vs. anode configuration • An LED which has its cathode connected to logic 0/GND, requires a microcontroller to output a logic 1 to light up. • An LED which has its anode connected to logic 1/5V, requires the microcontroller to output a logic 0 to the anode to light up. • Any other configuration such as the cathode connected to 5V is invalid as the current cannot flow through the LED(IT IS A DIODE HENCE THE SYMBOL!)
(c) 2012 Mark Roszko LEDs(continued) • A pin configured as an output can either sink(allows current to flow through the pin to ground internally) or source (allows current to flow from VDD internally to out the pin). • The amount of current being sourced or sinked also affects the output voltage of the pin. For example, you will not get 5V if drawing 20mA but maybe 4.8V instead • Atmel Datasheets provide pin output voltage characteristic curves showing voltage vs sink and source currents. (Usually provided on the exam if required)
(c) 2012 Mark Roszko LEDs(continued) Source: ATmega32 Datasheet
(c) 2012 Mark Roszko LEDs(continued) Source: ATmega32 Datasheet
(c) 2012 Mark Roszko LEDs (continued) • Value of current-limitingresistor should be computed or approximated • VH depends on the implementation • In the case of when a logic 1 is required to turn on the LED, VH is typically VOH at the desired current • In the case of when a logic 0 is required to turn on the LED, VH is (5V – VOL) at the desired current • All LEDs are not equal. Different color LEDs for example have different maximum forward voltage drops. These in turn correspond to different minimum values of the current-limiting resistor!
(c) 2012 Mark Roszko AVR Address Spaces • Composed of 5 main sections • Program memory • SRAM • Register file • I/O registers • EEPROM
(c) 2012 Mark Roszko AVR Address Spaces Source: ESE 380 Fall 2010 Exam
(c) 2012 Mark Roszko AVR Address Spaces • Program memory • Each row in program memory is 16 bits long(one word, or two bytes) • 8192(2^13) total rows in program memory(equates to 0x1fff/8191 as the first address starts at 0!) • FLASHEND is a constant to define the max value of 0x1ffff specifically for the end of program memory
(c) 2012 Mark Roszko AVR Address Spaces - SRAM Data Memory • SRAM Data Memory • An address space with a internal component and an optional external component. • Physically, the register file and I/O registers exist in SRAM. The register file and I/O registers can also be addressed via SRAM space. • 8 bits wide unlike the program memory
(c) 2012 Mark Roszko AVR Address Spaces - SRAM Data Memory • Register file • There are 32 registers r0 to r31, hence the register file takes up address 0x00 to 0x1f(32 total addresses. • I/O registers • The registers start immediately after the register file and start from 0x20. • There are 64 addresses set aside for I/O functions so it ends at 0x5f. • Internal SRAM • Starts immediately after I/O registers at 0x60 and run to 0x045f. (1024 bytes avaliable) • ATmega32 has double the SRAM, other AVRs can have more or less the SRAM amount.
(c) 2012 Mark Roszko AVR Address Spaces - SRAM Data Memory • Internal SRAM continued. • RAMEND defined as the end value for the INTERNAL SRAM. This means 0x45f in the case of the ATmega16 • External SRAM • Optional feature for external memory chip to be connected to an AVR to provide more memory, has the potential to be addresses as if it was internal
(c) 2012 Mark Roszko AVR Address Spaces - SRAM Data Memory Source: ATmega16 Datasheet
(c) 2012 Mark Roszko AVR Address Spaces – Register File • Physically located in SRAM but can be addressed separately from SRAM. • Starts at address 0x00 and runs to 0x1f like it does in SRAM. • The number of registers does not change(still 32) and the width is still 8 bits throughout it.
(c) 2012 Mark Roszko AVR Address Spaces – I/O Registers • Physically located in SRAM just like the register file • Can be addressed independently from the SRAM • Starts at address 0 and runs to address 0x3f for 64 total addresses
(c) 2012 Mark Roszko AVR Address Spaces - EEPROM • Another form of memory • Separate from the SRAM and program memory • Starts at address 0 and goes until 0x1fff for an ATmega16 for a total of 512 total addresses. • Total number of bytes/addresses stored in this memory depends on the AVR(i.e. ATmega32 has double for a total of 1024). • Has defined constant of EEPROMEND to represent the maximum address of 0x1fff for the case of ATmega16
(c) 2012 Mark Roszko AVR Instructions & Directives • Instructions ldi, in, out, com, rjmp, jmp nop, sbi, cbi and, andi, or, ori, eor, cbr, sbr lsl, lsr, rol, ror dec, inc • Directives include, list, nolist, def, undef, cseg, org, equ, device
(c) 2012 Mark Roszko AVR Instructions – ldi Example usage ldi r16,$FF ; load r16 with the value of 255/0xff Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – in Example usage in r16, PINA ;read PINA register into r16 Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – out Example usage ldi r16, 0b00001111 ;load r16 with port config out DDRA, r16 ;set PA7,6,5,4 to outputs; PA3,2,1,0 as inputs Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – com Example usage in r16, PINA ;read PINA input values com r16 ;complement r16 which was loaded with PINA(if we read 0b11110000, now it’s 0b00001111) out PORTB, r16 ;output to PORTB register 16 values Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – rjmp & jmp • rjmp (relative jump) • Jumps to another part of program memory to continue program execution • “Relative” because it does a +/- move to the new location based on your current location • Relative jump saves one word of program memory but is limited in range • jmp(jump) • Jumps to another part of program memory to continue execution just like rjmp • Can jump anywhere unlike rjmpbecause it uses 2 words of program memory able to store the full address
(c) 2012 Mark Roszko AVR Instructions – rjmp & jmp • In most cases you can use either rjmp or jmp. • rjmp should be preferred to save program memory • jmp should be used when rjmp cannot(due to range) • In assembly code you just include the label after the jump instruction and the compiler will automatically compute and place the jump address Example usage loop: in r16, PINA ;read PINA input values com r16 ;complement r16 which was loaded with PINA(if we read 0b11110000, now it’s 0b00001111) out PORTB, r16 ;output to PORTB register 16 values rjmp loop ;jump back to the label loop Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – nop • nopstands for nothing, it literally means no operation • The instruction is executed and it uses one clock cycle but nothing will occur as a result of the nop • Used for introducing delays and timing Example usage loop: in r16, PINA ;read PINA input values com r16 ;complement r16 which was loaded with PINA(if we read 0b11110000, now it’s 0b00001111) out PORTB, r16 ;output to PORTB register 16 values nop ;do nothing for one clock cycle rjmp loop ;jump back to the label loop
(c) 2012 Mark Roszko AVR Instructions – sbi Notes: • Sets a specified bit in a I/O register(as seen in the AVR address spaces) • This means registers r0 to r31, do NOT function directly with sbi, they are NOT I/O registers • Only works with the first 32 addresses in the I/O register Example usage sbi PORTD, 4 ; set PD4 to a 1 Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions –cbi Notes: • The exact opposite of sbi • Sets a specified bit in a I/O register(as seen in the AVR address spaces) • This means registers r0 to r31, do NOT function directly with cbi, they are NOT I/O registers • Only works with the first 32 addresses in the I/O register Example usage cbi PORTD, 4 ; set PD4 to a 0 Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – Logical AND & OR • and • Logical AND operation of two registers • All bits are ANDed with the matching bits in the second register based on position.(i.e. bit 0 in r16 with bit 0 in r17, bit 1 in r16 with bit 1 in r17,etc). Example usage ldi r16, 0b00001111 ldi r17, 0b11110000 and r16, r17 ;result should be r16 = 0, while r17 remains unchanged • or • Logical OR operation of two registers • All bits are ORed with the matching bits in the second register based on position.(i.e. bit 0 in r16 with bit 0 in r17, bit 1 in r16 with bit 1 in r17,etc). Example usage ldi r16, 0b00001111 ldi r17, 0b11110000 or r16, r17 ;result should be r16 = 0b11111111, while r17 remains unchanged
(c) 2012 Mark Roszko AVR Instructions – Logical ANDI & ORI • andi • Logical AND operation of destination register and a constant you define • All bits are ANDed with the matching bits in the constant based on position.(i.e. bit 0 in r16 with bit 0 in r17, bit 1 in r16 with bit 1 in r17,etc). Example usage ldi r16, 0b00001111 andi r16, 0b11110000 ;result should be r16 = 0 • ori • Logical OR operation of two registers • All bits are ORed with the matching bits in the constant based on position.(i.e. bit 0 in r16 with bit 0 in r17, bit 1 in r16 with bit 1 in r17,etc). Example usage ldi r16, 0b00001111 or r16, 0b11110000 ;result should be r16 = 0b11111111, while r17 remains unchanged
(c) 2012 Mark Roszko AVR Instructions – Logical EOR • eor • Logical XOR operation of two registers • All bits are XORed with the matching bits in the second register based on position.(i.e. bit 0 in r16 with bit 0 in r17, bit 1 in r16 with bit 1 in r17,etc). Example usage ldi r16, 0b11111111 ldi r17, 0b11110000 eor r16, r17 ;result should be r16 = 0b00001111, while r17 remains unchanged
(c) 2012 Mark Roszko AVR Instructions – sbr Notes: • Different from sbi!!! • Only works from registers r16 to r31 • Sets MULTIPLE BITS with the constant • Sets bits by using the OR operation with the constant K you define, meaning if the bits are already a logical 1, they remain a 1, if they are a zero then they become a 1. Example usage ldi r16, 0 ;set r16 to 0 to clear it sbr r16, 0b11001100 ;sets bits 7,6,3,2 to a 1 if they aren’t already Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – cbr Notes: • Different from cbi!!! • Opposite of sbr • Only works from registers r16 to r31 • Clears MULTIPLE BITS based on which bit is a 1 in your constant • Sets bits by using the AND operation with the 1’s complement of the constant. (i.e. a constant of 0b00001111 is complemented internally to 0b11110000 and then ANDed with the register). Example usage ldi r16, $FF ;set r16 to 0 to clear it cbr r16, 0b11001100 ;sets bits 7,6,3,2 to a 0 if they aren’t already Source: AVR Instruction Set
(c) 2012 Mark Roszko AVR Instructions – inc & dec • inc • Basically a math operation to add only 1 to the register • Works on any register r0 to r31 • This value will overflow if you are at the max 8 bit value 255 and increment, causing it to go to 0 • One use is when you need to keep track of how many iterations of a loop you have done Example usage ldi r16, 43 ;load r16 with the decimal value 43 inc r16 ;increment r16, it should now be 44 • dec • Basically a math operation to subtract only 1 from the register • Works on any register r0 to r31 • This value will underflow if you are at 0 and decrement, causing it to go to 255 Example usage ldi r16, 43 ;load r16 with the decimal value 43 decr16 ;decrement r16, it should now be 42
(c) 2012 Mark Roszko AVR Instructions – lsl& lsr • lsl • Shifts a register leftby one position, i.e. bit 6 becomes bit 7, bit 4 becomes bit 5,etc • A 0 is introduced at the “end” of the left shift in bit 0. • Bit 7 is “popped” off and placed into the CARRY bit of the STATUS REGISTER Example usage ldi r16, 0b11001100 ;load r16 lsl r16 ;shift r16, it should now be 0b10011000 • lsr • Shifts a register right by one position, i.e. bit 7 becomes bit 6, bit 5 becomes bit 4,etc • A 0 is introduced at the “end” of the right shift in bit 7. • Bit 0 is “popped” off and placed into the CARRY bit of the STATUS REGISTER Example usage ldi r16, 0b11001100 ;load r16 lsr r16 ;shift r16, it should now be 0b01100110
(c) 2012 Mark Roszko AVR Instructions – rol& ror • rol • Similar to lsl, only one real difference • The value in the CARRY bit of the STATUS REGISTER is shifted in as bit 0 • Bit 7 is “popped” off and placed into the CARRY bit of the STATUS REGISTER as before. THIS DOES NOT MAKE IT BIT 0. Example usage ldi r16, 0b11001100 ;load r16 rol r16 ;shift r16, it should now be 0b10011000 • ror • Similar to lsr, only one real difference • The value in the CARRY bit of the STATUS REGISTER is shifted in as bit 7 • Bit 0 is “popped” off and placed into the CARRY bit of the STATUS REGISTER. THIS DOES NOT MAKE IT BIT 0. Example usage ldi r16, 0b11001100 ;load r16 ror r16 ;shift r16, it should now be 0b01100110
(c) 2012 Mark Roszko AVR Instructions – Clock cycles • Every AVR instruction takes a predefined amount of clock cycles to execute • Some instructions like sbiand cbi which modify I/O register bits end up having no “visible” result until after the 2nd clock cycle. Meaning if you set a bit, it won’t change until after the second cycle. • The AVR Instruction Set document provided by Atmel has the number of cycles provided for each instruction
(c) 2012 Mark Roszko AVR Assembly Directives • .include • Includes another assembly file, at least one call is required for the definition file, in the case of the ATmega16, you need to use .include “m16def.inc” • Can include any file including other assembly files if you want to split up your code • Compiler ends up “joining” the files together into one large file before it actually compiles based on the .include files • .nolist • Typically used around the .include “m16def.inc” on purpose • Tells the compiler not to print out the contents of the file into the list file • This does not mean the file does not get used in compiling! • .list • The opposite of .nolist, allows the content after the directive to be included in the list file
(c) 2012 Mark Roszko AVR Assembly Directives • .def • Allows you to “name” registers to anything you want • Makes life easier if you reserve registers for specific uses • Example: .def REGISTER_OF_AWESOME = r16 ldi REGISTER_OF_AWESOME, $0f • This is simply a compiler feature, it will take care of replacing the names with the proper addresses when it compiles • You can give a register multiple symbolic names however you will get compiler warnings • .undef • The exact opposite of .def • Undefines a name, i.e. it tells the compiler to not associate the register with the name after some point • Example usage: .def REGISTER_OF_AWESOME = r16 ;associate r16 with REGISTER_OF_AWESOME ldi REGISTER_OF_AWESOME, $0f ..do some more stuff .undef REGISTER_OF_AWESOME ;r16 is no longer associated with REGISTER_OF_AWESOME • .equ • Allows you to define constant number values with a name • Similar to .def BUT you cannot name a register with this, nor can .def name a constant! • Example: .equwinningNumber = 200 ; define winning number! ldir16, winningNumber ; load our winning number for use • You must still load the constant into registers in some form if you wish to use them using commands like load immediate(there are others).
(c) 2012 Mark Roszko AVR Assembly Directives • .cseg • Tells the compiler the assembly code after the statement is meant to go into the code segment • By default the compiler assumes code goes into cseg, you only need this directive when you start defining things in .dseg and other places to return it back to cseg. • .org • Tells the compiler what the address of the following assembly code should be located at • Example: .org $1f reset: ldi r16, 0 ;gets located at address $1f nop ;gets put in the following address $20 • Typically you won’t need to use this until later on when you use interrupts and they need to be placed in specific addresses • .device • Tells the compiler what “device” or chip the code after the directive is meant for • Example: .device ATmega16 • Your projects in AVR Studio by default will have the device already set(ATmega16) • Useful for portable code, the compiler can generate warnings if you are attempting to compile for a different chip
(c) 2012 Mark Roszko Simple assembly programs • Notes • Assembly programs execute instructions one after another in order unless an instruction causes the microcontroller to start executing code in a different location • Put comments! Not just in your electronic versions of code but also on the test. It helps TAs know what you intended and they require it too.
(c) 2012 Mark Roszko Simple assembly programs • All full programs must start with the following directives: .nolist .include “m16def.inc” .list This includes a file that defines all the register addresses and names such as PORTA, PORTB,etc which changes between microcontrollers • Assembly programs start at the first instruction at the beginning of the main file(#include is not an instruction) • Typically the starting location is given a label called reset
(c) 2012 Mark Roszko Simple assembly programs • Upon “reset” a microcontroller needs to be configured. • Microcontrollers do not remember their previous pin states or even register values. • The reset section of your assembly code should usually set registers to some default values you desire reset: ; Configure I/O ports ldi r16, 0b11111111 ;load r16 with all 1s out DDRB, r16 ;PORTB - all bits configured as outputs ldi r16, 0b00000000 ;load r16 with all 0s out DDRD, r16 ;PORTD - all bits configured as inputs
(c) 2012 Mark Roszko Simple assembly programs • Main loop • An assembly program MUST have a main loop. • The loop will execute for infinity until the microcontroller loses power • If the loop is not used then the program will simply execute once and nothing will occur afterwards again until the microcontroller is reset. • Majority of code should exist within the main loop loop: in r16, PIND ;read switch values com r16 ;complement switch values to drive LEDs out PORTB, r16 ;output to LEDs values read from switches rjmp loop ;continually repeat previous two instructions
(c) 2012 Mark Roszko Simple assembly programs • Registers • You can use almost any register from r0 to r31 • Some registers are only able to be used with certain functions • Registers r16 to r31 are the only registers able to be used with any immediate functions such as ldi, andi, ori • Typically as seen in example code, r16 is used for configuring registers after reset simply it’s the first register that’s able to be used with ldi • Reuse registers when possible for smaller code. As seen in the previous example, r16 can be used multiple times in the program just to configure the microcontroller and read and output pins.
(c) 2012 Mark Roszko Other topics worth reviewing • Internal Pin Diagram • Debouncing(if covered in time)