Data Direction Register (DDR)
read/write
specifies for each bit of the corresponding port whether it is an input or an output bit
Port Register (PORT)
read/write
specifies for the output pins whether the output value is high or low
ATmega16: also used for controlling pull-up resistors for input pins (see next slides)
Port Input Register (PIN)
read only (writing has no effect or unintuitive semantics)
contains the current value (high or low) of all pins (input and output)
usual purpose: reading values of input pins
LED: Digital Output
PORT = logic 1 -> VCC PORT = logic 0 -> GND
Polling
Periodically check for event
Disadvantages:
• Waste of CPU time if the event occurs infrequently
•Polling sequence has to fit in the rest of the code (hard to modify or extend)
Interrupts (IRs)
MCU polls the signal and interrupts the main program if a state change is detected.
MCU calls an interrupt service routine (ISR) which handles the event
ISR is triggered by event
Save return address (PC) to stack
Clear global interrupt enable bit (I bit)
Clear interrupt flag bit (usually)
Jump to corresponding interrupt vector table entry (interrupt vector)
(Interrupts vs Polling) Interrupts is better if
Event occurs infrequently
Long intervals between two events
The exact time of the state change is important
Short impulses, polling might miss them
Nothing else to do in main, could enter sleep mode
(Interrupts vs Polling) Polling is better if
No precise timing is necessary
The state is important
Impulses are long
The signal is noisy (Interrupts would be triggered very often)
Counter
counts external events
e.g. number of rising edges at PINB2
Timer
counts clock cycles (with or without prescaler)
Each timer is basically a counter
Input capture
* Whenever the event occurs, the timer automatically copies its current count value to an input capture register
Output compare
Pulse Width Modulation (PWM)
* Timer generates a periodic digitaloutput signal with configurable high-time and period.
DAC
RC low-pass filter
Binary weighted resistor circuit
R-2R ladder
ADC
Simple: Analog Comparator
Flash Converter
Tracking Converter
Successive Approximation Converter