7/09/2014
Embedded Systems Design ELEN90066
Lecture 14 Microcontroller Internals 2 David Jahshan
Notes from last lecture
• Amicrocontrollershufflesdataaround,and manipulates that data in the ALU
• Instructionsarestoredinprogrammemoryand executed consecutively
• TheALUtakestwooperands,additionalinputsfor what function to perform and it outputs the result of the function on the two operands.
• Machinecodeisasequenceofbitsthatinstruct the MCU to which control wires to active and deactivate
The program from last lecture
ldi r1, 0x19
; Load Immediate Register 1 25 ldi r2, 0x24
; Load Immediate Register 2 36 add r1, r2
;ADD Register 1 to Register 2
• The program is stored in Flash Program memory
ATMEGA16 Architecture
ATMEL ATMEGA16 Datasheet page 8
Writing to an IO pin
• What if we want to output the result to the external pins of the microcontroller
• Add the following to line 4 of our program out PORTB, r1
IO Pin Single Bit
ATMEL ATMEGA16 Datasheet page 51
1
7/09/2014
PORTx (PORTA, PORTB…)
• Aregisterthatstoresdatathatisavailableatthe physical pin. Stores data on WPx rising edge.
• Thisregistercanbebothwrittentoandread from. Outputs to the data bus if RRx is enabled (you can use it to check what is already there)
• Theregister(all8bits)arereferredtoasPORTxif referring to a particular bit you refer to it as PORTxn where x is the register, n is the bit.
• AVRusesthemostsignificantbitasthehighest bit (0x80 is the MSB, 0x01 is the LSB)
DDRx (DDRA, DDRB…)
• DataDirectionRegisterx
• Aregisterthatstoreswhethertheoutputregister (PORTx) is available on the physical pin. Stored when there is a rising edge on WDx
• Connectedtothetri-statebufferattheoutputof PORTx.
• Thisregistercanbewrittenandreadfrom.(you can use it to check what is already there) Available on data bus when RDx is enabled
Pull up register
ATMEL ATMEGA16 Datasheet page 51
Pull Up Register
• Global pull up register is disabled
• Data Direction Register is set as an input
• Output register is high
• The MOSFET when active shorts the high side through a resistor
The program from last lecture
ldi r1, 0x19
; Load Immediate Register 1 25 ldi r2, 0x24
; Load Immediate Register 2 36 add r1, r2
;ADD Register 1 to Register 2
• The program is stored in Flash Program memory
Physical Pin Equivalent Circuit
ATMEL ATMEGA16 Datasheet page 50
2
7/09/2014
Equivalent Circuit
• Cpin is the parasitic capacitance of the pin
• Very low, max 10pF (page 294)
• Diodes ensure that voltage on the pin are not 1 diode drop higher than VCC or 1 diode drop lower than GND
• Rpu is between 20k and 50k ohms
D
Metastability
Q
Q’
Clk
• If the clock edge and the data edge occurs at
the same time then a race condition occurs and the output may enter a metastable state.
• It will remain in this state until noise knocks it out of that state.
• The time it is in the metastable state varies but takes on a probabilistic curve.
PINx and sychroniser
ATMEL ATMEGA16 Datasheet page 51
PINx and synchroniser
• Two registers, if meta stability occurs it occurs at the first register.
• By the time it arrives at the next register PINx it should be stable.
• There is a two I/O clock cycle delay before data arrives at the data bus
• When RPx is enabled the at PINx is available on the bus.
Execution of program line 4
• Programcounterpointstoprogramaddresswith
out PORTB, r1
• Onrisingedgeofclock4,theinstructionis
latched into the instruction register and the program counter is incremented
• Theinstructionisdecodedandthecontrolsignals are activated to do the following
– Output the value from register 1 onto the data bus
– The input of register for PORTB is enabled
• Onclock5risingedgeoutputofr1islatchedinto
PORTB, the output is then available on the external pins
How do you only change one pin?
• Sometimes you would like to only change one pin (ie turn a LED on)
• So to turn pin 3 of PORTA, you would write 0x08 (00001000) to PORTA.
• Difficult to keep track of what your current settings are.
• So how do you turn on a single pin?
3
7/09/2014
Single Pin
• Toturnonasinglepinyoucanreadbackwhat the current value of the port is and modify only the pin that you want to change in C:
• PORTA=PORTA|0x08;//|isbitwiseor
• Oryoucouldjustusetheinstructiontosetone
bit SBI PORTA, 3 (Set Bit in IO Register)
• Machinecode1001101011011011
• WhenyoucompiletheCcode,thecompiler(on max optimisation) will use SBI.
• ToClearthebitCBIPORTA,3
• Soldering
Next Lecture
4