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

2.2. Embedded C Programming New

5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx

Uploaded by

22z139
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 views36 pages

2.2. Embedded C Programming New

5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx5.1 Complete Design of Embedded Systems.pptx

Uploaded by

22z139
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/ 36

EMBEDDED SYSTEMS AND IoT UNIT 2

CS3691 Embedded Systems and Internet of


Things

2.2 Programming Embedded Systems in C 1


EMBEDDED SYSTEMS AND IoT UNIT 2

UNIT 2

2.1 Memory and I/O Devices Interfacing

2.2 Programming Embedded Systems in C

2.3 Need For RTOS

2.4 Multiple Tasks and Processes and Context Switching

2.5 Priority Based Scheduling Policies

2.2 Programming Embedded Systems in C 2


EMBEDDED SYSTEMS AND IoT UNIT 2

Programming in Embedded C

2.2 Programming Embedded Systems in C 3


EMBEDDED SYSTEMS AND IoT UNIT 2

Embedded C Programming
Embedded C is a programming language that is used in
the development of Embedded Systems.

Embedded C is used to program a wide range of


microcontrollers and microprocessors.

2.2 Programming Embedded Systems in C 4


EMBEDDED SYSTEMS AND IoT UNIT 2

C Vs. Embedded C
C Embedded C

C is a general-purpose programming Embedded C is an extension of C


language that can be used to write any programming language to write programs
type of programs. for embedded systems.

Different embedded systems provide


The executable file generated by C
different embedded C compiler with their
compiler is hardware independent as
own libraries, and their generated code
long as the OS is same.
can only be run in the same hardware.

Embedded C programs doesn't require


C programs generally run on top of an traditional OS. They run on embedded
operating system. systems hardware with minimal kernels
or RTOS.

It is used in wide variety of software It is used to develop firmware for


applications, games, etc. embedded systems, IoT devices, etc.

2.2 Programming Embedded Systems in C 5


EMBEDDED SYSTEMS AND IoT UNIT 2

WHY ❑ C programming is less time consuming, but has


PROGRAM larger hex file size
8051 IN C ❑ The reasons for writing programs in C

It is easier and less time consuming to write


in C than Assembly
C is easier to modify and update
You can use code available in function
libraries
C code is portable to other microcontroller with
little of no modification
❑ Compilers produce hex files that is downloaded
to ROM of microcontroller
The size of hex file is the main concern
▪ Microcontrollers have limited on-chip
ROM
▪ Code space for 8051 is limited to 64K
bytes
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2

DATA TYPES
❑ A good understanding of C data types for 8051
can help programmers to create smaller hex
files
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2

DATA TYPES ❑ The character data type is the


Unsigned char
most natural choice
8051 is an 8-bit microcontroller

❑ Unsigned char is an 8-bit data type


in the range of 0 – 255 (00 – FFH)
One of the most widely used data types
for the 8051
▪ Counter value
▪ ASCII characters

❑ C compilers use the signed char as


the default if we do not put the
keyword unsigned

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2

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;
}
2.2 Programming Embedded Systems in C 9
EMBEDDED SYSTEMS AND IoT UNIT 2

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];
}
2.2 Programming Embedded Systems in C 10
EMBEDDED SYSTEMS AND IoT UNIT 2

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


Solution:
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
p1=0x55;
p1=0xAA;
}
}

2.2 Programming Embedded Systems in C 11


EMBEDDED SYSTEMS AND IoT UNIT 2
❑ The signed char is an 8-bit data type
Use the MSB D7 to represent – or +
Give us values from –128 to +127
❑ We should stick with the unsigned char unless the data
DATA TYPES
needs to be represented as signed numbers
Signed char temperature
Write an 8051 C program to send values of –4 to +4 to port P1.
Solution:
//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2

❑ The unsigned int is a 16-bit data type


DATA TYPES
Takes a value in the range of 0 to 65535
Unsigned and (0000 – FFFFH)
Signed int Define 16-bit variables such as memory
addresses
Set counter values of more than 256
Since registers and memory accesses are
in 8-bit chunks, the misuse of int variables
will result in a larger hex file
❑ Signed int is a 16-bit data type
Use the MSB D15 to represent – or +
We have 15 bits for the magnitude of the
number from –32768 to +32767

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2
Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
DATA TYPES
Solution:
Single Bit
#include (cont’)
<reg51.h>

sbit MYBIT=P1^0; sbit keyword allows access to the


single bits of the SFR registers

void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2

❑ The bit data type allows access to single bits


of bit-addressable memory spaces 20 – 2FH
DATA TYPES ❑ To access the byte-size SFR registers, we
Bit and sfr use the sfr data type
Data Type Size in Bits Data Range/Usage
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 SFR bit-addressable only
bit 1-bit RAM bit-addressable only
sfr 8-bit RAM addresses 80 – FFH
only

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2

❑ There are two way s to create a time delay in


8051 C
TIME DELAY Using the 8051 timer (Chap. 9)
Using a simple for loop
be mindful of three factors that can affect the accuracy
of the delay
▪ The 8051 design
– The number of machine cycle
– The number of clock periods per machine cycle
▪ The crystal frequency connected to the X1 – X2 input pins
▪ Compiler choice
– C compiler converts the C statements and functions to
Assembly language instructions
– Different compilers produce different code

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2
Write an 8051 C program to toggle bits of P1 continuously forever with some
delay.
Solution:
//Toggle P1 forever with some delay in between
TIME DELAY //“on” and “off”
(cont’) #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++);
}
}
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2
Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
TIME DELAY {
(cont’) while (1)
{
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++);
}
2.2 Programming Embedded Systems in C
EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 21


EMBEDDED SYSTEMS AND IoT UNIT 2

1. Write a Program to read the number 1 from port 1,


number 2 from port 2 , then add them ,store the result
,send it to Port 3.

2.2 Programming Embedded Systems in C 22


EMBEDDED SYSTEMS AND IoT UNIT 2

1. Write a Program to read the number 1 from port 1,


number 2 from port 2 , then add them ,store the result ,send
it to Port 3.

#include<reg.51.h>
void main()
{
unsigned char a,b,c ;
P1 = 0XFF ; //make port 1 as input port
P2 = 0XFF ; //make port 2 as input port
a=P1;
b=P2;
c= a+b ;
P3= c;
}

2.2 Programming Embedded Systems in C 23


EMBEDDED SYSTEMS AND IoT UNIT 2

Write a program to transfer the data from


port P0 to port P1.

2.2 Programming Embedded Systems in C 24


EMBEDDED SYSTEMS AND IoT UNIT 2

Write a program to transfer the data from


port P0 to port P1.
#include<reg51.h>
void main (void )
{
unsigned char X;
P0=0XFF; // P0 as input port
P1=0X00; // P1 as output port
while(1)
{
X = P0; // read port0
P1 = X; // output data to port1
}

2.2 Programming Embedded Systems in C 25


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 26


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 27


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 28


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 29


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 30


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 31


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 32


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 33


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 34


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 35


EMBEDDED SYSTEMS AND IoT UNIT 2

2.2 Programming Embedded Systems in C 36

You might also like