Sei sulla pagina 1di 11

ExNo:7 Port Programming

Aim:

i) Write an 8051 C program to send values of –4 to +4 to port P1.

ALGORITHM:

1) Start
2) Create an array mynum
3) Store 1,-1,2,-2,3,-3,4,-4 in mynum array
4) Loop unsigned char z till z<=8
5) P1=mynum[z]
6) Display output
7) Stop

PROGRAM

//Signed numbers
#include <reg51.h> /* Include header file for the registers and SFRs of 8051. */
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<8;z++)
P1=mynum[z];
}

OUTPUT

RESULT:
AN 8051 (embedded C) program for Port Programming was successfully implemented.

ii)Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)


50,000 times.

1
ALGORITHM:

1) Start
2) Set sbit MYBIT=P1^0
3) Loop unsigned int z till z<=50000
4) Set MYBIT=0;
5) Set MYBIT=1;
6) Display output
7) Stop

PROGRAM:

#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}

OUTPUT

RESULT:
AN 8051 (embedded C) program for Port Programming was successfully implemented.

iii)Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms.

ALGORITHM:

1) Start
2) Initiate a while loop

2
3) Store p1=0x55
4) Get delay of 250
5) Initiate a loop for 0 to itime.
6) Initiate another loop from 0 to 500.
7) Stop

PROGRAM:

#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}

OUTPUT:

RESULT:

AN 8051 (embedded C) program for Port Programming was successfully implemented.

3
ExNo:8 Timer-Counter Programming

AIM:

i) Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2
(8-bit auto-reload) to create the delay.

ALGORITHM:

1) Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is selected
2) Load registers TL and TH with initial count value registers
3) Start the timer
4) Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see if it is raised.
5) Get out of the loop when TF becomes high
6) Stop the timer
7) Clear the TF flag for the next round
8) Go back to Step 2 to load TH and TL Again

PROGRAM:

#include <reg51.h>
void T0M2Delay(void);
sbit mybit=P1^5;
void main(void){
unsigned char x,y;
while (1) {
mybit=~mybit;
for (x=0;x<250;x++)
for (y=0;y<36;y++) //we put 36, not 40
T0M2Delay();
}
}
void T0M2Delay(void){
TMOD=0x02;
TH0=-23;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

4
OUTPUT

RESULT:

AN 8051 (embedded C) program for Timer-Counter Programming was successfully implemented.

5
ii) Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to
create delay.

ALGORITHM:

1) Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is selected
2) Load registers TL and TH with initial count value
3) Start the timer
4) Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see if it is raised.
5) Get out of the loop when TF becomes high
6) Stop the timer
7) Clear the TF flag for the next round
8) Go back to Step 2 to load TH and TL Again

PROGRAM:
#include <reg51.h>
void T1M2Delay(void);
sbit mybit=P2^7;
void main(void){
unsigned char x;
while (1) {
mybit=~mybit;
T1M2Delay();
}
}
void T1M2Delay(void){
TMOD=0x20;
TH1=-184;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}

OUTPUT

6
RESULT:

AN 8051 (embedded C) program for Timer-Counter Programming was successfully implemented.

7
ExNo:9 Serial Programming

AIM:

Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously.

ALGORITHM:

1) TMOD register is loaded with the value, indicating the use of timer 1 in mode 2 (8-bit auto-
reload) to set baud rate
2) The TH1 is loaded with one of the values to set baud rate for serial data transfer
3) The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is
framed with start and stop bits
4) TR1 is set to 1 to start timer 1
5) TI is cleared by CLR TI instruction
6) The character byte to be transferred serially is written into SBUF register
7) The TI flag bit is monitored with the use of instruction.
8) To transfer the next byte, go to step 5

PROGRAM:

/* Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously. */
#include<reg51.h>
void main(void)
{
TMOD=0x20; //use timer 1, 8-bit data
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1)
{
SBUF=’A’; //place value in buffer
while(TI==0);
TI=0;
}
}

8
OUTPUT:

RESULT:

AN 8051 (embedded C) program for Serial Programming was successfully implemented.

9
ExNo:10 Interrupt Programming

AIM:
Write a C program using interrupts to do the following in which the 8051:
a) Receive data serially and send it to P0,
b) Read port P1, transmit data serially, and give a copy to P2,
Assume that XTAL=11.0592 MHz set the baud rate at 4800.

ALGORITHM:

1) Start
2) Set sbit to P0^1 and start a serial function using interrupt.
3) The TI flag bit is monitored and set according to instructions.
4) If condition is not met then P0 is written to SBUF register.
5) Declare the main function.
6) TMOD register is loaded with the value, indicating the use of timer 1 in mode 2 (8-bit auto-
reload) to set baud rate
7) TMOD register is loaded with the value, indicating the use of timer 1 in mode 2 (8-bit auto-
reload) to set baud rate
8) The TH1 is loaded with one of the values to set baud rate for serial data transfer
9) The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is
framed with start and stop bits
10) TR1 is set to 1 to start timer 1
11) TI is cleared by CLR TI instruction
12) The character byte to be transferred serially is written into SBUF register
13) The TI flag bit is monitored with the use of instruction.
14) To transfer the next byte, go to step 11

PROGRAM:

#include<reg51.h>
sbit wave=P0^1;
void serial0 () interrupt 4
{
if(TI==1)
{
TI=0;
}
else
{
P0=SBUF;
RI=0;
}
}
void main (void)
{

10
unsigned char x;
P1=0xFF;
TMOD=0x22;
TH1=0xF6; // SET BAUD RATE APPROXIMATELY AS 9600
SCON=0x50;
TH0=0xA4;
IE=0x92;
TR1=1;
TR0=1;
while (1)
{
x=P1;
SBUF=x;
P2=x;
}
}

OUTPUT:

RESULT:

AN 8051 (embedded C) program for Interrupt Programming was successfully implemented.

11

Potrebbero piacerti anche