Total Pageviews

Monday, September 22, 2014

MASTER SLAVE CONTROL using AT89C51


Assalam u Alaikum ! Today I want to share my fourth year's Microcontroller AT89C51 based project of Master Slave Control. Hope it helps you in your studies ! :)




MASTER SLAVE CONTROL
AT89C51

Introduction

There has always been needs of performing many operations on different locations through a single controller. Master Slave Control is the technique that provides these features of performing and controlling various operations in different locations.
As the name suggests, there is one master controller and one or more slave controllers. AT89C51 supports up to 255 slave controllers that can be utilized in different areas for different requirements. Master Controller is used to perform certain operations and instructs the slave controls to perform required operations as well.
Transmission and reception between master and slave controls is done serially through the Rx and Tx pins of the microcontrollers. Multiprocessing ability of the microcontrollers is thus utilized in this technique. There can also be a shift in master and slave controllers, one controller can become master or slave at any instant of time to handle the shifting of controls for required operations.
In our project we have used master slave control for the delivering the input of keyboard on the LCD and the LED’s. In our project, keyboard and LCD is attached to the master controller and the LEDs’ are attached with slave controllers. Each controller has 8 LEDs’ to show the BCD equivalent of the Number entered from the keyboard.




Block diagram


PROGRAM
MASTER CODE

lcd_data equ 090h
lcd_e equ P3.5
lcd_rw equ P3.3
lcd_rs equ P3.4
CTWRDARST EQU 01100011B
CNTWRDAOPR EQU 00100011B
CNTRWRBSTART EQU 00011010B
CONFIG EQU 38H
ENTRYMODE EQU 6
OFFCUR EQU 0CH
LINECUR EQU 0EH
BLINKCUR EQU 0DH
COMBNCUR EQU 0FH
HOMECUR EQU 02H
SHLFCUR EQU 10H
SHRTCUR EQU 14H
CLRDSP EQU 01H
OFFDSP EQU 0AH
ONDSP EQU 0EH
SHLFDSP EQU 18H
SHRTDSP EQU 1CH



ORG 0000H
LJMP MAIN

ORG 0003H
LJMP KBRFDISP                                      // KEYBOARD INTERRUPT

MAIN :

LCALL INITRS232                                              // INITIALIZE SERIAL
LCALL RESETLCD                                              //RESET LCD
LCALL INITLCD                                                  // INITIALIZE LCD
MOV P2,#OFFH                                           // INPUT FROM KEYBOARD
MOV IE,#100000001B
SETB IT0                                                    // INTERRUPT ENABLE
SJMP $                                                                 // REPEAT FOREVER

KBRDISP:
MOV A,P2
ANL A,#0FH
LCALL BCDTOASCII                        // BCD TO ASCII FOR LCD
LCALL LCALL WRLCDDATA                     // LCD PRINTING
LCALL PUTCHAR                                     // SERIAL TRANSMISSION
RETI
PUTCHAR:
JNB TI,$              // WAIT FOR PREVIOUS DATA TO BE TRANSMITTED
CLR TI
CLR TB8                                           //PREPARE FOR TRANSMIT
MOV SBUF,A
SETB TB8
RET
INITRS232:
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#11001000B                         \\ FOR MODE-2 , TB8=1
SETB TR1
RET
RESETLCD:
CLR LCD_RS
CLR LCD_RW
CLR LCD_E
MOV R0,#30H
SETB LCD_E
CLR LCD_E
MOV A,#4
LCALL DELAY
MOV LCD_DATA,#30H
SETB LCD_E
CLR LCD_E
MOV A,#1
LCALL DELAY
MOV LCD_DATA,#30H
SETB LCD_E
CLR LCD_E
MOV A,#1
LCALL DELAY
MOV R0,#CONFIG
LCALL WRLCDCOM
MOV R0,#08H
LCALL WRLCDCOM
MOV R0,#01H
LCALL WRLCDCOM
MOV R0,#ENTRYMODE
LCALL WRLCDCOM
RET
DELAY:
MOV R7,A
AGA:
MOV TH0,#0FCH
MOV TL0,#17H
SETB TR0
WAIT2:
JNB TF0,WAIT2
CLR TF0
CLR TR0
DJNZ R7,AGA
RET
WRLCDCOM:
CLR LCD_E
CLR LCD_RS
CLR LCD_RW
MOV LCD_DATA,R0
LCALL PULSEEWAIT
RET
INITLCD:
CLR LCD_RS
CLR LCD_RW
CLR LCD_E
MOV R0,#38H
LCALL WRLCDCOM
MOV R0,#0EH
LCALL WRLCDCOM
MOV R0,#06H
LCALL WRLCDCOM
MOV R0,#01H
LCALL WRLCDCOM
RET

BCDTOASCII:
CJNE A,#0AH,$+3
JNC BIG
ADD A,#30H
SJMP EXIT
BIG:
ADD A, #37H
EXIT:
MOV R0,A
RET

WRLCDDATA:
CLR LCD_E
SETB LCD_RS
CLR LCD_RW
MOV LCD_DATA,R0
LCALL PULSEEWAIT
RET
PULSEEWAIT:
CLR LCD_E
SETB LCD_E
CLR LCD_E
MOV LCD_DATA,#0FFH
SETB LCD_RW
PUSH ACC
PEW:
INC B
SETB LCD_E
MOV A,LCD_DATA
CLR LCD_E
JB ACC.7,PEW
POP ACC
RET
SLAVE 1 CODING

ORG 0000H
MOV TMOD,#20H                                               //9600 BAUD
MOV TH,#-3
MOV SCON, #0F0H                //ENABLE RECEIVER MODE 3 , MULTIPROC ENABLED
SETB TR1

BACK:
JNB RI,$                                                    // WAIT FOR CODE
CLR RI
MOV A,SBUF
CJNE A,#0AH,BACK
CLR SM2                                                   // SIMPLE PROCESSOR


GAIN:
JNB RI,$                                                    // WAIT FOR DATA
CLR RI
MOV A,SBUF
CJNE A,#ODH,AGAIN                               // D IS USED FOR RESET
SETB SM2                                                 // MULTI PROC ENABLED
SJMP BACK
AGAIN:
MOV P1,A
SJMP GAIN

SLAVE  2 CODING

ORG 0000H
MOV TMOD,#20H                                     //9600 BAUD
MOV TH,#-3
MOV SCON, #0F0H                 //ENABLE RECEIVER MODE 3 , MULTIPROC ENABLED
SETB TR1
BACK:
JNB RI,$                                                    // WAIT FOR CODE
CLR RI
MOV A,SBUF
CJNE A,#0BH,BACK
CLR SM2                                                   // SIMPLE PROCESSOR
GAIN:
JNB RI,$                                                    // WAIT FOR DATA
CLR RI
MOV A,SBUF
CJNE A,#ODH,AGAIN                               // D IS USED FOR RESET
SETB SM2                                                 // MULTI PROC ENABLED
SJMP BACK
AGAIN:
MOV P1,A
SJMP GAIN

SLAVE 3 CODING

ORG 0000H
MOV TMOD,#20H                            //9600 BAUD
MOV TH,#-3
MOV SCON, #0F0H                 //ENABLE RECEIVER MODE 3 , MULTIPROC ENABLED
SETB TR1

BACK:
JNB RI,$                                           // WAIT FOR CODE
CLR RI
MOV A,SBUF
CJNE A,#0CH,BACK
CLR SM2                                          // SIMPLE PROCESSOR

GAIN:
JNB RI,$                                           // WAIT FOR DATA
CLR RI
MOV A,SBUF
CJNE A,#ODH,AGAIN                      // D IS USED FOR RESET
SETB SM2                                        // MULTI PROC ENABLED
SJMP BACK

AGAIN:
MOV P1,A
SJMP GAIN




CONSTRUCTION
In Master Controller:
P1 is connected with LCD with control inputs from P3.
P2 is connected with Keyboard through the keyboard decoder MM74C922.
TxD Pin of Master Controller is connected with RxD pins of all slave controllers.
Each controller is properly provided crystal frequency, VCC and Ground.
In Slave Controllers:
P1 of each controller is allotted to 8 LEDs’ to show the BCD code coming from the Master Controller.

WORKING

If we analyze the program step by step then,
In main routine:
1-    RS232 serial communication is initialized in MODE 2 UART and TB8=1 for addressing in multiprocessors using suitable value in SCON register also baud rate of 9600 is set for communication.
2-    LCD is reset and initialized using separate routines.
3-    Then Port 2 which is connected to keyboard is made ready to accept input.
4-     Interrupt zero is enabled which is related to keyboard and on every key press interrupt is activated KBRDISP routine is activated.
In KBRDISP routine:
1-    Data from keyboard in port 2 is moved to accumulator.
2-    Upper nibble of data is masked as keyboard consists of 15 keys 0-F.
3-    This masked BCD code is sent to BCDtoASCII routine where it is converted in ASCII to be shown on LCD by adding suitable values (30H or 37H) in the data in accumulator.
4-    This ready to display data is sent to WRLCDDATA routine where it is printed on LCD.
5-    Then a Routine called PUTCHAR is called to transmit data to slave controllers.
In PUTCHAR routine:
1-    TI flag is used to check if the transmitter is busy or ready to send data.
2-    Then by clearing RB8 data is made ready to send, and by using SBUF data is sent and RB8 is set again for addressing.

So the process of master controller which is taking data from the keyboard, writing it to LCD and sending to other slave controllers over TxD is completed.
Now we move to the Slaves coding where they are ready to accept data.
In Slaves:
1-    First of all Baud rate of slave controller is set to match with master controller to achieve proper communication.
2-    Then slave controllers are initialized in mode 3 UART and Multiprocessor is enabled by moving suitable value in SCON register.
3-    Then Receivers wait for the code to arrive and when it arrives it is checked for the value of A,B,C for slave 1,2,3 respectively.
4-    If the code of any slave is matched then that slave is activated and Simple processor is enabled.
5-    It again waits for the inputs from the keyboard and displays on the LEDs’ until D key is pressed from the keyboard is pressed.
6-    D key of keyboard acts as a reset for the controller and Multiprocessors are again enabled and the control is transferred to initial position where all the slave controllers wait for the coming code and all the process is repeated.

Conclusion


This technique can be employed in the wide distances, where the sent code from the Master microcontroller can still be seen on slave controllers instead of being distant from the slave controller. This technique can be employed in industries as well as other machineries where one needs to handle many machines or equipment using single remote microcontroller.

No comments:

Post a Comment