Session 3 - DIO Module & Interfaces
Session 3 - DIO Module & Interfaces
Any peripheral consists of Hardware Circuit, but unfortunately the processor deals with
memory only, so any peripheral must have a piece of memory to act as an intermediary
between the processor and the hardware circuit.
According to the last point, there is a new type of memory will be appeared, this is
“Input/Output Memory”.
The IN instruction tells the CPU to load one byte from an I/O
register to the GPR. After this instruction is executed, the
GPR will have the same value as the I/O register. For example,
the “in 20, 0x16” instruction will copy the contents of
location 16 (in hex) of the I/O memory into R20. Each location
in I/O memory has two addresses: I/O address and data
memory address. Each location in the data memory has
a unique address called the data memory address.
Each I/O register has a relative address in comparison to the
beginning of the I/O memory; this address is called the I/O
address.
Digital Input Output:
Accessing to I/O memory:
To Access I/O by using Port Mapping addresses, we must use Assembly Level by using for
example: “IN & OUT” instruction.
The OUT instruction tells the CPU to store the GPR to the
I/O register. After the instruction is executed, the I/O register
will have the same value as the GPR. For example, the
“out PORTD, R10” instruction will copy the contents of R10
into PORTD (location 12 of the I/O memory).
Notice that: in the OUT instruction, the I/O registers are
referred to by their I/O addresses (like the IN instruction).
Digital Input Output:
Difference between LDS & IN:
LDS instruction to copy the contents of a memory location to a GPR.
This means that we can load an I/O register into a GPR, using the LDS instruction.
So, what is the advantage of using the IN instruction for reading the contents of I/O
registers over using the LDS instruction? The IN instruction has the following advantages:
The CPU executes the IN instruction faster than LDS, IN instruction lasts 1-machine
cycle, where as LDS lasts 2-machine cycles.
The IN is a 2-byte instruction, whereas LDS is a 4-byte instruction.
This means that the IN instruction occupies less code memory.
When we use the IN instruction, we can use the names of the I/O registers instead
of their addresses.
The IN instruction is available in all of the AVRs, where as LDS is not implemented in
some of the AVRs.
Notice that in using the IN instruction we can access only the standard I/O memory, while
we can access all parts of the data memory using the LDS instruction,
Digital Input Output:
Difference between LDS & IN:
LDS instruction to copy the contents of a memory location to a GPR.
This means that we can load an I/O register into a GPR, using the LDS instruction.
So, what is the advantage of using the IN instruction for reading the contents of I/O
registers over using the LDS instruction? The IN instruction has the following advantages:
The CPU executes the IN instruction faster than LDS, IN instruction lasts 1-machine
cycle, where as LDS lasts 2-machine cycles.
The IN is a 2-byte instruction, whereas LDS is a 4-byte instruction.
This means that the IN instruction occupies less code memory.
When we use the IN instruction, we can use the names of the I/O registers instead
of their addresses.
The IN instruction is available in all of the AVRs, where as LDS is not implemented in
some of the AVRs.
Notice that in using the IN instruction we can access only the standard I/O memory, while
we can access all parts of the data memory using the LDS instruction,
Digital Input Output:
Pin Configuration of ATmega32:
ATmega32 has 40 pins into DIP manufacturing, 44 pins into TQFP
manufacturing, there is no difference between of them in the
features, but the extra 4 pins only extra pins for ground and Vcc.
Tri-State Buffer looks like the normal buffer, but it has an extra pin,
this pin controls the voltage flow between “A & B”, so if enable
pin is powered to “0”, there is no voltage will get through, if it is ?
powered to “1”, the voltage will get through from “A B”.
Digital Input Output:
Hardware Circuit of DIO:
As we mentioned before, any peripheral must have main
two components: Hardware Circuit and memory to manage
the processor to order or request a command from the
hardware circuit, and the main hardware component of our
DIO is Tri-State Buffer.
If we need to turn on a LED, we have two ways to access the DIO register “actually, we have
two addresses to access any I/O register: memory and port mapping, Lets start with
memory mapping addresses.
All IO registers can be accessed by “IN / OUT” instructions, but there are
Two instructions that can access IO registers from “0x00 0x1F”, which
are “SBI: Set Bit Instruction, CBI: Clear Bit Instruction” and they can
Set/Clear a specific bit into an IO register from “0x00 0x1F” as one
instruction that takes two clock cycles, and it can not be interrupted
because it is a single instruction not like Set/Clear in C level, because it
takes minimum there instructions “Read – Modify – Write”.
Digital Input Output:
Turning on a LED:
Light Emitting Diode:
A light-emitting diode (LED) is a semiconductor light source that emits light
when current flows through it. Electrons in the semiconductor recombine
with electron holes, releasing energy in the form of photons.
The color of the light (corresponding to the energy of the photons) is
determined by the energy required for electrons to cross the band gap of
the semiconductor.
White light is obtained by using multiple semiconductors or a layer of
light-emitting phosphor on the semiconductor device.
Most of LEDs run at “3 volts” maximum, and the maximum current that
it consumes is about “20 mA” maximum, so if it is connected to “5 volts”,
it must be connected in series with a resistor that its value can be calculated
by: 𝐑𝐞𝐬𝐢𝐬𝐭𝐨𝐫 𝐯𝐚𝐥𝐮𝐞 = 𝟓 − 𝟑 −𝟑 = 𝟏𝟎𝟎 𝜴 , but most common LEDs can be
𝟐𝟎 × 𝟏𝟎
connected to 𝟑𝟑𝟎 𝜴.
Digital Input Output:
Turning on a LED:
Types of Connection:
Source Connection:
It means that the LED will obtain the power source from the microcontroller itself,
like the next figure, which the microcontroller is considered as the power source
of LED, so its activation state is to power the Pin to High.
Source Connection:
It means that the LED will obtain the power source from external power source,
like the next figure, which the microcontroller is considered as the power sink
of LED, so its activation state is to power the Pin to Low.
Current sinking and sourcing:
Each I/O pin can sink or source a recommended current of 20mA.
Each I/O pin can sink or source a maximum current of 40mA.
It must be ensured that the current sourced or sunk from all the
ports combined, should not exceed 200mA.
Digital Input Output:
Turning on a LED:
To control a LED, first we must set the pin that will be connected to it as an output, then
the output register must be set/cleared according to the type of connection, so we have
two methods to control the LED by accessing “memory or port mapping addresses”.
Assume that the LED will be connected to “pin 3” in group “ C ”, so the accessing method
will be memory mapping “DDRC: 0x34 , PORTC: 0x35”.
We have the address of register, but it must be assigned into pointer or casted to be
accessible, so casting will be chosen to reduce memory consumption.
For example, “0x34” will be casted to pointer to the register size itself “1 Byte”, so it will be
casted to pointer to u8 “unsigned char”, as the following: (u8 *)0x34, now it becomes a
pointer, but we need to dereference this pointer, so the final result will be:
*( (u8 *)0x34 ) = any_value_you_need;
Digital Input Output:
Turning on a LED:
Example Code to turn on the led “using Memory Mapping”:
This code will turn on the LED that will be connected
to pin 3, in group C, now we use the <util/delay.h>
library to use its APIs:
“_delay_ms( time_in_milli_second ); ”
“_delay_us( time_in_micro_second );”
to make an application that blinks this LED with
one second interval.
Digital Input Output:
Turning on a LED:
Example Code to Blink the led “using Memory Mapping”:
The code besides is written to blink the
LED, but in fact it will be turned on once,
then turned off once, because there now
thing that orders the processor to repeat
the code, normally the execution of main
function will end and never to enter again,
so, we need to use a keyword in C that
can order the processor to run this code
forever.
Guess, what should we use?....!!
Digital Input Output:
Turning on a LED:
Super Loop:
The super loop is used for two reasons:
- For running the program for ever, and
making the main function never exists.
-To protect the program memory,
because if there is no super loop, PC
register will be increased forever, until
it is overflowed, so the Program Counter
will start from zero, and the processor will
access all flash sections that are not accessed
in normal cases, but if you try to do it, you will
not find any result that because the startup
code protect it be finalize function that only has
a super loop, and it is only executed if the main function is an exited function.
Digital Input Output:
Turning on a LED:
Super Loop:
This is our code after using super loop system,
so, the LED will be blinked forever.
Now, we want to access the pins using
“Port Mapping” addresses, so we must use
assembly level “IN/OUT 0x00 – 0x3F” or
“SBI/CBI 0x00 – 0x1F” to access, but if we need to
access it within a C code, is it available?.....!!
If an assembly instruction is written with a C code,
compiler will generate an error because the assembly
instructions are not defined in C syntax.
So, if an assembly instructions should be written within
a C code, we must use inline assembly.
Digital Input Output:
Turning on a LED:
Inline Assembly:
To use this feature, we will use the internal
function “asm”, so we can write the assembly
instruction can be written as a string, and be
passed as an argument for “asm”.
The actual color of the visible light emitted by an LED, ranging from blue
to red to orange, is decided by the spectral wavelength of the emitted
light which itself is dependent upon the mixture of the various impurities
added to the semiconductor materials used to produce it.
• The best sequence is to set all the columns by HIGH at Pull up Resistors
first.
• Then, change C1 signal to LOW then check if any row 1 2 3 A
read Zero or not. Input Pin 1
R1
• Assume that R3 readsZero signal, so the pressed 4 5 6 B R2 Input Pin 2
button is C1R3.
7 8 9 C R3 Input Pin 3
• If not, turn C1 to HIGH again and set C2 to LOW and
check if any Row reads Zero or not. Input Pin 4
# 0 * D R4
• Repeat this sequence until you find zero on any row
then return the pressed key, or until columnsare C1 C2 C3 C4
Output Pin 1
Output Pin 2
Output Pin 3
Output Pin 4
finished and return keypad not pressed.
HOW KEYPAD WORKS?
• The best sequence is to set all the columns by HIGH at Pull up Resistors
first.
• Then, change C1 signal to LOW then check if any row 1 2 3
read Zero or not. + R1
Input Pin 1
Output Pin 1
Output Pin 2
Output Pin 3
Output Pin 4
finished and return keypad not pressed.
KEYPAD ALGORITHM:
Output Pin 1
Output Pin 2
Output Pin 3
Output Pin 4
corresponding button in C1 is pressed.
KEYPAD ALGORITHM:
Output Pin 1
Output Pin 2
Output Pin 3
Output Pin 4
corresponding button in C1 is pressed.
KEYPAD DRIVER:
After deactivation of the coil 2, the first coil will be activated in inverse polarity.
Final step, coil 2 also will be activated in inverse polarity.
THEORY OF STEPPERMOTORS:
• Internal Modification:
• Aswe mentioned before, we can decrease the angle of step by activation double
coils or
micro stepping, but now we will decrease the step by increasing the number of
internal coils.
• Unfortunately, increasing the number of coils will increase the number of wires
outside the motor.
• It will be difficult to control this motor because of huge quantity of wires.
THEORY OF STEPPERMOTORS:
• Internal Modification:
• Thelast problem hasbeen solved by that: the coils has90 degrees
between of themwill have commonwires outside the motor, like the
following figure, all blue coils have the same wire outside the motor, and
pink coil also and yellow and orange and so on.
• So, after this modification, there are only four wires are outside of the
motor.
• If the blue coil is activated, all coils will generate electrical-magnetic
field, and the nearest coil to the permanent magnet will attract the
permanent magnet.
THEORY OF STEPPERMOTORS:
• Internal Modification:
• The last problem has been solved by that: the coils has 90 degrees between of
them will have common wires outside the motor, like the following figure, all blue
coils have the samewire outside the motor, and pink coil also and yellow and
orange and so on.
• So, after this modification, there are only four wires are outside of the motor.
• If the blue coil is activated, all coils will generate electrical-magnetic field, and
the nearest coil to the permanent magnet will attract the permanent magnet.
OUR STEPPERMOTOR:
• We will use a small stepper motor that works at 5-Volt power, it is Two-coil-common-wire
motor.
• The truth
Blue Pink
table for clockwise Yellowis:
rotation Orange
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
OUR STEPPERMOTOR:
360
= = 2048 𝑠𝑡𝑒𝑝𝑠.
(11.25 )
64
𝐴𝑛𝑔𝑙𝑒 𝑋 𝑠𝑡𝑒𝑝𝑠 𝑖𝑛𝑡𝑜 𝑎 𝑐𝑜𝑚𝑝𝑙𝑒𝑡𝑒𝑑 𝑐𝑦𝑐𝑙𝑒
• Steps in a specific angle = 𝑓𝑢𝑙𝑙 𝑟𝑜𝑡𝑎𝑡𝑖𝑜𝑛 𝑎𝑛𝑔𝑙𝑒
𝐴𝑛𝑔𝑙𝑒 𝑋 2048
=
THANK YOU!