0% found this document useful (0 votes)
18 views7 pages

Embedded C Basic

sdgh
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)
18 views7 pages

Embedded C Basic

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

Embedded C Programming:

Embedded System:- a microcontroller or microprocessor based system which is designed to


perform a specific task(e.g. Fire alarm system, Automation systems used in home appliances,
industrial control systems etc.)

Includes three basic components:-

It has hardware.
It has application software.
It has Real Time Operating system (RTOS) that supervises the application software and provide
mechanism to let the
processor run a process as per scheduling by following a plan to control the latencies.
• RTOS defines the way the system works.
• It sets the rules during the execution of application program.
• A small scale embedded system may not have RTOS.

Why C:
Compilers produce hex files that are downloaded into ROM of microcontroller. The assembly language
produces a hex file that is much smaller than C. Assembly language programming is tedious and time
consuming. C is less time consuming and much easier to write.

Advantages of C programming:
a. Easier and less time consuming to write in C than assembly programming.
b. C is easier to modify and update
c. You can use code available in function libraries
d. C code is portable to other microcontroller with little or no modification

Comparison between Assembly and Embedded C

Assembly language:-
● User can control exact instruction and its sequence
● Size of generated hex file is compact
● Tedious and time consuming
● Function library does not exists
● Program modification is complicated
● ASP code is not portable

C Language/ Higher level language:-


● Easier and less time consuming
● Easy to modify and update
● User can use function library
● C code can easily transferable from one controller to another controller
● Hex file size is greater than ALP
Data Types

Various data types of C:

i. Unsigned char

ii. Signed char

iii. Unsigned int

iv. Signed int

v. Sbit (single bit)

vi. Bit and sfr

i. Unsigned char:
Since 8051 is an 8-bit microcontroller, the character data type is the most natural
choice for many applications and most widely used data types for 8051. It is an 8-bit
data type that takes a value in the range of 0 – 255(00H – FFH). It occupies one byte
location in internal RAM. In setting a counter value, to store string of ASCII characters,
where there is no need for signed data, unsigned char should be used instead of
signed char. Default is signed char. „unsigned‟ keyword is used to declare unsigned
char.

E.g. Write an 8051 C program to send values 00 – FF to port P1.


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

Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A,


B, C, and D to port P1.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”; unsigned char z;
for (z=0;z<=10;z++) P1=mynum[z];
}
sbit: This keyword is used to define a special bit from SFR. It helps to set a particular variable to 1 in
the program.

E.g sbit Port1_1 = 0x90;

bdata: The variable chosen for the program will be stored in bit addressable memory of the
microcontroller.
unsigned char bdata v;

code: Its used to store a constant variable like string or large integer numbers in code memory.

unsigned char code str=” compiler program";

xdata: The variable used in the program will be stored in the RAM memory of the controller.

unsigned char xdata k;

_at_: Helps to store a variable used in the program on a location in ram.

unsigned char data i _at_ 0x50;

pdata: This will store the variable in the program in the paged data memory.

unsigned char pdata l;

using: is used to define register bank for a function.


sfr: It is used to define a 8-bit SFR (special function register).

sfr P1 = 0x40;

Pointers in Keil C
Generic and Memory-specific pointers are commonly used as pointers in Keil C.

Generic Pointers

● char *a;
Memory-Specific Pointers

● The memory type and the pointer variable is indicated in the program statement.

char data *c;

Functions
Function Declaration

[Return_type] Function_name ( [Arguments] ) [Memory_model] [reentrant] [interrupt n] [using n]

● Arguments: Arguments passed to function.


● Function_name: Name of function.
● Return_type: It tells about the value type returned from the function.
Time Delay:
There are two ways to create delay:
a. Using simple for loop
b. Using timers

Factors which affect delay using for loop:


1. 8051 uses 12 clock periods per machine cycle. Newer generation of 8051 like DS5000 uses
4 clock periods per machine cycle, DS89C420 uses only one clock periods per machine cycle.
Lesser the number of clock periods per machine cycle, faster the operation of microcontroller, lesser
the delay.
2. The crystal frequency connected to XTAL1 and XTAL2 input pins. The duration of clock
period for machine cycle is a function of crystal frequency.

3. C compiler converts C program to assembly language instructions. Different compilers


produce different code and hence produce different hex code.
Write an 8051 C program to toggle bits of P1 continuously forever with some delay.
Solution:
#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 250 ms.
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++);

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

Solution:

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

for (;;)

{ P1=0x55; P1=0xAA;

Write an 8051 C program to monitor bit P1.5. If it is high, send 55H to P0;
otherwise, send AAH to P2.

Solution:

#include <reg51.h> sbit mybit=P1^5; void main(void)

mybit=1; //make mybit an input while (1)

if (mybit==1) P0=0x55;
else P2=0xAA;

Logic operations in 8051 C:


One of the most important and powerful features of the C language is its ability to perform bit
manipulation.
Byte-wise operators in C:
a. AND (&&),
b. OR (||),
c. NOT (!)
Bit-wise operators in C:
a. AND (&),
b. OR (|),
c. EX-OR (^),
d. Inverter (~),
e. Shift Right (>>),
f. Shift Left (<<)
Bit-wise shift operation in C:
a. Shift Right (>>),
b. Shift Left (<<)

Format: data >> number of bits to be shifted right data << number of bits to be shifted left.

Example: 0x9A >> 3 = 0x13.

These operators are widely used in software engineering for embedded systems and control.

You might also like