0% found this document useful (0 votes)
20 views

Microprocessors and Microcontrollers-2

The document provides an overview of microprocessors and microcontrollers, focusing on the 8086 architecture, its pin configurations, modes of operation, and memory organization. It also includes programming examples for assembly language and C programming for the 8051 microcontroller, along with interfacing techniques for components like LCDs and timers. Additionally, it covers machine cycles, instruction cycles, and the use of timers for creating delays and counting events.

Uploaded by

raju.jagadale82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Microprocessors and Microcontrollers-2

The document provides an overview of microprocessors and microcontrollers, focusing on the 8086 architecture, its pin configurations, modes of operation, and memory organization. It also includes programming examples for assembly language and C programming for the 8051 microcontroller, along with interfacing techniques for components like LCDs and timers. Additionally, it covers machine cycles, instruction cycles, and the use of timers for creating delays and counting events.

Uploaded by

raju.jagadale82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

Microprocessors and

Microcontrollers
VNIT Nagpur
Pin configuration
Pin configuration
Vcc: +5 V power for internal operation

GND: ground for the internal circuit.


Modes of operation

8086 Modes of Operation

Minimum:
Maximum:
8086 operates as a
8086 operates with one or
standalone processor without
more external coprocessors
any external coprocessors or
or support chips
support chips
Pin configuration
HOLD input, HLDA output :

HOLD:

HIGH indicates another master is


requesting to take over the system bus.

HLDA: HIGH as an acknowledgment on


receiving HOLD signal processor.

LOW: give control of system bus back


to the processor.
Pin configuration
Output pin:

WR : LOW whenever the 8086 is writing


data into memory or an I/O device.
Pin configuration
Output pin

Memory data transfer (M/I0 = HIGH)

I/O data transfer (M/I0 = LOW)


Pin configuration
Output pin

DT/R (Data transmit/Receive):


Control data flow direction.

HIGH: 8086 is transmitting the data.


LOW: 8086 is receiving the data.
Pin configuration
Output pin

DEN (Data Enable) output :


Informs transceivers that CPU is ready.
Pin configuration
Output pin

ALE (Address Latch Enable): Signal


provided by 8086 to demultiplex the
AD0-AD15 into A0-A15 and D0-D15
using external latches.
Pin configuration
Output pin
INTA (Interrupt Acknowledge) Output :
Indicates recognition of interrupt request.

Consists TWO negative going pulses in


two consecutive bus cycles.
1. First pulse informs the interface that
its request has been recognized
2. Second pulse, the interface send the
interrupt type to the processor over
the data bus.
Pin configuration
AD0-AD15: Time multiplexed address
and data lines. Address available during
T1 state, data available during T2, T3, Tw
and T4. Tw wait state.
Machine Cycle

● A bus cycle or machine cycle defines the sequence of events


when the MPU communicates with an external device such as
memory or I/0
● Bus cycle consists of at least four clock periods(time states):T1,
T2, T3 and T4. This group of states is called a MACHINE CYCLE.
Instruction Cycle

● An instruction cycle consists of:


○ Instruction Fetch
○ Read the operands
○ Execution
● Starts with an address being output on the system bus followed by
a read or write data transfer.
T-States Operation
● T1: Address is transmitted by the processor.
● T2: The bus is tristated for changing the direction of bus for data read cycle.
● T3,T4: The data transfer takes place
● In case, an addressed device is slow and shows 'NOT READY'
● Tw are called idle states or inactive states used for internal housekeeping.
Multiplexed lines
AD0-AD15, A16/S3-A19/S6, and
BHE/S7 signals are multiplexed.

● Demultiplexed by external
latches and ALE signal.
● Three latch ICs (Intel
8282/8283), two for a 16-bit
address and three for full
20-bit address.
● 8286 contains eight
receivers, and eight drivers.
Two 8286s are required to
service 16 data lines
Write cycle example
8086 Architecture
Register organization of 8086
Non-Overlapping and Overlapping segments
In the Overlapped
Area Locations,

Physical Address

CS1 + IP1

CS2 + IP2

'+' indicates the


procedure of
physical address
formation.
Advantages of segmented memory
1. Allows the memory capacity to be 1 Mbytes although the actual addresses to
be handled are of 16-bit size.
2. Allows the placing of code, data and stack portions of the same program in
different parts (segments) of memory, for data and code protection
3. Permits a program and/or its data to be put into different areas of memory
each time the program is executed, i.e. provision for relocation is done.
Bus high enable and A0
Status
Status and Q-operation
Physical Memory Organization
General Bus
Operation Cycle
of 8086
Simulator
https://yjdoc2.github.io/8086-emulator-web/compile
eBook

https://ggnindia.dronacharya.info/Downloads/Sub-info/RelatedBoo
k/5thSem/MICROPROCESSOR-AND-INTERFACING-TEXT-BOO
K-2.pdf
Programming
File extension
When you assemble a program in EMU8086, it creates the following files:

File Type Description

.BIN (Binary file) Contains the raw machine code (executable instructions).

.LIST (Listing file) Shows assembly instructions with their corresponding


machine code.

.SYMBOL (Symbol file) Stores labels, variables, and their memory addresses.
Memory Notation
Memory addresses represented in segment:offset notation, where:

● 0100H → Segment Address (base address)


● 0000H → Offset (displacement from base)

The actual physical address in memory is calculated as:

Physical Address = Segment × 10H + Offset

Converting 0100:0000 to Physical Address


0100H × 10H + 0000H = 1000H

So, 0100:0000 in segment:offset format corresponds to 1000H in physical memory.


Program 1
MOV CX, 1000H
Add the contents of the memory
MOV DS, CX
location 1000H:2000H to contents of
MOV AX,[2000H]
3000H:4000H and store the result in
5000H:6000H. MOV CX, 3000H
MOV DS, CX
MOV BX,[4000H]
ADD AX,BX
MOV CX, 5000H
MOV DS, CX
MOV [6000H],AX
Program 2
MOV AX, 1000H
Copy 5 bytes from 1000:2000H to MOV DS, AX
1000:3000H
MOV SI, 2000H
MOV DI, 3000H
MOV CX, 05H
HERE: MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
DEC CX
JNZ HERE
Program 3
ARR DB 10, 20, 30, 40, 50 ; array of 5
Create an array of 5 numbers MOV SI, OFFSET ARR ; address of array
Program 4
MOV CX, 5 ; Set counter for 5 elements
Sum of array of 5 numbers MOV SI, OFFSET ARR ; Load array address
SUM_LOOP:
ADD AL, [SI] ; Add element to AL
INC SI ; Move to next element
LOOP SUM_LOOP ; Repeat until CX = 0
MOV AH, 4CH ; Exit program
INT 21H
ARR DB 10, 20, 30, 40, 50 ; Array elements
Program 5 ARR DB 10, 20, 30, 40, 50 ; Array element
MOV CX, 4 ; Set counter (array size)
Largest number in MOV SI, OFFSET ARR ; Load array address
an array MOV AL, [SI] ; Assume 1st ele is largest
[10,20,30,40,50] INC SI ; Move to next element
FIND_MAX: CMP AL, [SI] ; Compare AL with current
JAE SKIP ; If AL >= [SI], skip update
MOV AL, [SI] ; Update AL with new max
SKIP: INC SI ; Move to next element
LOOP FIND_MAX ; Repeat until CX = 0
Program 5 MOV AX, 1000H ; Load stack address MOV SI, OFFSET ARRAY ; Reset SI

MOV SS, AX ; Set stack segment ; Pop elements back in reverse order
Reverse order of and store them in the same location
MOV SP, 0FFFH ; Stack pointer (top of stack)
an array in-place POP AX
MOV SI, OFFSET ARRAY ; address of ARRAY
using stack! MOV [SI], AL

[1,2,3] POP AX
; Push array elements onto stack
MOV [SI+1], AL
MOV AL, [SI]
POP AX
PUSH AX
MOV [SI+2], AL
MOV AL, [SI+1]

PUSH AX

MOV AL, [SI+2]

PUSH AX
Program 5 (compact way?) MOV SI, OFFSET ARRAY ; Load address of ARRAY

Reverse order of
; Push all elements onto stack
an array in-place
PUSH WORD PTR [SI]
using stack!
PUSH BYTE PTR [SI+2]
[1,2,3]
; Pop back in reverse order

POP BYTE PTR [SI]

POP WORD PTR [SI+1]

; Original array (Modified in place)

ARRAY DB 01H, 02H, 03H


Legendary!

MOV SI, OFFSET ARRAY ; Load address of ARRAY

XCHG [SI], [SI+2] ; Swap first and last elements

ARRAY DB 01H, 02H, 03H ; Original array (Modified in place)


Introduction to C
Programming for 8051
Skeleton Code
#include <reg51.h>
void main()
{
// code;
}
First Example
#include <reg51.h>
void main()
{
while(1)
{
P1=~P1;
}
}
#include <reg51.h>
// Port 0.0 pin definition
Blink!
sbit LED = P0^0;
// Simple delay function for 1 second (approximation)
void delay_1s() {
unsigned int i;
for (i = 0; i < 50000; i++); // Simple delay loop
}
void main() {
while (1) {
LED = ~LED; // Toggle Port 0.0
delay_1s(); // Delay of 1 second
}
}
#include <reg51.h>

#define KEY_PIN P1_0 // Define the pin for the key (P1.0)
Switch! #define LED_PIN P1_1 // Define the pin for the LED (P1.1)

void main() {

// Initialize the LED pin as output

P1 |= 0x02; // Set P1.1 high (LED off, assuming active low)

while (1) {

// Check if the key is pressed (assuming active low)

if (!(P1 & 0x01)) { // If key is pressed (P1.0 is low)

P1 &= ~0x02; // Turn on LED (P1.1 low)

} else {

P1 |= 0x02; // Turn off LED (P1.1 high)

}
Seven Segment Display
Seven Segment Display
#include <reg51.h> void delay(unsigned int time) void main() {
Seven // Define codes for digits 0-9
{
unsigned char i;

Segment! unsigned char code seg_code[] =


unsigned int i, j;
{
for (i = 0; i < time; i++) {
while (1) {
0x3F, // 0
for (j = 0; j < 5000; j++); for (i = 0; i < 10; i++) {
0x06, // 1 // Adjust this value for delay
// Loop through digits 0-9
0x5B, // 2 }
display_digit(i);
0x4F, // 3 }
// Display the digit
0x66, // 4
delay(1000);
0x6D, // 5
void display_digit(unsigned
0x7D, // 6 char digit) {
// Delay for a while
0x07, // 7 P1 = seg_code[digit]; //
Output the segment code to }
0x7F, // 8
Port 1
}
0x6F // 9
}
}; }
Keypad
Keypad
Code Setup

#include <reg51.h>
sbit R0=P1^0;
sbit R1=P1^1;
sbit R2=P1^2;
sbit R3=P1^3;
sbit C0=P1^4;
sbit C1=P1^5;
sbit C2=P1^6;
sbit rs=P3^4;
sbit rw=P3^5;
sbit e= P3^6;
void delay();
void cmd(unsigned int);
void dat(unsigned char);
Interfacing LCD
Sample 16x2 LCD
Interfacing 2x16 LCD
Pinout
1. Ground 9. Data bit 2 (DB2)
2. VCC (Normally 5V) 10. Data bit 3 (DB3)
3. Contrast adjust input (Voltage between VCC and GND) 11. Data bit 4 (DB4)
4. Register select (For a command RS = 0. For data RS = 12. Data bit 5 (DB5)
1)
5. Read/Write 13. Data bit 6 (DB6)
(To write to the display = R/W = 0. To read = R/W = 1)
6. Enable 14. Data bit 7 (DB7)
7. Data bit 0 (DB0) 15. Backlight +
8. Data bit 1 (DB1) 16. Backlight -
Get-Set-Go with an LCD
Initialize LCD, RS = 0
1. Set data lines to: 00111000
"Function set" to 8 wire mode, 2 lines and 5x8 dots.
2. Set enable high, Wait 1uS, Set enable low, Wait until the screen is not busy
3. Set data lines to: 00000110
"Set entry mode" automatic shift of cursor after character
4. Set enable high , Wait 1uS, Set enable low, Wait until the screen is not busy
5. Set data lines to: 00001100
"Display on/off" command. Display on
6. Set enable high, Wait 1uS, Set enable low
Send Data, RS = 1
1. Set R/S = 1
2. Set data lines to 01001000
ASCII code for "H"
3. Set enable high, Wait 1uS,Set enable low
4. Set data lines to 01001001
ASCII code for "I"
5. Set enable high, Wait 1uS, Set enable low
Demo
Timers in 8051
Machine Cycle: time to execute instruction
• 6 States: S1, S2, S3, S4, S5, S6
• Each state → 2 clock cycles: phase 1 (P1) and phase 2 (P2).
• Phase 1: Arithmetic and logical operations
• Phase 2: Internal data transfers
• 1 Machine Cycle = 12 Clock cycles
Timer Registers in 8051
Highlights
• Use of timer:
• create time delay,
• counter to count the external events happening outside
• Difference between counter and timer is source of clock pulse
• Register overflow from FFFF h to 0000 h, it sets TF flag (interrupt)
Clock source for timer
Pin-out
Two 16-bit timers: T0 and T1
Timer Mode Control
TCON
Count for specific delay
1. Calculate time period of timer clock pulse.
2. Calculate number of timer clock pulses N, required to generate desired
time delay by dividing desired time delay
by period of timer clock,
3. Count = FFFFH – N (Hex) +1, and
4. Let us say, Count = n1n2n3n4, Load TH and TL as TH = n1n2 and TL =
n3n4
Activity
Find the count to be loaded into timer registers to generate a time
delay of 500 µs and also write a program for the same using Timer 0
Mode 1. Assume crystal frequency is 12 MHz.
1 KHz Square Wave: Example: Mode 1

MOV TMOD, #10H // configure Timer 1 as interval timer and in Mode 1


REPEAT: MOV TL1, #0CH // load count in timer registers TH1-TL1
MOV TH1, #0FEH
SETB TR1 // start Timer 1
WAIT: JNB TF1, WAIT // wait until timer overflows (wait for 500 µs)
CLR TR1 // stop timer
CLR TF1 // clear timer overflow flag
CPL P2.0 // toggle P2.0 to get square wave
SJMP REPEAT // repeat above steps to generate square wave
C
#include <reg51.h>

sbit square_wave = P0^0; // Define P0.0 as square_wave pin

void main() {
TMOD = 0x10; // Configure Timer 1 in Mode 1 (16-bit timer mode)

while (1) {
TL1 = 0x0C; // Load TL1 with 0x0C (low byte of timer value)
TH1 = 0xFE; // Load TH1 with 0xFE (high byte of timer value)
TR1 = 1; // Start Timer 1

while (!TF1); // Wait until Timer 1 overflow flag is set

TR1 = 0; // Stop Timer 1


TF1 = 0; // Clear Timer 1 overflow flag

square_wave = ~square_wave; // Toggle P0.0 to generate square wave


}
}
Mode 2: Autoreload
Max 8-bit mode
#include <reg51.h>

sbit square_wave = P0^0; // Define P0.0 as square_wave pin

void main() {
TMOD = 0x02; // Configure Timer 0 in Mode 2 (8-bit auto-reload mode)
TH0 = 0x00; // Load TH0

TR0 = 1; // Start Timer 0

while (1) {
if (TF0) { // Check if Timer 0 overflow flag is set
TF0 = 0; // Clear Timer 0 overflow flag
square_wave = ~square_wave; // Toggle P0.0 to generate square wave
}
}
}
Counters!

#include <reg51.h>

void main() {
TMOD = 0x05; // Configure Timer 0 in Mode 2 (8-bit auto-reload mode)
TR0 = 1; // Start Timer 0
}
ADC/DAC
DAC
DAC
A DAC takes an n-bit binary input and converts it into an equivalent analog voltage or current
using a reference voltage (Vref).

○ D = Digital input (decimal value of the binary number)

○ n = Number of bits (e.g., 8-bit, 10-bit, etc.)

○ Vref = Reference voltage


Example
8-bit DAC Example (DAC0808)
● Reference Voltage: Vref = 5V

● Binary Input: 10100000 (Decimal 160)

● Analog Output:
Vout = 5 × 160 / 256 = 3.125V
Example of Successive Approximation
For Vin = 3.5V the final digital output is 011 (3 in decimal).

Step SAR Value DAC Output Comparison Result


(V) (Vin vs DAC)

Start 100 4V 3.5V < 4V Bit 1 = 0

Next 010 2V 3.5V > 2V Bit 2 = 1

Next 011 3V 3.5V > 3V Bit 3 = 1

Final Digital Output 011 (Decimal 3)


Digital to Analog Converter

https://www.ti.com/lit/ds/symlink/dac0808.pdf
#include <reg51.h>

void delay() {
int i;
for (i = 0; i < 10000; i++); // Small delay
}

void main() {
unsigned char value = 0; // Start with 0
while (1) {
P0 = value; // Send data to DAC0808
value++; // Increment value (sawtooth wave)
delay(); // Small delay for waveform visibility
}
}
#include<reg51.h>

sfr DAC = 0x80; //Port P0 address

void main(){
int sin_value[12] = {128,192,238,255,238,192,128,64,17,0,17,64};
int i;
while(1){
for(i = 0; i<12; i++){
DAC = sin_value[i];
}
}
}
ADC
ADC
Start Conversion
ADC ● Apply Analog Input at Vin+, keeping Vin- at GND.

● WR = 0 (Low pulse for at least 100 ns) → Starts conversion.

● WR = 1 (Returns to high) → ADC starts processing.

● INTR = 1 initially (Conversion in progres)

2. Conversion Process

● ADC0804 internally runs a successive approximation algorithm.

● When conversion is complete, INTR goes LOW (0)

3. Read Digital Output

● Microcontroller detects INTR = 0 (Conversion complete).

● RD = 0 (Low pulse for at least 100 ns) → Reads 8-bit digital value from D0-D7.

● RD = 1 (Returns to high).

● INTR resets to 1 (Ready for next conversion).


ADC ICs

Feature ADC0801 ADC0802 ADC0803 ADC0804


Resolution 8-bit 8-bit 8-bit 8-bit
Reference Voltage
(Vref/2 Pin 9) Internal: 2.56V External: 0-2.5V Internal: 2.56V External: 0-2.5V
Conversion Time 100 µs 100 µs 50 µs 100 µs
Clock Frequency (RC or 640 kHz (Internal RC 640 kHz (External RC 1.28 MHz (Faster 640 kHz (External RC
External) possible) required) conversion) required)
Input Voltage Range
(Vin+ to Vin-) 0 to 5V 0 to 5V 0 to 5V 0 to 5V
Power Supply (VCC) 5V 5V 5V 5V
Internal Clock Yes No Yes No
Serial Communication
Why Serial?
• Within a microcontroller, the data is transmitted in parallel form
• Parallel transmission is faster
• Parallel data transfer not preferred as it requires many wires
• higher cost of a system.
• For long distance, Parallel -> Serial
Serial Communication
Synchronous/ Asynchronous modes
Frame Structure of Asynchronous Serial Data
Baud rate and Bit rate

Bits/s = # of data bits per signal transition


×
Baud rate

Standard: 300, 600, 1200, 2400, 4800, 9600, 19200


Bit rate vs Baud rate
• bit: a unit of information
• baud: a unit of signaling speed
• bit (data) rate [b]: # bits / sec
• baud (symbol) rate [s]: #symbols / sec

b (Bits/Sec) = s (Symbol/Sec) × n (Bits/Symbol)

E.g. AM: n = 1, b = s. QAM-16: b = s × 4


Buffers
SCON
SMOD
Mode 0
• fixed baud rate: 1/12 (osc freq)
• no need for timer/counters
• Set from SCON register
• Mode 0 is not asynchronous
• Simultaneous Tx & Rx not possible.
• Quick transfer
• TX: data in SBUF, TI set by HW
• RX: REN = 1, RI = 0
Sample Program Transmission in Mode 0
#include<reg51.h>
void main (void)
{
SCON = 0x00; // Step 1: Set Mode, e.g. Mode 0
SBUF = 0x41; // Step 2: Send data to SBUF register
while (1)
{
while (TI==0); // Step 3: Wait for Completion
TI = 0; // Step 4: Clear TI before sending next
SBUF = SBUF + 1;
}
}
Mode 1
• Variable Baud rate set by TH1

• compatible with COM port of PCs

• transmits 10 bits through TXD pin and receives through RXD pin

START bit (always 0) -> 8 data bits (LSB first) -> STOP bit (always 1)

• 10-bit parallel-to-serial conversion one bit at a time (LSB first)


Standard Baud rates

● Compatibility with Standard Crystal Baud Rate Factor Relation


Frequencies 300 2⁰×300 Base rate
● Old telephone-line modems started at 300
baud, speeds doubled as modulation 600 2¹×300 Double of 300
techniques improved:
a. Bell 103 modem: 300 baud 1200 2²×300 Quadruple of 300
b. Bell 212A: 1200 baud
2400 2³×300 8 times 300
c. V.32/V.34/V.90 standards moved up to
9600, 14400, 28800, etc. 4800 2⁴×300 16 times 300

9600 2⁵×300 32 times 300

19200 2⁶×300 64 times 300


Mode 1
If TI is set, last
character transmission
is completed, SBUF is
empty, new byte can
be transmitted.

If a new byte is written


to SBUF before TI is
raised, the
untransmitted part of
the previous byte will
be lost.
Setting Baud rate (Mode 1)

• 28800/3 = 9600, Timer 1 Mode 2 (auto-reload) is used to determine and


generate Baud rate for Serial Mode 1!
• 28000/6 = 4800
• 28800/24 = 1200 etc.
Baud rate and TH1 count calculation

F Osci × 2SMOD
BR = _____________________________

12 × 32 × (256 – TH1)

F Osci × 2SMOD
TH1 = 256 - _______________________

12 × 32 × BR
For example, with 11.0592 MHz:

● For 9600 baud: TH1 = 256 - (11.0592M / (32×12×9600)) = 0xFD

Perfect rounded value.


Error Otherwise Microcontrollers often use crystals like 11.0592 MHz or 16 MHz, which
divide cleanly to generate these baud rates without much error.

11.0592 MHz with Baud Rate 9600

12 MHz with Baud Rate 9600


TH1 values for various baud rates
PCON
Mode 1
Activity: Transmit Name with BR: 9600
Step 1: Set timer mode e.g. Mode 2
Step 2: Set Baud rate using TH1
Step 3: Set SCON. eg. Mode 2
Step 4: Start Timer 1 to generate clock for baud rate
Step 5: Send data to SBUF
STEP 6: Wait for TI to set
#include<reg51.h>
void serialtransmit (unsigned char);
Activity! void main(void)
{
unsigned char a[] = "Ankit";
unsigned char i;
Send Your TMOD = 0x20; // Timer 1 configured in Mode 2
TH1 = 0xFD; // set 9600bps baud rate
Name to Serial SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
Port TR1 = 1; // start Timer 1 to generate clock
for (i=0; i<15; i++) // transmit string, one character at a time
{
serialtransmit(a[i]);
}
}
void serialtransmit (unsigned char ch) // serial transmit function
{
SBUF = ch; // place the character value in buffer
while (TI==0); // wait until transmitted
TI = 0; // clear TI before sending next byte
}
I2C Serial Bus
I2C: NXP / Philips semiconductors
(1982)
• I2C (Inter-Integrated Circuit),
• Synchronous, multi-master, multi-slave,
bidirectional
• Serial communication bus
• used for lower-speed, short-distance, intra-board
communication.

microcontroller can control a network of


device chips with just two general-purpose
I/O pins and software!
I2C Applications:
● Enable "plug and play" operation, like
○ Detect EEPROMs
○ Extended Display Identification via VGA, DVI, HDMI.
● System management for PC systems via SMBus;
○ SMBus pins are allocated in both Conventional PCI and PCI
Express connectors.
● Access RTC and NVRAM chips that keep user settings.
● Accessing low-speed DACs and ADCs.
● Changing contrast, hue, and color balance settings in monitors
● Changing sound volume in intelligent speakers.
● Controlling small (e.g. feature phone) OLED or LCD displays.
● Reading hardware monitors and diagnostic sensors, e.g. a fan's
speed.
● Turning on and turning off the power supply of system
components.
Master Slave
Basic configuration of I2C
Protocol
Data validity
The data on the SDA line must be stable when SCLK is
high
Data Write Sequence
Example:
master data
1110 0011 (E3H) , 0110 0010 (62H)

to

slave address
1010000 (A0H)
SDA and SCL
S1CON
S1STA
SI = 1 indicates that any one of 25 possible states is
entered.
Status code in S1STA
Sample Code for Master Transmit

• Refer datasheet of P89C66x (89 pages)


• Example: Send data say 20H to address 55H (Code on next slide)
• To view data and reg values, Peripherals -> I2C Interface
Start
1. Set STA bit in SICON register.
2. Clear SI bit. (If it is already set by the previous operation)
3. Monitor the SI bit, wait until SI = 1 to make sure that START
condition is generated and transmitted completely.
Transmit Data
1. Write data (SLA + W or SLA + R) to SIDAT register.
2. Clear SI bit.
3. Monitor SI bit, wait until SI = 1 to make sure that the data is
transmitted completely.
4. If SLA + W (slave address + write) is transmitted, we should write
the next byte(s) to S1DAT register and repeat steps 2 and 3 until all
the bytes are transmitted.
Code
Example
0010 0101(25H) to a slave address 1010001
I2C Read
read a byte from a slave with the address 1010001.
What if native I2C support is unavailable?

You might also like