0% found this document useful (0 votes)
17 views

ESY_Unit 2

Uploaded by

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

ESY_Unit 2

Uploaded by

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

Unit II - Programming using Embedded C

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

1. Basic 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.

2. Derived Data Types:


o Arrays: Contiguous memory locations used to store multiple values of the
same data type.
o Pointers: Variables that store memory addresses. They are essential for
accessing specific memory locations or for passing addresses of variables to
functions.
oStructures (structs): Composite data types used to group variables of
different data types under a single name. Useful for organizing related data.
3. Enumerated Types (enum):
o Used to define custom data types that can take on a set of named values
called enumerators. Enums are helpful for improving code readability and
maintainability.

Data Type Size in Bits Data Range


unsigned char 8 - bit 0 to 255
signed char 8 - bit -128 to +127
unsigned int 16 - bit 0 to 65535
signed int 16 - bit -32768 to +32767
sbit 1 - bit SRF bit addressable
bit 1 - bit RAM bit addressable
sfr 8 - bit RAM address 80H – FFH only

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

(1) Write an 8051 c program to send values 00-FFh to port Pl.


#include <reg51.h>
void main (void)
{
unsigned char z;
for (z=0; z <= 255; z++)
P1=z;
}

(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];

(3) Write an 8051 C program to toggle all the bits of P1 continuously.

#include <reg51.h>
void main (void)
{
for (;;)
{
P1 = 0x55;
P1 = 0xAA;
}
}

(4) Write 8051 c program to toggle D0 of the port P1 5oooo times.


#include <reg51.h>
sbit MYBIT=P1^0;
void main (void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT = 1;
MYBIT = 0;
}
}
(5) Write C program to toggle bits of Pl continuously forever with some delay.
#include<reg51.h>
void main (void)
{
unsigned int x;
for (;;)
{
P1 = 0x55;
For (x=0; x <= 50000; x++)
P1 = 0xAA;
for (x=0; x < = 50000;x++)
}
}

(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

#include <reg51.h> // Include 89C51 register definitions

void delay(unsigned int count) {


unsigned int i, j;
for (i = 0; i < count; i++)
for (j = 0; j < 500; j++); // Adjust this delay for your specific clock frequency
}

void main() {
P1 = 0x00; // Initialize P1 to 0x00 (all bits low)

while (1) {
P1 = ~P1; // Toggle all bits of P1

delay(1000); // Delay for a while (adjust as needed)


}
}

Program: Write an 89C51 embedded C program to toggle all bits of P0,P1,P2,P3


continuously.

#include <reg51.h> // Include 89C51 register definitions

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

// Add suitable delay to control the toggle speed


// Adjust the delay value to control the speed of toggling
for (unsigned int i = 0; i < 50000; i++);
}
}
Program: Write an 89C51 embedded C program to mask the upper four bits of the
data given in port 0 and write the answer in port 1.

#include <reg51.h> // Include 89C51 register definitions

void main() {
unsigned char data_in, data_out;

while (1) {
data_in = P0; // Read data from Port 0

// Mask upper four bits (retain lower 4 bits)


data_out = data_in & 0x0F;

P1 = data_out; // Write masked data to Port 1


}
}

Program: Write an 89C51 embedded C program for multiplication of two 8 bit


numbers.

#include <reg51.h> // Include 89C51 register definitions

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)

result = 0; // Initialize result to 0

for (i = 0; i < num2; i++) {


result += num1; // Perform repeated addition
}

// 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.

#include <reg51.h> // Include 89C51 register definitions

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

// Mask lower 4 bits of P2


masked_p2 = data_p2 & 0xF0;

// Mask upper 4 bits of P0


masked_p0 = data_p0 & 0x0F;

// Combine masked results (if necessary)


// For example, if you want to combine masked_p2 and masked_p0 into P1:
// P1 = masked_p2 | masked_p0;

// Alternatively, you can output each masked result separately:


P1 = masked_p2; // Output masked P2 to Port 1
P3 = masked_p0; // Output masked P0 to Port 3

// Add suitable delay if needed


}
}
2.2 Timer/Counter program using embedded C for given microcontroller

Timer 0 & 1 delay using mode 1 (16-bit non-auto-reload)

Finding values to be loaded into the timer


Assuming that we know the amount of timer delay we need, the question is how to find the
values needed for the TH, TL registers. To calculate the values to be loaded into the TL and
TH registers where we use crystal frequency of 11.0592 MHz for the 8051 system.
Assuming XTAL = 11.0592 MHz we can use the following steps for finding the TH, TL
registers values.
1. Divide the desired time delay by 1.085 μs.
2. Perform 65536-n, where n is the decimal value we got in Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into
the timer's registers.
4. Set TL = xx and TH = yy.

Timer 0 & 1 delay using mode 1 (16-bit non-auto-reload)

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

Timer 0 & 1 delay using mode 2 (8-bit auto-reload)

Finding values to be loaded into the timer


Assuming that we know the amount of timer delay we need, the question is how to find the
values needed for the TH, TL registers. To calculate the values to be loaded into the TL and
TH registers where we use crystal frequency of 11.0592 MHz for the 8051 system.
Assuming XTAL = 11.0592 MHz we can use the following steps for finding the TH registers
value.
1. Divide the desired time delay by 1.085 μs.
2. Perform 256-n, where n is the decimal value we got in Step 1.
3. Convert the result of Step 2 to hex, where xx is the initial hex value to be loaded into the
timer's registers.
4. Set TH = xx.

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:

Timer 0 & 1 delay using mode 2 (8-bit auto-reload)

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


for (y=0;y=40;y++)

T0M2Delay();
}
}

void T0M2Delay (void)


{
TMOD=0x02; //Timer 0, mode 2 (8-bit auto-reload)
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
}

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:

Timer 0 & 1 delay using mode 2 (8-bit auto-reload)

#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

2.3 Serial communication program using embedded C for given microcontroller

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');
}
}

void SerTx (unsigned char x)


}
SBUF=x; //place value in buffer
while (TI==0); //wait until transmitted
TI=0;
}

2.4 Interrupt control program with embedded C for given microcontroller

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:

We will use timer 0 in mode 2 (auto-reload).


One half of the period is 100 us.
100 μs /1.085 μs = 92, and TH0= 256-92 = 164 or A4H

#include <reg51.h>
sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;

void timer0 (void) interrupt 1


{
WAVE = -WAVE; //toggle pin
}

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)

(12) Find the contents of port after execution of following code

(i) P2 = 0x74 >>3;


(ii) P3 = 0x74| 0x68,

(W-23)

(13) Estimate hex data values of TH0 & TL0 if 89C51 operating at crystal frequency 11.0592MHz & need
to generate delay of 5msec.

You might also like