Embedded Systems
Embedded Systems
MODULE 2
I/O INTERFACING TECHNIQUES
Weightage
Status
Register
CPU Device
Mechanism
Data
Register
PROGRAMMING INPUT & OUTPUT
q Busy-Wait I/O
q Checking an I/O device if it has completed the task by reading its
status register often called polling.
q Example: AGAIN: JNB TF0, AGAIN
q Interrupts
q It enables I/O devices to signal the completion or status and force
execution of a particular piece of code.
q Example: MOV IE, #82H
ANALOG TO DIGITAL (ADC) CONVERTER
𝑽𝒊
q ADC conversion D= × 𝟐𝒏
𝑽𝒓
Where,
q Vi is the input voltage
q Vr the reference voltage
q n the number of bits in the converter output
q D the digital output value.
0 17
ANALOG TO DIGITAL (ADC) CONVERTER
𝑽𝒊 = 2.12 V
ANALOG TO DIGITAL (ADC) CONVERTER
Analog I/O
q AREF pin and analogReference(type) ADC reference other than 5 V.
q Increase the resolution available to analog inputs
qThat operate at some other range of lower voltages below +5
qMust be declared before analogRead()
analogReference(type)
Parameters type:
which type of reference to use (DEFAULT,
INTERNAL, INTERNAL1V1, INTERNAL2V56, or
EXTERNAL)
Returns
None
ANALOG TO DIGITAL (ADC) CONVERTER
Analog I/O
analogRead(pin)
qReads the value from a specified analog pin with a 10-bit
resolution.
q Works for analog pins (0–5).
q It will return a value between 0 to 1023. analogRead(pin)
q100 microseconds for one reading.
Parameters pin:
q Reading rate 10000 per sec. the number of the analog input pin to read
from (0-5)
q INPUT nor OUTPUT need not be declared.
Returns
int(0 to 1023)
ANALOG TO DIGITAL (ADC) CONVERTER
EXAMPLE 1
void setup()
{
//monitor output in serial port
Serial.begin(9600); //intitating serial communication with 9600 baud rate
}
void loop()
{
sensorValue = analogRead(A0);
//analog input is read from A0 pin
Serial.println(sensorValue);
//printing the output in serial port
delay(100); // simple delay of 100ms
}
ANALOG TO DIGITAL (ADC) CONVERTER
CODE
ANALOG TO DIGITAL (ADC) CONVERTER
EXAMPLE 2
void loop() {
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin); /* Read Temperature */
temp_val = (temp_adc_val * 4.88); /* Convert adc value to equivalent voltage */
temp_val = (temp_val/10); /* LM35 gives output of 10mv/°C */
if (temp_val > 40) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.print(" Degree Celsius\n");
delay(1000);
}
ANALOG TO DIGITAL (ADC) CONVERTER
CODE
DIGITAL TO ANALOG (DAC) CONVERTER
+ 𝑫
𝑫 DAC 𝑽𝒐 𝑽𝒐 = ×𝑽𝒓
- 𝟐𝒏
Control Lines
DIGITAL TO ANALOG (DAC) CONVERTER
void loop()
{
potvalue=analogRead(A0)/4;
analogWrite (6, potvalue);
}
ANALOG TO DIGITAL (ADC) CONVERTER
TIMER / COUNTER
Control Registers
Timer/Counter Modes
Returns
None
TIMER / COUNTER
Parameters
us: the number of microseconds to pause
(unsigned int)
Returns
None
TIMER / COUNTER
Returns
Number of milliseconds since the program
started (unsigned long)
TIMER / COUNTER
micros()
Parameters
None
Returns
Number of microseconds since the program
started (unsigned long)
TIMER / COUNTER
EXAMPLE
Reset
!
Inter
rupt!
RESET Interrupt
by watch dog
timer (WDT)
Watch Dog Timers
WATCH DOG TIMERS
07/08/24 53
Watch Dog Timers
WATCH DOG TIMERS
07/08/24 54
Watch Dog Timers
WATCH DOG TIMERS
07/08/24 55
ENCODER (4:2)
• The encoder is a device or a transducer or a circuit.
• The encoder will convert the information from one
format to another format i.e like electrical signals to
counters or a PLC.
• The feedback signal of the encoder will determine the
position, count, speed, and direction.
• The control devices are used to send the command to
a particular function.
3-56
• A robotic vehicle
with the metal
detector
Speed
Synchronization of
Multiple Motors in
Industries
INTRODUCTION
q UART stands for Universal Asynchronous Receiver/Transmitter
https://www.circuitbasics.com/basics-uart-communication/
INTRODUCTION
q UARTs has no clock signal and follows asynchronous data
communication
q Start and stop bits are added to data packet for identifying
beginning and end of data packet
q Start bit and stop bit are used instead of clock signal
INTRODUCTION
q After detecting start bit UART starts to read the incoming data
packet
INTRODUCTION
Parameters Specification
Wires used 2
Maximum speed 115200 baud rate
Synchronous ? No
Serial? Yes
Max number of master 1
Max number of slave 1
https://www.circuitbasics.com/basics-uart-communication/
BECE102L, Dr. C. Sridhar Page 69
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER
q The transmitting UART adds a start bit, stop bit and a parity bit
to parallel data from data bus
https://www.circuitbasics.com/basics-uart-communication/
BECE102L, Dr. C. Sridhar Page 71
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER
UART IN ARDUINO
q UART allows the Atmega chip to do serial communication
while working on other tasks, through 64 byte serial buffer.
q All Arduino boards have at least one serial port (also known as
a UART or USART): Serial.
q EXAMPLE-1: Print data received through serial communication on to the serial monitor of
Arduino
void setup() {
Serial.begin(9600); //set up serial library baud rate to 9600
}
void loop() {
if(Serial.available()) //if number of bytes (characters) available for reading from serial port
{
Serial.print("I received:"); //print I received
Serial.write(Serial.read()); //send what you read
}
}
q EXAMPLE-1: Arduino code for serial interface to blink switch ON LED when “a” is
received on serial port int inByte; // Stores incoming command
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // Led pin
Serial.println("Ready"); // Ready to receive commands
}
void loop() {
if(Serial.available() > 0) { // A byte is ready to receive
inByte = Serial.read();
if(inByte == 'a') { // byte is 'a'
digitalWrite(13, HIGH); This function can be used
Serial.println("LED - On"); if(Serial.find(“a"))
}
else { // byte isn't 'a'
digitalWrite(13, LOW);
Serial.println("LED - off");
}}}
BECE102L, Dr. C. Sridhar Page 77
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER
q EXAMPLE-3: Arduino code to Receives from the hardware serial, sends to software serial
and Receives from software serial, sends to hardware serial.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(57600); // Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(4800); // set the data rate for the SoftwareSerial port
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}}
BECE102L, Dr. C. Sridhar Page 80
EEPROM interface using SPI
q Arduino interface with an AT25HP512 Atmel serial EEPROM using the Serial
Peripheral Interface (SPI) protocol.
q EEPROM chips such as this are very useful for data storage.
q Note that the chip on the Arduino board contains an internal EEPROM.
EEPROM interface using SPI
q Serial Peripheral Interface (SPI) is a synchronous serial data protocol
used by Microcontrollers for communicating with one or more peripheral
devices quickly over short distances.
q Data registers simply hold bytes. For example, the SPI data register
(SPDR) holds the byte which is about to be shifted out the MOSI line, and
the data which has just been shifted in the MISO line.
q It can only be written 128 bytes at a time, but it can be read 1-128 bytes
at a time.
q The device also offers various degrees of write protection and a hold pin
EEPROM interface using SPI
EEPROM interface using SPI
q The device is enabled by pulling the Chip Select (CS) pin low.
q Instructions are sent as 8 bit operational codes (opcodes) and are shifted
in on the rising edge of the data clock.
q Connect EEPROM pin 1 to Arduino pin 10 (Slave Select - SS), EEPROM pin
2 to Arduino pin 12 (Master In Slave Out - MISO), EEPROM pin 5 to
Arduino pin 11 (Master Out Slave In - MOSI), and EEPROM pin 6 to
Arduino pin 13 (Serial Clock - SCK).
EEPROM interface using SPI
q The first step is setting up our pre-processor directives.
q We define the pins we will be using for our SPI connection, DATAOUT,
DATAIN, SPICLOCK and SLAVESELECT. Then we define our opcodes for the
EEPROM.
EEPROM interface using SPI
q char buffer [128] this is a 128 byte array we will be using to store the data
for the EEPROM write:
EEPROM interface using SPI
q First we initialize our serial connection, set our input and output pin
modes and set the SLAVESELECT line high to start.
q This deselects the device and avoids any false transmission messages
due to line noise:
EEPROM interface using SPI
q Now we set the SPI Control register (SPCR) to the binary value 01010000.
In the control register each bit sets a different functionality.
q The eighth bit disables the SPI interrupt, the seventh bit enables the SPI,
the sixth bit chooses transmission with the most significant bit going first,
the fifth bit puts the Arduino in Master mode, the fourth bit sets the data
clock idle when it is low, the third bit sets the SPI to sample data on the
rising edge of the data clock, and the second and first bits set the speed
of the SPI to system speed / 4 (the fastest).
q After setting our control register up we read the SPI status register (SPSR)
and data register (SPDR) in to the junk clr variable to clear out any
spurious data from past runs:
EEPROM interface using SPI
EEPROM interface using SPI
q The EEPROM MUST be write enabled before every write instruction.
q To send the instruction we pull the SLAVESELECT line low, enabling the
device, and then send the instruction using the spi_transfer function.
q Note that we use the WREN opcode we defined at the beginning of the
program. Finally we pull the SLAVESELECT line high again to release it:
EEPROM interface using SPI
q Now we pull the SLAVESELECT line low to select the device again after a
brief delay.
q We send a WRITE instruction to tell the EEPROM we will be sending data to
record into memory.
q We send the 16 bit address to begin writing at in two bytes, Most
Significant Bit first.
q Next we send our 128 bytes of data from our buffer array, one byte after
another without pause.
q Finally we set the SLAVESELECT pin high to release the device and pause
to allow the EEPROM to write the data:
EEPROM interface using SPI
EEPROM interface using SPI
q In the main loop we just read one byte at a time from the EEPROM and print it out the serial port.
We add a line feed and a pause for readability.
q Each time through the loop we increment the EEPROM address to read.
q When the address increments to 128 we turn it back to 0 because we have only filled 128
addresses in the EEPROM with data.
EEPROM interface using SPI
q The spi_transfer function loads the output data into the data transmission register, thus starting the
SPI transmission.
q It polls a bit to the SPI Status register (SPSR) to detect when the transmission is complete using a bit
mask, SPIF.
q It then returns any data that has been shifted in to the data register by the EEPROM.
EEPROM interface using SPI
q The read_eeprom function allows us to read data back out of the EEPROM.
q Then we transmit a READ instruction, followed by the 16-bit address we wish to read from, Most
Significant Bit first.
q Next we send a dummy byte to the EEPROM for the purpose of shifting the data out.
q Finally we pull the SLAVESELECT line high again to release the device after reading one byte, and
return the data.
EEPROM interface using SPI
SENSOR'S AND ACTUATORS
l Actuators
l DC Motor
l Servo Motor
l Stepper Motor
l Sensors
l Phototransistor
l Reflectance Sensor
l IR Distance Sensor
l Contact Switch
l Bend Sensor
l Other Sensors
l Actuators
l DC Motor
l Servo Motor
l Stepper Motor
l Sensors
l Phototransistor
l Reflectance Sensor
l IR Distance Sensor
l Contact Switch
l Bend Sensor
l Other Sensors
l 1 ms = 0○
l 1.5 ms = 90○
l 2 ms = 180○
l At 4.8 V
l Speed: 0.12 sec / 60
degrees (83 RPM)
l Stall Torque: 16.7 oz--in
10
BECE102L,
6 Dr. C. Sridhar Page 106
ARDUINO INTERFACING
11
Sensor & Actuators
07/08/24 114
Sensor & Actuators
07/08/24 115
PHOTOTRANSISTOR
jameco.com
l Photodiodes
l Similar to phototransistors
l Lower sensitivity
void setup()
{
Saerial.begin(9600);
// configure sensors
pinMode(PHOTO_TRANS,INPUT);
}
void loop()
{
int sensor;
// test sensors
sensor = analogRead(PHOTO_TRANS); // analogRead uses analog port #
Serial.print("Reflectance sensor: ");
Serial.println(sensor);
delay(500);
}
BECE102L, Dr. C. Sridhar
11 Page 119
9
SUPPLIERS
l Engineering Stockroom
l Robu.in
l Hobbyist
l Pegasus Hobbies
l 5515 Moreno St., Montclair, an easy bike ride from campus
l Sparkfun
l Pololu
l Jameco
l All Electronics, Futurlec, Inventables, Goldmine Electronics, …
l Professional
l DigiKey (very wide selection, fewer hobby parts, higher cost)