Module-4
Module-4
TE(ETRX) SEM-V
Microcontrollers & Applications
By
Dr. Arun Chavan
What is Embedded C ?
Bits
Data Type Range
(Bytes)
bit 1 0 or 1 (bit addressable RAM)
signed int 16 (2) -32768 to +32767
Modifiers in Embedded C
These are keywords in C to modify the default properties of int and char data
types. There are 4 modifiers in C as follows.
1. Short (2 Bytes)
It limits user to store small integer values from -32768 to +32767. It can be used
only on int data type.
3. signed
It is default modifier of int and char data type if no modifier is specified. It says that
user can store negative and positive values.
4.unsigned
When user intends to store only positive values in the given data type (int and char).
Qualifiers in Embedded C
Two types of qualifiers available in C , 'const' and 'volatile'.
'Const' qualifier will impose a restriction on the variable, such a way that its value
can't be changed or modified. The main use of 'const' is to define constants in your
program; so that it's value can't be changed.
Please take a look at the example given below to understand the use case of
'const' qualifier.
For example:
#include <stdio.h>
// PI is defined as 'const' so that you may not change it's value accidentally.
Qualifiers in Embedded C
If a variable is declared as 'volatile', its value can be changed from outside the
program.
Let's look at it with the help of an example using 'volatile' keyword.
For example:
#include <stdio.h>
volatile int signal = 0;
{
while (signal == 0)
{
}
return 1;
} Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Macros are pre-processed which means that all the macros would be processed
before your program compiles. However, functions are not pre-processed but
compiled.
Macro Function
Macro Function
Macros are useful when small code is Functions are useful when large code
repeated many times is to be written
};
#include<reg 51.h>
void main()
{
int x;
while (1)
{
P2=0x00;
for (x=0;x<=5000;x++) //software delay
P2=0xff;
for (x=0;x<=5000;x++) //software delay
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to toggle pin 1 of Port 1 continuously
without affecting other pins of Port 1
#include<reg 51.h>
sbit mybit=P1^1; // Pin 1 of Port 1 named as mybit
void main()
{
int x;
while (1)
{
mybit=0; //clear pin 1 of port 1
for (x=0;x<=5000;x++) //software delay
mybit=1; //set pin 1 of port 1
for (x=0;x<=5000;x++) //software delay
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to monitor Port 1 pin 0, if it is high then make Port 1 =
00H, else make Port 2=00H
#include<reg 51.h>
sbit mybit=P1^0; // Pin 0 of Port 1 named as mybit
void main()
{
while (1)
{
if (mybit==1) // check if P1.0=1
{
P1=0x00; //if yes P1=00h
}
else // otherwise
{
P2=0x00H; //P2=00h
}
}
}
#include<reg 51.h>
void main()
{
unsigned char data1 = 0x36;
unsigned char data2 = 0x38;
P0 = (data2 & 0x0f) + ((data1 & 0x0f) << 4);
while (1)
}
#include<reg 51.h>
sbit door=P1^1; // Pin 1 of Port 1 named as door
sbit alarm=P1^7; // Pin 7 of Port 1 named as alarm
void main()
{
int x;
alarm = 0;
while (1)
{
if (door==1) // checking P1.1 pin
{
alarm = 1;
for (x=0;x<=5000;x++) // software delay
alarm = 0;
}
} Dr. Arun Chavan, Department of Electronics
} and Computer Science
8051 Programming in Embedded C
Program for LED Blinking using 8051 Microcontroller
#include<reg51.h> #include<reg51.h>
void delay() void delay()
{ {
TMOD=0x10; int count=0;
TL1=0x0c; while(count!=20);
TH1=0xfe; {
TR1=1; TMOD=0x01;
while (TF1==0); TL0=0xb0;
TR1=0; TH0=0x3c;
TF1=0; TR0=1;
} while(TF0==0);
TR0=0;
TF0=0;
count++;
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
WAP to transmit the character ‘S’ to the serial port, use 9600 as the baud rate
(assume the oscillator frequency as 12 MHz)?
#include<reg51.h>
void main()
{
while(1)
{
SCON=0x40; //program the serial port//
PCON=0x00; // program SMOD bit = 0 //
TMOD=0x20; //program timer 1 in mode 2//
TL1=0xfd; // load the count for 9600 baud//
TH1=0xfd; //load the count for 9600 baud//
TR1=1; //timer 1 ON//
SBUF=’S’; //write the character in the register//
while(TI==0); //check TI for ‘Transmit Interrupt’//
TI=0; // reset TI when found set//
TR1=0; //timer 1 OFF//
}
} Dr. Arun Chavan, Department of Electronics
and Computer Science
Assignment no - 7
Short Questions (2 Marks)
1. Write Embedded C program for LED blinking at the regular interval of 2 secs. Assume the
oscillator frequency as 12 MHz and LEDs connected to Port 1.
2. Write Embedded C program to transmit character ‘A’ through the serial port at the baud
rate of 2400. Assume the oscillator frequency as 12 MHz.
Thank You