Microcontroller - Communication
Microcontroller - Communication
Communication-Interrupt-Timer
Contoh Komunikasi Parallel
• 11 pin digunakan untuk komunikasi Arduino
dengan LCD
• Praktis
• Boros Pin
I2C
What is I2C?
◼ Shorthand for an “Inter-integrated circuit” bus
◼ Developed by Philips Semiconductor for TV sets in the
1980’s
◼ I2C devices include EEPROMs, thermal sensors, and real-
time clocks
◼ Used as a control interface to signal processing devices
that have separate data interfaces, e.g. RF tuners, video
decoders and encoders, and audio processors.
◼ I2C bus has three speeds:
◼ Slow (under 100 Kbps)
Register Data IO
Addr
110 Receiver Shift Register
Register
External connection
Register List
Name Address Width Access Description
PRERlo 000 8 RW Clock Prescale Register
Lo-byte
PRERhi 001 8 RW Clock Prescale Register
Hi-byte
Receive Register
Command Register
Status Register
I2C Bus Configuration
Register Data IO
Addr
110 Receiver Shift Register
Register
2 Arduino I2C Communication
Master
Slave
SPI
Serial Peripheral Interface (SPI)
In the early days of microcontrollers, both National Semiconductors and Motorola started
introducing simple serial communication, based on the previous Figure. Each formulated
a set of rules which governed how their microcontrollers worked, and allowed others to
develop devices which could interface correctly. These became de facto standards.
Motorola called its standard Serial Peripheral Interface (SPI), and National
Semiconductors called theirs Microwire. They’re very similar to each other.
An SPI network with one Master and multiple slaves can be constructed as shown.
SPI on the mbed: Master
The mbed has two SPI ports, each can be configured
as Master or Slave.
Functions Usage
SPI Create a SPI master connected to the specified pins
format Configure the data transmission mode and data length
frequency Set the SPI bus clock frequency
write Write to the SPI Slave and return the response
SPI on the mbed: Mode Mode
0
Polarity
0
Phase
0
1 0 1
2 1 0
3 1 1
http://dlnware.com/theory/SPI-Transfer-Modes
Clock
Serial data
• Data is synchronised to the clock
• When there is no data being sent, there is no signal on the clock line
• Every time the clock pulses, one bit is output by the transmitter and should be
read by the receiver
• The receiver synchronises its reading of the data with an edge of the clock.
Parallel Parallel
Read/write Read/write
SDO SDI
SDO – serial data out
SDI – serial data in
Shift Shift SCLK – serial clock
Register SDI SDO Register
msb lsb msb lsb
SCLK SCLK
Clock
Master Slave
SPI on the mbed: Slave
• The Slave program (next slide) uses the mbed functions shown in the
Table.
• It is almost the mirror image of the Master program, with small but key
differences.
• It also declares variables switch_word and recd_val.
• The Slave program configures its switch_word just like the Master.
• While the Master initiates a transmission when it wishes, the Slave must wait.
The mbed library does this with the receive( ) function. This returns 1 if data
has been received, and 0 otherwise.
• If data has been received from the Master, then data has also been sent from
Slave to Master.
Functions Usage
SPISlave Create a SPI slave connected to the specified pins
format Configure the data transmission format
frequency Set the SPI bus clock frequency
receive Polls the SPI to see if data has been received
read Retrieve data from receive buffer as slave
reply Fill the transmission buffer with the value to be written out as
slave on the next received message from the master.
Evaluating SPI
The SPI standard is extremely effective. The electronic hardware is simple and therefore
cheap, and data can be transferred rapidly.
There are disadvantages.
• There is no acknowledgement from the receiver, so in a simple system the Master
cannot be sure that data has been received.
• There is no addressing. In a system where there are multiple slaves, a separate |SS line
must be run to each Slave, as seen earlier. Therefore we begin to lose the advantage that
serial communications should give us, i.e. a limited number of interconnect lines.
• There is no error-checking. Suppose some electromagnetic interference was
experienced in a long data link, data or clock would be corrupted, but the system would
have no way of detecting this, or correcting for it.
Overall SPI could be graded as: simple, convenient and low-cost, but not appropriate for
complex or high reliability systems.
Parallel Parallel
Read/write Read/write
SDO SDI
SDO – serial data out
SDI – serial data in
Shift Shift SCLK – serial clock
Register SDI SDO Register
msb lsb msb lsb
SCLK SCLK
Clock
Master Slave
Assume the shift registers in the Master and Slave are 8-bit – each has 8 D-type
flip-flops
Each register is loaded with a new word via the parallel I/O lines
The Master then produces 8 clock pulses.
For each clock pulse, data is output from the msb of each register (SDO)
Each SDO is connected to the lsb of the other shift register (SDI)
As each bit is clocked out of one register, it is clocked into the other.
After 8 clock cycles, the word that was in the master shift register as been
transferred to the slave shift register, and vice versa.
SPI: Master-Slave Configuration
• Configuration
• master data output/slave data input (MOSI)
• master data input/slave data output (MISO)
• slave chip select (CS) – usually active low
• When multiple slave devices exist, the master must output a unique CS signal
for each slave, but data lines can be shared. The programmer must
know how data is timed between peripherals when shared.
SPI
• The master device starts an SPI data transfer by
doing the following :
• Configures to SPI clock (SCLK) to be a frequency that the
recipient slave device can handle (max is 70 MHz)
• Sets the chip select line (CS) of the intended recipient to
0V
• Starts the clock pulses on SCLK to indicate that that
data is to be transferred
• Simultaneously sends data as consecutive bits on MOSI
•Number of bits in each data frame can be configured, but is usually
between 4 and 16 bits.
•The slave returns data in the same manner on MISO.
CLK
DATA
1 0 1 0 0 0 0 1
A 1
Disadvantages of Synchronous
Serial Data Communication
• Synchronous serial communication protocols are
extremely useful ways of moving data around. But
taking a clock signal to every node does have these
disadvantages:
• An extra (clock) line needs to go to every data node.
• The bandwidth needed for the clock is always twice the
bandwidth needed for the data; therefore, it is the
demands of the clock which limit the overall data rate.
• Over long distances, clock and data themselves could lose
synchronisation.
2 Arduino SPI
RS-485
Different signals with RS485
Longer distance and higher bit rates
CAN BUS
Interrupt
What is an Interrupt?
• An interrupt is a signal that tells the processor to
immediately stop what it is doing and handle some
high priority processing. That high priority
processing is called an Interrupt Handler.
• An interrupt handler is like any other void function.
If you write one and attach it to an interrupt, it will
get called whenever that interrupt signal is
triggered. When you return from the interrupt
handler, the processor goes back to continue what
it was doing before.
Interrupt source
• Timer interrupts from one of the
Arduino timers.
• External Interrupts from a change in
state of one of the external interrupt
pins.
• Pin-change interrupts from a change in
state of any one of a group of pins.
What are they good for?
• Using interrupts, you don’t need to write loop code
to continuously check for the high priority interrupt
condition. You don't have to worry about sluggish
response or missed button presses due to long-
running subroutines.
• The processor will automagically stop whatever it is
doing when the interrupt occurs and call your
interrupt handler. You just need to write code to
respond to the interrupt whenever it happens.
Timer Interrupts
• Timers and timer interrupts let us do exactly that.
We can set up a timer to interrupt us once per
millisecond. The timer will actually call us to let us
know it is time to check the clock!
Arduino Timers
• The Arduino has 3 timers: Timer 0, Timer 1 and
Timer 2. Timer 0 is already set up to generate a
millisecond interrupt to update the millisecond
counter reported by millis().
Frequency and Counts
• Timers are simple counters that count at some
frequency derived from the 16MHz system clock. You
can configure the clock divisor to alter the frequency
and various different counting modes. You can also
configure them to generate interrupts when the timer
reaches a specific count.
• Timer0 is an 8-bit that counts from 0 to 255 and
generates an interrupt whenever it overflows. It uses a
clock divisor of 64 by default to give us an interrupt rate
of 976.5625 Hz (close enough to a 1 KHz for our
purposes). We won't mess with the freqency of Timer0,
because that would break millis()!
Comparison Register
• Arduino timers have a number of configuration
registers. These can be read or written to using special
symbols defined in the Arduino IDE. For comprehensive
description of all these registers and their functions,
see the links in "For further reading" below.
• We'll set up a up a comparison register for Timer 0 (this
register is known as OCR0A) to generate another
interrupt somewhere in the middle of that count. On
every tick, the timer counter is compared with the
comparison register and when they are equal an
interrupt will be generated.
• The code below will generate a 'TIMER0_COMPA'
interrupt whenever the counter value passes 0xAF.