التعشيق بالحاسوب المرحلة الرابعة
التعشيق بالحاسوب المرحلة الرابعة
Controller
(Hardware & Software)
Power
Interface
INTEGRATION Signal
Conditioning
Actuator
Sensor
System to
Control
Why do we need to learn Microcontroller
cell phone
air conditioner,
washer/dryer
Toys
Component of microcontroller
Component of microcontroller
Types of Microcontrollers
ATmega328 Internal Architecture
http://www.adafruit.com/index.php?main_page=popup_image&pID=50
ATmega328 Features
http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-
ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf
Arduino Duemilanove
http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
See the handout: Arduino_ATmega328_pin_mapping_and_schematic
Pin 13 LED
Digital pins header
USB
connector Reset button
ATmega328 MCU
Power-ground header
http://arduino.cc/en/uploads/Main/ArduinoDuemilanove.jpg
Arduino Uno R3
ATmega16u2 replaces FT232RL for USB-serial communication
http://www.adafruit.com/index.php?main_page=popup_image&pID=50
See: http://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-uno-faq
Arduino Due
Note: 3.3 V !!
http://www.adafruit.com/index.php?main_page=popup_image&pID=1076
See: http://arduino.cc/en/Main/ArduinoBoardDue
Arduino Duemilanove/Uno Features
Microcontroller ATmega168/328
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limits) 6-20V
Digital I/O Pins 14 (of which 6 provide PWM output)
Analog Input Pins 6
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
16 KB (ATmega168) or 32 KB (ATmega328) of which 2 KB
Flash Memory
used by bootloader
SRAM 1 KB (ATmega168) or 2 KB (ATmega328)
EEPROM 512 bytes (ATmega168) or 1 KB (ATmega328)
Clock Speed 16 MHz
http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
ATmega328 Microcontroller
Pin number
Pin name
Special
function
Note the
limitations!
p. 316 Source:http://www.atmel.com/dyn/products/product_card.asp?PN=ATmega328P
Port Pin Data Directionality
• Input
• When you want to take information from the external
world (sensors) into the MCU
• Output
• When you want to change the state of something outside
the MCU (turn a motor on or off, etc.)
• Pins default to input direction on power-up or reset
• Your program can set or change the directionality of
a pin at any time
Setting the Pin Data Direction
• Arduino
• pinMode(pin_no., dir)
• Ex. Make Arduino pin 3 (PD3) an output
• pinMode(3, OUTPUT);
• pinMode(PIN_D3, OUTPUT); // with me106.h
• Note: one pin at a time
• Suppose you wanted Arduino pins 3, 5, and 7 (PD3, PD5, and PD7) to be outputs?
• Is there a way to make them all outputs at the same time?
• Yes! Answer coming later…
Pin Voltages
SPST
momentary
Pins as Inputs and Pull-up Resistors - 2
• Switch as a sensor, cont.
• Make the voltage on the pin ATmega328
determinate by turning on the pull-up VTG= +5V
resistor for PD3
• Assuming PD3 is an input:
• digitalWrite(PIN_SWITCH,HIGH); 1
PD3
turns on the “pull-up” resistor
0
• pinMode(PIN_SWITCH,INPUT_PULLUP);
• What will the voltage on PD3 be when the
switch is open?
• VTG
• What will the voltage on PD3 be when the
switch is closed?
Pins as Inputs and Pull-up Resistors - 3
• Switch as a sensor, cont.
• To turn off the pull-up resistor ATmega328
• Assuming PD3 is an input:
digitalWrite(PIN_SWITCH,LOW); VTG= +5V
turns the “pull-up” resistor off
1
PD3
0
Pins as Inputs and Pull-up Resistors - 4
• Possibility of ‘weak drive’ when pull-up resistor
is turned on ATmega328
• Pin set as an input with a pull-up resistor turned on
can source a small current VTG= +5V
• Remember this!
iweak
1
PD3
0
Structure of an Arduino Program
/* Blink - turns on an LED for DELAY_ON msec,
then off for DELAY_OFF msec, and repeats
• An arduino program == ‘sketch’ BJ Furman rev. 1.1 Last rev: 22JAN2011
• Must have: */
#define LED_PIN 13 // LED on digital pin 13
• setup() #define DELAY_ON 1000
• loop() #define DELAY_OFF 1000
• setup() void setup()
• configures pin modes and {
registers // initialize the digital pin as an output:
pinMode(LED_PIN, OUTPUT);
• loop() }
• runs the main body of the
program forever // loop() method runs forever,
// as long as the Arduino has power
• like while(1) {…}
• Where is main() ? void loop()
• Arduino simplifies things {
digitalWrite(LED_PIN, HIGH); // set the LED on
• Does things for you delay(DELAY_ON); // wait for DELAY_ON msec
digitalWrite(LED_PIN, LOW); // set the LED off
delay(DELAY_OFF); // wait for DELAY_OFF msec
}
Digital IO – Practice 1
• ‘Reading a pin’
• Write some lines of C code for the ATmega328
Arduino to determine a course of action
if the seat belt has been latched (switch
closed).
• If latched, the ignition should be enabled PD3
through a call to a function ig_enable().
• If not latched, the ignition should be disabled
through a call to a function ig_disable()
• Write pseudocode first
Digital IO – Practice 1 Pseudocode
• ‘Reading a pin’
• Pseudocode: ATmega328
Set up PD3 as an input
VTG= +5V
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THEN 1
call function ig_enable() PD3
ELSE 0
call function ig_disable()
Digital IO – Practice 1 Code
• ‘Reading a pin’ ATmega328
• Pseudocode:
VTG= +5V
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
1
IF PIN_D3 voltage is LOW (latched), THEN PD3
call function ig_enable() 0
ELSE
call function ig_disable()
• Data direction
• Input is default, but okay to set explictly
• Output
• Arduino style: pinMode(pin_no, mode)
• Alternate: Set bits in DDRx
• Pull-up resistors
• Pin must be an input
• Arduino style: digitalWrite(pin_no, state)
• Alternate style: Set bits in PORTx
Summary, cont.
Accuracy Speed
What is the uses of ADC (successive-
approximation and the Integrator)?
1- Adder
2- Integrator
3- Comparator
4- D/A
What is dual slope A/D Converter
Components draw the CCT
Integrator
Electronically
Cont
Counter
Clock
Control Logic
Comparator
Digital to Analog Converters
What is the D/A? why is it needed? Do we
always need it?
𝑉𝑜𝑢𝑡 = −𝐼 𝑅𝑓
𝑉1 𝑉2 𝑉3 𝑉𝑛
= −𝑅𝑓 + + + ⋯ 𝑛−1
𝑅 2𝑅 4𝑅 2 𝑅
IF 𝑅𝑓 = 𝑅/2
𝑉1 𝑉2 𝑉3 𝑉𝑛
𝑉𝑜𝑢𝑡 = − + + +⋯ 𝑛
2 4 8 2
𝑅
IF 𝑅𝑓 =
6
𝑉1 𝑉2 𝑉3 𝑉𝑛
𝑉𝑜𝑢𝑡 = − + + + ⋯ 𝑛
6 12 24 2
void setup()
{
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("hello everyone");
lcd.setCursor(1,1);
lcd.print("konichiwaa");
}
void loop()
{
}
#include<UltraDistSensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
UltraDistSensor mysensor;
float reading;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
mysensor.attach(12,13);//Trigger pin , Echo pin
}
void loop() {
reading=mysensor.distanceInCm();
lcd.setCursor(0,0);
lcd.print("Distance :- ");
lcd.print(reading);
delay(1000);
}
Introduction to
PC Interfacing
Software Engineering Dept.
Technical College of Kirkuk
4th Class
Introduction to
Introduction
• The pin functions of the two connectors are shown in the table below.
C0
D0 To D7
S6
S7
S5
S4
C1
S3
C2
C3
• The I/O lines in the port are organized into three groups, namely, the data group,
the control group and the status group.
• Figure (1.b) shows the logic structure of the LPT port.
D0 2
D1 3
Data Ports D2 4
D3 5
(Output) D4 6
D5 7
Base Address
D6 8
D7 9
Control Ports C0 1
(Output) C1 14
C2 16
Base Address + 2 C3 17
S3 15
Status Ports 13
S4
(Input) S5 12
S6 10
Base Address + 1 S7 11
This group sends data from PCs to external devices. It has 8 latched output lines and the
group is associated with an 8-bit CPU port. The address is : base address.
Control Group
This group controls the operation of external devices. It contains 4 latched output lines.
The group is controlled by a CPU port having an address : base address + 2.
Note that bit 0,1 and 3 are inverted, and bit 2 is not.
Status Group
The group is used by the computer to obtain the current status of external devices. It
contains 5 lines. It is fed into a CPU port, the address of which base address is : base
address + 1. Note that bit-7 is inverted.
➢ The base address of LPT1 varies. This depends on the hardware configuration of
the computer.
➢ The base address can be found directly from the user’s program by using the
facilities provided by the computer Basic Input Output System (BIOS).
➢ When a computer is powered on or reset, the BIOS checks all possible Centronic
ports. If it finds one, it writes the addresses (a 2 byte or Word) of that port to two
specific memory locations as the size of each memory location is 8 bit.
➢ The port address is stored in a reserved memory location specifically designed for
this task.
➢ By reading the contents of these memory locations, the base address of LPT1 or
others can be obtained.
➢ The memory locations for where the base address of LPT1 to LPT4 are listed
below:
7 6 5 4 3 2 1 0
* * - - - - - -
10 DEF SEG = 0
20 PRINT “The number total number of Centronic ports is:”,(PEEK(&H0411) AND (128 + 64)) / 64
30 PRINT “Address of LPT1:”,PEEK(&0408) + 256 * PEEK(&H0409)
40 PRINT “Address of LPT2:”,PEEK(&040A) + 256 * PEEK(&H040B)
50 PRINT “Address of LPT3:”,PEEK(&040C) + 256 * PEEK(&H040D)
60 PRINT “Address of LPT4:”,PEEK(&040E) + 256 * PEEK(&H040F)
70 INPUT X
Line 20 reads the byte stored in memory location 0411H using the “PEEK()”
command. Bits 6 and 7 of this byte are masked by “AND (128 + 64)”. Then, the
result is shifted 6 bits towards the LSB using a division command “/64”. Line 30
reads two bytes from two memory locations holding the LSB and MSB part of the
base address for LPT1.
Lines 40,50 and 60 perform the same action for LPT2, LPT3 and LPT4.
The Centronic port is treated as three components as three separate I/O ports:
Two of which is output and one is input.
Let us take an example of controlling the LPT1. Assuming that the address of the
data, control and status ports is 888,890 and 889 respectively. In order to send
data to data port and control port, the following QBasic commands are used:
OUT 888 , X
OUT 890, X
Some lines of the control and status ports are inverted. This has to be taken into
consideration when outputting data.
To read data from the status port, the following command can be used:
Y = INP(889)
The input data bits correspond to bits 3 to 7 of the status port, and one line is
inverted. This has to be taken into account.
➢ Bit Weight:
The relationship between a bit and its weight is given below:
10 X = original_Data OR 8
20 OUT 888, X
Assumptions:
o One of the two computers is the master and the other should be the slave.
o The master sends the data (4 – bit) and the slave receives it.
o The selection of a computer to be master or a slave is performed manually.
SLAVE MASTER
HOMEWORK (1):
Baseaddress = 888
D_PORT = Baseaddress
S_PORT = Baseaddress + 1
C_PORT = Baseaddress + 2
TSET = 20D
OUT D_PORT , 0
200 X = (INP(S_PORT) XOR 1000 00002)
IF (X AND 248) / 8 < TSET GOTO 300
HOMEWORK 2:
HOMEWORK 3:
(A) Suggest a suitable hardware for a computer to control a LED bulletin ()نشرة ضوئية
consisted of 8 LED, the LED bulletin should only be on at night and automatically
shut off at daylight. The bulletin has two patterns:
The first pattern of the LED bulletin should be as follows and repeated for 5 times
(LED1 – ON) (LED2 – OFF) (LED3 – ON) (LED4 – OFF) (LED5 – ON) (LED6 – OFF) (LED7 – ON) (LED8 – OFF)
Note: Assume that each LED will emit light for 1 sec during operation.
Serial
Interfacing
Data transfer within a system is generally in parallel. All the data bits are transferred in
parallel at the same instant. In some cases particularly in transferring in long distance,
it is preferred to transfer the data in serial form. The transmitted bits are converted to
a serial stream of bits by parallel to serial converter and one bit at a time is transferred
on a single line to a receiving system. At the receiving end the data bits are
reconstructed by serial to parallel conversion.
In this format, the transmitter and the receiver use a common clock signal for
synchronizing the data transfer. The error in synchronous mode is detected by
checksum technique in which the sum of all the bytes in the block is found and it’s 2’s
complement is transmitted as the last byte in the block. The receiver adds all the bytes
(including the last) and checks it is result is zero in which it indicates no error. This
format is used in high speed communication.
Asynchronous Communication:
Asynchronous data transfer is used for low speed communication, typically at standard
rates 110, 300, 600, 1200, 2400, 4800, 9600 and 112500 baud rate. The baud rate is the
speed of data transfer in serial communication, it represents the rate at which the bits
are transmitted and it is given as the number of bits per second.
The asynchronous communication format does not use any synchronizing clock or
timing signal, instead it adds a framing bits with each character being transmitted, when
no character is sent, the transmitter output logic high. Transmission of a character starts
with a start bit (logic 0) followed by the character LSB bits first, a parity bit and end up
with one or two stop bits (logic 1), this is called as one frame. The line remains in logic 1
till the transmission of next character begins with another start bit. The figure below
shows the transmission of two 7-bit ASCII characters, ‘M’ (4DH) and ‘E’ (45H). It
transmits a start bit, character bits (data bits), parity bit (for even parity) and two stop
bits.
The serial data stream contains the information of synchronounzation and the actual
data to be transferred.
The electronic device that is responsible for generating and receives asynchronous serial
data format is called Universal Asynchronous Receiver / Transmitter (UART). The serial
data transmission format is generated by the transmitting UART.
The receiver detects the leading edge of the start bit, it then waits for one and a half bit
times before reading the data bit. The reading should come exactly in the middle of the
19 Computer Interfacing, 4th Class, Software Engineering Dept. | Mohammed H. Rasheed
first data bit. Then it waits for one bit time and reads the second bit. After reading all
the data bits, the receiver detects the parity of the received data for error checking and
reset itself during the stop bit. It is then ready for receiving the next data transmission.
There are several standards that specify the protocol for data transfer between devices
such as RS-232, RS-422A, RS-423A and RS485. The RS-232 is the most commonly used
standard.
Unlike parallel I/O ports, which consist of a number of data lines and each time transmits
a byte; the serial data transmission requires only one line. A byte is transmitted bit-by-
bit. This reduces data lines between devices. It reduces the rate of the data transferred
too.
There are two types of RS232 link between a computer and an external device, as shown
in the figure below (Null Modem)
An IBM-PC computer can have up to 4 RS232 interfaces. They are labeled COM1 to
COM4. Each COM port is associated with a 16450 UART inside a computer.
The offset and functions of the UART registers are summarized below:
OFFSET 03H: is the data format register which defines the serial data format such as the
baud rate, number of data bits, number of stop bits and parity check.
7 6 5 4 3 2 1 0
DLAB BRK PAR2 PAR1 PAR0 STOP DAB1 DAB0
1 = access to the divisor latch in which it is used to load the divisor to find the baud rate.
0 = access to the receiver buffer / transmitter hold register (offset 00H) and the interrupt
enable register (offset 01H)
BRK (Break):
1 = Break on , 0 = break off
PAR2,1,0 (Parity):
000 = none (no parity)
001 = odd parity
011 = even parity
101 = mark
111 = space
STOP:
1 = 2 stop bits
0 = 1 stop bit
OFFSET 00H: is the receiver buffer register and the transmitter hold register. The
transmitter hold register can be accessed if the DLAB bit in the data format register
After a serial data is successfully received and converted into parallel format the data is
transferred in the receiver buffer register. After reading the data from the register, the
receiver buffer is cleared and is ready for receiving the next data.
When DLAB bit is 1, the receiver buffer / transmitter hold register (00H) and the
interrupt enable register (01H) are used for loading the divisor. The first one holds the
LSB byte and the second one holds the MSB byte of the divisor, they form a 16 bit divisor,
and the value is calculated using the following equation:
In a computer the clock frequency to the UART is 1.8432 MHz. inside the UART, the
reference frequency is the clock frequency divided by 16, giving 115200 HZ, the
relationship between the divisor and the baud rate is :
OFFSET (07H): is a scratch pad memory which is a random access memory byte. Writing
data into this register has no effect on the operation of the UART.
Most AT computers uses the 16450 UARTs. XT computers use 8250 UART in which they
are the same in specifications and internal architecture.
The UART uses the TTL voltage level. In order to achieve a longer distance
communication, the TTL voltage level is converted to a higher voltage level (logic0=-12
to -3, logic 1 = +3 to +12). This is achieved by using dedicated RS232 drivers/Receiver’s.
All drivers and receivers have an inverting actions. Al the UART produces the TTL or
CMOS voltage levels only.
The RS 232 line driver/ receiver are connected between the UART and the RS-232
connector. The logic structure of the RS-232 port is shown in the below figure:
RS232
8250/16450 UART driver Port Pins
COM1 : 3F8H
COM2 : 2F8H
COM3 : 3E8H
COM4 : 2E8H
When a computer is switched or reset, the BIOS checks all possible RS232 addresses. If
its fins an installed one, it writes the base address (a 2 byte address) into a specific
memory locations.
Another usefull 1 byte memory location is the 411H memory location. It stores the total
number of COMs installed. The information is obtained be checking the values of bits
0,1 and 2 as follows:
10 SEG DEF = 0
20 PRINT “the Number of COM Ports installed is:”, (PEEK(&H411) AND (1+2+4))
30 PRINT “the base address for COM1 is:”, PEEK(&H400) +256 * PEEK(&H401)
40 PRINT “the base address for COM2 is:”, PEEK(&H402) +256 * PEEK(&H403)
50 PRINT “the base address for COM3 is:”, PEEK(&H404) +256 * PEEK(&H405)
60 PRINT “the base address for COM4 is:”, PEEK(&H406) +256 * PEEK(&H407)
70 INPUT X
Before a COM port can be used, it must be configured to have a specific serial data
format. The configuration includes the setting of the baud rate, number of data bits,
parity and number of stop bits.
1. DOS prompt
2. BIOS INT 14
3. Direct configuration to data format register.
The first method is to use the ‘MODE’ command under windows (DOS prompt). The
syntax of the comman is:
MODE COM1:96,n,8,1 configures COM1 port to have a baud rate of 9600 bps, no parity
check, 8-bit data length and 1 stop bit. The disadvantage of this method is that it does
not allow users to change the serial data format within the user’s program.
The INT 14 that the BIOS use has 4 different service functions and it deals with the AH,AL
and DX registers only.
The register AH is used to indicate the INT 14H service function that the BIOS has to
execute (Configure, read, write or get port status) and can be selected as shown below:
If AH = 00H, the AL register should contain the initialization bits of the COM port such
as baud rate, data bits, stop bits and parity bits
7 6 5 4 3 2 1 0
BD2 BD1 BD0 PAR1 PAR0 STOP DA1 DA0
Bit 4 and 3 will define the parity: 00 – for no parity, 01 for odd and 11 for even parity.
00 -5 bits data
01 - 6 bits data
10 - 7 bits data
11 – 8 bits data
The table below will summarize the functions of the registers used in INT 14H as well as
the AL register.
*Bit Pattern of Port Status byte # Bit Pattern of modem status byte
D7 Time – out D7 Receive line signal detected
D6 Transmitter shift register empty D6 Ring indicator
D5 Transmitter hold register empty D5 Data set ready
D4 Break detected D4 Clear to send
D3 Framing error D3 Change in receive line signal detect
D2 Parity error D2 Ring indicator
D1 Overrun error D1 Change in data set ready status
D0 Data ready D0 Change in clear to send status
As a Summary, the limitation of this method is that we can not get higher buad rate as
the maximum is 9600 bps while the 16450 UART can run at 115200 bps.
Baseaddress = &H3F8
PRINT “enter the baudrate”
INPUT baudrate
PRINT “enter the number of data bits”
INPUT data
PRINT “Select a parity option [0] no parity,[1] odd parity,[3] even parity
[5]mark or [7] space”
Input parity
PRINT “enter the number of stop bits: 1 or 2 ”
INPUT stopbits
Write software algorithm to configure COM1 port according to the following values, bit
rate = 9600 bps, data bits = 8 bits, parity = even and stop bit = 1.
Solution 1
BASEADDRESS = &H3F8
Or
Solution 2
BASEADDRESS = &H3F8
OUT (BASEADDRESS + &H03), 128
OUT BASEADDRESS, 12
OUT (BASEADDRESS + 01), 0
OUT (BASEADDRESS + 03), 1BHEX
It is required to design a pc-based control system to control the operation of two fans
(ON or OFF) using COM1 port according to the state of three separated digital sensors,
please note that each sensor has one output only. The operation of the fans should be
as follow
Fans
Sensor condition
Fan 1 Fan 2
All of the sensors are 0 OFF OFF
Only one of the sensors (any one) is 1 and the other two is 0 ON OFF
Only two of the sensors (any two) is 1 and the other one is 0 OFF ON
All of the sensors are 1 ON ON
BASEADD =&H3F8
OUT (BASEADD + 04H) , 00
10 SENSOR_STATUS = INP (BASEADD + 06H)
S1 = (SENSOR_STATUS AND 128) / 128
S2 = (SENSOR_STATUS AND 32) / 32
S3 = (SENSOR_STATUS AND 16) / 16
SUM = S1 + S2 + S3
IF SUM = 0 THEN
OUT (BASEADD + 04H), 00
IF SUM = 1 THEN
OUT (BASEADD + 04H), 02
IF SUM = 2 THEN
OUT (BASEADD + 04H), 01