Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Srikanth
Write a program to toggle the LEDs (all at once) with some delay. Write the code both in
Assembly language and Embedded C.
Aim
Components
89C51RD2 microcontroller - 1
Light Emitting Diodes - 8
Procedure
1 $ MOD51
2 ORG 00H
3 MOV DPTR,#0FFC0H ;move LEDs addr into DPTR
9
10 DELAY: MOV R0,#02H ;move some data to R0
#include <8051.h>
__xdata __at (0xffc0) unsigned char ledptr;
void delay();
void main()
{
while (1)
{
ledptr = 255;
delay();
ledptr = 0;
delay();
}
}
void delay()
{
unsigned int i,j;
for (i=0;i<0x002f;i++)
for (j=0;j<0x0fff;j++);
}
Result
The LEDs will toggle continuously with some delay. A pictorial representation is shown below
●●●●●●●●
○○○○○○○○
Write a program to read status of switches and display on LEDs. Write the code both in
Assembly language and Embedded C.
Aim
Components
89C51RD2 microcontroller - 1
Seven segment displays - 2
Procedure
1) 8 LEDs are interfaced to memory location FFC0H and 8 toggle switches are interfaced to
FFC1H.
2) Move the address location of switches (FFC1H) into DPTR.
3) Read the data from address location in DPTR to A.
4) Move the address location of LEDs (FFC0H) into DPTR.
5) Move the contents of A into the address location pointed by DPTR.
6) Repeat steps 2 through 5 in an infinite loop to continuously monitor the switches.
1 $ MOD51
2 ORG 00H
;move the address of
3 MONITOR: MOV DPTR,#0FFC1H switches onto DPTR
;read the data from switches
4 MOVX A,@DPTR into A
;move the address of LEDs
5 MOV DPTR,#0FFC0H into DPTR
;move the data present in A
6 MOVX @DPTR,A onto the LEDs via DPTR
7 SJMP MONITOR ;jump to MONITOR
#include <8051.h>
__xdata __at(0xffc0) unsigned char ledptr;
__xdata __at(0xffc1) unsigned char swtptr;
void main()
{
while(1)
{
ledptr=swtptr;
}
}
Result
The switches are continuously monitored and the status of LEDs shows the status of switches.
Aim
To display numbers 0 to 9 on a seven segment display – first on left side then on the right and
then on both.
Components
89C51RD2 microcontroller
Development software : RIDE, Eclipse (SDCC)
Deployment software : Flash magic
Procedure
1) Two 7 segment displays are interfaced to the address location FFC2. Data sent to FFC2 is
latched onto either one or both the displays depending on the bits 2 and 3 in FFC3. If both are 0
then both are selected. If bit 2 is low then left display is selected and if bit 3 is low then right
display is selected.
2) Store the 7 segment codes of numbers 0 to 9 in any address location.
3) Set bits 2 and 3 of FFC3 to select any one of the displays.
4) Move the data from the starting address location onto A. Move the data in A to FFC2.
5) Increment the address location pointer and repeat step 4. Repeat this step until all digits are
displayed. Give a delay between display of each digit.
6) Repeat steps 4 and 5 by changing bits 2 and 3 of FFC3 to all possible and permitted
combinations..
1 $ MOD51
2 ORG 95H
3 digits DB 3FH,06H,5BH,4FH, ;defining a table of digits
66H,6DH,7DH,07H,
7FH,6FH
4 ORG 00H
5 SEG7: MOV DPTR,#0FFC3H ;move address of 7 segment
configuration
6 MOV A,#04H ;move data to A to select
the left 7 segment display
7 MOVX @DPTR,A ;move data in A to DPTR
9 MOV DPTR,#0FFC3H
10 MOV A,#08H ;move data to select right 7
segment display
11 MOVX @DPTR,A
12 ACALL DISPDIG
13 MOV DPTR,#0FFC3H
14 MOV A,#00H ;move data to select both 7
segment displays
15 MOVX @DPTR,A
16 ACALL DISPDIG
17 SJMP SEG7 ;loop through all the steps
18
19 DISPDIG: MOV R3,#0AH ;initialize with no of
digits
20 MOV DPTR,digits ;move the initial location
of digits table
21 DIGLOOP: MOVC A,@DPTR ;move the data at DPTR
location into A
22 MOV DPTR,#0FFC2 ;move address of seven
segment display into DPTR
23 MOVX @DPTR,A ;move data in A to DPTR
27 RET ;return
28
29 DELAY: MOV R0,#02H ;move some data to R0
C Program
#include <8051.h>
void disp09();
void delay();
void main() {
__xdata __at (0xFFC3) char ledconf;
while (1) {
ledconf = 0x004;
disp09();
ledconf = 0x008;
disp09();
ledconf = 0x000;
disp09();
}
}
Result
The numbers 0 to 9 are displayed first on the left seven segment display then on the right one
and then on both the displays. A snapshot of the numbers is shown below.
01234
56789
Write a program to transmit characters (infinitely) via Serial Communication. Write the code both
in Assembly language and Embedded C.
Aim
Components
89C51RD2 microcontroller - 1
RS-232 connection cable - 1
Procedure
1) Configure Timer 1 as 8 bit auto-reload timer by moving appropriate data (20H) into TMOD.
2) Load TH1 with the time required to transmit a character serially. This is calculated using the
formula given below.
2
1 = 256 + ×
32d 12 ×
3) Configure SCON to indicate mode 1 is in use.
4) Clear the TI bit so as to start a new transmission.
5) Start timer 1 by setting the TR1 bit.
6) Move the data to be transmitted into SBUF.
7) Monitor the TI bit until it is set. When set the transmission is done.
8) To send another character repeat steps 4 through 7.
$ MOD51
ORG 00H
MOV TMOD,#20H ;set timer 1 to autoreload
mode (mode 2)
MOV TH1,#0FDH ;initialize TH1 with the
baud rate
MOV SCON,#50H ;configure the serial
transmit to mode 1
MAIN: MOV A,#’C’ ;move the desired char to be
transmitted into A
ACALL TRANSMIT ;call the TRANSMIT
subroutine
MOV A,#’V’
ACALL TRANSMIT
MOV A,#’R’
ACALL TRANSMIT
SJMP MAIN ;infinite loop
RET ;return
C Program
#include <8051.h>
#include <serial_io.h>
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
for (i = 0; i < 3; i++)
transmit(str[i]);
}
void transmit(unsigned char ch) {
TI = 0;
TR1 = 1;
SBUF = ch;
while (!TI);
}
Result
The characters C, V, R are transmitted to the com port of the PC and can be viewed on the
Hyper Terminal..
Write a program to receive characters via Serial Communication and display on 7 segment
display. Write the code both in Assembly language and Embedded C.
Aim
Components
89C51RD2 microcontroller - 1
RS-232 connection cable - 1
Procedure
1) Configure Timer 1 as 8 bit auto-reload timer by moving appropriate data (20H) into TMOD.
2) Load TH1 with the time required to transmit a character serially. This is calculated using the
formula given below.
2
1 = 256 + ×
32d 12 ×
3) Configure SCON to indicate mode 1 is in use.
4) Clear the RI bit so as to start a new reception of data.
5) Start timer 1 by setting the TR1 bit.
6) Monitor the RI bit until it is set. Once set the received data is in SBUF.
7) Move the data in SBUF to A. Convert it into 7 segment form and send to any of the 7 segment
displays.
8) Repeat steps 4 through 7 to receive data continuously.
1 $ MOD51
2 ORG 00H
3 MOV TMOD,#20H ;set timer 1 to autoreload
mode (mode 2)
4 MOV TH1,#0FDH ;initialize TH1 with the
baud rate
5 MOV SCON,#50H ;configure the serial
transmit to mode 1
6 MAIN: ACALL RECEIVE ;call the RECEIVE subroutine
9
10 RECEIVE: CLR RI ;clear Receive Interrupt
14 RET ;return
#include <8051.h>
#include <serial_io.h>
unsigned char receive();
void dispdig(unsigned char);
void main() {
unsigned char ch;
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
while (1) {
ch = receive();
dispdig(ch);
}
}
The digits 0 to 9 entered via the Hyper terminal are displayed on the 7 segment display. Any
other character is displayed as E.
Write a program to display a string in two rows on the LCD display. Write the code both in
Assembly language and Embedded C.
Aim
Components
89C51RD2 microcontroller - 1
LCD display - 1
Procedure
1) The data lines of the LCD display are interfaced to the address location FFC4H and command
mode/data mode can be chosen by resetting/setting bit 0 of FFC3H.
2) Configure the LCD display in command mode and send commands to set the display to 2 rows
and 5x7 matrix, move the cursor to starting of first row, display on and cursor blinking and clear
the display. Give sufficient delay in between the issuing of commands.
3) Move the character to be transmitted into A.
4) Configure the LCD in data mode. Move the data in A onto the LCD. Give sufficient delay for the
data to be latched and displayed.
5) Configure the LCD in command mode and issue the command to move the increment the cursor.
6) Repeat steps 3 to 5 to display another character.
7) Whenever a line break is required, configure the LCD in command mode and issue the command
to move the cursor to next line.
1 $ MOD51
2 ORG 00H
3 MOV A,#38H ;set display to 2 rows and
5x7 matrix mode
4 ACALL GIVE_CMD
5 MOV A,#80H ;force cursor to beginning
of first line
6 ACALL GIVE_CMD
7 MOV A,#0EH ;display on and cursor blink
8 ACALL GIVE_CMD
9 MOV A,#01H ;clear the display
10 ACALL GIVE_CMD
11 MOV A,#’C’ ;display character C
12 ACALL MOVE_DAT
13 MOV A,#06H ;increment cursor
14 ACALL GIVE_CMD
15 MOV A,#’V’
16 ACALL MOVE_DAT
17 MOV A,#06H
18 ACALL GIVE_CMD
19 MOV A,#’R’
20 ACALL MOVE_DAT
21 MOV A,#0C0H ;force cursor to beginning
of second line
22 ACALL GIVE_CMD
23 MOV A,#’C’
24 ACALL MOVE_DAT
25 MOV A,#06H
26 ACALL GIVE_CMD
27 MOV A,#’E’
26 ACALL MOVE_DAT
29 END: SJMP END ;infinite loop
33 MOVX @DPTR,A
34 MOV DPTR,#0FFC4H ;point to data lines
38 RET ;return
39
40 MOVE_DAT: MOV B,A ;save data in A to B
43 MOVX @DPTR,A
44 MOV DPTR,#0FFC4H ;point to data lines
48 RET ;return
49
50 DELAY: MOV R0,#02H ;move some data to R0
C Program
#include <8051.h>
void delay();
void main() {
__xdata __at (0xffc3) unsigned char lcdconf;
__xdata __at (0xffc4) unsigned char lcddata;
char str[] = "CVRCE";
int i;
lcdconf = 0x00;
lcddata = 0x38;
delay();
lcddata = 0x80;
delay();
lcddata = 0x0E;
delsy();
lcddata = 0x01;
delay();
for (i = 0; i < 5; i++) {
lcdconf = 0x01;
lcddata = str[i];
delay();
if (i == 2) {
lcdconf = 0x00;
lcddata = 0x0C0;
delay();
}
lcdconf = 0x00;
lcddata = 0x06;
delay();
}
}
void delay() {
unsigned short i,j;
for (i = 0; i < 0xff; i++)
for (j = 0; j < 0xff; j++);
}
Result
The characters CVR-CE are displayed in two rows of the LCD display after the execution of the
program.
CVR
CE
Write a C program to read a string via serial communication and apply simple encryption and
decryption operations on it.
Aim
To perform simple encryption and decryption operations on a string read via serial
communication.
Components
89C51RD2 microcontroller
Development software - Eclipse (SDCC)
Deployment software - Flash magic
Procedure
C Program
#include <8051.h>
#include <serial_io.h>
void transmit(char* str) {
unsigned short i=0;
while (str[i] != '\0') {
TI = 0;
TR1 = 1;
SBUF = str[i];
while (!TI);
i++;
}
}
char* receive() {
char str[10], ch;
unsigned short i=0;
while (i < 9 && ch != ' ') {
RI = 0;
TR1 = 1;
while (!RI);
ch = SBUF;
return str;
}
main() {
char msg[10], enc[10], dec[10];
unsigned short i;
TMOD = 0x20;
TH1 = 0xFD;
SMOD = 0x50;
transmit("Enter a message ");
msg = receive();
transmit("Original Message : ");
transmit(msg);
i = 0;
while (msg[i] != '\0') {
enc[i] = msg[i] + 7;
i++;
}
enc[i] = '\0';
i = 0;
while (enc[i] != '\0') {
dec[i] = enc[i] - 7;
i++;
}
dec[i] = '\0';
transmit("Decrypted Message : ");
transmit(dec);
}
Result
The user is prompted to enter a message. After entering the message, the original message,
encrypted message and decrypted message are displayed on the screen.
Aim
Components
Procedure
1) 8 active low LEDs are interfaced to the microcontroller port pin 2.7 via a shift register. Data on
P2.7 is latched onto the shift register whenever a high-low-high transition is received on P2.6.
2) To glow the first LED, 0 is sent to P2.7 and then 1-0-1 is sent to P2.6. The 0 at P2.7 is then
latched to the shift register and the first LED starts glowing.
3) Repeat step 2 8 times to glow all the 8 LEDs.
C Program
# include <at89c51xd2.h>
void set_clk();
sbit clk = P2^6;
sbit led = P2^7;
main() {
int I;
for (I = 0; I < 8; i++) {
led = 0;
set_clk();
}
while(1);
}
void set_clk() {
clk = 1;
clk = 0;
clk = 1;
}
Result
●●●●
●●●●
Aim
Components
Procedure
1) Configure Timer 1 as 8 bit auto-reload timer by moving appropriate data (20H) into TMOD.
2) Load TH1 with the time required to transmit a character serially. This is calculated using the
formula given below.
2
1 = 256 + ×
32d 12 ×
3) Configure SCON to indicate mode 1 is in use.
4) Clear the TI bit so as to start a new transmission.
5) Start timer 1 by setting the TR1 bit.
6) Move the data to be transmitted into SBUF.
7) Monitor the TI bit until it is set. When set the transmission is done.
8) To send another character repeat steps 4 through 7.
C Program
#include <at89c51xd2.h>
#include <serial_io.h>
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
for (i = 0; i < 3; i++)
transmit(str[i]);
}
void transmit(unsigned char ch) {
TI = 0;
TR1 = 1;
SBUF = ch;
while (!TI);
}
..
CVR COLLEGE OF ENGINEERING
Vastunagar, Mangalpally(V), Ibrahimpatnam(M), R.R Dist, Pin - 501510
Page No: 20
Result
The characters C, V, R are transmitted to the com port of the PC and can be viewed on the
Hyper Terminal.