8051 Microcontroller
8051 Microcontroller
INDEX
1
EXPERIMENT:-1
Code:
start:
DEC P1 ; decrement port 1
JMP start ; and repeat
Outputs:
1.1 1.2
Result: -
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
2
EXPERIMENT:-2
OUTPUT:
2.1
Result: -
The given experiment is verified.
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
3
EXPERIMENT:-3
Output:
3.1
3.2
4
Result: -
The given experiment is verified.
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
5
EXPERIMENT:-4
Theory: This program sends the alphabets or number on the LCD module one by
one with the delay that can be change by the user according to the need of the user.
This program can be used to display different on the public places like railway station,
bus stand, malls etc.
Code:
Start:; put data in RAM
MOV 30H, #'A'
MOV 31H, #'B'
MOV 32H, #'C'
MOV 33H, #0 ; end of data marker
; initialise the display
; see instruction set for details
CLR P1.3 ; clear RS - indicates that instructions are being sent to the
module
; function set
CLR P1.7 ;|
CLR P1.6 ;|
SETB P1.5 ;|
CLR P1.4 ; | high nibble set
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
; same function set high nibble sent a second time
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
SETB P1.6 ;|
SETB P1.5 ; |low nibble set
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
SETB P1.7 ;|
SETB P1.6 ;|
SETB P1.5 ;|
SETB P1.4 ; | low nibble set
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
; send data
SETB P1.3 ; clear RS - indicates that data is being sent to module
MOV R1, #30H ; data to be sent to LCD is stored in 8051 RAM, starting at
location 30H
loop:
MOV A, @R1 ; move data pointed to by R1 to A
JZ finish ; if A is 0, then end of data has been reached - jump out
of loop
CALL sendCharacter ; send data in A to LCD module
7
INC R1 ; point to next piece of data
JMP loop ; repeat
finish:
JMP $
sendCharacter:
MOV C, ACC.7 ;|
MOV P1.7, C ;|
MOV C, ACC.6 ;|
MOV P1.6, C ;|
MOV C, ACC.5 ;|
MOV P1.5, C ;|
MOV C, ACC.4 ;|
MOV P1.4, C ; | high nibble set
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
MOV C, ACC.3 ;|
MOV P1.7, C ;|
MOV C, ACC.2 ;|
MOV P1.6, C ;|
MOV C, ACC.1 ;|
MOV P1.5, C ;|
MOV C, ACC.0 ;|
MOV P1.4, C ; | low nibble set
SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
delay:
MOV R0, #50
DJNZ R0, $
RET
OUTPUT:
4.1.
8
4.2
4.3
Result: -
The given experiment is verified.
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
9
EXPERIMENT:-5
Theory: This program generates a ramp on the DAC output. You can try adding
values other than 8 to the accumulator to see what this does to the ramp signal.
Code:
CLR P0.7 ; enable the DAC WR line
loop:
MOV P1, A ; move data in the accumulator to the ADC inputs (on
P1)
ADD A, #4 ; increase accumulator by 4
JMP loop ; jump back to loop
OUTPUT:
5.1
10
5.2
5.3
Result: -
The given experiment is verified.
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
11
EXPERIMENT:-6
OBJECTIVE: Taking Samples from the ADC and Displaying them on the Scope
via the DAC -
Material required: EdSim51 simulator, microcontroller, crystal oscillator of
desire frequency ,two(2)load capacitor, register 10kohm & 1 polar capacitor 10μF,
keil compiler software version-(4 or 5), microcontroller burner & their drivers.
Theory: This program reads the analogue input voltage on the ADC and displays it
on the scope via the DAC.A sample is taken from the ADC every 50 us This is
achieved by setting timer 0to interrupt the main program every 50 us.The timer 0 ISR
then initiates an ADC conversion.When the conversion is complete the ADC interrupt
line goes low. This line is interfaced with the 8051 external 0 interrupt line. The
external 0 ISR therefore takes the reading from the ADC on P2 and passes it to the
DAC on P1.Therefore, while the program is running,the scope voltage level should be
the same as the ADC input voltage. However, when a change is made to the ADC
input voltage it will take some time for the scope to update (ie; until the next timer 0
interrupt).Note: when running this program make sure the ADC is enabled (not the
comparator).
Code:
ORG 0 ; reset vector
JMP main ; jump to the main program
MOV TH0, #-50 ; | put -50 into timer 0 high-byte - this reload value,
12
; | with system clock of 12 MHz, will result in a timer 0
overflow every 50 us
MOV TL0, #-50 ; | put the same value in the low byte to ensure the timer starts
counting from
; | 236 (256 - 50) rather than 0
OUTPUT:
6.2
6.3
13
Result: -
The given experiment is verified.
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
14
EXPERIMENT:-7
The pressed key number will be stored in R0. Therefore, R0 is initially cleared.Each key is
scanned, and if it is not pressed R0 is incremented. In that way,when the pressed key is
found, R0 will contain the key's number.The general purpose flag, F0, is used by the column-
scan subroutine to indicate whether or not a pressed key was found in that column. If, after
returning from colScan, F0 is\ set, this means the key was found.
Code:
start:
; scan row0
SETB P0.3 ; set row3
CLR P0.0 ; clear row0
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
15
; | (because the pressed key was found and its number
is in R0)
; scan row1
SETB P0.0 ; set row0
CLR P0.1 ; clear row1
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number
is in R0)
; scan row2
SETB P0.1 ; set row1
CLR P0.2 ; clear row2
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number
is in R0)
; scan row3
SETB P0.2 ; set row2
CLR P0.3 ; clear row3
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number
is in R0)
finish:
JMP $ ; program execution arrives here when key is found -
do nothing
; column-scan subroutine
colScan:
JNB P0.4, gotKey ; if col0 is cleared - key found
INC R0 ; otherwise move to next key
JNB P0.5, gotKey ; if col1 is cleared - key found
INC R0 ; otherwise move to next key
JNB P0.6, gotKey ; if col2 is cleared - key found
INC R0 ; otherwise move to next key
RET ; return from subroutine - key not found
gotKey:
SETB F0 ; key found - set F0
RET
16
OUTPUT:
7.1
Result: -
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
17
EXPERIMENT:-8
Theory: This program sends the text abc down the 8051 serial port to the external
UART at 4800 Baud.To generate this baud rate, timer 1 must overflow every 13 us
with SMOD equal to 1 (this is as close as we can get to 4800 baud at a system clock
frequency of 12 Mz).See the notes on the serial port for more information.The data is
sent with even parity,therefore for it to be received correctly the external UART must
be set to Even Parity
Code:
CLR SM0 ;|
SETB SM1 ; | put serial port in 8-bit UART mode
MOV A, PCON ;|
SETB ACC.7 ;|
MOV PCON, A ; | set SMOD in PCON to double baud rate
OUTPUT:
8.1
Result: -
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
19
EXPERIMENT:-9
Theory: This program receives data on the 8051 serial port. Once the
program'sinitialisation is complete, it waits for data arriving on the RXD line (data
that was transmitted by the external UART).Any text typed in the external UART is
sent once the Tx Send button is pressed. As each character is transmitted fully,it
disappears from the Tx window.The data is appended with the return character (0D
HEX).The default external UART baud rate is 19,200. To generate this baud rate,
TH1 must be set to -3. If the system clock is 12 MHz, the error when attempting to
generate such a high baud rate such that very often garbage is received. Therefore, for
19,200 Baud, the system clock should be set to 11.059 HMz.See the notes on the
serial port for more information.The program is written using busy-waiting.It
continuously tests the RI flag. Once this flag is set by the 8051 hardware (a byte has
been received) the program clears it and then moves the byte from SBUF to A.The
received byte's value is checked to see if it is the terminating character (0D HEX).If it
is the program jumps to the finish,otherwise it moves the byte to data memory and
returns to waiting for the next byte.
Code:
CLR SM0 ;|
SETB SM1 ; | put serial port in 8-bit UART mode
MOV A, PCON ;|
SETB ACC.7 ;|
MOV PCON, A ; | set SMOD in PCON to double baud rate
MOV TMOD, #20H ; put timer 1 in 8-bit auto-reload interval timing mode
MOV TH1, #0FDH ; put -3 in timer 1 high byte (timer will overflow every
3 us)
MOV TL1, #0FDH ; put same value in low byte so when timer is first
started it will overflow after approx. 3 us
SETB TR1 ; start timer 1
20
MOV R1, #30H ; put data start address in R1
again:
JNB RI, $ ; wait for byte to be received
CLR RI ; clear the RI flag
MOV A, SBUF ; move received byte to A
CJNE A, #0DH, skip ; compare it with 0DH - it it's not, skip next instruction
JMP finish ; if it is the terminating character, jump to the end of
the program
skip:
MOV @R1, A ; move from A to location pointed to by R1
INC R1 ; increment R1 to point at next location where data will
be stored
JMP again ; jump back to waiting for next byte
finish:
JMP $ ; do nothing
Output:
9.1
Result: -
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
21
EXPERIMENT:-10
The motor sensor is connected to P3.5,which is the external clock source for timer 1.
Therefore, timer 1 is put into event counting mode. In this way, the timer increments once
every motor revolution.The value in timer 1 low byte is moved to A and this value together
with the data pointer (DPH and DPL) are used to get the 7-segment code from program
memory.The code is then sent to P1 to put the appropriate number on the Display 0.
The motor can be changed from clockwise to anti-clockwise by pressing SW0 (on P2.0).The
motor direction is stored in F0 (1 for clockwise, 0 for anti-clockwise). The value in F0 is sent
to Display 0's decimal point (P1.7).
This indicates the motor's direction - if the decimal point is lit, the motor is rotating anti-
clockwise, while if it is not lit the motor is rotating clockwise.
The value in F0 is compared with the value of SW0. If they are the same the motor direction
does not need to be changed. If they are not the same it means the user has pressed SW0 and
the motor direction must be reversed. When this happens the new motor direction is then
stored in F0.
Code:
MOV TMOD, #50H ; put timer 1 in event counting mode
SETB TR1 ; start timer 1
MOV DPL, #LOW(LEDcodes) ; | put the low byte of the start address of the
; | 7-segment code table into DPL
22
MOV DPH, #HIGH(LEDcodes) ; put the high byte into DPH
CLR P3.4 ;|
CLR P3.3 ; | enable Display 0
again:
CALL setDirection ; set the motor's direction
MOV A, TL1 ; move timer 1 low byte to A
CJNE A, #10, skip ; if the number of revolutions is not 10 skip
next instruction
CALL clearTimer ; if the number of revolutions is 10, reset
timer 1
skip:
MOVC A, @A+DPTR ; | get 7-segment code from code table - the
index into the table is
; | decided by the value in A
; | (example: the data pointer points to the
start of the
; | table - if there are two revolutions, then A
will contain two,
; | therefore the second code in the table will
be copied to A)
setDirection:
PUSH ACC ; save value of A on stack
PUSH 20H ; save value of location 20H (first bit-
addressable
;location in RAM) on stack
CLR A ; clear A
MOV 20H, #0 ; clear location 20H
MOV C, P2.0 ; put SW0 value in carry
MOV ACC.0, C ; then move to ACC.0
MOV C, F0 ; move current motor direction in carry
MOV 0, C ; and move to LSB of location 20H (which
has bit address 0)
clearTimer:
CLR A ; reset revolution count in A to zero
CLR TR1 ; stop timer 1
MOV TL1, #0 ; reset timer 1 low byte to zero
SETB TR1 ; start timer 1
RET ; return from subroutine
OUTPUT:
10.1
24
10.2
Result: -
Precaution: -
1.Back up your files: Always make sure to back up your project files, source code, and other
important data before making any changes to your project or using the simulator.
2.Use the latest version: Use the latest version of the software to ensure that you have access
to the latest bug fixes and security updates.
25
26