Sie sind auf Seite 1von 123

2/18/2012

MTS Technologies Ltd

Microprocessor Vs Microcontroller

2/18/2012

MTS Technologies Ltd

y Microprocessor, by itself is useless. y It does not have Memory & I/O Ports on the chip

itself.
y Must have the External Peripherals to interact with

the Physical Things.

2/18/2012

MTS Technologies Ltd

y But, Microcontroller has a Processor along with

fixed amount of RAM, ROM, I/O Ports, Timers, & Serial Communication channel on the chip itself.

Microcontroller Based System


2/18/2012 MTS Technologies Ltd

8051 Architecture

2/18/2012

MTS Technologies Ltd

Features of 8051
y y y y y y y y

8051 is an 40 pin IC. 8051 is an 8-bit Microcontroller. 128 Bytes of RAM. 4KBytes of Internal ROM. 4 I/O Ports (P0, P1, P2 & P3). Two 16-bit Timers (T0 & T1). Supports 5+1 Interrupts. Supports One Serial Communication Channel.

2/18/2012

MTS Technologies Ltd

Cont
y It has 4 Register Banks (Bank0 y 16-bit Program Counter. y 8-bit Stack Pointer. y 16-bit DPTR (Data Pointer). y External Code and Data Memory up to 64KB. y 8-bit PSW (Program Status Word).

Bank 3)

2/18/2012

MTS Technologies Ltd

y Atmel AT89C51 is a powerful microcomputer which

provides a highly-flexible and cost-effective solution to many embedded control applications.

2/18/2012

MTS Technologies Ltd

Pin Configuration for 8051

2/18/2012

MTS Technologies Ltd

Pin Description
RESET

y PIN 9 is the reset pin

which is used reset the microcontroller s internal registers and ports upon starting up. y A high on this pin for two machine cycles while the oscillator is running resets the device.
2/18/2012 MTS Technologies Ltd

XTAL1: Input to the inverting oscillator amplifier and input to the internal clock operating circuit. XTAL2: Output from the inverting oscillator amplifier.

y The 8051 has a built-in

oscillator amplifier hence we need to only connect a crystal at these pins to provide clock pulses to the circuit.
2/18/2012 MTS Technologies Ltd

Port 0
y PORT P0 can be used as a general purpose 8 bit

port when no external memory is present, but if external memory access is required then PORT P0 acts as a multiplexed address and data bus that can be used to access external memory in conjunction with PORT P2. y P0 doesn t have internal Pull-up resistors, because to access external memory devices. y If we want to use Port0 as I/O Port, we need to connect external pull-up resisters to P0.

2/18/2012

MTS Technologies Ltd

Port 1
y Port 1 is an 8-bit bidirectional I/O Port with

internal pull-ups.

Port 2
y Port 2 is an 8-bit bi-directional I/O port with

internal pull-ups. y Port 2 also used as the high-order address byte during accessing external memory that use 16-bit addresses.

2/18/2012

MTS Technologies Ltd

Port 3
y Port 3 is an 8-bit bi-directional I/O port with

internal pull-ups. y Port 3 Has some additional features.

P3.0 P3.1 P3.2 P3.3 P3.4 P3.5 P3.6 P3.7


2/18/2012

RXD TXD INT0 INT1 T0 T1 WR RD


MTS Technologies Ltd

EA (External Access) Pin: This is active Low signal. When we are accessing any external memory this pin should be low. y EA should be connected to VCC for internal program executions. PSEN Pin: (Program Store Enable) This is an active low pin. To Store our program into the Chip, we need to make this pin Low.

2/18/2012

MTS Technologies Ltd

ALE (Address Latch Enable) Pin:


y Port 0 is used for both Address and Data. To

differentiate between that we will use this ALE Pin. y If ALE pin is high means we are sending the value from Port0 is for address.

2/18/2012

MTS Technologies Ltd

Machine cycle
y A machine cycle consists of a sequence of 6

states, numbered S1 through S6. y Each state is divided into a Phase1 half and a Phase2 half. y Each state time lasts for two oscillator periods. y Thus a machine cycle takes 12 oscillator periods or 1s if the oscillator frequency is 12MHz.

2/18/2012

MTS Technologies Ltd

y In the case of Crystal Frequency = 12 MHz, the machine cycle frequency is 1MHz (12MHz / 12). y Machine cycle Time Period is 1 S (1 / 1MHz). y In case of Crystal Frequency = 11.0592MHz, the machine cycle frequency is 921.6KHz (11.0592MHz / 12). y Machine Cycle Time Period is 1.085 S (1 / 921.6 KHz).

2/18/2012

MTS Technologies Ltd

Program Start Address


y The 8051 starts executing program instructions

from address 00h in the program memory.


Direct Memory

y The 8051 has 256 bytes of internal addressable

RAM, although only the first 128 bytes are available for general use by the programmer. y The first 128 bytes of RAM (from 00h to 7Fh) are called the Direct Memory, and can be used to store data.
2/18/2012 MTS Technologies Ltd

Special Function Register


y The Special Function Register (SFR) is the

upper area of addressable memory, from address 80h to FFh. y The 8051 is a flexible microcontroller with a relatively large number of modes of operations. y Your program may inspect and/or change the operating mode of the 8051 by manipulating the values of the 8051's Special Function Registers (SFRs).

2/18/2012

MTS Technologies Ltd

SFR Table

 YELLOW Background are I/O Ports SFR  PINK Background are Control SFR  RED Background are Other SFR
2/18/2012 MTS Technologies Ltd

General Purpose Registers


y The 8051 has 4 selectable banks of 8 addressable 8-

bit registers, R0 to R7. y This means that there are essentially 32 available general purpose registers, although only 8 (one bank) can be directly accessed at a time. y To access the other banks, we need to change the current bank number in the flag status register.

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

y An "Addressing Mode" refers to how you are

addressing a given memory location. y The Addressing Modes are as follows:


y y y y y y

Immediate Addressing Register Addressing Direct Addressing Indirect Addressing External Direct Code Indirect

MOV A,#20H MOV A,R0 MOV A,30H MOV A,@R0 MOVX A,@A+DPTR MOVC A,@A+DPTR

Each of these addressing modes provides important flexibility.


2/18/2012 MTS Technologies Ltd

Immediate Addressing Mode


y Immediate addressing is so-named because the

value to be stored in memory immediately follows the operation code in memory. y The instruction itself dictates what value will be stored in memory. MOV A,#20h y This instruction uses Immediate Addressing because the Accumulator will be loaded with the value that immediately follows.

2/18/2012

MTS Technologies Ltd

Register Addressing Mode


y This mode involves the use of registers to hold

the data to be manipulated. MOV A,R0 MOV R2,A y The source &destination registers must match in size. y If the source is an 8-bit register, the destination should be 8-bit register. MOV DPTR,#2456H MOV A,DPL
2/18/2012 MTS Technologies Ltd

Direct Addressing Mode


y Direct addressing is so-named because the

value to be stored in memory is obtained by directly retrieving it from another memory location. MOV A,30h y This instruction will read the data out of Internal RAM address 30 (hexadecimal) and store it in the Accumulator.

2/18/2012

MTS Technologies Ltd

Indirect Addressing Mode


y Indirect

addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility. MOV A,@R0 y This instruction causes the 8051 to analyze the value of the R0 register. y The 8051 will then load the accumulator with the value from Internal RAM which is found at the address indicated by R0.

2/18/2012

MTS Technologies Ltd

External Direct
y External Memory is accessed using a suite of instructions which use "External Direct" addressing. y Because it appears to be direct addressing, but it is used to access external memory rather than internal memory. y There are only two commands that use External Direct addressing mode:
MOVX A,@DPTR MOVX @DPTR,A

y Both commands utilize DPTR. y In these instructions, DPTR must first be loaded with the address of external memory that you wish to read or write.
2/18/2012 MTS Technologies Ltd

Code Indirect
This addressing mode is used to access the data from the code (Program) memory. MOVC A,@A+DPTR If we want to use this addressing mode make sure that the accumulator should be clear before calling this function.

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

Alphabetical List of Instructions


y y y y y y y y y y y y y

ACALL ADD/ADDC ANL CJNE CLR CPL DA DEC DIV DJNZ INC JB JNB

y y y y y y y y y y y y y

JC JNC JZ JNZ LCALL LJMP MOV MOVC MOVX MUL NOP ORL POP

PUSH RET RETI RL RLC RR RRC SETB SJMP SUBB SWAP XCH XRL

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

How to make Port as Input Port?


y By default all the ports acts as Output Ports. y To make any port as input port, we need to set all

the bits of that port to 1. y To make any port pin as input port we need to make that pin as 1. mov p1,#0ffh setb p2.5 To make port1 as i/p. To make p2.5 as i/p.

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

Summary of Topics

y Understanding of Keil C Programming y Device Driver Development on 8051 y Example Programs

2/18/2012

MTS Technologies Ltd

Important considerations for C on a microcontroller are:


y Control of On and Off-chip peripheral devices. y Servicing of interrupts. y Making the best use of limited instruction sets. y Supporting different ROM/RAM configurations. y A very high level of optimization to conserve

code space. y Control of register bank switching.


2/18/2012 MTS Technologies Ltd

Why we use Super Loop?


The Super loop or endless loop is required, because we have no Operating System to returned to: Our application will keep looping until the system power is removed. Example : While(1) { statements; }
2/18/2012 MTS Technologies Ltd

What is the minimum S/w environment need to create an embedded C program?

2/18/2012

MTS Technologies Ltd

Example:
Void main (void) { while (1) //This is Super Loop. { P1=0xAA; delay (20); P1=0x55; delay(20); } } //This Program continuously toggles the Port1.
2/18/2012 MTS Technologies Ltd

Reading or Writing to Port Pins?


y For reading the data from Port, we need to

make that Port as Input Port by moving 0xFF.

y For writing the data from Port, no need to

initialize any thing. Directly we can access.

y In order to read from a pin, we need to ensure

that the last thing written to the pin was a 1.

2/18/2012

MTS Technologies Ltd

SFR s and PORTS


y SFR means Special Function Register. y Control of 8051 ports through S/w is carried out

using SFRs. y SFR is area of memory in Internal RAM.

2/18/2012

MTS Technologies Ltd

Address of Ports:
y P0 is at address 0x80 y P1 is at address 0x90 y P2 is at address 0xA0 y P3 is at address 0xB0

Ex: sfr P0 = 0x80; sfr P1 = 0x90; etc.

2/18/2012

MTS Technologies Ltd

SFR16
y Some 8051 derivatives use two SFRs with

consecutive addresses to specify 16-bit values. y For example, the 8052 uses addresses 0xCC and 0xCD for the low and high bytes of timer/counter 2. y The Cx51 compiler provides the sfr16 data type to access 2 SFRs as a 16-bit SFR.

2/18/2012

MTS Technologies Ltd

Memory Models
Different types are: TINY SMALL MEDIUM COMPACT LARGE HUGE

2/18/2012

MTS Technologies Ltd

Memory Models Contd...

y Whereas the PC programmer chooses one of the type

to control how the processor segmentation of the RAM is to be used (overcome!)

y The 8051 user has to decide where the program and

data are to reside.

2/18/2012

MTS Technologies Ltd

Memory Models Cont...


y SMALL - all variables and parameter-passing

segments will be placed in the 8051 s internal memory. y Total RAM 128 bytes (8051/31) y COMPACT- variables are stored in paged memory addressed by ports 0 and 2. Indirect addressing opcodes are used. On-chip registers are still used for localsand parameters. y Total RAM 256 bytes off-chip, 128 or 256 bytes on-chip.

2/18/2012

MTS Technologies Ltd

Memory Models Cont...


y LARGE - variables etc. are placed in external

memory addressed by @DPTR. y On-chip registers are still used for locals and parameters. y Total RAM up to 64KB, 128 or 256 bytes on-chip.

2/18/2012

MTS Technologies Ltd

Memory Models Cont...


y Single chip 8051 users only use the SMALL model. y If they have an external RAM fitted which can be page

addressed from Port 0 or Port 2, using MOVX A,@R0

2/18/2012

MTS Technologies Ltd

SBIT
y To write a single pin, we can make use of an sbit

variable in keil(c51). y Syntax: sbit asd = P1^0; y Here, P1^0 is a single bit assigned to asd.

2/18/2012

MTS Technologies Ltd

8051 Memory Configurations


y This starts at D:00 (the D: prefix implies DATA

segment) and ends at 07fH (127 decimal). y This RAM can be used for program variables. y It is directly addressable, so that instructions like MOV A, x are usable.

2/18/2012

MTS Technologies Ltd

Memory Configurations....
y A second memory area exists between 80H and 0FFH

which is only indirectly addressable and is prefixed by I: and known as IDATA. y It is only accessible via indirect addressing (MOV A, @Ri) y It effectively overlays the directly addressable sfr area.

2/18/2012

MTS Technologies Ltd

Memory Configurations....
y A third memory space, the CODE segment, also

starts at zero, but this is reserved for the program. y It typically runs from C:0000 to C:0FFFFH (65536 bytes) but as it is held within an external EPROM, it can be any size up to 64KB (65536 bytes). y The CODE segment is accessed via the program counter (PC) for opcode fetches and by DPTR for data.

2/18/2012

MTS Technologies Ltd

Memory Configurations....
y A fourth memory area is also off-chip, starting at X:0000. y This exists in an external RAM device and, like the C:0000 segment, can extend up to X:0FFFFH (65536 bytes). y The X: prefix implies the external XDATA segment. y The 8051 s only 16 bit register, the DPTR (data pointer) is used to access the XDATA. y Finally, 256 bytes of XDATA can also be addressed in a paged mode. y Here an 8-bit register (R0) is used to access this area, termed PDATA.

2/18/2012

MTS Technologies Ltd

C51 Variables
y bit =1 bit 0-1 y char =8 bits 0 - +/- 127 y unsigned char =8 bits 0 - 255 y int =16 bits 0 - +/-32768 y unsigned int =16 bits 0 - 65535 y long =32 bits 0 - +/- 2.147483648x109 y unsigned long =32 bits 0 - 4.29496795x109 y float =32 bits +/-1.176E-38 to +/-3.4E+38 y pointer =24/16/8 bits Variable address
2/18/2012 MTS Technologies Ltd

Modular Programming In C51


y As an electronic system is composed of several

modules, each with a unique function, so a software system is built from a number of discrete tasks. y In the electronic case, each module is designed and perfected individually and then finally assembled into a complete working machine.

2/18/2012

MTS Technologies Ltd

Bit Wise Operators


Following are the bit wise operators: y & Bit Wise y | Bit Wise OR y ^ Bit Wise XOR y << Left Shift y >> Right Shift y ~ 1 s Compliment AND

2/18/2012

MTS Technologies Ltd

Example:
#include<reg51.h> void main (void) { P0 = 0x12 & 0x34; P1 = 0x23 | 0x56; P2 = 0x45 ^ 0x 41; P3 = ~P2; P1 = 0x62 >> 5; P3 = 0x45 << 6; }
2/18/2012 MTS Technologies Ltd

Bit Wise Operators Cont..


y Bit wise operators use only with unsigned data

types. y 8051 does not support signed arithmetic. y Extra code is required to manipulate signed data. y This reduces your program speed and increases the program size.

2/18/2012

MTS Technologies Ltd

Command Data Type:


y y

The typedef keyword allows us to provide aliases for data types. We can use these aliases in place of the original types.

Example: typedef unsigned int tint; y If you want to use unsigned int, we can directly use tint.

2/18/2012

MTS Technologies Ltd

Accessing External Memory:


y To access external memory, we use XBYTE.

Syntax: XBYTE [loc]. y loc is an address in the range 0x0000- 0xffff y XBYTE is part of the absacc.h header file.

2/18/2012

MTS Technologies Ltd

Example:
#include <reg51.h> #include <absacc.h> void main() { unsigned char i; XBYTE[0]='A'; XBYTE[1]='B'; XBYTE[2]='C'; XBYTE[3]='D'; XBYTE[4]='E'; XBYTE[5]='F'; for (i=0;i<5;i++) { P1 = XBYTE [i]; } }
2/18/2012 MTS Technologies Ltd

Explanation
y In the above example, I have stored six alphabets

starting address from 0x0000 to 0x0005.


y And retreived these values to Port1.

2/18/2012

MTS Technologies Ltd

Interrupt Programming in C51


Two Major Features: y We can assign a unique number to 8051 interrupts. y We can also assign a register bank to an ISR. y This ISR avoids code overhead due to the pushes and pops of the R0- R7 registers.

2/18/2012

MTS Technologies Ltd

8051 Interrupt Numbers in C


y External interrupt0 (INT0) y Timer interrupt0 y External interrupt1 y Timer interrupt1

0 1 2 3 4

(TF0) (INT1) (TF1)

y Serial communication (RI+TI)

2/18/2012

MTS Technologies Ltd

Example:
#include<reg51.h> sbit switch1 = P1^0; sbit switch2 = P1^2; sbit led = P1^5; void timer0(void) interrupt 1 { switch2=~switch2; } void main() { switch1 = 1; TMOD = 0x02; TH0 = 0x43; IE = 0x82; while(1) { TR0 = 1; led = switch1; } } MTS Technologies Ltd

2/18/2012

Explanation:
y In the above example, the value corresponding switch1 is

represented by LED.

y Timer0 is enabled by using IE register. y When ever timer0 interrupt is generated then only the

switch2 is toggled.

2/18/2012

MTS Technologies Ltd

_____ _______

2/18/2012

MTS Technologies Ltd

y 8051 has two 16-bit timers and counters

Timer/Counter 0 y Timer/Counter 1
y

y Timer is used to generate some delay. y Counter is used to count external events occurred

in the physical world.

2/18/2012

MTS Technologies Ltd

y These are 16-bit Timers/ Counters. y These are accessed as Low byte (TLx) and High

byte (THx).
y For Timer0, TH0 and TL0. y For Timer1, TH1 and TL1.

y To control these Timers/Counters we will use

TMOD & TCON SFR s.

2/18/2012

MTS Technologies Ltd

TIMER 0

TH0

TL0

TIMER 1

TH1

TL1

2/18/2012

MTS Technologies Ltd

TMOD (Timer Mode) Register


y The TMOD SFR is used to control the mode of

operation of both timers. y Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. y The higher four bits (bits 4 through 7) are related to Timer 1 where as the lower four bits (bits 0 through 3) perform the exact same functions, but for timer 0.
2/18/2012 MTS Technologies Ltd

GATE: y When this bit is set the timer will start through external Hardware only. y When this Gate bit is 0, the timer will run through software only. C/T: (Counter/ Timer) y When this bit is 0, the timer/counter is used as Timer to generate delay. y When this bit is 1, the timer/counter is used as Counter to count external events.
2/18/2012 MTS Technologies Ltd

y M1 & M0 are used to select the Mode of the timer.

M1 0 0 1 1

M0 0 1 0 1

Mode 0 1 2 3

Description 13-bit Timer 16-bit Timer 8-Auto Reload Split Timer Mode

2/18/2012

MTS Technologies Ltd

13-bit Timer Mode


y Timer mode "0" is a 13-bit timer. y Generally the 13-bit timer mode is not used in new

development.

2/18/2012

MTS Technologies Ltd

16-bit Timer Mode


y Timer mode "1" is a 16-bit timer. y This is a very commonly used mode. y TLx is incremented from 0 to 255. y When TLx is incremented from 255, it resets to

0 and causes THx to be incremented by 1. y Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. y If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.
2/18/2012 MTS Technologies Ltd

8-bit Auto Reload


y Timer mode "2" is an 8-bit auto-reload mode.

What is that?
y When a timer is in mode 2, THx holds the

"reload value" and TLx is the timer itself. y Thus, TLx starts counting up. y When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of mode 1), it will be reset to the value stored in THx.
2/18/2012 MTS Technologies Ltd

Split Timer Mode


y Timer mode "3" is a split-timer mode. y When Timer 0 is placed in mode 3, it essentially

becomes two separate 8-bit timers. y That is to say, Timer 0 is TL0 and Timer 1 is TH0. y Both timers count from 0 to 255 and overflow back to 0. y All the bits that are related to Timer 1 will now be tied to TH0.
2/18/2012 MTS Technologies Ltd

The TCON SFR

y Upper Nibble for Timer/Counter and Lower Nibble

for Interrupts. y TR (Timer Run), If timer/counter will run.


y y

we set the bit TRx,

TR = 0 means Off TR = 1 means On

2/18/2012

MTS Technologies Ltd

TF (Timer Flag)

y TF0 for Timer Flag 0 y TF1 for Timer Flag 1 y Initially TFx=0, When THx and TLx roll over from 0000

to FFFFh, then TFx will set to 1.

2/18/2012

MTS Technologies Ltd

Counter
y Counter is used to count external events occurred in the physical world. y When the timer is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL. y When the C/T=1, the counter counts up as pulses are fed from
y y

Timer 0 input pin (Pin 14, P3.4). Timer 1 input pin (Pin 15, P3.5).

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

What is Device Driver?


y A device driver is a program that controls a particular type of device that is attached to the microcontroller. y Different Devices we can control using microcontroller are:
y Switch y LED y UART y LCD y 4x4 Keypad y DC Motor y Stepper Motor y ADC

2/18/2012

MTS Technologies Ltd

How we will interface a Switch to 8051?


y Any switch have 2 leads. y One is connected to any Port pin

of the 8051. y Other one is connected to ground. y When the switch is pressed we will get a 0 signal at the port pin.

2/18/2012

MTS Technologies Ltd

How we will interface an LED to 8051?


y For any LED we have 2 leads. y Anode is connected to the Port pin with the help of a small resistor. y Cathode pin is connected to the Ground. y When we give the high signal to the 8051 port pin the LED will Glow. y If we clear the port pin LED will be Off.

2/18/2012

MTS Technologies Ltd

Example:
y Let us assume that,
Program:
ORG 00h SETB P2.5 BACK:JB P2.5,ON CLR P1.0 SJMP BACK ON:SETB P1.0 SJMP BACK END

an LED is connected to P1.0 and a switch is connected to P2.5 of the 8051. y Our aim is, when we press the switch then only the LED will glow.

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

LCD Interfacing
y Generally we will use 2x16 LCD.

y It has 2 rows and 16 columns.

2/18/2012

MTS Technologies Ltd

LCD Pin Description


y 2x16 LCD has 16 pins.
y 1st pin is Vss (Ground) y 2nd pin is Vcc (Power Supply) y 3rd pin is Vee (Brightness / Contrast) y 4th pin is RS (Register Select) y 5th pin is R/W (Read / Write) y 6th pin is E

(Enable)

2/18/2012

MTS Technologies Ltd

y Pin 7
y

14 are Data Pins.


7th pin is D0. .. 14th pin is D7.

y 15th Pin is Vcc (Back Light Purpose). y 16th pin is Ground.

If we are not connect the pins 15 & 16, the back light of LCD will be off.

2/18/2012

MTS Technologies Ltd

LCD Registers
y RS (Register Select)
y If RS = 0, the instruction command code is selected,

allowing the user to send a command.


y If RS = 1, the data register is selected, allowing the

user to send data to be displayed on the LCD.

2/18/2012

MTS Technologies Ltd

y R/W (Read / Write):


y R/W =1, input allows the user to read data from the

LCD. y R/W = 0, input allows the user to send data to the LCD.

y E (Enable):
y A High-to-Low pulse must be applied to this pin in

order to transmit the data to LCD. y The Pulse must be a minimum of 450nS.

2/18/2012

MTS Technologies Ltd

LCD Command Codes


Code Command to the LCD

38h 01h 06h 0Eh 80h C0h

Initialization of 2 lines, 5x7 matrix. To clear the display screen. To increment the cursor to Right side. Display on, cursor blinking. Force cursor to beginning of 1st line. Force cursor to beginning of 2nd line.

In this way there are so many commands to initialize the LCD. These are some use full commands.
2/18/2012 MTS Technologies Ltd

Example: To display HAI on LCD


ORG 00H

MOV P2,#38H ACALL COMMAND MOV P2,#01H ACALL COMMAND MOV P2,#06H ACALL COMMAND MOV P2,#0EH ACALL COMMAND MOV P2, #80H ACALL COMMAND
2/18/2012 MTS Technologies Ltd

MOV P2,# H ACALL DISPLAY MOV P2,# A ACALL DISPLAY MOV P2,# I ACALL DISPLAY BACK: SJMP BACK

COMMAND: CLR P1.0 CLR P1.1 SETB P1.2 NOP CLR P1.2 ACALL DELAY RET

DISPLAY: SETB P1.0 CLR P1.1 SETB P1.2 NOP CLR P1.2 ACALL DELAY RET

DELAY: MOV R0,#255 REP: MOV R1,#255 DJNZ R1,$ DJNZ R0,REP RET

END

2/18/2012

MTS Technologies Ltd

LCD interface program


#include<reg51.h> #define DATA P1 //INCLUDE THE HEADER FILE //MAKE P1 AS DATA PORT void lcd_command (unsigned char);//PROTOTYPE FOR //COMMAND SUBROUTINE void lcd_data (unsigned char); //PROTOTYPE FOR //DATA SUBROUTINE void delay (unsigned int); sbit rs = P2^0; sbit rw = P2^1; sbit e = P2^2; void main( ) { lcd_command(0x38); delay(250); lcd_command(0x0E); delay(250);
2/18/2012 MTS Technologies Ltd

// P2.0 is for RS // P2.1 is for R/W // P2.2 is for E

//SELECT THE LCD (16X2) //CURSOR BLINKING

lcd_command(0x06); delay(250); lcd_command(0x01); delay(250); lcd_command(0x80); delay(250); lcd_data ('U'); delay(250); lcd_data ('T'); delay(250); lcd_data ('L'); delay(250); } void delay (unsigned int value) { int i, j; for (i=0; i<1275; i++) for (j=0; j<value; j++); }
2/18/2012 MTS Technologies Ltd

//SHIFT CURSOR TO RIGHT //CLEAR THE LCD //FORSE THE CURSOR AT //THE BEGINNING OF //FIRST LINE // SEND Character TO THE LCD

//DELAY SUBROUTINE

void lcd_command (unsigned char value) //COMMAND SUBROUTINE { DATA = value; // SEND VALUE TO PORT rs = 0; //MAKING RS PIN AS LOW rw = 0; //MAKING RW PIN AS HIGH e = 1; delay(20); e = 0; //MAKING E PIN HIGH TO LOW return; } void lcd_data (unsigned char value) { DATA = value; rs = 1; rw = 0; e = 1; delay(20); e = 0; return; }
2/18/2012 MTS Technologies Ltd

//DATA SUBROUTINE

2/18/2012

MTS Technologies Ltd

Serial Communication
y There are two types of Communication.
y

Serial and Parallel.

y In Parallel communication 8-bits of data

transmitted at a time and are used for shorter distance.


y

Printers, Hard Disks, etc.

y In Serial Communication 0ne bit of data is send

at a time and used for longer distance.


y

Telephone, Modems, etc.

2/18/2012

MTS Technologies Ltd

Basics of Communication

y Serial communication uses a single data line

instead of 8-bit data lines in parallel communication. y At the transmitter bytes of data is converted to serial bits using a parallel-in-serial-out register. y At the receiver end serial data is packed into bytes by serial-in-parallel-out register.
2/18/2012 MTS Technologies Ltd

Serial Communication
y Serial Communication uses two methods:
y y

Synchronous Asynchronous

y Synchronous: Transfers a block of data at a time. y Asynchronous: Transfers single byte at a time. y There are special IC s made by different

manufacturers for serial data communication. y UART (Universal Asynchronous Receiver Transmitter) y USART (Universal Synchronous Asynchronous Receiver Transmitter.

2/18/2012

MTS Technologies Ltd

Asynchronous Serial Communication


y Each character is placed between a start bit and

stop bits. y Start bit is always a Low (0), and Stop bit is always a High (1). y Baud Rate: The rate of data transfer in serial data communication is stated as bps or Baud Rate.

2/18/2012

MTS Technologies Ltd

y Serial communication is a form of I/O in which the

bits of byte is transferred one after the other in a timed sequence on a single wire. y Serial communication is often used to transmit or receive data from the embedded processor.

2/18/2012

MTS Technologies Ltd

RS232 Standards
y To

allow compatibility among data communication equipment made by various manufacturers, an interfacing standard called RS232 was set by the Electronics Industries Association (EIA) in 1960. y In RS232, a 1 is represented by -3 to -25V, while a 0 bit is +3 to +25V, making -3 to +3V undefined. y But, 8051 microcontrollers voltage levels are TTL compatible.

2/18/2012

MTS Technologies Ltd

How to rectify this problem?


y For

this reason, to connect any RS232 to microcontroller system, we must use voltage converters. y MAX232 is the IC used to convert the TTL Levels into RS232 voltage levels and vice versa. y These MAX232 IC s are commonly referred as line drivers.

2/18/2012

MTS Technologies Ltd

MAX232
y The MAX232 has two sets of line drivers for transferring data. y The line drivers used for TxD are called T1 & T2. y The line drivers used for RxD are referred as R1 & R2. y In many applications only one of each is used. y T1 and R1 are used together for TxD and RxD of the 8051 and the second set is left open.

2/18/2012

MTS Technologies Ltd

Interfacing 8051 with PC


will use DB9 Connectors to interface 8051 with PC. y Among these 9 pins we will use 3 pins only. y Those are
y We
y 2 pin for RxD. y 3 pin for TxD. y 5 pin for Ground.

And the remaining pins are hand shaking signals.


2/18/2012 MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

Serial Port Programming


y To allow data transfer between the PC and an

8051 system with out any error, we must make sure that the baud rate of the 8051 system matches the baud rate of the PC s COM port. Some of the baud rates supported by PC BIOS are listed in the table.

2/18/2012

MTS Technologies Ltd

Baud Rate in the 8051


y The 8051 transfers and receives data serially at

many different baud rates. y The baud rate in the 8051 is programmable. This is done with the help of Timer 1. y The 8051 divides the crystal frequency by 12 to get the machine cycle frequency. y The 8051 serial communication UART circuitry divides the machine cycle frequency of 921.6 KHz by 32 once more before it is used by Timer1 to set the baud rate. y When timer1 is used to set the baud rate it must be programmed in Mode 2, that is 8-bit, auto reload.
2/18/2012 MTS Technologies Ltd

Timer 1 TH1 register values for Various Baud Rates

Baud Rate

TH1 (Decimal)

TH1 (HEX)

9600 4800 2400 1200

-3 -6 -12 -24

FD FA F4 E8

Note: Crystal Frequency = 11.0592MHz


2/18/2012 MTS Technologies Ltd

SBUF Register
y SBUF is an 8-bit register used solely for serial

communication in the 8051. y To transmit any data from 8051 via TxD line, it must be placed in the SBUF register. y Similarly, SBUF holds the byte of data when it is received by the 8051 RxD line. Example:
MOV SBUF, #D MOV SBUF, A MOV A, SBUF
2/18/2012

;load SBUF=44H, ASCII for D. ;Copy Accumulator into SBUF. ;Copy SBUF into Accumulator.

MTS Technologies Ltd

SCON (Serial Control) Register

y It is an 8-bit register used to program the start bit,

stop bit, and data bits of data framing, among other things. y SM0, SM1: These two bits determine the framing of data by specifying the number of bits per character, and the start and stop bits.

2/18/2012

MTS Technologies Ltd

SM0 SM1 0 0 Serial Mode 0 0 1 Serial Mode 1 1 0 Serial Mode 2 1 1 Serial Mode 3 Serial Mode 0: 9-bit data with variable baud rate. Serial Mode 1: 8-bit data, 1 start, 1 stop bit with variable baud rate. Serial Mode 2: 9-bit data with fixed baud rate. Serial Mode 3: 8-bit data with fixed baud rate.
Out of these 4 modes, we will use only Mode 1.
2/18/2012 MTS Technologies Ltd

y SM2: We will make this bit as 0, because we are not

using the 8051 in a multiprocessor environment. y REN (Receive Enable): When the REN bit is high, it allows the 8051 to receive data on RxD pin of 8051. If we want the 8051 to both transfer and receive data, REN must be set to 1. By making REN=0, the receiver is disabled.

2/18/2012

MTS Technologies Ltd

y TB8 & RB8: These are used for serial modes 2

and 3.
y We will make TB8 and RB8 as 0, since it is not used

in our applications.

y TI (Transmit Interrupt): This is an important

flag bit in the SCON register.


y When the 8051 finishes the transfer of the 8-bit

character, it raises the TI flag to indicate that it is ready to transfer another byte. y This bit is raised by the beginning of the Stop bit.

2/18/2012

MTS Technologies Ltd

y RI (Receive Interrupt):
y When the 8051 receives data serially via RxD, it

gets rid of start and stop bits and places the byte in the SBUF register. y Then it raises the RI flag bit to indicate that a byte has been received and should be picked up before it lost.

2/18/2012

MTS Technologies Ltd

Programming the 8051 for serial communication


y Configure the TMOD register. y Set the baud rate using TH1. y Configure the SCON register. y Start the Timer.

Transmitting data to PC: y Send data to the serial buffer. y Monitor the TI flag. Receiving data from PC: y Monitor the RI flag. y Receive data from serial buffer.
2/18/2012 MTS Technologies Ltd

Example:
Transmitting:
ORG 00h MOV TMOD,#20H MOV SCON,#50H MOV TH1,#0FDH SETB TR1 MOV SBUF,#S HERE:JNB TI,HERE CLR TI BACK:SJMP BACK END

Receiving:
ORG 00h MOV TMOD,#20H MOV SCON,#50H MOV TH1,#0FDH SETB TR1 HERE:JNB RI,HERE MOV A,SBUF MOV P2,A CLR RI BACK:SJMP BACK END

2/18/2012

MTS Technologies Ltd

Example:
#include<reg51.h> void main (void)
{

TMOD =0X20; SCON = 0X50; TH1 = 0XFD; TR1 = 1; WHILE(1) { SBUF = A ; WHILE(TI==0); TI=0; } }
2/18/2012 MTS Technologies Ltd

2/18/2012

MTS Technologies Ltd

Das könnte Ihnen auch gefallen