Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
344 views

Unit II Programming Using Embedded C Final

The document compares Assembly Language Programming and Embedded C programming, noting that Embedded C requires more execution time, memory, and is easier to code and debug than Assembly. It then discusses software development tools for Embedded C like compilers, cross assemblers, linkers, simulators, debuggers, and IDEs. It also covers logical operators in C, data types, including assembly code in C using pragmas, timers and interrupts, serial communication basics, baud rate calculations, and steps for transmitting and receiving data serially.

Uploaded by

mdwalunjkar5565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views

Unit II Programming Using Embedded C Final

The document compares Assembly Language Programming and Embedded C programming, noting that Embedded C requires more execution time, memory, and is easier to code and debug than Assembly. It then discusses software development tools for Embedded C like compilers, cross assemblers, linkers, simulators, debuggers, and IDEs. It also covers logical operators in C, data types, including assembly code in C using pragmas, timers and interrupts, serial communication basics, baud rate calculations, and steps for transmitting and receiving data serially.

Uploaded by

mdwalunjkar5565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming using Embedded C | U-2

Unit II
Programming using Embedded C
 Differentiate between Assembly Language Programming (ALP) and Embedded
C programming
Sr.
Parameter Assembly Language Embedded C
No.
1 Execution Time Faster(requires Less ET) Slower(requires more ET)
2 Time for coding More time required Less time required
3 Hex file size Less More
4 Debugging/Ease of Not so easy Easy
coding
5 Processor dependency Processor/Controller Processor/Controller
dependent independent
6 Type of Language Low level language High level language
7 Memory required for Less memory required More memory required.
Program coding

 Software Development Tools:


i. Compiler
ii. Cross assembler
iii. Cross compiler
iv. Linker/Locators
v. Simulators
vi. Debugger
vii. Integrated development environment (IDE)
i. Compiler: It is a computer program that transforms the source code written in
a programming or source language into another computer language i.e. target
language i.e. binary code known as object code.
ii. Cross assembler: It is useful to convert object codes for microcontrollers or
processor to other codes for another microcontrollers or processor and vice
versa.
iii. Cross compiler: It is used to create executable code other than one on which
the compiler is run. They are used to generate executable for embedded systems
or multiple platforms.
iv. Linker/Locator:  It is used for relocation process.  It is done during
compilation also it can be done at run time by a relocating loader.  It is a
program that takes one or more objects generated by compiler and combines
them into a single executable program.
v. Simulators:  A simulator is the s/w that simulates an h/w unit like emulator,
peripheral, network and I/O devices on a PC.  It defines a processor or
processing device as well as various versions for the target system.  Monitors
FTCCOERP P a g e 1 | 10
Programming using Embedded C | U-2

the detailed information of as source code part with labels and symbols during
the execution for each single step.  Provides the detailed information of the
status of memory RAM and simulated ports, simulated peripheral devices of the
defined target system.
vi. Debugger: - A Debugger allows you to download your code to the emulator's
memory and then control all of the functions of the emulator from a PC. Common
debugging features include the capability to examine and modify the
microcontroller's on-chip registers, data- and program-memory; pausing or
stopping program executing at defined program locations by setting breakpoints;
single-stepping (execute one instruction at a time) through the code; and looking
at a history of executed code (trace).
vii. Integrated Development Environment (IDE):- An IDE is a software application
that provides comprehensive facilities to computer programmers for software
development. An IDE consists of:  A source code editor.  A compiler and or
interpreter.  Build automation tools.  A debugger. IDE is dedicated to a specific
programming language, allowing a feature set that most closely matches
the programming paradigms [model] of the language. IDE’s typically present a
single program in which all development is done. This program typically provides
many features for authoring, modifying, compiling, deploying, and debugging
software.
 Logical operators in C-

Operator Symbol Example Description


~ -Called Logical NOT Operator.
NOT ~ Z=~x; It is used to reverse the logical state of its operand. If a
condition is true, then Logical NOT operator will make it false.
& -Called Logical AND operator.
AND & P=q & r; If both the operands are non-zero, then the condition becomes
true.
| -Called Logical OR Operator.
OR | B=a | d; If any of the two operands is non-zero, then the condition
becomes true.
EXOR ^ -Called logical Ex-or operator
^ C=a ^ b;
Output is true for odd no. of non-zero operands.
Left Shift << U=s<<1; << -Rotates data left side by mentioned position
Data in variable s will be shifted left by 1 bit position.
Right >> -Rotates data left side by mentioned position
Shift >> G=h>>4; Data in variable h will be shifted right by 4 bit position.

FTCCOERP P a g e 2 | 10
Programming using Embedded C | U-2

 DATATYPES used in embedded ‘C’ with their size and ranges


Data types Size in bits Data Range/usage
unsigned char 8 0 to 255
signed char 8 -128 to +127
unsigned int 16 0 to 65535
signed int 16 -32768 to +32767
sbit 1 SFR bit addressable only
bit 1 RAM bit addressable only
sfr 8 RAM addresses 80-FF H only
 How assembly language instructions included in ‘C’ program-
o In C program it’s possible to write assembly code in the embedded software
development.
Assembly code can be called in the C program or one can write assembly code
in line in their C program.
o To do so, we can use pair of inline assembly directives #pragma asm and
#pragma endasm to include the assembly code in C program.
o The ASM directive signals the beginning merge of a block of source text into
the .SRC file generated using the SRC directive.
o The ENDASM directive is used to signal the end of the source block.
 Concept structure:
#include<reg51.h>
void main(void)
{
C instructions;
#pragma asm
Assembly intructions
#pragma endasm
C instructions
}

FTCCOERP P a g e 3 | 10
Programming using Embedded C | U-2

 TIMERS AND INTERRUPTS-


1. Timers-
o TMOD Register-

o Pin Description-

o Timer Modes-

2. Interrupts with their vector address and priority-

Interrupt Source Vector address Interrupt priority


External Interrupt 0 – INT0 0003H 1
Timer 0 Interrupt – TF0 000BH 2
External Interrupt 1 – INT1 0013H 3
Timer 1 Interrupt – TF1 001BH 4
Serial Interrupt – RI/TI 0023H 5

FTCCOERP P a g e 4 | 10
Programming using Embedded C | U-2

o Interrupt Enable (IE) Register-

FTCCOERP P a g e 5 | 10
Programming using Embedded C | U-2

 SERIAL COMMUNICATION-
1. SCON Register-

o Serial communication Mode-

o PCON Register-

FTCCOERP P a g e 6 | 10
Programming using Embedded C | U-2

O BAUDRATE-
O BAUDRATE CALCULATIONS-

O STANDARD BAUDRATES-

The following Decimal or hexa decimal values will be loaded in TH1 register to
generate standard baud rate.

O STEPS TO RECEIVE DATA SERIALLY-

1. TMOD register is loaded with the value 20H to select 8 bit Auto Reload Mode…
2. TH1 is loaded with Decimal or Hex value to set the baud rate
3. SCON register is loaded to select serial communication mode…
4. TR1 is set to 1 to start Timer 1
5. RI is cleared.
6. RI flag bit is monitored to see if an entire character has been received yet
7. When RI=1 then SBUF has the byte, and its contents are moved into a safe
place
8. To receive the next character, go to Step 5.

FTCCOERP P a g e 7 | 10
Programming using Embedded C | U-2

O STEPS TO TRANSMIT DATA SERIALLY-

1. TMOD register is loaded with the value 20H to select 8 bit Auto Reload Mode…
2. TH1 is loaded with Decimal or Hex value to set the baud rate
3. SCON register is loaded to select serial communication mode…
4. TR1 is set to 1 to start Timer 1
5. TI is cleared.
6. The data which is to be transmitted is copied to SBUF register…
7. Now monitor TI flag, when TI=1 it means that data in SBUF has been
transmitted completely.
8. To transmit the next character, go to Step 5.

FTCCOERP P a g e 8 | 10
Programming using Embedded C | U-2

1. Addition of two 8-bit numbers.


#include<reg51.h>
void main()
{
unsigned char a=0x10; //a=10h
unsigned char b=0x20; //b=20h
unsigned char c;
c=a+b; //perform addition of a and b
P0=c; //transfer value of c to port P0
}
2. Transfer five bytes from source to destination.
#include<reg51.h>
void main()
{
unsigned char source[5]={0x0A,0x0B,0x0C,0x0D,0x0E}; //source array
unsigned char destination[5]; //destination array
unsigned char i; //counter
while(1)
{
for(i=0;i<5;i++) //repeat five times
destination[i]=source[i]; //transfer value from source to destination array

}
}
3. Exchange five bytes from source to destination.
#include<reg51.h>
void main()
{
unsigned char source[5]={0x0A,0x0B,0x0C,0x0D,0x0E}; //source array
unsigned char destination[5]={0X01,0X02,0X03,0X04,0X05};//destination array

unsigned char i, a; //counter

FTCCOERP P a g e 9 | 10
Programming using Embedded C | U-2

while(1)
{
for(i=0;i<5;i++) //repeat five times
{
a=source[i]; //copy value from source array
source[i]=destination[i]; //transfer value from destination to source array

destination[i]=a; //transfer value to destination array


}
}
}
4. Embedded C program to add 5 values continuously

#include<reg51.h>
void main()
{
unsigned char source[5]={0x01,0x02,0x03,0x04,0x05}; //source array
unsigned char i,a;//counter
a=0x00;
while(1)
{
for(i=0;i<5;i++) //repeat five times
{
a=a+source[i]; //add value from source array to a
P0=a; //transfer value of a to port P0
}
}
}

FTCCOERP P a g e 10 | 10
Programming using Embedded C|U 2

5. Perform logical operations...


/*
and &
or |
not ~
xor ^
*/
#include<reg51.h>
void main()
{
unsigned char a=0x01,b=0x00;
unsigned char c;
while(1)
{
c=a|b; //performs logical OR operation.
P0=c; //transfer value of c to port P0
c=a&b; //performs logical AND operation.
P1=c; //transfer value of c to port P1
c=~a; //performs logical NOT operation.
P2=c; //transfer value of c to port P2
c=a^b; //performs logical Ex-OR operation.
P3=c; //transfer value of c to port P3
}
}
6. Toggle port pins after some delay...
/* Embedded C program to toggle port pins after some delay*/
#include<reg51.h>
unsigned char i,j;
sbit c=P0^0; //Port pin P0.0 is refered as c.
void main()
{
while(1)
{
c=~c; //performs NOT operation
for(i=0;i<3;i++) //these two for loops
for(j=0;j<1275;j++)// generate delay.
{}
}
}
7. Toggle port pins as per switch position...
/* Embedded C program to toggle port pin as per
switch position.
*/
#include<reg51.h>
unsigned char a;
sbit c=P0^0;//Port pin P0.0 is refered as c.
sbit sw=P2^0;//switch is connected to port pin P2.0
void main()
{
sw=0; //switch is off initially
while(1)
{
if(sw==1) //check if switch is ON
{
c=~c; //if switch is ON, toggle c.
FTCCOERP
Programming using Embedded C|U 2

}
else
{
c=0; //if switch is OFF, make c=0.
}
}
}
8. Write ‘C’ language program to check bit P1.2.
If it is high send 55 H to P0, otherwise send AAH to P2.
/* Embedded C program to check P1.2
if high, send 55H to P0 port
else send AAH to P2 port.
*/
#include<reg51.h>
sbit c=P1^2; //switch is connected to port pin P1.2
void main()
{
while(1)
{
if(c==1)
{
P0=0x55; // if p1.2=1 send 55H to P0 poprt.
}
else
{
P2=0xAA; //if p1.2=0 send AAH to P2 port.
}
}
}
9. Write ‘C’ language program to mask the lower 4 bits of port P0
and upper 4 bits of port P2 using logical operator
/* Embedded C program to mask
lower 4 bits of P0 and
upper 4 bit of P2
using logical operator=> AND.
*/
#include<reg51.h>
void main()
{
P0=0xFF;//make port 0 input port
P2=0xFF;//make port 2 input port
while(1)
{
P0= 0xF0 & P0;//P0=0xFF
// 0XF0
P2=0x0F & P2;// P2=0xFF
// 0x0F
}
}
10. Write C language program to read P1 and store
the two’s complement of P1 to P2.
/* Embedded C program to read P1 and
store 2's complement of P1 to P2.
*/
#include<reg51.h>
FTCCOERP
Programming using Embedded C|U 2

void main()
{
unsigned char c;
while(1)
{
c=P1; //read value from P1 port in c.
c=(~c)+1;//2's compliment=1's compliment+1.
P2=c; //send value of c to port 2.
}
}
11. Write a C language program to read P2 and P3. Shift the
bits of P2 to right by 2 bits and P3 to left by 4 bits.
Store the content of P2 to P0 and P3 to P1
/* Embedded C program to read P2, P3 and
right shift P2 by 2 bits and store in P0.
left shift P3 by 4 bits and store in P1.
*/
#include<reg51.h>
void main()
{
while(1)
{
P0=P2>>2;//right shift data in P2 by 2 bit position
P1=P3<<4;//left shift data in P3 by 4 bit position
}
}
12. Write ‘C’ program for 89C51 to read data from port P1 and P2.
Compare the data and send the bigger data on port 3 continuously
/* Embedded C program to read P1, P2 and
compare the data and
send bigger data to P3 continuosly.
*/
#include<reg51.h>
void main()
{
unsigned char a,b;
while(1)
{
a=P1; //read data from port 1 to a.
b=P2; //read data from port 2 to b.
if(a>b)
{
P3=a; // if P1>P2 send data on P1 to P3.
}
else if(b>a)
{
P3=b; // if P2>P1 send data on P2 to P3.
}
else
{
P3=0x00;// if both P1 & P2 are same make P3=00.
}
}
}
13. Write c language program to toggle all bits of P0, P1, P2 and P3
FTCCOERP
Programming using Embedded C|U 2

ports continuously with certain delay.


/* Embedded C program to toggle all the bits of
P0, P1, P2, P3 with certain dealay.
*/
#include<reg51.h>
void delay(int);
void main()
{
while(1)
{
P0=0x00;
P1=0xff;
P2=0x00;
P3=0xff;
delay(50);//call delay
P0=0xff;
P1=0x00;
P2=0xff;
P3=0x00;
delay(50);//call delay
}
}
void delay(int t)//t=50
{
unsigned int i,j;
for(i=0;i<t;i++) //i<50....50mSec
for(j=0;j<1275;j++)
{}
}
14. Write ‘C’ language program to toggle bit P1.5 of port 1 continuously
after 50 ms delay. Generate delay using for loops.
/* Embedded C program to toggle the bit P2.1 of
P2 with certain dealay.
*/
#include<reg51.h>
sbit b=P1^5;//pin P1.5 is labelled as b
void delay(int);
void main()
{
while(1)
{
b=~b;//toggle bit b i.e. P1.5
delay(50);//call delay.
}
}
void delay(int t)//t=50
{
unsigned int i,j;
for(i=0;i<t;i++) //i<50....50mSec
for(j=0;j<1275;j++)
{}
}
15. Write a ‘C’ program to read the status of key connected to P1.3.
If the key is pressed, turn on the LED connected to P3.5 for 20m sec.
/* Embedded C program
FTCCOERP
Programming using Embedded C|U 2

to read key at P1.3 and if key is pressed


then toggle the LED at P3.5 for 20mSec.
*/
#include<reg51.h>
sbit key=P1^3;//key is connected to pin P1.3
sbit led=P3^5;//led is connected to pin P3.5
void delay(int);
void main()
{
P1=0xff;//input port
P3=0x00;//output port
while(1)
{
key=0;//make key off.
led=0;//make led off.
if(key==1)//check key status
{
led=1; //if key is pressed toggle led
delay(50);// call delay.
led=0;
delay(50);
}
else
{
led=0;//if key is off make led off.
}
}
}
void delay(int t)//t=50
{
unsigned int i,j;
for(i=0;i<t;i++) //i<50....50mSec
for(j=0;j<1275;j++)
{}
}
16. Write a ‘C’ program to generate a square wave of 100 Hz on P1.3.
Also draw the output observed on P1.3
/* Embedded C program
to generate square wave of 100Hz on P1.3
Solution:
to generate square wave of 100Hz on port P1.3 pin
for timer delay use T0 in Mode 1
clock freq for timer 1 is = oscialltor freq/12 -->11.0592MHz
i.e. = 11.0592M/12 = 1MHz means 1.085 uSec.
period of square wave T=1/100= 10msec.
on time= 5msec; off time= 5msec
value to be loaded in timer for 5mSec=65536-(Req. delay/Time for 1 machine cyale)
value to be loaded in timer for 5mSec delay=65536-(5m/1.085u)
value to be loaded in timer for 5mSec delay=65536-4608 =60928
value to be loaded in timer for 5mSec delay=EE00
*/
#include<reg51.h>
sbit sq=P1^3;//port pin P1.3 is labelled as sq.
void delay();
void main()
FTCCOERP
Programming using Embedded C|U 2

{
P1=0x00;//output port

while(1)
{
sq=1;
delay();
sq=0;
delay();
}
}
//delay is generated using timer 0.
void delay()
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TF0=0;
TH0=0XEE;
TL0=0X00; //EE00 EE01 EE02 upto FFFE FFFF
TR0=1; //start timer.
while(TF0!=1)//wait for timer overflow
{}
TF0=0;//clear overflow flag
TR0=0;//stop timer.
}
17. Write 89C51 ‘C’ language program to generate square wave of 10 KHz
on pin P2-7 using timer 0. Assume crystal frequency as 12 MHz.
/* Embedded C program
to generate square wave of 10kHz on P2.7
Solution:
to generate square wave of 10kHz on port P2.7 pin
for timer delay use T0 in Mode 1
clock freq for timer 1 is = oscialltor freq/12 -->12MHz
i.e. = 12M/12 = 1MHz means 1 uSec.
period of square wave T=1/10k= 100 uSec.
on time= 50usec; off time= 50usec
value to be loaded in timer for 50uSec=65536-(Req. delay/Time for 1 machine cyale)
value to be loaded in timer for 50uSec delay=65536-(50u/1.085u)
value to be loaded in timer for 50uSec delay=65536-46.08 =65489.92=65490(approx)
value to be loaded in timer for 50uSec delay=FFD2
*/
#include<reg51.h>
sbit sq=P2^7; //port pin P2.7 is labelled as sq.
void delay();
void main()
{
P2=0x00;//output port
while(1)
{
sq=1;
delay();
sq=0;
delay();
}
}
//delay is generated using timer 0.
FTCCOERP
Programming using Embedded C|U 2

void delay()
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TF0=0;
TH0=0XFF;
TL0=0XD2; //FFD2 FFD3 to FFFE FFFF
TR0=1; //start the timer
while(TF0!=1) //wait for timer overflow
{}
TF0=0; //clear timer overflow flag
TR0=0; //stop the timer
}
18. Write ‘C’ language program to generate saw tooth wave continuously.
/* Embedded C program
to generate SAWTOOTH WAVEFORM

Solution:
WAVEFORM FREQUENCY DEPENDS ON CRYSTAL FREQUENCY.
WAVEFORM WILL BE GENERATED ON PORT P2
*/
#include<reg51.h>
sfr sw= 0xA0; //P2 port is labelled as sw. here 0xA0 is address of P2 port.
unsigned char x;
void main()
{
P2=0x00;//output port

while(1) // p2==> 8bit 0000 0000


{ // 0000 0001
// 0000 0010
for(x=255;x>=0;x--) //1111 1111
{ //256= 0001 0000 0000
sw=x;
}

}
}
19. Write C language program to generate a square wave of 5 kHz on pin P3.5
by using timer 0 and mode 1. Assume crystal frequency is 24MHz

/* Embedded C program
to generate square wave of 5kHz on P3.5 pin
XTAL frequency= 24
use timer 1 mode 1 to generate delay
Solution:
given square wave freq (f)= 5kHz
Time Periode of given square wave (T)= 1/f=1/5k=200uSec
on time(Ton)=off time (Toff)= T/2= 100uSec

Xtal freq (fosc)=24MHz


freq of machine cycle= fosc/12= 24M/12=2MHz
Time periode of machine cycle= 1/(2M)=0.5uSec

value tobe loaded for 100uSec= 100u/0.5u=200


value tobe loaded in timer for
FTCCOERP
Programming using Embedded C|U 2

delay of 100usec= 65536-200


= 65336
= FF38
*/
#include<reg51.h>
sbit sq=P3^5;//port pin P3.5 is labelled as sq.
void delay();
void main()
{
P3=0x00;//output port

while(1)
{

sq=1;
delay();
sq=0;
delay();

}
}
//delay is generated using timer 1.
void delay()
{
TMOD=0x10; // 0 0 0 1 0 0 0 0 T1 is used in Mode 1
TF1=0;
TH1=0XFF;
TL1=0XA4; //FFA4 FFA5 to FFFD FFFE FFFF
TR1=1; //start the timer 1
while(TF1!=1) //wait for timer overflow
{}
TF1=0; //clear timer overflow flag 1
TR1=0; //stop the timer 1
}
20.
/* develop a program to generate stair-case waveform on port 2...
solution:
P2===>8bit==> 0000 0000 to 1111 1111 ===> 0 to 255
===> 00H to FFH

total steps= 5
so, every step =255/5===>51
*/
#include<reg51.h>
sfr sc=0xA0;//port 2 is labelled as sc, here 0xA0 is address os port 2.
void delay(int);
void main()
{
unsigned char x;
sc=0x00;//output port
while(1)
{
for(x=0;x<5;x++)
{
delay(10);
FTCCOERP
Programming using Embedded C|U 2

sc=sc+51; //to generate stair case waveform


}
for(x=0;x<5;x++)
{
delay(10);
sc=sc-51; //to generate stair case waveform
}
}
}
void delay(int t)//t=10
{
unsigned int i,j;
for(i=0;i<t;i++) //generates delay of 10 mSec.
for(j=0;j<1275;j++)
{}
}
21.
/* develop a program to generate Triangular waveform on port 2...
solution:
P2===>8bit==> 0000 0000 to 1111 1111 ===> 0 to 255
===> 00H to FFH

*/
#include<reg51.h>
sfr tr=0xA0;
void main()
{
unsigned char x;
tr=0x00;//output port
while(1)
{
x=0;
do
{
tr=x; //to generate rising part
x++;
}while(x!=255);

do
{
tr=x; //to generate falling part
x--;
}while(x!=0);
}
}
22. Write 89C51 ‘C’ language program to generate square wave of 10 KHz on
pin P2-7 using timer 0 interrupt.
Assume crystal frequency as 12 MHz

/* Embedded C program
to generate square wave of 10kHz on P2.7 using interrupt

Solution:
to generate square wave of 10kHz on port P2.7 pin
for timer delay use T0 in Mode 1
FTCCOERP
Programming using Embedded C|U 2

clock freq for timer 1 is = oscialltor freq/12 -->12MHz


i.e. = 12M/12 = 1MHz means 1 uSec.
period of square wave T=1/10k= 100 uSec.
on time= 50usec; off time= 50usec
timer register count for 50usec delay is=65536-(50u/1u) =65486
=FFCE

*/
#include<reg51.h>
sbit sq=P2^7;
void timer0() interrupt 1
{
sq=~sq;
}
void main()
{
P2=0x00;//output port
while(1)
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TH0=0XFF;
TL0=0XCE; //FFFB FFFC FFFD FFFE FFFF 10000
IE=0x82;
TR0=1;
while(1)
{}
}
}
23.
/*
Write a 'C' program to transfer the letter
"M" serially at 9600 baud rate continuously
solution:
data or character==> "M"
baudrate==> 9600
*/
#include<reg51.h>
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-3;//TH1=0xFD..to set baudrate=9600
SCON=0xc0;//serial mode 0 selected
TR1=1;//to start the timer 1
while(1)
{
TI=0;
SBUF='M';//data copied to SBUF
while(TI!=1)
{}
}
}
24. Write a ‘C’ program to transfer the message “MSBTE” serially
at 9600 baud rate continuously
/*
Write a 'C' program to transfer the message
FTCCOERP
Programming using Embedded C|U 2

"MSBTE" serially at 9600 baud rate continuously


solution:
data or character==> "M","S","B","T","E"
baudrate==> 9600
*/
#include<reg51.h>
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-3;//TH1=0xFD..to set baudrate=9600
SCON=0x00;//serial mode 0 selected
TR1=1;//to start the timer 1
while(1)
{
TI=0;
SBUF='M';//data copied to SBUF
while(TI!=1)
{}
TI=0;
SBUF='S';//data copied to SBUF
while(TI!=1)
{}
TI=0;
SBUF='B';//data copied to SBUF
while(TI!=1)
{}
TI=0;
SBUF='T';//data copied to SBUF
while(TI!=1)
{}
TI=0;
SBUF='E';//data copied to SBUF
while(TI!=1)
{}
}
}
25.
/*
Write a 'C' program to transfer the message
"data copied to SBUF" serially at 9600 baud rate continuously
solution:
data or Message==> "data copied to SBUF "
baudrate==> 9600
*/
#include<reg51.h>
unsigned char msg[20]= "data copied to SBUF ";
unsigned char i;
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-3;//TH1=0xFD..to set baudrate=9600
SCON=0x00;//serial mode 0 selected
TR1=1;//to start the timer 1
while(1)
{
FTCCOERP
Programming using Embedded C|U 2

for(i=0;i<20;i++)
{
TI=0;
SBUF=msg[i];//get character in SBUF
while(TI!=1)//wait for TI flag.
{}
}
}
}
26. Write 89C51 ‘C’ program to transfer the message “INDIA”,
serially at 9600 band rate continuously. Use 8 bit data and 1 stop bit
/*
Write 89C51 'C' program to transfer the message "INDIA",
serially at 4800 band rate continuously.
Use 8 bit data and 1 stop bit

solution:
data or Message==> "INDIA"
baudrate==> 4800
Use 8 bit data and 1 stop bit===>serial mode 1
*/
#include<reg51.h>
unsigned char msg[5]= "INDIA";
unsigned char i;
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-6;//TH1=0xFA..to set baudrate=4800
SCON=0x80;//serial mode 1 selected
TR1=1;//to start the timer 1 i.e. 8 bit data and 1 stop bit
while(1)
{
for(i=0;i<5;i++)
{
TI=0;
SBUF=msg[i]; //get character in SBUF
while(TI!=1) //wait for TI flag.
{}

}
}
}
27.
/*
program using INT1 interrupt...
*/
#include<reg51.h>
unsigned char i;
void isrx() interrupt 2 //ISR for INT1 interrupt
{
for(i=0;i<5;i++)
{
P2=0x00;
P2=0xFF;
}
FTCCOERP
Programming using Embedded C|U 2

}
void main()
{
unsigned char x,y;
IE=0x84;//enable INT1 interrupt..
while(1)
{
y=0;
for(x=0;x<10;x++)
{
P1=y++;
}
}
}
28.
/*
program to handle all the interrupts...
*/
#include<reg51.h>
unsigned char i,a,b;
void isr0() interrupt 0 //ISR for INT0 interrupt
{
for(a=0;a<10;a++)
{
P0=0x00;
P0=0xFF;
}
}
void isr1() interrupt 1 //ISR for TF0 interrupt
{
for(a=0;a<10;a++)
{
P1=0x00;
P1=0xFF;
}
}
void isr2() interrupt 2 //ISR for INT1 interrupt
{
for(a=0;a<10;a++)
{
P2=0x00;
P2=0xFF;
}
}
void isr3() interrupt 3 //ISR for TF1 interrupt
{
for(a=0;a<10;a++)
{
P3=0x00;
P3=0xFF;
}
}
void isr4() interrupt 4 //ISR for serial interrupt
{
for(i=0;i<10;i++)
FTCCOERP
Programming using Embedded C|U 2

{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
P0=0xFF;
P1=0xFF;
P2=0xFF;
P3=0xFF;
}
}
void main()
{
unsigned char x,y;
IE=0x84;//enable INT1 interrupt..
while(1)
{
y=0;
for(x=0;x<10;x++)
{
P1=y++;
}
}
}

FTCCOERP

You might also like