0% found this document useful (0 votes)
32 views39 pages

Es Unit-Ii

Uploaded by

kic.ilc2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views39 pages

Es Unit-Ii

Uploaded by

kic.ilc2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Programming using Embedded C (22532)

UNIT-II (12 Marks)


Contents-
2.1.Programming with ‘Embedded C’: arithmetic and logical operations
data transfer with memory and port, decision control & looping
2.2.Timer/Counter program using embedded C’ for given Microcontroller
2.3.Serial communication program using ‘embedded C’ for given
microcontroller
2.4.Interrupt control program with ‘embedded C’ for given microcontroller

2.1.Programming with ‘Embedded C’: arithmetic and logical operations


data transfer with memory and port, decision control & looping:

Arithmetic and logical processing- Embedded C-


Data Types-
Q. State any two data types used in C with their range.
A data structure provides means of organizing several data elements of same types or
different type together at consecutive memory address. A data element in a data structure can
be identified and accessed by using a few pointers/ or indices/ a function. Any data structure
can be accessed using the pointers.
C language is rich in data types. Data type represents the type of data held by variable.
Number of addresses allocated (memory allocated) depends on data type. C allows following
primitive data types.
Data type Size Comment
Char 8 bits Characters (-127 to 128)

Int 16 bits Integers

Unsigned short int 16 bits 0 to 65536

Signed short int 16 bits -32768 to 32767

Unsigned int 16 bits 0 to 65536

Signed int 16 bits -32768 to 32767

Long double 64 bits

Float 32 bits Fraction numbers

Double 64 bits Fraction numbers

Long int 32 bits -2147483648 to 2147483647

Signed long int 32 bits -2147483648 to 2147483647


Data type Size Comment
Unsigned long int 32 bits 0 to 4294967296

In this lecture, we will discuss about the Logical operators in Embedded C. Data
conversion programs in embedded C will also be written and discussed. At the end Data
Serialization in 8051 using embedded C will be discussed.

Bitwise Operations in C
Q. Illustrate any two logical operators used in C with their examples (2M)
One of the most important and powerful features of the C language is its ability to perform
bit wise manipulation. This section describes the action of bitwise logic operators. Figure1.1
shows the bitwise logical operators.
Using these bit wise operators Embedded c will perform the logical operations bit wise on
binary numbers. Following are some examples which show how they are used.

Bit-wise Shift Operation in C

There are two bit-wise shift operators in C:

(1) shift right ( »), and

(2) shift left («).

Their format in C is as follows:

data » number of bits to be shifted right

data « number of bits to be shifted left

Some examples are shown below:


Shift operators
Shift operators:
x >> y (right shift operand x by y bit positions)
x << y (left shift operand x by y bit positions)
Vacated bits are filled with 0’s.
Shift right/left fast way to multiply/divide by power of 2
B = A << 3; A 1 0 1 0 1 1 0 1
(Left shift 3 bits) B 0 1 1 0 1 0 0 0
B = A >> 2; A 1 0 1 1 0 1 0 1
(Right shift 2 bits) B 0 0 1 0 1 1 0 1
B = ‘1’; B = 0 0 1 1 0 0 0 1 (ASCII 0x31)
C = ‘5’; C = 0 0 1 1 0 1 0 1 (ASCII 0x35)
D = (B << 4) | (C & 0x0F);
(B << 4) = 0 0 0 1 0 0 0 0
(C & 0x0F) = 0 0 0 0 0 1 0 1
D = 0 0 0 1 0 1 0 1 (Packed BCD 0x15)

Following program will show the demo of the logical operations. Run the following program
on your simulator and examine the results.

#include <reg51.h>

void main(void)

P0=0x35 & 0x0F; //ANDing

P1=0x04 | 0x68; //ORing

P2=0x54 ^ 0x78; //XORing

P0=~0x55; //inverting

P1=0x9A >> 3; //shifting right 3

P2=0x77 >> 4; //shifting right 4

P0=0x6 << 4; //shifting left 4

Next program will show the operation on port pins to access data bit-wise and manipulate
through logical operator.

1.2.1 Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it.

Solution:
#include <reg51.h>

sbit inbit=P1^0;

sbit outbit=P2^7;

bit membit;

void main(void)

while (1)

membit=inbit; //get a bit from P1.0

outbit=~membit; //invert it and send

//it to P2.7

1.3 Data Conversion

We have seen BCD numbers in previous modules. As stated there, many newer
microcontrollers have a real-time clock (RTC) where the time and date are kept, even when
the power is off. Very often the RTC provides the time and date in packed BCD. However, to
display them they must be converted to ASCII. Like this example, we need to convert the
data from one form to another form. In this section we show some example programs to
demo how Embedded C helps for data conversions. Some data conversions needed are listed
below.

• Packed BCD to ASCII conversion

• ASCII to packed BCD conversion

• Checksum byte in ROM

• Binary to decimal and ASCII conversion in C

Figure.2 shows the ASCII, binary and BCD codes for the digits 0 to 9.
Following program shows Packed BCD to ASCII conversion.

1.3.1 Example.1
Write an 8051 C program to convert packed BCD 0x29 to ASCII and display the bytes on P1
and P2.

Solution:

#include <reg51.h>

void main(void)

unsigned char x,y,z;

unsigned char mybyte=0x29;

x=mybyte&0x0F;

P1=x|0x30;

y=mybyte&0xF0;

y=y>>4;

P2=y|0x30;

Following program shows the ASCII to packed BCD conversion.

1.3.2 Example.2
Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and
display them on P1.

Solution:

#include <reg51.h>

void main(void)

unsigned char bcdbyte;

unsigned char w=‘4’;

unsigned char z=‘7’;

w=w&0x0F;

w=w<<4;

z=z&0x0F;

bcdbyte=w|z;

P1=bcdbyte;

Following program is for finding the Checksum byte in ROM.

1.3.3 Example.3

Write an 8051 C program to calculate the checksum byte for the data 25H, 62H, 3FH, and
52H.

Steps to calculate Checksum byte in ROM are:

1. Add the bytes together and drop carries.

2. Take the 2’s complement (invert and then add 1) of the total sum. This is the

Checksum byte, which becomes the last byte of the series.

Solution:

#include <reg51.h>

void main(void)
{

unsigned char mydata[]={0x25,0x62,0x3F,0x52};

unsigned char sum=0;

unsigned char x;

unsigned char chksumbyte;

for (x=0;x<4;x++)

P2=mydata[x];

sum=sum+mydata[x];

P1=sum;

chksumbyte=~sum+1; //logical operator used here

P1=chksumbyte;

Following program will demo Binary to Decimal and ASCII Conversion in 8051 C

1.3.4 Example.4

Write an 8051 C program to convert 11111101 (FD hex) to decimal and display the digits
on P0, P1 and P2.

Solution:

#include <reg51.h>

void main(void)

unsigned char x,binbyte,d1,d2,d3;

binbyte=0xFD;

x=binbyte/10;
d1=binbyte%10;

d2=x%10;

d3=x/10;

P0=d1;

P1=d2;

P2=d3;

1.4 Accessing Code ROM space in 8051 C

Using the code (program) space for predefined data is a widely used option in 8051. We
saw how to use the Assembly language instruction MOVC to access the data stored in the
8051 code space. Here, we see the same concept with 8051 C.

In 8051, we have three spaces to store data:

1. The 128 bytes RAM space with address range 00-7FH

✓ If you declare variables (eg.: char) to store data, C compiler will allocate a RAM
space for these variable.

2. User code space

✓ External code memory (64K) + on-chip ROM (64K)

✓ Data is embedded to code or is separated as a data section.

3. External data memory for data

✓ RAM or ROM is used.

2.1 RAM data space usage

The 8051 C compiler allocates RAM locations as follows:

1. Bank 0 – addresses 0 – 7

2. Individual variables – addresses 08 and beyond

3. Array elements – addresses right after variables


✓ Array elements need contiguous RAM locations and that limits the size of the array
due to the fact that we have only 128 bytes of RAM for everything.

4. Stack – addresses right after array elements.

Following is an example program to access data from memory.

2.1.1 Write, compile and single-step the following program on your 8051 simulator.
Examine the contents of the code space to locate the values.

#include <reg51.h>

void main(void)

unsigned char mydata[100]; //RAM space

unsigned char x,z=0;

for (x=0;x<100;x++)

z–;

mydata[x]=z;

P1=z;

While running this program you can see how the data will be stored in an array and
displayed in a port.

2.2 8052 RAM Space

One of the new features of the 8052 was an extra 128 bytes of RAM space.

✓ The extra 128 bytes of RAM helps the 8051/52 C compiler to manage its registers and
resources much more effectively.

✓ Based on 8052 architecture, you should use the reg52.h header file. Choose the 8052
option when compiling the program.

2.2.1 Using ROM to Store Data


To make C compiler use the code space (on-chip ROM) instead of RAM space, we can put
the keyword “code” in front of the variable declaration.

unsigned char mydata[] = “HELLO”

✓ HELLO is saved in RAM.

code unsigned char mydata[] = “HELLO”

✓ HELLO is saved in ROM.

This is discussed in the following examples.

2.2.2 Examples 5

Let us compare and contrast the following programs and discuss the advantages and
disadvantages of each one.

Example 5(a):

#include <reg51.h>
void main(void)

P1=“H”;

P1=“E”;

P1=“L”;

P1=“L”;

P1=“O”;

Data is embedded into code. Simple, short, not flexible.

Example 5(b):

#include <reg51.h>

void main(void)

unsigned char mydata[]=“HELLO”;


unsigned char z;

for (z=0; z<5; z++)

P1 = mydata[z];

Data is stored in RAM and does not occupy ROM.

Example 5(c):

#include <reg51.h>

void main(void)

Code unsigned char mydata[]=“HELLO”;

unsigned char z;

for (z=0; z<5; z++)

P1 = mydata[z];

Data is stored in ROM. However, data and code are separate.

2.3 Data serialization using 8051 C

Q. Develop a ‘C’ program to transfer the data from port P0 to port Pl. (2M)

Program:
#include <reg51.h> // contains all the port registers and
internal
void main (void)
{
unsigned char z; // z is allotted a space of 1 byte
for (z=0; z<=170;z++)

P1=z ; // values 00H-0FFH sent to port 1


P2=z; // values 00H-0FFH sent to port 2
}

Q. Write a C language program to operate port 0 and port 2 as output port and port
1 and port 3 as input port. (4M)
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
unsigned char mybyte;
P1=0xFF; //make P1 input port
P3=0xFF; //make P3 input port

while (1)
{
mybyte=P1; //get a byte from P1
P0= mybyte;
mybyte=P3;
P2= mybyte;

}
}

Q. Write a ‘C’ language program to mask the upper four bits of the data given in port 0
and write the answer in port 1.(4M)
Prog-
#include <reg51.h>
void main( )
unsigned char mybyte;
P0=0xFF;
{

P0=P0&0x0F;
while(1);
mybyte=P1;

}
Q. Write ‘C’ program to generate delay of 50msec for microcontroller
89C51 with crystal frequency of 11.0592 MHz. (4M)

#include<reg51.h>

sbit led = P2 ^ 0; // led at PORT 2 pin 0

void Delay(void); // Delay function declaration

void main() // main function


{
led = 0; //output PORT

while (1) // infinite loop


{
led = 1; // LED ON
Delay();

led = 0; // LED OFF


Delay();
}
}

void Delay()
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xDC; //initial value for 10ms
TL0 = 0x00;
TR0 = 1; // timer0 start

while (TF0 == 0); // check overflow condition


TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}

Q.Write a ‘C’ language program for 89C51 to generate triangular waveform.(4M)

#include<reg51.h>
void delay(unsigned int);
void main(void)
{
unsigned int x;
for(;;) //repeat forever

{
for(x=0;x<250;x++)
{
P1=x;
delay(100);
}

for(x=250;x>0;x–)
{
P1=x;
delay(100);
}
}
}

void delay(unsigned int time)


{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}

Q. Write a ‘C’ language program for serial communication to transfer


letter ‘M’ serially at 9600 baud continuously. (4M)
✓ Using the serial port (discussed in Serial Communication module)

✓ Transfer data one bit a time and control the sequence of data and spaces in between
them.

In many new generations of devices such as LCD, ADC, and ROM, the serial versions are
becoming popular since they take less space on a PCB.

The following are examples that demonstrate data serialization using embedded
C. Example. 6

Write a C program to send out the value 44H serially one bit at a time via P1.0. The LSB
should go out first.

Solution :

#include <reg51.h>

sbit P1b0=P1^0;

sbit regALSB=ACC^0;

void main(void)

unsigned char conbyte=0x44;

unsigned char x;

ACC=conbyte;

for (x=0;x<8;x++)

P1b0=regALSB;

ACC=ACC>>1;

2.3.1 Write a C program to bring in a byte of data serially one bit at a time via P1.0. The
MSB should come in first.

Example.7
#include <reg51.h>

sbit P1b0=P1^0;

sbit regALSB=ACC^0;

bit membit;

void main(void)

unsigned char x;

for (x=0;x<8;x++)

membit=P1b0;

ACC=ACC<<1;

regALSB=membit;

P2=ACC;

Following code shows examples for using unsigned bit


and sbit.

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


50,000 times.

Solution:

#include <reg51.h>

sbit MYBIT=P1^0;

void main(void)

{
unsigned int z;

for (z=0;z<=50000;z++)

MYBIT=0;

MYBIT=1;

TMOD Register (8-bit):

Fig .3

Among 8 bits, four bits are for timer 1 and the remaining four bits are for
timer 0. The bits of timer 0 and timer 1 are identical to each other.

 Gate: Gate means enable hardware control. The timer should run or
stop. If the gate is equal to one then counting is controlled by
external Interrupt (0 or 1) using a hardware pin. If the gate is zero
then it is independent of external Interrupt. By default, the gate is
one.
 C/T: If this bit is equal to one then it acts as a counter or else timer.
 M1, M0: Decides the mode of the timer.

00: Mode 0

01: Mode 1

10: Mode 2
11: Mode 3

Timer Modes

5. Time Delay

There are two ways to create a time delay in 8051 C : (i) using
the 8051 timer and (ii) using a simple for-loop. When using a loop
to create the time delay, there are three factors that can affect
the accuracy of the time delay. They are: (i) Crystal frequency of
the 8051 system, (ii) 8051 machine cycle timing and (iii) Compiler
used for 8051 C.

Following are example programs for creating time delay:


Write an 8051 C program to toggle bits of P1 continuously forever
with some delay.

Solution:

//Toggle P1 forever with some delay in between “on” and “off”

#include <reg51.h>

void main(void)

unsigned int x;

for (;;) //repeat forever

p1=0x55;

for (x=0;x<40000;x++); //delay size unknown

p1=0xAA;

for (x=0;x<40000;x++);

}
}

Write an 8051 C program to toggle bits of P1 ports continuously


with a 250 ms delay.

Solution:

#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++);

}
The operating modes of the 8051 can be changed by
manipulating the values of the 8051’s Special Function Registers
(SFRs). SFRs are accessed as if they were normal Internal RAM.
The only difference is that Internal RAM is from address 00h
through 7Fh whereas SFR registers exist in the address range of
80h through FFh.Each SFR has an address (80h through FFh) and
a name.

Write an 8051 C program to toggle all the bits of P0, P1, and P2
continuously with a 250 ms delay. Use the sfr keyword to declare
the port addresses.

Solution:

//Accessing Ports as SFRs using sfr data type

sfr P0=0x80;

sfr P1=0x90;

sfr P2=0xA0;

void MSDelay(unsigned int);

void main(void)

while (1)

P0=0x55;

P1=0x55;

P2=0x55;

MSDelay(250);

P0=0xAA;

P1=0xAA;
P2=0xAA;

MSDelay(250);

Example 1:
Write an 8051 C program to toggle only bit P1.5 continuously every 50ms. Use
Timer 0, mode1 (16-bit) to create the delay.
Solution:
#include <reg51.h>
Void T0MDelay (void):
Sbit mybit = P1^5;
Void main (void)
{
While (1)
{
mybit= ̴mybit; // toggle P1.5
T0M1Delay (); // Timer 0, mode 1 (16-bit)
}
}
Tested for AT89C51, XTAL=11.0592 MHz, using the Proview 32 compiler.
Void T0M1Delay (void)
{
TMOD=0x01; // Timer 0, mode 1 (16-bit)
TMOD=0xFD; // load TL0
TMOD=0x4B; // load TH0
TR0=1; // turn on T0
While (TF0==0); // wait for TF0 to roll over
TR0=0; // turn off T0
TF0=0; // clear TF0
}
Example 2:
Write an 8051 C program to toggle all bits of P2 continuously every 500ms. Use
Timer 1, mode1 to create the delay.
Solution:
// tested for DS89C420, XTAL=11.0592 MHz, using the Proview 32 compiler.
#include <reg51.h>
Void T1MDelay (void):
Void main (void)
{
Unsigned char x;
P2=0x55;
While (1)
{
P2= ̴P2;
For (x=0; x<=20; x++)
T1M1Delay ();
}
}
Void T1M1Delay (void)
{
TMOD=0x10; // Timer 1, mode 1 (16-bit)
TL1=0xFE; // load TL1
TH1=0xA5; // load TH1
TR1=1; // turn on T1
While (TF1==0); // wait for TF1 to roll over
TR1=0; // turn off T1
TF1=0; // clear TF1
}
A5FEH=42494 in decimal
65536-42494=23042
23042x 1.085µs=25ms and 20x 25 ms=500ms
Timers 0 & 1 delay using mode 1 (8-bit auto-reload):
Example 1:
Write an 8051 C program to toggle only bit P1.5 continuously every 250ms. Use
Timer 0, mode2 (8-bit auto-reload) to create the delay.
Solution:
// tested for DS89C420, XTAL=11.0592 MHz, using the Proview 32 compiler.
#include <reg51.h>
Void T0M2Delay (void):
Sbit mybit = P1^5;
Void main (void)
{
Unsigned char x, y;
While (1)
{
mybit= ̴mybit; // toggle P1.5
for (x=0; x=250; x++) // due to for loop overhead
for (y=0; y=36; y++) // we put 36 and not 40
T0M2Delay ();
}
}
Void T0M2Delay (void)
{
TMOD=0x02; // Timer 0, mode 2 (8-bit auto-related)
TH0=-23; // load TH0 (auto-reload value)
TR0=1; // turn on T0
While (TF0==0); // wait for TF0 to roll over
TR0=0; // turn off T0
TF0=0; // clear TF0
}
256-23=233
23x1.085 µs=25 µs
25 µs x 250 x 40=250 ms by calculation.
 However, the scope output does not give us this result. This is due to
overhead of the ‘for loop’ in C. to correct this problem, put 36 instead of 40.
Example 2:
Write an 8051 C program to toggle only bit P1.5 continuously every 250ms. Use
Timer 0, mode2 (8-bit auto-reload) to create the delay.
Solution:
// tested for DS89C420, XTAL=11.0592 MHz, using the Proview 32 compiler.
#include <reg51.h>
Void T1M2Delay (void):
Sbit mybit = P2^7;
Void main (void)
{
Unsigned char x;
While (1)
{
mybit= ̴mybit; // toggle P2.7
T1M2Delay ();
}
}
Void T1M2Delay (void)
{
TMOD=0x20; // Timer 1, mode 2 (8-bit auto-related)
TH1=-184; // load TH1 (auto-reload value)
TR1=1; // turn on T1
While (TF1==0); // wait for TF1 to roll over
TR1=0; // turn off T1
TF1=0; // clear TF1
}
}
1. Write a Program to read the number 1 from port 1, number 2 from port 2 , then add
them ,store the result ,send it to Port 3.

#include<reg.51.h>
void main()
{
unsigned char a,b,c ;
P1 = 0XFF ; //make port 1 as input port
P2 = 0XFF ; //make port 2 as input port
a=P1;
b=P2;
c= a+b ;
P3= c;
}
2. Write a program to Turn on and off the LED with some delay.

#include<reg51.h>
sbit LED=P1.1;
void delay(void);
void main(void)
{
while(1)
{
LED=1;
delay();
LED=0;
delay();
}
}
void delay(void)
{
unsigned char i,k;
for(i=0;i<70;i++)
for(k=0;k<255;k++);
}
3. Write a program to transfer the data from port P0 to port P1.

#include<reg51.h>
void main (void )
{
unsigned char X;
P0=0XFF; // P0 as input port
P1=0X00; // P1 as output port
while(1)
{
X = P0; // read port0
P1 = X; // output data to port1
}

2.3.8051 Embedded C Programming for Serial Port


Communication

SFR registers of 8051 are accessible directly in 8051 C


compilers by using reg51.h. It has already been described in the
previous modules. Example 1 shows how to program the serial
port in 8051 using Embedded C. For testing operation,
HyperTerminal is used for this example.

Example 1

Write a C program for 8051 to transfer the letter “A” serially at


4800 baud continuously.
Use 8-bit data and 1 stop bit.
Solution:
#include <reg51.h>
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1) {
SBUF=‘A’; //place value in buffer
while (TI==0);
TI=0;
}
}

Example 2 described below gives the details about how to


transfer the message serially with 9600 baud continuously.

Example 2

Write an 8051 C program to transfer the message “YES” serially


at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.

Solution:

#include <reg51.h>

void SerTx(unsigned char);


void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFD; //9600 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) {
SerTx(‘Y’);
SerTx(‘E’);
SerTx(‘S’);
}
}
void SerTx(unsigned char x){
SBUF=x; //place value in buffer
while (TI==0); //wait until transmitted
TI=0;
}
Example 3 described below gives the details about how to
receive the data serially and put in Port P1 with 4800 baud and 1
stop bit. Here, Timer 1 in mode 2 is used for stopping the bit while
receiving the data. The received data stored in the SBUF is then
written to port P1.

Example 3
Program the 8051 in C to receive bytes of data serially and put
them in P1. Set the baud rate at 4800; use 8-bit data, and 1 stop
bit.

#include <reg51.h>
void main(void){
unsigned char mybyte;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) { //repeat forever
while (RI==0); //wait to receive
mybyte=SBUF; //save value
P1=mybyte; //write value to port
RI=0;
}
}

Example 4 described below gives the details about sending two


messages to serial port with 28,800 and 56K baud. Here Timer 1
in mode 2 is used. The data is stored in the SBUF and then it waits
to transmit to the corresponding destination.

Example 4

Write an 8051 C Program to send the two messages “Normal


Speed” and “High Speed” to the serial port. Assuming that SW is
connected to pin P2.0, monitor its status and set the baud rate as
follows:

SW = 0, 28,800 baud rate


SW = 1, 56K baud rate
Assume that XTAL = 11.0592 MHz for both cases.

Solution:
#include <reg51.h>

sbit MYSW=P2^0; //input switch


void main(void){
unsigned char z;
unsigned char Mess1[]=“Normal Speed”;
unsigned char Mess2[]=“High Speed”;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFF; //28800 for normal
SCON=0x50;
TR1=1; //start timer
if(MYSW==0) {
for (z=0;z<12;z++) {
SBUF=Mess1[z]; //place value in buffer
while(TI==0); //wait for transmit
TI=0;
}
}
else {
PCON=PCON|0x80; //for high speed of 56K
for (z=0;z<10;z++) {
SBUF=Mess2[z]; //place value in buffer

while(TI==0); //wait for transmit


TI=0;
}
}
}

Example 5 described below gives the details about transferring


the letter A serially with 4,800 baud continuously. Here Timer 1 in
mode 2 is used to set the baud rate. The data is stored in the
SBUF and new addresses for DS89C4x0 chip are declared using
SFR registers using sfr keyword.

Example 5
Write a C program for the DS89C4x0 to transfer the letter “A”
serially at 4800 baud continuously. Use the second serial port
with 8-bit data and 1 stop bit. (note: We can only use Timer 1 to
set the baud rate).
Solution:
#include <reg51.h>
sfr SBUF1=0xC1;
sfr SCON1=0xC0;
sbit TI1=0xC1;
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON1=0x50; //use 2nd serial port SCON1
TR1=1; //start timer
while (1) {
SBUF1=‘A’; //use 2nd serial port SBUF1
while (TI1==0); //wait for transmit
TI1=0;
}}

Example 6 described below gives the details about receiving the


data serially with 9,600 bauds via second serial port. Here Timer 1
in mode 2 is used to set the baud rate.

Example 6

Program the DS89C4x0 in C to receive bytes of data serially via


the second serial port and put them in P1. Set the baud rate at
9600, 8-bit data and 1 stop bit. Use Timer 1 for baud rate
generation.

Solution:
#include <reg51.h>
sfr SBUF1=0xC1;
sfr SCON1=0xC0;
sbit RI1=0xC0;
void main(void){
unsigned char mybyte;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFD; //9600 baud rate
SCON1=0x50; //use 2nd serial port SCON1
TR1=1; //start timer
while (1) {
while (RI1==0); //monitor RI1

mybyte=SBUF1; //use SBUF1


P2=mybyte; //place value on port
RI1=0;
}
}
2.5.Interrupt Programming in Embedded C
Example 3:

A switch is connected to pin P3.2. When switch is pressed the


corresponding line goes low. Write a C program to Light all LEDS
connected to Port 0 if the switch is pressed. Display “y” at port2.

Solution:

# include <reg51.h> Sbit switch = P3^2;


void extint0() // interrupt 0
{
P0=0xFF;
}

void main()
{
Switch=1;
IE=0x81;
While(1)
{
P2=“y”;
}
}

2.2 Interrupt Flag Bits for 8051

The TCON register holds four of the interrupt flags in the 8051.
The interrupt flag bits for 8051 are shown in Table 4. The SCON
register has the RI and TI flags.
Example 4 shown below gives the C program to receive data
serially and send it to P0.

Example 4

Write a C program using interrupts to do the following: (a)


Receive data serially and send it to P0 (b) Read port P1, transmit
data serially, and give a copy to P2 (c) Make timer 0 generate a
square wave of 5 kHz frequency on P0.1. Assume that XTAL =
11.0592 MHz. Set the baud rate at 4800.

Solution:

void main()
{
unsigned char x;
P1=0xFF; //make P1 an input
TMOD=0x22;
TH1=0xF6; //4800 baud rate
SCON=0x50;
TH0=0xA4; //5 kHz has T=200us

IE=0x92; //enable interrupts


TR1=1; //start timer 1
TR0=1; //start timer 0
while (1)
{
x=P1; //read value from pins
SBUF=x; //put value in buffer
P2=x; //write value to pins
}
}

Example 5 gives the details about how to write an Embedded C


program to generate 10 KHz frequency wave and use timer 1 as
an event counter to count up to 1-Hz pulse.

Example 5

Write a C program using interrupts to do the following:


(a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-
reload
(b) Use timer 1 as an event counter to count up a 1-Hz pulse
and display it on P0. The pulse is connected to EX1. Assume that
XTAL = 11.0592 MHz. Set the baud rate at 9600.
Solution:

#include <reg51.h>
sbit WAVE =P2^1;
Unsigned char cnt;
void timer0() interrupt 1
{
WAVE=~WAVE; //toggle pin
}
void timer1() interrupt 3
{
cnt++; //increment counter
P0=cnt; //display value on pins
}
void main()
{
cnt=0; //set counter to 0
TMOD=0x42;
TH0=0x-46; //10 KHz
IE=0x86; //enable interrupts
TR0=1; //start timer 0
while (1); //wait until interrupted

Write C language program to toggle all bits of P0, P1, P2 and P3 ports continuously
with certain delay.

#include < reg51.h>


void Add_delay (unsigned int);
void main (void)
while(1) //repeat loop
{
P0=0xff; //toggle all bits of port 0
Add_delay (200); // add delay
P0=0x00; //toggle all bits of port 0
Add_delay (200); //add delay
P1=0xff; //toggle all bits of port 1
Add_delay (200); //add delay
P1=0x00; //toggle all bits of port 1
Add_delay (200); //add delay
P2=0xff; //toggle all bits of port 0
Add_delay (200); //add delay
P2=0x00; //toggle all bits of port 2
Add_delay (200); //add delay
P3=0xff; //toggle all bits of port 3
Add_delay (200); //add delay
P3=0x00; //toggle all bits of port 3
Add_delay (200); //add delay
}
void Add_delay (unsigned int delay)
{
unsigned int x,y;
for(x=0; x< delay; x++)
for (y=0; y<1275; y++);
}
Write a C language program to transfer message “MSBTE” serially at 9600 band rate.
Assume crystal frequency 12 MHz

#include <reg51.h>
void main(void)
{
unsigned char text[ ] = “MSBTE”; //initialize array
TMOD = 0x20; //Initialize timer 1 in mode 2
TH1 = 0xFD; //baud rate 9600
SCON = 0x50; //start serial communication ( 8bit , 1 stop bit , REN )
TR1 = 1; //start timer 1

for(i=0;i<6;i++) //Read array and transmit serially till end


{
SBUF = text[i];
while(TI==0); //check interrupt
TI = 0; //clear interrupt
}
}
Write C language program to rotate stepper motor by 90 degree clockwise. Assume step
angle is 1.8 degree and 4 step sequence.

Step angle=1.8 degree


Total steps required=90/1.8=50
Four step sequence so
Count=50/4=12.5=approximately 13

#include< regx51.h>
void delay();
void main()
{
int x;
for (x=0;x,13;x++)
{
P0=0x33;
delay(10);
P0=0x66;
delay(10);
P0=0xcc
delay(10);
P0=0x99;
delay(10);
}
}
void delay(unsigned int t)
{
unsigned int x,y;
for(x=0;x<=t;x++)
for(y=0;y<=1275;y++);
}
Write a 89C51 C program to display ‘WELCOME’ on 16 × 2
LCD display

#include reg51.h

//LCD
sbit rs = P3^0; //register select pin
sbit rw = P3^1; //read write pin
sbit e = P3^2; //enable pin

void delay(unsigned int time) //Function to provide time delay in msec.

int i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);

void lcdcmd(unsigned char item) //Function to send command to LCD

{
P2 = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
return;
}

void lcddata(double item) //Function to send data to LCD

P2 = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
return;
}

void main()

{
lcdcmd(0x0E); //turn display ON for cursor blinking
lcdcmd(0x01); //clear screen
lcdcmd(0x06); //display ON

lcddata('H');
lcddata('E');
lcddata('L');
lcddata('L');
lcddata('O');
lcddata('!');

while(1){} //Stuck into infinite loop after displaying HELLO! so to


avoid blinking
}

You might also like