34. 31
.بٛد خٛاٞذ صیش ٝؿبی چیضی ٜؿذ تِٛیذ اػٕبّی وذ
.وٙیذ ٝاضاف ٓبٛدی ٝ٘ٛؿت لبال ٝو وذٞایی ٝب سا ٖػىؼتا اػٕبّی وذٞای
.بذٞیذ ٜاؿاس ٞا ٜؿٕاس ٗای ابتذای ٝب سا ٌش ٜاؿاس
ٗسٚتی ٚdrawing_page.وٙیذ خٛا٘ی فشا سا
…
…
;----------------------------------------------------
mov DPTR,#my_logo
acall drawing_page
here:sjmp here
;---------------------drawing_page-------------------
…
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 128,128,64,64,64,64,64,32,32,32,32,32,64,64,64,64
DB 64,128,128,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,128,0,0,192,224,0,0,0,128,0
DB 0,0,0,0,0,128,0,0,64,224,64,192,224,0,0,0
DB 0,0,0,0,0,64,96,64,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,128,192,32,24,24,12,10,10,9,9
DB 8,16,16,32,64,128,0,0,192,48,8,8,4,4,2,2
DB 2,2,2,5,5,10,10,52,200,24,32,192,0,0,0,30
DB 25,27,14,0,12,24,27,27,14,3,31,0,128,193,207,125
DB 0,0,0,12,10,15,30,24,24,25,15,3,31,0,0,0
DB 14,24,24,24,24,30,13,31,0,88,78,88,24,14,24,14
DB 24,14,0,0,120,192,195,194,200,124,24,0,24,14,24,14
DB 24,30,26,25,25,15,15,25,24,24,31,31,30,0,0,0
DB 0,0,0,0,248,62,193,0,0,0,0,0,0,0,0,56
DB 124,254,206,108,56,193,62,31,96,128,0,56,124,238,206,254
DB 124,56,0,0,0,0,0,128,96,31,0,0,1,6,248,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,15,240,0,32,65,66,132,4,8,8,8,8,8
DB 8,4,4,2,1,128,64,32,32,33,34,66,132,4,8,8
DB 8,8,8,4,4,2,2,1,128,64,32,16,8,0,240,15
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,1,6,8,48,64,128,1,2,2,4,4,8
DB 8,16,16,16,32,32,32,32,32,32,32,32,32,32,32,32
DB 16,16,8,8,4,2,2,1,0,128,64,48,8,6,1,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,1,1,2,4,4,8,8
DB 16,16,32,32,32,32,32,64,64,64,64,64,32,32,32,32
DB 32,16,16,8,8,4,4,2,1,1,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 1,1,1,1,225,33,33,193,33,33,193,1,193,33,33,33
DB 193,1,253,65,33,33,193,1,65,161,33,65,1,193,161,161
DB 161,193,1,225,65,33,33,193,1,1,1,1,1,1,1,65
DB 161,33,65,1,193,161,161,161,193,1,229,1,249,37,1,225
DB 25,9,197,37,37,229,9,25,225,1,225,1,1,225,1,1
DB 161,161,161,193,1,253,65,33,33,193,1,193,33,33,33,193
DB 1,193,33,33,33,193,1,1,1,193,33,33,33,65,1,193
DB 33,33,33,193,1,225,33,33,193,33,33,193,1,1,1,1
DB 0,0,0,0,7,0,0,7,0,0,7,0,3,4,4,4
DB 3,0,7,0,0,0,7,0,2,4,5,2,0,3,4,4
DB 4,2,0,7,0,0,0,7,16,16,16,16,16,16,0,2
DB 4,5,2,0,3,4,4,4,2,0,7,0,7,0,0,1
DB 6,4,8,9,9,8,9,1,1,16,17,14,2,1,0,3
DB 4,4,4,7,0,7,0,0,0,7,0,3,4,4,4,3
DB 0,3,4,4,4,3,3,3,0,3,4,4,4,2,0,3
DB 4,4,4,3,0,7,0,0,7,0,0,7,0,0,0,0
35. 32
…
;-----------------------my_logo-----------------------
my_logo:
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 128,128,64,64,64,64,64,32,32,32,32,32,64,64,64,64
DB 64,128,128,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,128,0,0,192,224,0,0,0,128,0
DB 0,0,0,0,0,128,0,0,64,224,64,192,224,0,0,0
DB 0,0,0,0,0,64,96,64,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,128,192,32,24,24,12,10,10,9,9
DB 8,16,16,32,64,128,0,0,192,48,8,8,4,4,2,2
DB 2,2,2,5,5,10,10,52,200,24,32,192,0,0,0,30
DB 25,27,14,0,12,24,27,27,14,3,31,0,128,193,207,125
DB 0,0,0,12,10,15,30,24,24,25,15,3,31,0,0,0
DB 14,24,24,24,24,30,13,31,0,88,78,88,24,14,24,14
DB 24,14,0,0,120,192,195,194,200,124,24,0,24,14,24,14
DB 24,30,26,25,25,15,15,25,24,24,31,31,30,0,0,0
DB 0,0,0,0,248,62,193,0,0,0,0,0,0,0,0,56
DB 124,254,206,108,56,193,62,31,96,128,0,56,124,238,206,254
DB 124,56,0,0,0,0,0,128,96,31,0,0,1,6,248,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,15,240,0,32,65,66,132,4,8,8,8,8,8
DB 8,4,4,2,1,128,64,32,32,33,34,66,132,4,8,8
DB 8,8,8,4,4,2,2,1,128,64,32,16,8,0,240,15
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,1,6,8,48,64,128,1,2,2,4,4,8
DB 8,16,16,16,32,32,32,32,32,32,32,32,32,32,32,32
DB 16,16,8,8,4,2,2,1,0,128,64,48,8,6,1,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,1,1,2,4,4,8,8
DB 16,16,32,32,32,32,32,64,64,64,64,64,32,32,32,32
DB 32,16,16,8,8,4,4,2,1,1,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DB 1,1,1,1,225,33,33,193,33,33,193,1,193,33,33,33
DB 193,1,253,65,33,33,193,1,65,161,33,65,1,193,161,161
DB 161,193,1,225,65,33,33,193,1,1,1,1,1,1,1,65
DB 161,33,65,1,193,161,161,161,193,1,229,1,249,37,1,225
DB 25,9,197,37,37,229,9,25,225,1,225,1,1,225,1,1
DB 161,161,161,193,1,253,65,33,33,193,1,193,33,33,33,193
DB 1,193,33,33,33,193,1,1,1,193,33,33,33,65,1,193
DB 33,33,33,193,1,225,33,33,193,33,33,193,1,1,1,1
DB 0,0,0,0,7,0,0,7,0,0,7,0,3,4,4,4
DB 3,0,7,0,0,0,7,0,2,4,5,2,0,3,4,4
DB 4,2,0,7,0,0,0,7,16,16,16,16,16,16,0,2
DB 4,5,2,0,3,4,4,4,2,0,7,0,7,0,0,1
DB 6,4,8,9,9,8,9,1,1,16,17,14,2,1,0,3
DB 4,4,4,7,0,7,0,0,0,7,0,3,4,4,4,3
DB 0,3,4,4,4,3,3,3,0,3,4,4,4,2,0,3
DB 4,4,4,3,0,7,0,0,7,0,0,7,0,0,0,0
end
38. 35
1. کامپیوتر اکامپیوتر ابب :ارتباط:ارتباطضمیمهضمیمه
پٛست سٚی ِٚتاطcomُؿحى كحٛست ٝبح سا ْوا پٛست ٚس ٗای اص ٘یؼت ٔتٙاػب ٚٔیىش ِٚتاط با وأپیٛتش
ایؼی ٝب ابتذا صیشmax 232.ٔیىٙٙذ ُٔتل
شکل1-1
.٘یؼت ُٔوا بخؾ ٗای..
اطالػات َاسػا خٟت وأپیٛتش با استباا بشلشاسی چٍٍٛ٘ی ٔذاسٞا ٌٝ٘ٛ ٗای ػاخت دس خاِب ٔباحث اص یىی
اػتخلٛف ٗای دس بیـتش اطالػات یافت دس بشای ٝؤطا ٗای یا ٚ .وٙیذ ٝٔشاخؼ صیش آدسع ٝب ٔیتٛا٘یذِب
.وٙیذ ٝٔطاِؼ ٝٔادا دس سا
http://www.8051projects.net/serial-communication/
►Introduction
UART (Universal Asynchronous Receiver Transmitter) or USART (Universal Synchronous
Asynchronous Receiver Transmitter) are one of the basic interface which you will find in
almost all the controllers available in the market till date. This interface provide a cost
effective simple and reliable communication between one controller to another controller or
between a controller and PC.
►RS-232 Basics
RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting
between a DTE (Data terminal equipment) and a DCE (Data Circuit-terminating Equipment).
Voltage Levels:
The RS-232 standard defines the voltage levels that correspond to logical one and logical zero
39. 36
levels. Valid signals are plus or minus 3 to 25 volts. The range near zero volts is not a valid
RS-232 level; logic one is defined as a negative voltage, the signal condition is called
marking, and has the functional significance of OFF. Logic zero is positive, the signal
condition is spacing, and has the function ON.
So a Logic Zero represented as +3V to +25V and Logic One represented as -3V to -25V.
شکل 1-2
►RS-232 Level Converters
Usually all the digial ICs works on TTL or CMOS voltage levels which cannot be used to
communicate over RS-232 protocol. So a voltage or level converter is needed which can convert
TTL to RS232 and RS232 to TTL voltage levels.
The most commonly used RS-232 level converter is MAX232. This IC includes charge pump
which can generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also
includes two receiver and two transmitters and is capable of full-duplex UART/USART
communication.
شکل3-1
Fig A. - MAX232 Pin Description
شکل4-1
Fig B. - MAX232 Typical Connection Circuit
40. 37
►MAX232 Interfacing with Microcontrollers
To communicate over UART or USART, we just need three basic signals which are namely,
RXD (receive), TXD (transmit), GND (common ground). So to interface MAX232 with any
microcontroller (AVR, ARM, 8051, PIC etc..) we just need the basic signals. A simple
schematic diagram of connections between a microcontroller and MAX232 is shown below
شکل5-1
In the next part of this tutorial we will discuss programming microcontroller to communicate
over UART and software implementation of half duples UART.
►8051 Programming for UART
In 8051, we make use of Timer 1 to generate the required baud rate. Following are the
registers that are need to be configured to commnunicate over UART.
TMOD
SCON
TH1
TL1
TCON
TMOD: This register is used to set the mode of Timer0 and Timer1. It is also used to select
whether the timers are used as Timer or Counter.
SCON: Serial Control register has various functions like.. it has flags for Framing error,
Transmit interrup and receive interrupt. Its used to select the serial port mode, to enable or
disable the reception etc.
TCON: This register has varios flag and control bits e.g. Timer overflow flags, interrupt edge
flags, timer control bits to start/stop the timer.
41. 38
TH1 & TL1: Timer registers for Timer 1 determines the baudrate of UART.
More information on the above registers can be found in the 8051 Hardware manual.
►Initializing USART in 8051
CODE:
Serial_Init:
;Set timer 1 mode to 8-bit Auto-Reload
mov TMOD,#20H
;Enable reception
;Set Serial port mode to 8-bit UART
mov SCON,#50H
;Set baudrate to 9600 at 11.0592MHz
mov TH1,#0FDH
mov TL1,#0FDH
;Start Timer
setb TR1
ret
in C we can do this as..
CODE:
#include <reg51.h>.
void serial_init(){
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
To Send data to the serial port we just have to move the data in SBUF (serial buffer register)
and wait for the Transmit Interrupt flag to be set. While receiving we wait for the Receive
interrupt flag to be set and read the data from SBUF register. This can be done as shown
below...
CODE:
Serial_Send:
;wait for last data to be
;sent completely
jnb TI,Serial_Send
;clear the transmit interrupt flag
clr TI
;Then move the data to send in SBUF
mov SBUF,A
ret
42. 39
Serial_Read:
;Wait for Receive interrupt flag
jnb RI,Serial_Read
;If falg is set then clear it
clr RI
;Then read data from SBUF
mov A,SBUF
ret
in C we can do this as..
CODE:
void serial_send(unsigned char dat){
while(!TI);
TI = 0;
SBUF = dat;
}
unsigned char serial_read(){
while(!RI);
RI = 0;
return SBUF;
}
►Software UART for 8051
To implement UART successfully we first need to know how the protcol works.
Diagram above shows the waveform in which the bits has to be transferred. First is the start
bit.. then 8-bit data and at last a stop bit. There is a secret formula to calculate the delay time
which is needed between bits to get correct baudrate.
Below is a software implemented UART, which can be used in C as well as Assembly
programs. It is written for Keil software. But with a little modification you can use it in your
programs.
CODE:
?SU?PUTC SEGMENT CODE
?SU?GETC SEGMENT CODE
PUBLIC _putc
PUBLIC getc
txd_pin EQU P3.1 ;Transmit on this pin
rxd_pin EQU P3.0 ;Receive on this pin
43. 45
;Formula to calculate the bit time delay constant
;This constant is calculated as: (((crystal/baud)/12) - 5) / 2
;crystal is the frequency of crystal in Hz
;baud is required baudrate
;Please try to keep baudrate below 9600
;to get best results :)
BITTIM EQU 45; (((11059200/9600)/12) - 5) / 2
;--------------------------------------------
;To send data serially
;For C programs
;Protype definition:
; void putc(unsigned char);
;Usage:
; putc(data);
;Return:
; This function returns nothing
;
;For Assembly Programs:
;
;Usage:
; data to be send has to be moved to R7
; for example:
; mov R7,#'a'
; lcall _putc
;--------------------------------------------
RSEG ?SU?PUTC
_putc:
push ACC
Push PSW
mov a,r7
CLR txd_pin ;Drop line for start bit
MOV R0,#BITTIM ;Wait full bit-time
DJNZ R0,$ ;For START bit
MOV R1,#8 ;Send 8 bits
putc1:
RRC A ;Move next bit into carry
MOV txd_pin,C ;Write next bit
MOV R0,#BITTIM ;Wait full bit-time
DJNZ R0,$ ;For DATA bit
DJNZ R1,putc1 ;write 8 bits
SETB txd_pin ;Set line high
RRC A ;Restore ACC contents
MOV R0,#BITTIM ;Wait full bit-time
DJNZ R0,$ ;For STOP bit
POP PSW
pop ACC
RET
;--------------------------------------------
;To receive data Serially
44. 41
;If you want to use this routine in your
;C program then define function prototype
; as:
; unsigned char getc(void);
;
; Usage:
; data = getc();
; Return value:
; Returns data received
;
;
;If you are using it in assembly program
; Usage:
; lcall getc
; Return:
; data received is stored in R7
;--------------------------------------------
RSEG ?SU?GETC
getc:
Push ACC
Push PSW
JB rxd_pin,$ ;Wait for start bit
MOV R0,#BITTIM/2 ;Wait 1/2 bit-time
DJNZ R0,$ ;To sample in middle
JB rxd_pin,getc ;Insure valid
MOV R1,#8 ;Read 8 bits
getc1:
MOV R0,#BITTIM ;Wait full bit-time
DJNZ R0,$ ;For DATA bit
MOV C,rxd_pin ;Read bit
RRC A ;Shift it into ACC
DJNZ R1,getc1 ;read 8 bits
mov r7,a
POP PSW
pop ACC
RET ;go home