Stefan Kowalewski, 4. November 25 Introduction to Embedded Systems Part 2: Microcontrollers. Basics 2. Structure/elements 3. Digital I/O 4. Interrupts 5. Timers/Counters
Introduction to Embedded Systems Part 2: Microcontrollers Slide 2 Difference between microprocessors and microcontrollers? Microcontroller: - stand-alone device for embedded applications - low-end microprocessor + memory + I/O + additional peripherals - not a general-purpose device - cost-optimized control unit for particular application area (but more general than Application Specific Instruction Set Processors (ASIPs) and Systems-on-Chip (SoCs) Compared with high-end microprocessors: - no pipelining, cache etc. - predictable timing Microcontroller family: - Same microprocessor - Scalability w.r.t. memory, I/O capabilities, on-chip peripherals, etc.
Introduction to Embedded Systems Part 2: Microcontrollers Slide 3 Basic structure of a microcontroller Processor core Memory other on-board peripherals (or on-chip with processor) Internal bus I/O
Introduction to Embedded Systems Part 2: Microcontrollers Slide 4 Basic structure of a microcontroller - refined Processor core Volatile memory (SRAM) Non-volatile memory (EEPROM, Flash) Counter, Timer Clock Watchdog Internal bus Digital I/O (parallel) Serial interface Interrupt controller A/D converters D/A converters, PWM output Bus controller External bus
Introduction to Embedded Systems Part 2: Microcontrollers Slide 5 How to access internal blocks? - memory Processor core Volatile memory (SRAM) Non-volatile memory (EEPROM, Flash) Counter, Timer Clock Watchdog Internal bus Digital I/O (parallel) Serial interface Interrupt controller A/D converters D/A converters, PWM output Bus controller External bus Memory: - all memory types share a common address range or - different memory types are mapped into one address range (if you use C the compiler handels most of it)
Introduction to Embedded Systems Part 2: Microcontrollers Slide 6 How to access internal blocks? - digital I/O & on-chip peripherals Processor core Volatile memory (SRAM) Non-volatile memory (EEPROM, Flash) Counter, Timer Clock Watchdog Internal bus Digital I/O (parallel) Serial interface Interrupt controller A/D converters D/A converters, PWM output Bus controller External bus Digital I/O and on-chip peripherals are accessed by dedicated registers.
Stefan Kowalewski, 4. November 25 Introduction to Embedded Systems Part 2: Microcontrollers. Basics 2. Structure/elements 3. Digital I/O 4. Interrupts 5. Timers/Counters
Introduction to Embedded Systems Part 2: Microcontrollers Slide 8 Basic structure of a microcontroller - refined Processor core Volatile memory (SRAM) Non-volatile memory (EEPROM, Flash) Counter, Timer Clock Watchdog Internal bus Digital I/O (parallel) Serial interface Interrupt controller A/D converters D/A converters, PWM output Bus controller External bus
Introduction to Embedded Systems Part 2: Microcontrollers Slide 9 Digital I/O pins Basic means to monitor and control external hardware. Usually, digital I/O pins - are grouped into ports of 8 pins (byte access). - are bidirectional (i.e., can be used as input or output pins) - can have alternate functions (i.e., can be used for purposes different than digital I/O, e.g., as analog I/O pins) Monitoring, access and control of digital I/O pins is done via three special registers for each port: - Data Direction Register (DDR) - Port Register (PORT) - Port Input Register (PIN)
Introduction to Embedded Systems Part 2: Microcontrollers Slide Control of digital I/O pins via registers 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 - ATmega6: also used for controlling pull-up resistors for input pins (see next slides) Port Input Register (PIN): - read only - contains the current value (high or low) of all pins (input and output) - usual purpose: reading values of input pins
Introduction to Embedded Systems Part 2: Microcontrollers Slide Example: turn on an LED 5V 5V DD RA PO RT A Signal on PINA PinA Pin = (V) LED PortA PortA= Z PortA X: don t care U: unknown LED is on!
Introduction to Embedded Systems Part 2: Microcontrollers Slide 2 Example: turn the LED off 5V 5V DD RA PO RT A Signal on PINA PinA Pin = (5V) LED PortA PortA= Z PortA X: don t care U: unknown LED is off!
Introduction to Embedded Systems Part 2: Microcontrollers Slide 3 Example: Reading Input 5V DD RA PO RT A Signal on PINA PinA S closed: S open: U S closed: S open: PortA PortA= Z S PortA GND X: don t care U: unknown Input is not defined if S is open!
Introduction to Embedded Systems Part 2: Microcontrollers Slide 4 Example: Reading Input 5V DD RA PO RT A Signal on PINA PinA S closed: S open: U S closed: S open: PortA PortA= Z S PortA GND X: don t care U: unknown Input is now also defined if S is open!
Introduction to Embedded Systems Part 2: Microcontrollers Slide 5 Digital input: Sampling Sampling with every clock cycle causes a worst-case delay of ~ clock cycle. Impulses shorter than a clock cycle may be undetected. clock signal PIN register worst-case delay cycle missed impulse one cycle
Introduction to Embedded Systems Part 2: Microcontrollers Slide 6 Digital input: Sampling (2) Problem: Signal does not always have a well-defined level. V CC +.5 V v input high Operating voltage V CC [4.5V, 5.5V].6 V CC undefined.2 V CC -.5 V low Remedy: Schmitt-trigger
Introduction to Embedded Systems Part 2: Microcontrollers Slide 7 Digital input: Sampling (3) Noisy signals noise Bouncing Button pressed Button released Solutions: - by hardware: a) Low pass filter b) built in noise cancellation - by software: read signal twice or more
Introduction to Embedded Systems Part 2: Microcontrollers Slide 8 Digital output If DDR bit of a pin is set to output, the controller will drive the pin according to the corresponding PORT bit. The controller applies the corresponding voltage. The maximal current is depending on the external circuit (could be shortcircuited). External current protection is necessary.
Stefan Kowalewski, 4. November 25 Introduction to Embedded Systems Part 2: Microcontrollers. Basics 2. Structure/elements 3. Digital I/O 4. Interrupts 5. Timers/Counters
Introduction to Embedded Systems Part 2: Microcontrollers Slide 2 Why Interrupts? Microcontrollers have to react to events (internal external) How to ensure proper and timely reaction?. 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) 2. 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
Introduction to Embedded Systems Part 2: Microcontrollers Slide 2 Interrupt Control To use Interrupts they have to be activated by modifying the according registers. Usually there is a - global activation for all interrupts (global IE) and - an individual activation for each interrupt. Mapping of interrupts on the according ISR is done by an Interrupt Vector Table: Vector No. 2 3 Source Reset External Interrupt External Interrupt Prg. Addr. $ $4 $8 A jump instruction to the according ISR has to be placed at each prg. address
Introduction to Embedded Systems Part 2: Microcontrollers Slide 22 Interrupt Handling Detecting the Interrupt condition - If interrupts are enabled all according signals will be monitored - If an IR condition is detected the interrupt flag will be set - Then the interrupt logic will call the according ISR if the according IR is enabled and no other IR with higher priority is pending. Calling the ISR - It is more than just a jump to the appropriate address - Save the return address & some registers on the stack - MCU generally disables interrupts (global) - Nested interrupts are possible but should be avoided - The individual IR-flag is cleared by entering the according ISR.
Introduction to Embedded Systems Part 2: Microcontrollers Slide 23 Interrupt Handling (2) Executing the ISR - After the actions described before the execution in the ISR is like a normal subroutine - ISR must be exit with a special return from interrupt (RETI) instruction which undoes what the controller has done before calling the ISR Some controllers, like the ATmega6 make sure that after returning from an ISR, at least one instruction of the main program is executed before the next ISR is called. In C there are often some predefined ISR bodies and the compiler helps you with some of the work (see example next slide)
Introduction to Embedded Systems Part 2: Microcontrollers Slide 24 Example: External IR 2 //LEDs at PORTA to 5V & Switch at PINB2(external IR2) to GND int main (void) { outp(xff,ddra); //PORTD: output outp(x,ddrb); //PORTB: input outp(xff,portb); //PORTB: Pull-ups activated cbi(mcucsr,6); //IR2 with falling edge (default) sbi(gicr,5); //ext. IR2 activated (individual IR enable) sbi(sreg,7); //Global IR enable (global IR enable) while() {} //some main program } SIGNAL (SIG_INTERRUPT2) //ISR Interrupt 2; is executed as soon as a falling edge is detected at the according PIN (PORTB2) { //alternative: SIGNAL(_VECTOR(8)) if(porta==xff) { outp(x,porta); //Pin=low => LEDs on } else { outp(xff,porta); //Pin=high => LEDs off } }
Introduction to Embedded Systems Part 2: Microcontrollers Slide 25 Interrupt Control (2) IR Sources - External Interrupts (button, external communication controller ready, ) - Timer Events (Overflow, Compare, ) - Analog to Digital Conversion ready - Communication event (message in input buffer, EEPROM ready, ) - IR Priorities - Usually the interrupt vector No. is used as an indication to the priority - Some controllers allow individual settings of priorities - Usually the priorities are static, - Some controllers allow dynamic assignments of priorities
Introduction to Embedded Systems Part 2: Microcontrollers Slide 26 Interrupt vs. Polling Hard to decide, but the following should give some hints: Interrupts should be favored if - Event occurs infrequently - Long intervals between two events - The state change is important (time critical) - Short impulses, polling might miss them - Nothing else to do in main, could enter sleep mode Polling might be a better choice if - No precise timing is necessary - The state is important - Impulses are long - The signal is noisy (debouncing etc. is easier here)
Introduction to Embedded Systems Part 2: Microcontrollers Slide 27 Interrupts final remarks A long ISR delays the main program for a long time! - Sometimes it is useful to move some of the ISR code to the main routine. If more than one IR is used the side effects have to be considered. - The execution of an ISR delays the reaction on all other IRs. - The order of IR events may have influence on the behavior. If IRs are enabled the main program can be interrupted everywhere. - For some (short!) parts of the program is might be necessary to disable IRs.
Stefan Kowalewski, 4. November 25 Introduction to Embedded Systems Part 2: Microcontrollers. Basics 2. Structure/elements 3. Digital I/O 4. Interrupts 5. Timers/Counters
Introduction to Embedded Systems Part 2: Microcontrollers Slide 29 Timer/Counter On chip peripherals (dedicated hardware) 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 Most controllers provide one or more timer/counter with 8 and/or 6 bit resolution.
Introduction to Embedded Systems Part 2: Microcontrollers Slide 3 Timer/Counter Each Timer/Counter unit is based on a counter register, which can be incremented or decremented. Important for the behavior of the Timer/Counter is (are) the according control register(s) - mode of operation - which prescaler to use (timer) - start & stop counting - enable/disable interrupts Often there is also a compare register, which can be used to generate an interrupt if its content is equal to the counter register.
Introduction to Embedded Systems Part 2: Microcontrollers Slide 3 Timer/Counter (ATmega6) Control Register Timer Overflow IR Counter Register Compare Register
Introduction to Embedded Systems Part 2: Microcontrollers Slide 32 Timer/Counter (ATmega6) Control Register Timer Overflow IR Counter Register Compare Match Output Mode clock select Compare Register
Introduction to Embedded Systems Part 2: Microcontrollers Slide 33 Timer/Counter (ATmega6) Control Register Counter Overflow IR Counter Register Compare Register
Introduction to Embedded Systems Part 2: Microcontrollers Slide 34 Timer/Counter More than just counting events and measuring time. Other features - Input capture Used to timestamp (mostly external) events Whenever the event occurs, the timer automatically copies its current count value to an input capture register - Output compare Used to generate signals Whenever a certain timer value is reached, the output compare event is triggered (can automatically set or clear an output line). - Pulse Width Modulation (PWM) Special case of output compare Timer generates a periodic digital output signal with configurable high-time and period.
Introduction to Embedded Systems Part 2: Microcontrollers Slide 35 Watchdog Timer (WD) Special Timer; used to monitor software execution If enabled it counts down and resets the controller as soon as the count value zero is reached. During SW execution the WD has to be reset to its initial value before it reaches zero. If this fails the WD resets the controller. Useful if the program execution hangs and a restart solves the problem. However the WD can also be the source of problems (see Pathfinder problem)
Introduction to Embedded Systems Part 2: Microcontrollers Slide 36 More information about Interrupts and Timer&Counter can be found in any microcontroller data sheet like the one used in the exercise.