ESY_Unit 2
ESY_Unit 2
Topics
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.
Variables
Variables in embedded C are named storage locations in memory used to hold data
temporarily during program execution. They can store different types of data such as
integers, characters, floating-point numbers, etc. Variables must be declared before they
can be used, specifying their data type.
Data Types
o int: Used to store integers (whole numbers). Sizes can vary depending on the
microcontroller architecture (e.g., 16-bit, 32-bit).
Range: Typically from -32,768 to 32,767 for a 16-bit int, and from -
2,147,483,648 to 2,147,483,647 for a 32-bit int.
o char: Used to store characters (ASCII values).
Range: Usually from -128 to 127 for signed char, and from 0 to 255 for
unsigned char.
o float: Used to store floating-point numbers (numbers with decimal points).
Range: Depends on the specific floating-point implementation
(typically single-precision or double-precision).
o double: Used for double-precision floating-point numbers, providing greater
precision compared to float.
Range: Larger range and precision compared to float.
Logical operations in C
A&B A|B A^B Y = ~A
A B AND OR EX-OR NOT
0 0 0 0 0 A = 0 THEN Y = 1
0 1 0 1 1 A = 1 THEN Y = 0
1 0 0 1 1
1 1 1 1 0
Program
(2) White 8051 c program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C &
D on Port 1.
#include < reg51.h>
void main (void)
{
unsigned char mynum [] = "12345ABCD”;
unsigned char z;
for (z=0; z < = 10; z++)
p1 = mynum [z];
#include <reg51.h>
void main (void)
{
for (;;)
{
P1 = 0x55;
P1 = 0xAA;
}
}
(6) LED’s are connected to bit PI & P2. Write 8051 C program that shows the count from 0
to FFH on LEDS.
#include <reg51.h>
# define LED P2
void main (void)
{
P1 = 00
LED = 0;
for (;;)
{
P1 ++;
LED ++;
}
}
(7) Write c program to get a byte of data from Pl, wait 1/2 second, and then send it P2.
#include <reg51.h>
void MsDelay (unsigned int);
void main (void)
{
unsigned char mybyte;
P1 = 0xFF
while (1)
{
mybyte = P1;
MsDelay (500);
P2 = mybyte;
}
}
void MsDelay (unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
(8) Write an C program to get a byte of data from P0. if it is less than 100, send it to P1;
otherwise send it to P2.
#include <reg51.h>
void main (void)
{
unsigned char mybyte;
P0 = 0xFF;
while (1)
{
mybyte = P1;
if (mybyte<100)
P1 = mybyte;
else
P2 = mybyte;
}
}
(9) Write an 8051 C program to monitor bit P1.5. If it is high, send 55H to P0; otherwise
send AAH to P2.
#include < reg51.h>
sbit mybit=P1^5;
void main (void)
{
mybit = 1;
while(1)
{
if (mybit == 1)
P0 = 0x55;
else
P2 = 0xAA;
}
}
Program: Write an 89C51 embedded C program to toggle all bits of P1 continuously
void main() {
P1 = 0x00; // Initialize P1 to 0x00 (all bits low)
while (1) {
P1 = ~P1; // Toggle all bits of P1
void main() {
unsigned char toggle_mask = 0xFF; // Initialize toggle mask to all bits set
while (1) {
P0 ^= toggle_mask; // Toggle all bits of P0
P1 ^= toggle_mask; // Toggle all bits of P1
P2 ^= toggle_mask; // Toggle all bits of P2
P3 ^= toggle_mask; // Toggle all bits of P3
void main() {
unsigned char data_in, data_out;
while (1) {
data_in = P0; // Read data from Port 0
void main() {
unsigned char num1, num2, result;
unsigned int i;
while (1) {
num1 = 0x12; // Example: First 8-bit number (you can change as needed)
num2 = 0x34; // Example: Second 8-bit number (you can change as needed)
// Output the result (for testing, you can modify this based on your application)
P1 = result;
}
}
Program: Write an 89C51 embedded C program to mask the lower 4 bits of P2 &
upper 4 bits of P0 using logical operator.
void main() {
unsigned char data_p0, data_p2, masked_p0, masked_p2;
while (1) {
data_p0 = P0; // Read data from Port 0
data_p2 = P2; // Read data from Port 2
Example
Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use
Timer 1, mode 1 to create the delay.
Solution:
#include <reg51.h>
void TIMIDelay (void);
void main(void)
{
unsigned char x;
P2=0x55;
while (1)
}
{
P2=~P2; //toggle all bits of P2
for (x=0; x<20; x++)
T1M1Delay();
void TIM1Delay (void)
{
TMOD=0x10 //Timer 1, mode (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
}
Count calculations
25 ms/1.085 μs = 23042
65536 – 23042 = 42494
(A5FE)H=42494 in decimal
Number of iterartion :
23042 x 1.085 μs = 25 ms and 20 x 25 ms = 500 ms
Example
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.
Solution:
#include <reg51.h>
void T0M2Delay (void);
sbit mybit-P1^5;
void main(void)
{
unsigned char x, y;
while (1)
{
mybit=~mybit; //toggle P1.5
T0M2Delay();
}
}
Count Calculations
256-23=233
23 x 1.085 μs = 25 μs
25 μs x 250 x 40 = 250 ms by calculation.
Example
Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1,
mode 2 to create the delay.
Solution:
#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-reload)
THI= - 184, //load TH1 (auto-reload value)
TRI=1; //turn on T1
while (TF1==0); //wait for TF1 to roll over
TR1=0; //turn off T1
TF1=0; //clear TF1
}
Count Calculations
1/2500 Hz= 400 μs
400 μs/2 = 200 μs
200 us/1.085 μs = 184
Example
Write a C program for the 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,8-BIT auto-reload
TH1=0XFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1)
{
SBUF='A’; //place value in buffer
while (TI==0);
TI=0;
}
}
Example
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,8-BIT auto-reload
TH1=0xFD; //9600 baud rate
SCON=0x50;
TR1=1; //start timer
while (1)
{
SerTx (Y');
SerTx ('E');
SerTx ('S');
}
}
Example
Write a C program that continuously gets a single bit of data from P1.7 and sends it to
P1.0, while simultaneously creating a square wave of 200 us period on pin P2.5. Use
timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz.
Solution:
#include <reg51.h>
sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;
void main()
{
SW = 1; //make switch input
TMOD = 0x02;
TH0 = 0xA4; //THO= -92
IE= 0x82; //enable interrupts for timer 0
while (1)
{
IND = SW; //send switch to LED
}
}
Unit 2 Question Bank
2marks
(1) Illustrate any two logical operators used in C with their examples (W-19) (S-23)
(2) Develop a 'C' program to transfer the data from Port P0 to Port P1 (W-19)
(3) State any two data types used in C with their ranges (5-22) (W-23)
(4) Illustrate any four data types in Embedded C language with their range in bits and data range (S-23)
4 marks
(1) Write C language program to operate post o and port 2 as output port and port 1 and port a as input
port. (W-19)
(2) 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. (W-19)
(3) Write C program to generate delay of 50msec for microcontroller 89c51 with crystal Frequency =
11.0592MHz (W-19)
(4) Write C program for serial communication to transfer program 10 letter 'M' serially at 9600 baud
rate continuously. (W-19)
(5) Write C program to mask the lower 4 bits of P2 & upper 4 bits of PO using logical operators (S-22)
(6) Write 89c51 c program for multiplication of two 8-bit numbers. (S-22)
(7) Write C program to send character "ESY" serially at 9600 baud rate continuously. Assume crystal
frequency 11.0592MHz (S-22)
(8) Write C program to toggle all bits of port I continuously with 60ms delay in between. Use timer 0 in
mode 1 to generate delay crystal frequency = 11.0592MHz (S-22)
(9) Write C program to toggle all bits of P0, P1, P2, P3 continuously with certain delay.
(W-23)
(10) Write C program to transfer message "MSBTE" serially at 9600 baud rate. Crystal frequency =
11.0592MHz (W-23) (S-23)
(11) Describe how assembly language can be included in 89C51 C program. Give an example. (W-23)
(W-23)
(13) Estimate hex data values of TH0 & TL0 if 89C51 operating at crystal frequency 11.0592MHz & need
to generate delay of 5msec.