Microcontrollers
Wrap-up
Peripheral Control
Each peripheral is a state machine that is set by moving information to and from registers
Peripherals can communicate asynchronously with the processor through the use of interrupts
Interrupts
What are they?
-
A method that an asynchronous process can communicate with the processor
-
I/O is slow, for example, compared to the processor speed
How are they implemented?
-
An interrupt bit is set when a device signals for an interrupt
-
The processor saves its current instruction on the stack and disables interrupts
-
The processor then enters an interrupt service routine
-
The processor polls the interrupt flags to find
-
The processor takes care of the situation that caused the interrupt
-
The processor then returns to the instruction just after the interrupt occurred
-
Interrupts are re-enabled on the return
-
Interrupt timing diagram shows the process (pp. 8-10 of reference manual)
On the PIC the interrupts all come into a single line
-
A couple of big OR gates (pp. 8-4 of reference manual)
-
The GIE (Global Interrupt Enable) bit is ANDed with the Interrupt input line
-
The interrupt service routine must poll the interrupt flags to see what caused the interrupt
-
More than one interrupt could happen at the same time, so the polling order is important
A/D conversion using Interrupts
-
Converter uses three registers to control operation, plus the A/D interrupt flag and the A/D interrupt enable bit
-
ADCON0: controls the operation of the converter
-
Bits 7:6 control the sampling frequency by specifying a divider on the internal clock
-
Bits 5:3 specify which line to read
-
Bit 2 is how you tell the A/D converter to begin, it resets the bit when it is finished
-
Bit 1 is reserved
-
Bit 0 turns the A/D converter on (1) or shuts it off so it doesn't draw current (0)
-
ADCON1: specifies the functionality of the A/D pins
-
Bits 7:3 not used
-
Bits 2:0 specify which pins are Analog, which pins are Digital, and whether to use an external reference instead of the internal reference voltage source
-
ADRES: Holds the result of the conversion
-
Process
-
Configure the A/D module
-
Configure the converter pins (ADCON1)
-
Select the input channel (ADCON0)
-
Select the input clock (ADCON0)
-
Turn on the A/D module (ADCON0)
-
Configure the A/D interrupt
-
Clear the ADIF bit
-
Set the ADIE bit (enables or disables interrupts)
-
Set the GIE bit (enables or disables all interrupts)
-
Wait the required acquisition time so the A/D capacitor can charge
-
Start the conversion (ADCON0)
-
Wait for the conversion to finish
-
Poll the GO/DONE bit
-
Wait for the interrupt
-
Read the A/D result register
-
Clear the ADIF bit, if required
Microcontroller Flow Chart
Look at some specific instructions and how they would work
-
MOVF: move from register F to W or back to F (to set status flags)
-
Decode, Read, Process (run through the ALU), Write
-
ADDWF: add contents of W to contents of F and put result in W or F
-
Decode
-
Read (using either direct or indirect address)
-
Run through the ALU
-
Save to either W or F
-
CALL
-
Decode
-
Latch PC+1 to the top of the stack
-
Latch 11-bit literal and top two bits (PCLATH) to PC
-
Second cycle is no-op instructions while new PC value is being fetched into the IR
-
RETLW
-
Decode
-
Read literal
-
Send it through the ALU and put in the W register
-
Load PC from the top of the stack
-
No-op instructions during second cycle while new PC value is used to fetch next instr.
-
BTFSC: Bit test register F, skip if the bit is clear
-
Decode
-
Read register F
-
Pass it through the ALU to set status flags
-
No-op on write cycle
-
If the bit is clear, this becomes a two-cycle operation and No-ops are performed for one cycle while the instruction after the skipped instruction is fetched
-
If the bit is set, the next instruction is executed
VHDL: VHSIC Hardware Description Language
Why do you want a hardware description language?
-
To simulate hardware before implementing it
-
To make design entry simpler than moving boxes and connecting wires
-
To simplify verification and testing
-
To simplify communication between different tools in a CAD environment
VHDL offers a standard language (IEEE Standard 1076 plus some additional standards)
-
There are competitors (AHDL, Verilog HDL)
-
Latest VHDL standard is 1993
-
Standard 1164 standardizes data types for simulation
-
Standard 1076.3 standardizes data types as they relate to hardware
How do you use VHDL?
-
Design specification stage
-
can describe subcomponents in VHDL using input/output specifications
-
Design entry
-
following certain styles of VHDL, you can map VHDL onto hardware
-
Design simulation
-
you can simulate your design before putting it into hardware
-
Design documentation
-
since VHDL is a standard, you can use the VHDL code as a formal specification
-
provides an alternative to schematics
-
provides an alternative to proprietary languages (Altera's AHDL is one example)
What is the overall structure of a hardware description language?
-
At its essence, the HDL consists of methods of describing the two parts of a circuit
-
The input/output characteristics of the circuit (entities)
-
The internal functioning of the circuit (architectures)
-
The other main structures of the language are designed to allow hierachical design
Simple Example 1: A comparator
entity compare is
port(A, B: in bit_vector(0 to 7);
EQ: out bit);
end compare;
architecture compare1 of compare is
begin
EQ <= `1' when (A=B) else `0';
end compare1;
The Entity: Compare
-
The entity describes the input/output behavior of the circuit
-
bit is a data type that describes a single wire carrying a Boolean value
-
bit_vector is a data type that describes a set of wires carrying Boolean values
-
The architecture describes the function of the circuit
-
When the bits on the A and B wire sets are identical, then the circuit outputs a `1'
-
Otherwise, it outputs a `0'
-
The conditional assignment statement is a basic tool of VHDL
-
Note the architecture has to have a unique name, since there can be more than on architecture for a given entity
-
A complete VHDL design must contain at least one entity and at least one architecture for that entity.
Other major language elements
-
Packages
-
Like the class declaration for a C++ class
-
Specify constants, data types, function prototypes, aliases, etc..
-
Tell the rest of the world how to use the functions declared in the related package body
-
Package Body
-
A set of functions/routines/circuits you can put into a library for reuse
-
Configurations
-
Specify which architectures go with which entities, etc.
Design Levels
-
Behavioral
-
Dataflow (Register-Transfer Level)
-
Structural