Você está na página 1de 75

M.Tech.

Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 1 LCD INTERFACE TO 8051


Aim: To interface the LCD display with the 8051-SDK and display the required message on the LCD. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply LIQUID CRYSTAL DISPLAY (LCD) LCD data pins are connected to Port 0, and the control pins RS (Data/Command) pin is connected to Port3.4, RW pin is grounded and EN(Enable) pin is connected to Port3.5.

Diagram: LCD

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) General Description:

Roll No.: 11M31D5704

The Liquid Crystal Display (LCD) is a low power device (microwatts). Now-adays in most applications LCDs are being used instead of using LED displays because of its specifications like low power consumption, ability to display numbers and special characters which are difficult to display with other displaying circuits and easy to program. An LCD requires an external or internal light source. Temperature range of LCD is 0C to 60C and lifetime is an area of concern, because LCDs can chemically degrade these are manufactured with liquid crystal material (normally organic for LCDs) that will flow like a liquid but whose molecular structure has some properties normally associated with solids. Classification of LCDs 1. Dynamic-scattering LCDs and 2. Field-effect LCDs Field-effect LCDs are normally used in such applications where source of energy a prime factor (e.g., watches, portable instrumentation etc.).They absorb considerably less power than the light-scattering type. However, the cost for field-effect units is typically higher, and their height is limited to 2 inches. On the other hand, light-scattering units are available up to 8 inches in height. Field-effect LCD is used in the project for displaying the appropriate information. RS (Command / Data): This bit is to specify whether received byte is command or data. So that LCD can recognize the operation to be performed based on the bit status. RS = 0 => Command RS = 1 => Data RW (Read / Write): RW bit is to specify whether controller wants READ from LCD or WRITE to LCD. The READ operation here is just ACK bit to know whether LCD is free or not. RW = 0 => Write RW = 1 => Read Department of Electronics & Communications Nishitha College of Engineering and Technology 2

M.Tech.Sem II (ES Lab) EN (Enable LCD):

Roll No.: 11M31D5704

EN bit is to ENABLE or DISABLE the LCD. When ever controller wants to write some thing into LCD or READ acknowledgment from LCD it needs to enable the LCD. EN = 0 => High Impedance EN = 1 => Low Impedance ACK (LCD Ready): ACK bit is to acknowledge the MCU that LCD is free so that it can send new command or data to be stored in its internal Ram locations ACK = 1 => Not ACK ACK = 0 => ACK Hardware Explanation LCD Diagram:

Diagram: LCD

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) Block Diagram:

Roll No.: 11M31D5704

Hardware Connections:

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704 LCD ALGORITHM:

STEP1: Configure port pins for all hardware connections STEP2: Initialize the LCD by passing the proper set of COMMANDS STEP3: Display the DATA in LCD

STEPS FOR WRITING COMMAND TO LCD STEP4: Write each COMMAND to LCD COMMAND WRITE ADDRESS of LCD STEP5: Repeat STEP4 and STEP5 until writing all COMMANDS

STEPS FOR WRITING DATA TO LCD STEP6: Write each Character to LCD DATA WRITE ADRESS of LCD STEP7: Repeat STEP7 and STEP8 until writing total data. STEP8: End

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Flow Chart for LCD Module:

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/************************************************************/ /* PROJECT NAME: LCD */ /************************************************************/ /* Filename: LCD.c /* Language: C /* Compiler: Keil uV2 /* Assembler: #include <REGX51.H> #define LCD P0 #define RS P3_4 #define EN P3_5 void lcdInit(void); void putComL(unsigned char); void putCharL(unsigned char); void putStrL(unsigned char *,unsigned char); void delay(unsigned int d); int main(void) { lcdInit(); delay(1000); putStrL("LCD TEST",0x01); putStrL("OK",0xC0); while(1); } void lcdInit(void) { putComL(0x38); putComL(0x0c); putComL(0x06); putComL(0x01); putComL(0x80); } void putComL(unsigned char cmd) { RS=0; } Results: As per programmed application LCD is interface to the microcontroller AT89s51 with neat schematic . Hence LCD application is verified on embedded 8051 kit by displaying data. Department of Electronics & Communications Nishitha College of Engineering and Technology 7 */ */ */ */

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 2 KEYPAD INTERFACING


Aim: To interface the keypad with the 8051 SDK microcontroller Development Kit and observe the encryption and decryption of data. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply 4 x 4 Keypad
A membrane keypad is provided as an add on module, the 4 Rows and 4 Column signal lines are connected to Port1. The keypad can be unplugged from the board if required. Schematic

Schematic of 4x4 Keypad:

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/**********************************************************************/ /*KEYPAD INTERFACING*/ /**********************************************************************/ ;KEYCOUNT EQU 20H.0 ORG 00H MOV P2, #0FFH MOV R1, #40H MOV R2, #0 MOV TMOD, #20H MOV SCON, #50H MOV TH1, #0FDH MOV DPTR, #CMD CONTINUE: CLR A MOVC A, @A+DPTR JZ STARTPROCESS ACALL COMMAND INC DPTR SJMP CONTINUE STARTPROCESS: MOV DPTR, #STARTMSG ACALL DISPLAY MOV A, #0C0H ACALL COMMAND MOV DPTR, #STARTMSG1 ACALL DISPLAY ACALL DELAY ACALL DELAY MOV A, #01H ACALL COMMAND MOV DPTR, #MSG ACALL DISPLAY MOV A, #0C0H ACALL COMMAND MOV DPTR, #MSG1 ACALL DISPLAY ACALL DELAY ACALL DELAY ACALL DELAY MOV A, #01H Department of Electronics & Communications Nishitha College of Engineering and Technology 9 ;P2 AS COLUMNS

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

ACALL COMMAND KEYPAD: MOV P0, #0 ;P1 AS ROWS MOV A, P2 ANL A, #00000111B; CHECKING FOR ALL KEYS OFF CONDITION CJNE A, #00000111B, KEYPAD BACK1: MOV A, P2 ANL A, #00000111B ;CHECK IF ANY KEY IS PRESSED DUE TO NOISE CJNE A, #00000111B, CHECK SJMP BACK1 CHECK: ACALL DELAY ;DELAY FOR 20MSEC MOV A, P2 ANL A, #00000111B CJNE A, #00000111B, START MOV P0, #11111110B ;GROUND ROW 0 MOV A, P2 ANL A, #00000111B CJNE A, #00000111B, ROW_0 MOV P0, #11111101B ;GROUND ROW 1 MOV A, P2 ANL A, #00000111B CJNE A, #00000111B, ROW_1 MOV P0, #11111011B ;GROUND ROW 2 MOV A, P2 ANL A, #00000111B CJNE A, #00000111B, ROW_2 MOV P0, #11110111B ;GROUND ROW 3 MOV A, P2 ANL A, #00000111B CJNE A, #00000111B, ROW_3 JMP BACK1 ROW_0: JMP XX ROW_1: MOV DPTR, #ROW0 MOV DPTR, #ROW1 10

SJMP BACK1 START:

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) JMP XX ROW_2: ROW_3: XX: RRC A JNC KEYFOUND INC DPTR JMP XX KEYFOUND: ; JMP LOOP CNTINUE: ACALL DATA1 ;MOV @R1, A INC R1 INC R2 MOV R1,#40H MOV A, #0C0H MOV R0, #60H ;MOV B, #2 MOV A, #0C0H ACALL COMMAND MOV DPTR, #MSG2 ACALL DISPLAY ACALL DELAY ACALL DELAY ACALL DELAY MOV A, #01H ACALL COMMAND MOV DPTR, #MSG2A ACALL DISPLAY MOV A, #0C0H Department of Electronics & Communications Nishitha College of Engineering and Technology CLR A MOVC A, @A+DPTR MOV R1, #40H MOV @R1, A CJNE A, #23H, CNTINUE MOV DPTR, #ROW2 JMP XX MOV DPTR, #ROW3 JMP XX

Roll No.: 11M31D5704

JMP KEYPAD LOOP: ACALL COMMAND

11

M.Tech.Sem II (ES Lab) ACALL COMMAND LOOP1: MOV A, @R1 ADD A, #3H SUBB A, #9H MOV @R0, A ACALL TRANSMIT ACALL DATA1 ACALL DELAY ACALL DELAY INC R1 INC R0 DJNZ R2, LOOP1 JMP $ ;STARTPROCESS2

Roll No.: 11M31D5704

/*STARTPROCESS2: ACALL COMMAND ACALL DELAY ACALL DELAY ACALL DELAY JMP STARTPROCESS */ ;STARTPROCESS1: TRANSMIT:

MOV A, #01H

JMP STARTPROCESS SETB TR1 MOV SBUF, A JNB TI, $ CLR TI

RET RECEPTION: RET DISPLAY: JZ XXX ACALL DATA1 INC DPTR SJMP DISPLAY XXX: RET Department of Electronics & Communications Nishitha College of Engineering and Technology 12 CLR A MOVC A, @A+DPTR JNB RI, $ CLR RI MOV A, SBUF

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

COMMAND:

MOV P1, A CLR P3.5 CLR P3.6 SETB P3.7 CLR P3.7

ACALL DELAY RET DATA1: MOV P1, A SETB P3.5 CLR P3.6 SETB P3.7 CLR P3.7 RET DELAY: HERE: RET CMD: ROW0: ROW1: ROW2: ROW3: STARTMSG: STARTMSG1: STARTMSG2: MSG: MSG1: STARTMSG3: STARTMSG4: STARTMSG5: MSG2: END DB 38H, 0EH, 01H, 06H, 80H, 0 DB '1','2','3' DB '4','5','6' DB '7','8','9' DB '*','0','#' DB "------------------------------------------------------------------", 0 DB " ENCRYPTION ", 0 DB "------------------------------------------------------------------", 0 DB "ENCRYPTED DATA: DB "ENTER THE PASSWORD: ", 0 ", 0 MOV R6, #255 MOV R7, #255 DJNZ R7, $ DJNZ R6, HERE

ACALL DELAY

DB "------------------------------------------------------------------", 0 DB " DECRYPTION ", 0 DB "------------------------------------------------------------------", 0 DB " DECRYPTED DATA: , 0

Department of Electronics & Communications Nishitha College of Engineering and Technology

13

M.Tech.Sem II (ES Lab) Output of the Program:

Roll No.: 11M31D5704

Results: As per programmed application Data Encryption and Decryption, is that which is possible with the help of microcontroller AT89s51, LCD with keypad, and PC. Hence application is verified LCD on embedded 8051 kit as well as in PC Communication terminal window.

Department of Electronics & Communications Nishitha College of Engineering and Technology

14

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 3 SQUARE WAVE DEVICE DRIVER DEVELOPMENT


Aim:
Linux is a clone of UNIX.AS in all version of UNIX, hardware devices are

presented to normal programs as special files. Therefore, devices implement file semantics within ihe kernel.Because of this, it is worth taking a short look at how files in general are treated in linux before attempting to understand how device drivers are written.

Files
The generic filesystems header file, <linux/fs.h>, defines several structures for accessing files, super_block holds basic information about each filesystemm,and super_oeratoins is a structure of pointers to fuynctions which are associated with a filesystems superblock.Through that structure are reached inode_operations, the last defing functions that can be used to access files.In normal filesystems, there is one set of file operations on device special files.insted, those devices define their own file operations and register their own file_operations structure with the VFS.

The VFS
The VFS is the common abbreviaton for the Virtual Filesystem Switch. Generic filesystem operations are handled by generic filesystem code, and only when filesystem dependent or device dependent operations need to be done is the code for that specific filesystem or device actually called. The function needed is looked up in the proper instance of one of the *_operations structures and called.The VFS code is kept in the fs/ subdirectory of the Linux kernel source, and the code to the individual filesystems is kept in subdirectories of the fs/ subdirectory.

Operations
What I mean by operations may not be very clear at this point.An operation is something that needs to be done as a result of a system call or buffer cache activity, or beacauseof hardware irregularities.Nearly all operatons are caused directly or indirectly Department of Electronics & Communications Nishitha College of Engineering and Technology 15

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

by system calls, and so you can think of the VFS as code that translates raw system calls into filesystem operations.

Special files and file systems


All versions of Unix have device special files. The concept has even been picked up by such primitive operating systems as Microsofts DOS. However, the VFS is so flexible that not only can special files be created, special filesstems can to.Linux has a filesystems called the proc filesystem ,or profs, which is essentially a special filesystems. The file in this filesystem are not stored on disk;they are instead generated on-the fly from kernel data structures.they are,you could say,virtual devices designed to report on the state of the kernel. /************************************************************/ /* PROJECT NAME: SQUARE WAVE */ /************************************************************/ #include<stdlib.h> #include<stdio.h> #include<time.h> #include<sched.h> #include<sys/io.h> #define PORT 0x378 #define NSEC_PER_SEC 1000000000 /*using clock_nanosleep of librt*/ extern int clock_nanosleep(clockid_t_clock_id, int_flags,_const struct timespec*_req,struct timespec*_rem); static inline void tsnorm(stuct timespec *ts) { While(ts->tv_nsec>= NSEC_PER_SEC) { ts->tv_nsec -=NSEC_PER_SEC; ts->tv_sec++; } } /*increment counter and write to parallelport*/ void out() Static unsigned char state=0; Outb(state++,PORT); } int main(int argc,char** argv) { struct timespec t; struct sched_param param; /*defaultinterval=50000ns=50us;*cycle duration=100us*/ Department of Electronics & Communications Nishitha College of Engineering and Technology 16

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

int interval=50000; /*set permissions of parallelport*/ ioperm(PORT,1,1); if(argc>=2&&atoi(argv[1])>0) { printf(using realtime,priority:%d/n,atoi(argv[1])); param.sched_priority =atoi(arvg[1]); /*enable realtime fifo scheduling*/ if(sched_setscheduler(0.SCED_FIFO,&param)==-1) { perror(sched_setscheduler failed); exit(-1); } } if(argc>=3) interval=atoi(avgv[2]); clock_gettime(0,&t); t.tv_sec++; while(1) { clock_nanosleep(0,TIMER_ABSTIME,&t,NULL); out(); t.tv_nsec+=interval; tsnorm(&t); } Return 0; } Compilation: gcc_0 square square_wave.c-lrt-wall Execution: ./square ./square 8 20000 Explanation: By default, this driver program will generate the square wave on the system parallel port with a timer interval of 50us.At this point of time, the default system resources are used. If we give the priority less than the default, this driver uses more no. of system resources so all other processes get less priority. The parallel pin color notations and respective PRI of generating square wave are, 2nd pin - Blue 3rd pin - White 4th pin - Yellow 5th pin - Orange (2*x) (4*x) (8*x) (16*x) 17

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) 6th pin Black 10th pin Navy Blue 25th pin Green(GND) (32*x) (64*x)

Roll No.: 11M31D5704

RESULT
Hence the Square wave Device Driver, the default system resources are used. If we give the priority less than the default, this driver uses more no. of system resources so all other processes get less priority.

Department of Electronics & Communications Nishitha College of Engineering and Technology

18

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 4 LED INTERFACE TO 8051


Aim: To read inputs from switches and produce different dancing patterns for LEDs by interfacing the 8051-SDK kit with the computer. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply Theory: It is a semiconductor diode having radiative recombination. It requires a definite amount of energy to generate an electron-hole pair. The same energy is released when an electron recombines with a hole. This released energy may result in the emission of photon. Here the amount of energy released when the electro reverts from the conduction band to the valence band appears in the form of radiation. Alternatively the released energy may result in a series of photons causing lattice vibration. Finally the released energy may be transferred to another electron. The recombination radiation may lie in the infra-red and visible light spectrum. In forward biased region the radiation is peaked around the band gap energy and the phenomenon is called injection luminescence. In a junction biased in the avalanche break down region, there results a spectrum of photons carrying much higher energies. Almost White light then gets emitted from micro-plasma breakdown region in silicon junction. Diodes having radiative recombination are termed as Light Emitting Diodes, abbreviated as LEDs.

Department of Electronics & Communications Nishitha College of Engineering and Technology

19

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

In gallium arsenide diode, recombination is predominantly a radiation recombination and the probability of this radiative recombination far exceeds that in either germanium or silicon. Hence GaAs LED has much higher efficiency in terms of photons emitted per carrier. The internal efficiency of GaAs LED may be very close to 100% but because of high index of refraction, only a small fraction of the internal radiation can usually come out of the device surface. In spite of this low efficiency of actually radiated light, these LEDs are efficiently used as light emitters in visual display units and in optically coupled circuits. The efficiency of light generation increases with the increase of injected current and with decrease in temperature. The light so generated is concentrated near the junction since most of the charge carriers are obtained within one diffusion length of the diode junction. The following are the merits of LEDs over conventional incandescent and other types of lamps 1. Low working voltages and currents 2. Less power consumption 3. Very fast action 4. Emission of monochromatic light 5. Small size and weight 6. No effect of mechanical vibrations 7. Extremely long life Typical LED uses a forward voltage of about 2V and current of 5 to 10mA.GaAs LED produces infra-red light while red, green and orange lights are produced by gallium arsenide phosphide (GaAs) and gallium phosphide(Gap) . Eight leds connected on Port-2 through a latch. All the 8-leds are active high.

Department of Electronics & Communications Nishitha College of Engineering and Technology

20

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Schematic of LEDS:

Diagram: LEDS

Eight-Tactile (SPST) switches


Eight Tactile switches are connected to port-1. Each key generates an interrupt on INT0 (port 3.2) with the help of diodes.

Schematic of Eight Tactile (SPST) switches:

Department of Electronics & Communications Nishitha College of Engineering and Technology

21

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704


Diagram: Eight Tactile (SPST) switches

/**********************************************************************/ /*PROGRAM TO READ INPUTS FROM SWITCHES AND PRODUCE DIFFERENT DANCING PATTERNS ON LEDS*/ /**********************************************************************/ SW1 EQU P0.0 SW2 EQU P0.1 SW3 EQU P0.2 SW4 EQU P0.3 SW5 EQU P0.4 SW6 EQU P0.5 SW7 EQU P0.6 SW8 EQU P0.7 ORG 00H

/*START OF THE PROGRAM*/

/*INITIALIZATION OF PORTS*/ MOV P0,#0FFH MOV P1,#0FFH MOV P2,#0FFH MOV P3,#0FFH CHECK: BACK:

MOV P1, #0FFH MOV P2, #0FFH MOV P3, #0FFH JNB SW1, PTRN1 /*CHECKING OF

EIGHT SWITCHES CONTINUOUSLY*/ JNB SW2, PTRN2 JNB SW3, PTRN3 JNB SW4, PTRN4 JNB SW5, PTRN5A JNB SW6, PTRN6A JNB SW7, PTRN7A JNB SW8, PTRN8A JMP BACK /*STAY IN THE LOOP UNTIL ANY ONE OF THE SWITCHES*/ /* IS PRESSED*/ PTRN1: JNB SW1, PTRN1 PTRN_1: MOV P1, #11111111B MOV P2, #11111111B MOV P3, #11111111B Department of Electronics & Communications Nishitha College of Engineering and Technology /*PATTERN 1*/

22

M.Tech.Sem II (ES Lab) ACALL DELAY1 MOV P1, #0 MOV P2, #0 MOV P3, #0 ACALL DELAY1 JMP PTRN_1 PTRN5A: PTRN6A:

Roll No.: 11M31D5704

/*SWITCH IS PRESSED TO COME OUT OF THIS LOOP*/ JMP PTRN5 JMP PTRN6

/*PATTERN 2*/ PTRN2: JNB SW2, PTRN2 PTRN_2: MOV P1, #11110000B ACALL DELAY1 MOV P1, #00001111B ACALL DELAY1 MOV P2, #11110000B ACALL DELAY1 MOV P2, #00001111B ACALL DELAY1 MOV P3, #11110000B ACALL DELAY1 MOV P3, #00001111B ACALL DELAY1 JMP PTRN_2 PTRN8A: JMP PTRN8 /*PATTERN 3*/ PTRN3: JNB SW3, PTRN3 PTRN_3: MOV P1, #10101010B ACALL DELAY1 MOV P1, #01010101B ACALL DELAY1 MOV P2, #10101010B ACALL DELAY1 MOV P2, #01010101B ACALL DELAY1 MOV P3, #10101010B ACALL DELAY1 MOV P3, #01010101B ACALL DELAY1 JMP PTRN_3 PTRN7A: JMP PTRN7 /*PATTERN 4*/ PTRN4: PTRN_4: JNB SW4, PTRN4 ACALL DELAY1 23

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) MOV P1, #11001100B MOV P2, #11001100B MOV P3, #11001100B ACALL DELAY1 MOV P1, #00110011B MOV P2, #00110011B MOV P3, #00110011B ACALL DELAY1 /* MOV P3, #11011101B MOV P2, #10101010B ACALL DELAY1 /* MOV P1, A PTRN_4: MOV P2, A ACALL DELAY1 RL A MOV P3, A ACALL DELAY1 RR A

Roll No.: 11M31D5704

/* MOV P2, #11111100B ACALL DELAY1 MOV P3, #00111111B ACALL DELAY1 /* MOV P1, #00110011B ACALL DELAY1 MOV P2, #00110011B ACALL DELAY1 MOV P3, #00110011B ACALL DELAY1 */ JMP PTRN_4 PTRN5: /*PATTERN 5*/ PTRN_5: ACALL DELAY1 MOV P2, #0FFH ACALL DELAY1 MOV P3, #0FFH ACALL DELAY1 MOV P1, #7EH ACALL DELAY1 MOV P2, #7EH JNB SW5, PTRN5 MOV P1, #0FFH

Department of Electronics & Communications Nishitha College of Engineering and Technology

24

M.Tech.Sem II (ES Lab) ACALL DELAY1 MOV P3, #7EH ACALL DELAY1 MOV P1, #3CH ACALL DELAY1 MOV P2, #3CH ACALL DELAY1 MOV P3, #3CH ACALL DELAY1 MOV P1, #18H ACALL DELAY1 MOV P2, #18H ACALL DELAY1 MOV P3, #18H ACALL DELAY1 MOV P1, #0H ACALL DELAY1 MOV P2, #0H ACALL DELAY1 MOV P3, #0H ACALL DELAY1 MOV P1, #0H ACALL DELAY1 MOV P2, #0H ACALL DELAY1 MOV P3, #0H ACALL DELAY1 MOV P1, #18H ACALL DELAY1 MOV P2, #18H ACALL DELAY1 MOV P3, #18H ACALL DELAY1 MOV P1, #3CH ACALL DELAY1 MOV P2, #3CH ACALL DELAY1 MOV P3, #3CH ACALL DELAY1 MOV P1, #7EH ACALL DELAY1 MOV P2, #7EH ACALL DELAY1 MOV P3, #7EH ACALL DELAY1 MOV P1, #0FFH Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

25

M.Tech.Sem II (ES Lab) ACALL DELAY1 MOV P2, #0FFH ACALL DELAY1 MOV P3, #0FFH ACALL DELAY1 JMP PTRN_5 PTRN6: JNB SW6, PTRN6 MOV A, #10000000B MOV P1, A ACALL DELAY1 ;MOV P2, A ;ACALL DELAY1 ;MOV P3, A ;ACALL DELAY1 PTRN_6: MOV P1, A ACALL DELAY1 RR A MOV P2, A ACALL DELAY1 RR A MOV P3, A ACALL DELAY1 RR A

Roll No.: 11M31D5704

/*PATTERN 6*/

JMP PTRN_6 PTRN7: JNB SW7, PTRN7 MOV A, #11111110B MOV P1, A ACALL DELAY1 MOV P2, A ACALL DELAY1 MOV P3, A ACALL DELAY1 PTRN_7: RL A MOV P1, A ACALL DELAY1 RR A MOV P2, A ACALL DELAY1 RL A MOV P3, A ACALL DELAY1 JMP PTRN_7

/*PATTERN 7*/

Department of Electronics & Communications Nishitha College of Engineering and Technology

26

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

PTRN8: PTRN_8: ACALL DELAY1 MOV P1, #0FFH ACALL DELAY1 MOV P2, #0H ACALL DELAY1 MOV P2, #0FFH ACALL DELAY1 MOV P3, #0H ACALL DELAY1 MOV P3, #0FFH ACALL DELAY1 JMP PTRN_8 PTRN8A: DELAY1: HERE: BACKK:

JNB SW8, PTRN8 MOV P1, #0H

/*PATTERN 8*/

JMP PTRN8 MOV R6, #150 MOV R7, #150 JNB SW1, PTRN_1A /*DELAY TO VISUALISE THE PATTERNS*/ /*MORE CLEARLY*/ /*CHECKING OF EIGHT SWITCHES

CONTINUOUSLY*/ JNB SW2, PTRN_2A JNB SW3, PTRN_3A JNB SW4, PTRN_4A JNB SW5, PTRN_5A JNB SW6, PTRN_6A JNB SW7, PTRN_7A JNB SW8, PTRN8_A DJNZ R7, BACKK DJNZ R6, HERE RET PTRN8_A: JMP PTRN8 PTRN_7A: JMP PTRN7 PTRN_6A: JMP PTRN6 PTRN_5A: JMP PTRN5 PTRN_4A: JMP PTRN4 PTRN_3A: JMP PTRN3 PTRN_2A: JMP PTRN2 PTRN_1A: JMP PTRN1 Department of Electronics & Communications Nishitha College of Engineering and Technology 27

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

END /*END OF THE PROGRAM*/ /**********************************************************************/ /* Program for 8 Switches and 8 LEDs using C Language*/ /**********************************************************************/ #include <REGX51.H> sfr LEDS = 0X80; void delay(int ); void PATTERN1(void); void PATTERN2(void); void PATTERN3(void); void PATTERN4(void); void PATTERN5(void); void PATTERN6(void); void PATTERN7(void); void PATTERN8(void); sbit sw1=P1^0; sbit sw2=P1^1; sbit sw3=P1^2; sbit sw4=P1^3; sbit sw5=P1^4; sbit sw6=P1^5; sbit sw7=P1^6; sbit sw8=P1^7; void main() { P1=0xff; P2=0xff; while(1) { //while(P1==0xff); if(sw1==0) PATTERN1(); if(sw2==0) PATTERN2(); if(sw3==0) PATTERN3(); if(sw4==0) PATTERN4(); if(sw5==0) PATTERN5(); if(sw6==0) Department of Electronics & Communications Nishitha College of Engineering and Technology 28

M.Tech.Sem II (ES Lab) PATTERN6(); if(sw7==0) PATTERN7(); if(sw8==0) PATTERN8(); } } void PATTERN1(void) { while(sw1==0); { LEDS=0; delay(100); LEDS=0XFF; delay(100); } while(sw1==0); } void PATTERN2(void) { while(sw2==0); while(sw2==1) { LEDS=0X55; delay(100); LEDS=0XAA; delay(100); } while(sw2==0); } void PATTERN3(void) { while(sw3==0); while(sw3==1) { LEDS=0XF0; delay(100); LEDS=0X0F; delay(100); Department of Electronics & Communications Nishitha College of Engineering and Technology /*debounce check */

Roll No.: 11M31D5704

/*debounce check */

while(sw1==1) /* repeat this loop till switch1 is pressed */

/*debounce check */

29

M.Tech.Sem II (ES Lab) } while(sw3==0); } void PATTERN4(void) { while(4==0); while(sw4==1) { LEDS=0X33; delay(100); LEDS=0XCC; delay(100); } while(sw4==0); } void PATTERN5(void) { while(sw5==0); while(sw5==1) { LEDS=0X81; delay(100); LEDS=0X42; delay(100); LEDS=0X24; delay(100); LEDS=0X18; delay(100); LEDS=0X24; delay(100); LEDS=0X42; delay(100); LEDS=0X81; delay(100); } while(sw5==0); } void PATTERN6(void) { while(sw6==0); LEDS=0XFC; while(sw6==1) Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

30

M.Tech.Sem II (ES Lab) { delay(100); LEDS=LEDS>>2; if(LEDS==0) LEDS=0XC0; delay(100); } while(sw6==0); } void PATTERN7(void) { while(sw7==0); LEDS=0XFE; while(sw7==1) { delay(100); LEDS=LEDS<<1; LEDS+=1; //delay(100); } while(sw7==0); } void PATTERN8(void) { while(sw8==0); LEDS=0xFC; while(sw8==1) { delay(500); LEDS=LEDS<<2; delay(10); LEDS=LEDS+3; if(LEDS==0XFF) LEDS=0XFC; } while(sw8==0); } void delay(int n) { int x,y; for(x=0;x<n;x++) for(y=0;y<1000;y++); }

Roll No.: 11M31D5704

Department of Electronics & Communications Nishitha College of Engineering and Technology

31

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Results: As per programmed application leds and switches are interface to the microcontroller AT89s51 with neat schematic . Hence dancing of LEDs application is verified on embedded 8051 kit.

Department of Electronics & Communications Nishitha College of Engineering and Technology

32

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 5 SERIAL DATA TRANSMISSION USING 8051


Aim: To perform an experiment demonstrating the serial communication i.e., transmission and reception, between the PC and the 8051 SDK microcontroller development kit. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply General Description:
There are 2 ways to transfer the data in computer one is serial and another is the parallel communication. In parallel communication data is transferred using 2 or more lines. Parallel communication is used to transfer data to the devices which are a few feet away and it is fast. In Serial communication the data is sent one bit at a time. Data can be transferred to longer distances with minimum number of wires efficiently. It is economical and error free compared to parallel communication. To establish serial communication we need a RS232 Serial connector, MAX232 (to match voltage levels of 2 devices) and a serial cable.

Schematic of Serial Communication:

Department of Electronics & Communications Nishitha College of Engineering and Technology

33

M.Tech.Sem II (ES Lab) Hardware Explanation: RS-232C:

Roll No.: 11M31D5704

RS-232 (ANSI/EIA-232 Standard) is a standard serial protocol used to establish a communication between the two same or different processors. RS232 is developed to support different voltage levels of devices in the range of +3V to +25V for logic 0 -3V to -25V for logic 1 RS-232 hardware can be used for serial communication up to a distance of 12 feet. RS232 or DB-9 pin connector:

Pin Functions:
Data: TX on pin 3, RX on pin 2 Handshake: RTS on pin 7, CTS on pin 8, DSR on pin 6, CD on pin 1, DTR on pin 4 Common: Common pin 5(ground) Other: RI on pin 9 The method used by RS-232 for communication allows for a simple connection of three lines: TX, RX, and Ground. The three essential signals for 2 way RS-232 communication are: TXD: carries data from DTE to the DCE. RXD: carries data from DCE to the DTE MAX232: Max232 is used to convert the TTL voltage levels of microcontroller (logic0-0v, logic15v) into voltage levels of RS232 standards (logic0 - +3 to +25v, logic1 - 3 to 25v). MAX232 is a DC-to-DC converter, which takes TTL levels as input and produces RS232 levels, which are required for DTE to DTE communication. RS-232 communication is Department of Electronics & Communications Nishitha College of Engineering and Technology 34

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

asynchronous. That is a clock signal is not sent with the data. Each word is synchronized using its start bit, and an internal clock on each side, keeps tabs on the timing.

The RS232 levels are generated internally using switching latches and capacitors of 10uf each. Max232 pin configuration:

Serial Communication Block Diagram: Serial communication can be established with the help of 2 wires one for transmit and the one for receiving data.

Department of Electronics & Communications Nishitha College of Engineering and Technology

35

M.Tech.Sem II (ES Lab) Hardware connections:

Roll No.: 11M31D5704

SFRs Used for Serial Communication: The special function registers used in serial communication are, (i) SCON (ii) TMOD Register (iii) TCON Register (iv) Timer(T1) Registers SCON (Serial Control register): Load the SCON register with 0x50 to select serial communication in mode 1 (8 bit with variable baud rate).

Serial Mode Selection:

SM2: This bit is used in multi processor communication, hence place SM2=0 REN: (Receive Enable): To receive data in serial communication REN bit must be 1 Department of Electronics & Communications Nishitha College of Engineering and Technology 36

M.Tech.Sem II (ES Lab) Enabling of this bit will not effect the transmission

Roll No.: 11M31D5704

TB8 & RB8: Transmit 8th bit and receive 8th bit these are used for 9 bit data transfer Mode when 8-bit data is transmitting place TB8 =0 & RB8 = 0 TI & RI: The Transmit and Receive Interrupt bits these are system control bits activate When a byte of data is transmitted or received. TI =0 & RI =0 TMOD (Timer Mode Register): Load the TMOD register with 0x20 to select the TIMER1 in auto reload mode which generates corresponding Baud Rate. of each of the two TMOD is dedicated solely to the two timers (T0 & T1). The timer mode SFR is used to configure the mode of operation

timers. Using this SFR your program may configure each timer to be a 16-bit timer, or 13 bit timer, 8-bit auto reload timer, or two separate timers. Additionally you may configure the timers to only count when an external pin is activated or to count events that are indicated on an external pin. It can consider as two duplicate 4-bit registers, each of which controls the action of one of the timers.

To set TMOD for serial communication the bits in this register are Gate bit =0 TIMER Mode operation:

TCON (Timer Control Register): Department of Electronics & Communications Nishitha College of Engineering and Technology 37

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

The timer control SFR is used to configure and modify the way in which the 8051s two timers operate. This SFR controls whether each of the two timers is running or stopped and contains a flag to indicate that each timer has overflowed. Additionally, some non-timer related bits are located in TCON SFR. These bits are used to configure the way in which the external interrupt flags are activated, which are set when an external interrupt occurs.
D7 D6 D5 D4 D3 D2 D1 D0

TF1: Timer 1 over flow flag. Set by hardware when Timer/Counter 1 overflows TR1: Timer 1 run control bit. Set (or) clear by software to turn Timer/Counter 1 ON and OFF. TF0: Timer 0 over flow flag. Set by hardware when Timer/Counter 0 overflows TR0: Timer 0 run control bit. Set (or) clear by software to turn Timer/Counter 0 ON and OFF. IE1: External interrupt 1. Edge flag set by hardware IT1: Interrupt 1 type control bit. Set (or) cleared by software IE0: External interrupt 0. Edge flag set by hardware. IT0: Interrupt 0 type control bit. Set (or) cleared by software Set the TR1 bit in TCON register to run the TIMER 1 which helps in generation of Baud rate for serial communication i.e. TR1=1. BAUD RATE CALCULATION Internal timer stages are as follows

Department of Electronics & Communications Nishitha College of Engineering and Technology

38

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Divided by X block can be replaced with T1 timer so that by changing the value of timer we can obtain the required baud rate. Let XClk Baud Rate = = 11.0592 MHz (XClk / 12 / 16 / 2 / X)

For attaining 9600 baud Rate X can be calculated as = = (11.0592 x 106 ) / (12 * 16 * 2 * 9600) 3

So set the 2s Complement of 3 (FD) in Timer 1 so that we can achieve 9600 baud rates. Note: Assuming 8-bit Auto reload mode and 8-bit variable baud rate modes. Algorithm: Serial Transmition Algorithm: STEP1: Set the baud rate using SCON and TMOD. STEP2: Copy the data into SBUF. STEP3: Checks if TI is HIGH, if no go to 3. STEP4: Clear TI. STEP5: End. Serial Reception Algorithm: STEP1: Set the baud rate using SCON and TMOD. STEP2: Checks if RI is HIGH, if no go to 2. STEP3: Clear RI. STEP4: Copy SBUF into Memory STEP5: En

Department of Electronics & Communications Nishitha College of Engineering and Technology

39

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Flow Chart:

Department of Electronics & Communications Nishitha College of Engineering and Technology

40

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/**********************************************************************/ /*SERIAL RECEPTION*/ /*FROM PC TO MICROCONTROLLER*/ /**********************************************************************/ RS11 EQU P2.0 RW11 EQU P2.1 EN EQU P2.2 ORG 00H BACK1: ACALL COMMAND INC DPTR SJMP BACK1 NEXT: MOV TMOD, #20H ;TO SELECT TIMER 1 IN MODE 2 (AUTO RELOAD) MOV SCON, #50H ; TO SELECT SERIAL COMMUNICATION IN MODE 1 ; AND RECEIVER ENABLE MOV TH1, #0FDH BACK: SETB TR1 ; TO SET THE BAUD RATE TO 9600 ; TO START THE TIMER MOV DPTR, #CMD ;INITIALIZATION OF LCD COMMANDS CLR A MOVC A, @A+DPTR JZ NEXT ;ASSIGN NAMES TO THE PORT PINS AS MENTIONED

JNB RI,$ ;WAIT FOR THE CHARACTER TO BE LOADED INTO SBUF CLR RI ; CLEAR RI FOR THE NEXT RECEPTION MOV A, SBUF ACALL DATA1 JMP BACK COMMAND: MOV P1, A CLR RS11 41 ; TO MOVE THE RECEIVED CHARACTER INTO A ; CALL SUBROUTINE FOR DISPLAY ON THE LCD

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) CLR RW11 SETB EN ACALL DELAY CLR EN RET DATA1: MOV P1, A SETB RS11 CLR RW11 SETB EN CLR EN RET DELAY: HERE: DJNZ R6, HERE RET CMD: END DB 38H, 0EH, 01H, 06H, 80H, 0 MOV R6, #255 MOV R7, #255 DJNZ R7, $

Roll No.: 11M31D5704

ACALL DELAY

Department of Electronics & Communications Nishitha College of Engineering and Technology

42

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/**********************************************************************/ /*SERIAL TRANSMISSION*/ /*FROM MICROCONTROLLER TO PC*/ /**********************************************************************/ ORG 00H MOV TMOD, #20H ;TO SELECT TIMER 1 IN MODE 2 (AUTO RELOAD) MOV SCON, #50H ; TO SELECT SERIAL COMMUNICATION IN MODE 1 ; AND RECEIVER ENABLE MOV TH1, #0FDH ; TO SET THE BAUD RATE TO 9600 MOV DPTR, #MSG BACK: CLR A MOVC A,@A+DPTR ; TO LOAD THE MESSAGE INTO ; ACCUMULATOR CHARACTER BY CHARACTER JZ NEXT ACALL TRANSMIT ;CALL SUBROUTINE FOR TRANSMISSION INC DPTR SJMP BACK NEXT: SJMP NEXT ;TERMINATION OF THE PROGRAM /*TRANSFERRING DATA SERIALLY*/ TRANSMIT: SETB TR1 ; TO START THE TIMER MOV SBUF, A ; LOAD DATA INTO SBUF JNB TI, $ ;WAIT FOR THE TRANSMISSION TO BE ;COMPLETED CLR TI ;CLEAR TI FOR NEXT TRANSMISSION RET MSG: DB 13, 10,"Welcome to Nishitha College Of Engineering & Technology, Hyderabad, 13,10, 13,10, "You are working with WINKIT Brand World Class, State-of-Art Embedded Systems Lab Trainer Board", 13, 10, 0 END

Results: As per programmed application serial communication, LCD and PC interface to the microcontroller AT89s51 with the help of RS-232 cable. Hence Serial

Department of Electronics & Communications Nishitha College of Engineering and Technology

43

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

communication application is verified on embedded 8051 kit and PC with the help of keil uvision and Encript, with communication terminal in the PC.

Output of the Program:


Screen 1 : Transmitter Window

Screen 2 : Receiver Window

Department of Electronics & Communications Nishitha College of Engineering and Technology

44

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

RESULTS: Demonstrating the serial communication i.e., transmission and reception, between the PC and the 8051 SDK microcontroller has been verified.

Experiment No: 6 ANALOG-TO-DIGITAL CONVERTER


Aim: To interface an analog-to-digital converter (ADC) with the 8051-SDK Microcontroller Development Kit and send the analog data such as the temperature to the PC. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply ANALOG TO DIGITAL CONVERTER, DIGITAL TO ANALOG CONVERTER (PCF8591P) It is an I2C device which consists of 4 Channel 8-bit Analog to Digital Converter and 1 channel 8 bit Digital to Analog Converter, which is connected to I2C bus. General Description:
The PCF8591 is a single-chip, single-supply low power 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus. The functions of the device include analog input multiplexing, on-chip track and hold function, 8-bit analog-to-digital conversion and an 8-bit digital-to-analog conversion. The maximum conversion rate is given by the maximum speed of the I2C.

Department of Electronics & Communications Nishitha College of Engineering and Technology

45

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Schematic of ADC/DAC:

Department of Electronics & Communications Nishitha College of Engineering and Technology

46

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Features:

Single power supply Operating supply voltage 2.5 V to 6 V Low standby current Serial input/output via I2C-bus Address by 3 hardware address pins Sampling rate given by I2C-bus speed 4 analog inputs programmable as single-ended or differential inputs Auto-incremented channel selection Analog voltage range from VSS to VDD On-chip track and hold circuit 8-bit successive approximation A/D conversion Multiplying DAC with one analog output. Applications: Closed loop control systems Low power converter for remote data acquisition Battery operated equipment Acquisition of analog values in automotive, audio and TV applications.

Department of Electronics & Communications Nishitha College of Engineering and Technology

47

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/*********************************************************************/ /* program to send the adc data(temperature) to pc */ **********************************************************************/ #include <REGX51.H> #include<stdio.h> sbit RD1 = P2^5; sbit WR1 = P2^6; sbit INTR1 =P2^7; sfr adcdata = 0x90; //void display(unsigned char ); void delay(int ); void main() { unsigned char value; SCON=0X50; TMOD=0X20; TH1=0XFD; TR1=1; printf("temeperature:"); adcdata=0xff; INTR1=1; RD1=1; while(1) { WR1=0; delay(5); WR1=1; while(INTR1==1); RD1=0; value=adcdata; printf("%d",value); //display(value); RD1=1; } } /*void display(unsigned char ) { unsigned char x,d1,d2,d3; x=value/10; d1=value%10; d2=x%10; d3=x/10; */ void delay(int n) { int x,y; for(x=0;x<n;x++) Department of Electronics & Communications Nishitha College of Engineering and Technology 48

M.Tech.Sem II (ES Lab) for(y=0;y<1000;y++); }

Roll No.: 11M31D5704

Results: As per programmed application ADC is interface to the microcontroller AT89s51 with neat schematic. Hence Temperature reading is possible with help of ADC and LM35 temperature sensor application is verified on embedded 8051 kit.

Department of Electronics & Communications Nishitha College of Engineering and Technology

49

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 7 REAL TIME CLOCK


Aim: To interface a real time clock (RTC) with the 8051-SDK Microcontroller Development Kit. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply : /*********************************************************************/ /* program for RTC */ **********************************************************************/ #include<8051.h> #include<string.h> #include<stdio.h> #include<serial_io.h> void send(unsigned char dat) // uart baud rate set to 9600 { TMOD = 0X20; TH1 TR1 = 0XFD; = 1; SCON = 0X50; SBUF = dat; while(TI == 0){} TI TR1 = 0; = 0; 50

Department of Electronics & Communications Nishitha College of Engineering and Technology

M.Tech.Sem II (ES Lab) } void print(char *str) { while(*str != '\0') { send(*str); str++; } } void ToASCII(unsigned int t) { if(t<=9) { t=t+0x30; send(t); } else { t=t+0x37; send(t); } } void I2C_WRITE(unsigned char); void Data_Write(unsigned char [],unsigned int); unsigned char I2C_READ(); void Delay_Time(); void busycheck(); void I2C_START(); void I2C_STOP(); unsigned int READ; __idata unsigned char dat; __idata unsigned char i; Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

51

M.Tech.Sem II (ES Lab) __bit __at(0x94) SCLK; __bit __at(0x95) SDA; bit ACK; #define HIGH 01; #define LOW 00; unsigned int read_time[0x03]; void set_rtc_time(); void get_rtc_time(); int get_hour(); int get_minute(); int get_second(); unsigned int hour,minute,second; void set_hour() { I2C_START(); I2C_WRITE(0XA0); I2C_WRITE(0x04); I2C_WRITE(0x02); I2C_STOP(); } void set_minute() (0x03) { I2C_START(); I2C_WRITE(0XA0); I2C_WRITE(0x03); I2C_WRITE(0x15); I2C_STOP(); } void set_second() { I2C_START(); I2C_WRITE(0XA0); Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

// load the RTC hour register (0x04)

// load the RTC minute register

// load the RTC second register (0x02)

52

M.Tech.Sem II (ES Lab) I2C_WRITE(0x02); I2C_WRITE(0x56); I2C_STOP(); } int get_second() { unsigned char s; I2C_START(); I2C_WRITE(0XA0); I2C_WRITE(0x02); I2C_STOP(); I2C_START(); I2C_WRITE(0XA1); s = I2C_READ(); I2C_STOP(); return(s); } int get_minute() { unsigned char m; I2C_START(); I2C_WRITE(0XA0); I2C_WRITE(0x03); I2C_STOP(); I2C_START(); I2C_WRITE(0XA1); m = I2C_READ(); I2C_STOP(); return(m); } int get_hour() { unsigned char h; Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

// read RTC second register (0x02)

// read the RTC minute register (0x03)

// read the RTC hour register (0x04)

53

M.Tech.Sem II (ES Lab) I2C_START(); I2C_WRITE(0XA0); I2C_WRITE(0x04); I2C_STOP(); I2C_START();

Roll No.: 11M31D5704

//RTC write command //RTC Control Register address

I2C_WRITE(0XA1); //RTC read command h = I2C_READ(); I2C_STOP(); return(h); } void main() { set_hour(); set_hour(); set_minute(); set_second(); while(1) { second = get_second(); minute = get_minute(); hour = get_hour(); minute = get_minute(); second = get_second(); hour = get_hour(); print("\n\r Real Time Clock = "); ToASCII(hour/16); ToASCII(hour%16); send(0x20); ToASCII(minute/16); ToASCII(minute%16); minute = get_minute(); minute = get_minute(); second = get_second(); Department of Electronics & Communications Nishitha College of Engineering and Technology 54

M.Tech.Sem II (ES Lab) send(0x20); ToASCII(second/16); ToASCII(second%16); } }

Roll No.: 11M31D5704

void I2C_START() and clearing the port pins { SCLK =LOW; SDA =LOW; Delay_Time(); Delay_Time(); SCLK=HIGH; Delay_Time(); Delay_Time(); SDA=HIGH; Delay_Time(); Delay_Time(); SDA=LOW; Delay_Time(); Delay_Time(); SCLK=LOW; } void I2C_WRITE(unsigned char j) { dat=j; for(i=0;i<8;i++) { SDA = dat & 0x80; dat=dat<<1; SCLK=HIGH;

//starting i2c by creating a clock pulse by setting

//write data/command to RTC registers

Department of Electronics & Communications Nishitha College of Engineering and Technology

55

M.Tech.Sem II (ES Lab) Delay_Time(); Delay_Time(); SCLK = LOW; } SDA=HIGH; Delay_Time(); Delay_Time(); SCLK = HIGH; Delay_Time(); Delay_Time(); ACK = SDA; Delay_Time(); Delay_Time(); SCLK=LOW; } void I2C_STOP() { SCLK=LOW; Delay_Time(); Delay_Time(); SDA=HIGH; } unsigned char I2C_READ() { unsigned char i,j; j=0; j=SDA; for(i=0;i<8;i++) { j<<=1; SCLK=HIGH; j|=SDA; Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

56

M.Tech.Sem II (ES Lab) Delay_Time(); SCLK=LOW; } Delay_Time(); Delay_Time(); SDA = LOW; Delay_Time(); Delay_Time(); SCLK = HIGH; Delay_Time(); Delay_Time(); SCLK = LOW; Delay_Time(); Delay_Time(); SDA = HIGH; return(j); } void Delay_Time() { unsigned long int i; for(i=0;i<100;i++); }

Roll No.: 11M31D5704

RESULT : AT89s51.

As per programmed application RTC is interface to the microcontroller

Department of Electronics & Communications Nishitha College of Engineering and Technology

57

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 8 DIGITAL-TO-ANALOG CONVERTER


Aim: To interface an digital-to-analog converter (DAC) with the 8051-SDK Microcontroller Development Kit using I2C protocol. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply /*********************************************************************/ /* program for DAC */ **********************************************************************/ #include<8051.h> #define HIGH 01; #define LOW 00; __xdata __at 0xffc5 unsigned char swt; __idata unsigned char dat; __idata unsigned char i; __bit __at(0x94) SCLK; __bit __at(0x95) SDA; bit ACK; void I2C_Start(void); void I2C_Stop(void); void I2C_Write(unsigned char j); Department of Electronics & Communications Nishitha College of Engineering and Technology 58

M.Tech.Sem II (ES Lab) unsigned char I2C_Read(void); void Delay_Time(); void I2C_Start(void) pins { SCLK=LOW; SDA=LOW; Delay_Time(); SCLK = HIGH; Delay_Time(); SDA=HIGH; Delay_Time(); SDA = LOW; Delay_Time(); SCLK = LOW; } void I2C_Stop(void) { SCLK = LOW; Delay_Time(); SDA = HIGH; } void I2C_Write(unsigned char j) { dat=j; for(i=0;i<8;i++) { SDA = dat & 0x80; dat=dat<<1; SCLK = HIGH; Delay_Time(); Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

//Start I2C by creating clock by setting and clearing the port

//Stop I2C operation by clearing the SCLK Pin

// shifting data bit by bit to MSB and moving through i2c

59

M.Tech.Sem II (ES Lab) SCLK = LOW; } SDA=HIGH; Delay_Time(); SCLK = HIGH; Delay_Time(); ACK = SDA; Delay_Time(); SCLK = LOW; } unsigned char I2C_Read(void) { unsigned char i,j; j = 0; i = SDA; for(i=0;i<8;i++) { j<<=1; SCLK = HIGH; j |= SDA; Delay_Time(); SCLK = LOW; } Delay_Time(); SDA = LOW; Delay_Time(); SCLK = HIGH; Delay_Time(); SCLK = LOW; Delay_Time(); SDA = HIGH; return j; } Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

//reading from i2c device bit by bit

60

M.Tech.Sem II (ES Lab) void main() { unsigned int i=0x00; I2C_Start(); I2C_Write(0x9E); I2C_Write(0x40); I2C_Start(); I2C_Write(0x9E); while(1) { if((swt & 0x1f)== 0x1E) { i=i+0x0a; if(i>0xff) i=0x00; I2C_Write(i); Delay_Time(); } } } void Delay_Time() { unsigned int i; for(i=0;i<=5000;i++); }

Roll No.: 11M31D5704

//send device address //send device's control register address // send command for read

RESULT : As per programmed application DAC is interface to the microcontroller AT89s51.

Department of Electronics & Communications Nishitha College of Engineering and Technology

61

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 9 STEPPER MOTOR


Aim: To interface a stepper motor with the 8051-SDK Microcontroller Development Kit. Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply : /*********************************************************************/ /* program for stepper motor */ **********************************************************************/ #include<reg52.h> void serial_intr (void) interrupt 4; void delay_ms(unsigned int j) { unsigned int k; while(j-->0) { for(k=0;k<500;k++); } } void main() { while(1) { P1 = 0x55; Department of Electronics & Communications Nishitha College of Engineering and Technology 62

M.Tech.Sem II (ES Lab) delay_ms(5); P1 = 0x66; delay_ms(5); P1 = 0xaa; delay_ms(5); P1 = 0x99; delay_ms(5); } }

Roll No.: 11M31D5704

RESULT : As per programmed application stepper motor is interface to the microcontroller AT89s51.

Department of Electronics & Communications Nishitha College of Engineering and Technology

63

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Experiment No: 10 SEVEN SEGMENT DISPLAY


Aim: To interface seven segment display with the 8051-SDK Microcontroller Development Kit Apparatus, Equipments and Tools: 1. 12V DC, 1.5A Adaptor 2. 8051 SDK Microcontroller Development Kit 3. RS 232 serial communication cable 4. Personal Computer 5. Keil Software 6. AC Power Supply Description: Each of the segments of the display is connected to a pin on the 8051 (the schematic shows how to do this). In order to light up a segment on the the pin must be set to 0V. To turn a segment off the corresponding pin must be set to 5V. This is simply done by setting the pins on the 8051 to '1' or '0'. LED displays are

Power-hungry (10ma per LED) Pin-hungry (8 pins per 7-seg display)

But they are cheaper than LCD display 7-SEG Display are available in two types -1. Common anode & 2. common cathode , but command anode display are most suitable for interfacing with 8051 since 8051 port pins can sink current better than sourcing it.

Department of Electronics & Communications Nishitha College of Engineering and Technology

64

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

CREATING DIGIT PATTERN For displaying Digit say 7 we need to light segments -a ,b, c. Since we are using Common anode display , to do so we have to to provide Logic -0 (0 v) at anode of these segments. so need to clear pins- P1.0 ,P1.1,P1.2.
Hex Code Digit 0 1 2 3 4 Seg. h 1 0 1 1 1 Seg. g 1 0 0 0 0 Seg. f 0 0 1 1 0 Seg. e 0 0 0 1 1 Seg. d 0 0 0 0 1 Seg. c 0 1 1 0 0 Seg. b 0 1 0 0 0 Seg. a 0 0 0 0 1 HEX C0 06 A4 B0 99

that is 1 1 1 1 1 0 0 0 -->F8h .

Department of Electronics & Communications Nishitha College of Engineering and Technology

65

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

Connection
Segment number a b c d e f g h(dp) 8051 pin number P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 p1.6 P1.7

You can also do this for some characters like A ,E .. but not for D or B because it will be same as that of 0 & 8 . So this is one of limitation of 7-seg display. Since we can Enable only one 7-seg display at a time ,we need to scan these display at fast rate .The scanning frequency should be high enough to be flicker-free. At least 30HZ .Therefore time one digit is ON is 1/30 seconds INTERFACING Note that I am using Common Anode display. so the common Anode pin is tied to 5v .The cathode pins are connected to port 1 through 330 Ohm resistance (current limiting).

Common Anode display

Department of Electronics & Communications Nishitha College of Engineering and Technology

66

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/*********************************************************************/ /* program for seven segment display*/ **********************************************************************/ #include<reg52.h> #define delay_us _nop_(); sbit WIRE=P3^7; #define dig1 0x01 #define dig2 0x03 #define dig3 0x02 #define dig4 0x04 #define dig5 0x05 #define dig6 0x06 #define dig7 0x07 #define dig8 0x08 sbit DI=P2^1; sbit LOAD=P2^0; sbit CLK=P2^2; void init(void); void write(unsigned char,unsigned char); void send_byte(unsigned char); void clr(void); void delay_micro(unsigned int); void delay_ms(unsigned int); unsigned char reset(); void write_bit0(); void write_bit1(); unsigned char read_bit(); void write_byte(unsigned char); unsigned char read_byte(); void write_ds1822(void); void read_ds1822(void); float measure_ds1822(void); Department of Electronics & Communications Nishitha College of Engineering and Technology 67 //include at89c51 microcontoller header file //generates 1 microsecond

M.Tech.Sem II (ES Lab) float measure_temp(void); unsigned char pulse,temp_id[9]; unsigned char a,b,c; bit t_flag=0; unsigned char cnt; int n; float m; void main (void) { unsigned char k,l,u,r,g,h,j; float f; init(); clr(); while(1) { j=0xf0; f=measure_temp(); k=f; l=k/10; u=k%10; j=j+u; m=f-k; m=m*100; r=m; g=r/10; h=r%10; write(1,15); write(2,15); write(3,15); write(4,15); write(5,l); write(6,j); Department of Electronics & Communications Nishitha College of Engineering and Technology

Roll No.: 11M31D5704

68

M.Tech.Sem II (ES Lab) write(7,g); write(8,h); delay_ms(1000); } } float measure_temp(void) { float f; pulse=reset(); if(pulse==0) write_byte(0xcc); //skip rom write_byte(0x44); //convert temp delay_ms(750); pulse=reset(); if(pulse==0) write_byte(0xcc); //skip rom read_ds1822(); f=measure_ds1822(); return f; } float measure_ds1822(void) {

Roll No.: 11M31D5704

unsigned char temp_msb,temp_lsb,temp,mask=0x01,decimal=0; int sign=1,cnt=16; float f=0.0; temp_lsb=temp_id[0]; temp_msb=temp_id[1]; if(temp_msb>=0x80) { sign=-1; temp_lsb=(~temp_lsb)+1; Department of Electronics & Communications Nishitha College of Engineering and Technology 69

M.Tech.Sem II (ES Lab) temp_msb=~temp_msb; } decimal=(temp_lsb<<4); decimal=decimal>>4; temp=temp_msb<<4|temp_lsb>>4; do { if(decimal&mask) f=f+((float)1/cnt); cnt=cnt/2; mask=mask*2; }while(mask<=0x08); f=(f+temp)*sign; return f; } void write_ds1822(void) { write_byte(0x4e); write_byte(0x25); write_byte(0x0f); write_byte(0x7f); } void read_ds1822(void) { unsigned char i; write_byte(0xbe); for(i=0;i<9;i++) temp_id[i]=read_byte(); }

Roll No.: 11M31D5704

Department of Electronics & Communications Nishitha College of Engineering and Technology

70

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

/*-------------------------------------------------------------------------------------------*/ unsigned char reset() { unsigned char presence; WIRE=0; delay_micro(29); WIRE=1; delay_micro(3); presence=WIRE; delay_micro(25); return presence; } void write_byte(unsigned char c) { int mask1; for(mask1=1;mask1<=128;mask1*=2) { if(c&mask1) write_bit1(); else write_bit0(); } } unsigned char read_byte() { int mask1; unsigned char c=0; for(mask1=1;mask1<=128;mask1*=2) { if(read_bit()) Department of Electronics & Communications Nishitha College of Engineering and Technology 71

M.Tech.Sem II (ES Lab) c=c|mask1; } return c; } void write_bit1() { WIRE=0; _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); //6us WIRE=1; delay_micro(3); } void write_bit0() { WIRE=0; delay_micro(3); //64us WIRE=1; _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); //10us } unsigned char read_bit() { unsigned char b; WIRE=0; _nop_();_nop_(); _nop_();_nop_(); Department of Electronics & Communications Nishitha College of Engineering and Technology //60us

Roll No.: 11M31D5704

72

M.Tech.Sem II (ES Lab) _nop_();_nop_(); //6us WIRE=1; _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_();_nop_(); //9us b=WIRE; delay_micro(2); //55us return b; }

Roll No.: 11M31D5704

/*-------------------------------------------------------------------------------------------*/ //generates delay in milli seconds void delay_ms(unsigned int i) { unsigned int j; while(i-->0) { for(j=0;j<500;j++) { ; } } } //generates delay in micro seconds void delay_micro(unsigned int i) { unsigned int s; for(s=0;s<i;s++) { ; } } Department of Electronics & Communications Nishitha College of Engineering and Technology 73

M.Tech.Sem II (ES Lab)

Roll No.: 11M31D5704

void init(void) { write(0x09,0xff);//decode mode reg set to nodecode operation write(0x0a,0x0f);//intensity reg set to maximum intensity write(0x0b,0x07);//scan limit reg set to 4 seven segments write(0x0c,0x01);//shutdown reg set tonormal mode write(0x0f,0x00);//display test reg is ste to 0 i.e. normal operation clr(); } void write(unsigned char address, unsigned char dataa) { unsigned char i,mask=0x80; LOAD=0; for(i=0;i<=7;i++) { CLK=0; if(address & mask) DI=1; else DI=0; CLK=1; mask=mask>>1; } mask=0x80; for(i=0;i<=7;i++) { Department of Electronics & Communications Nishitha College of Engineering and Technology 74

M.Tech.Sem II (ES Lab) CLK=0; if(dataa & mask) DI=1; else DI=0; CLK=1; mask=mask>>1; } LOAD=1; } void clr(void) { write(dig1,0x00); write(dig2,0x00); write(dig3,0x00); write(dig4,0x00); write(dig5,0x00); write(dig6,0x00); write(dig7,0x00); write(dig8,0x00); }

Roll No.: 11M31D5704

RESULTS: Interface seven segment display with the 8051-SDK Microcontroller Development Kit.

Department of Electronics & Communications Nishitha College of Engineering and Technology

75

Você também pode gostar