Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

Embedded

PLAN

C programming

1

C Revision

Why C?

why we are using C?

Portable , more readable

1

Memory allocation , When dealing with the hardware

2

Allow a lot of features

3

Compilation process

Compilation process

what will be the output?

what will be the output of this program?

Program 3.

Program 2.

Program 1.

#include <stdio.h>

int main()

{

void foo();

printf("1 ");

foo();

}

void foo()

{

printf("2 ");

}

#include <stdio.h>

int main()

{

void foo();

void f()

{

foo();

}

f();

}

void foo()

{

printf("2 ");

}

#include <stdio.h>

int main()

{

int x=123;

int i={

printf("c" "++")

};

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

printf("%x ",x);

}

return 0;

}

what are pointers?

- A pointer is a variable that holds a memory address.

- This address is the location of another object (typically another variable) in memory.

For example, if one variable contains the address of another variable, the first variable is said to point to the second.

2

C pointers

Pointers

Some pointers Deceleration:

int *ip; /* pointer to an integer */

double *dp; /* pointer to a double */

float *fp; /* pointer to a float */

char *ch /* pointer to a character */

How to Use Pointers?

How to use pointers ?

#include <stdio.h>

int main () {

int var = 20; /* actual variable declaration */

int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */

printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */

printf("Value of *ip variable: %d\n", *ip );

return 0;

}

why pointers?

why do we need pointers?

Allows dynamic memory allocation

Make it more simple when using Data structure

Saving memory size

Pointers types

Pointers types:

Pointer to a variable

Pointer to a function

Pointer to structure

Pointer to array

Pointer arithmetic

- A pointer in c is an address, which is a numeric value.

- Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value.

- There are four arithmetic operators that can be used on pointers: ++, --, +, and -

3

Pointers cont..

Incrementing a Pointer

Incrementing a Pointer.

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};

int i, *ptr;

/* let us have array address in pointer */

ptr = var;

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

printf("Address of var[%d] = %x\n", i, ptr );

printf("Value of var[%d] = %d\n", i, *ptr );

/* move to the next location */

ptr++;

}

return 0;

}

Decrementing a Pointer

Decrementing a Pointer

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};

int i, *ptr;

/* let us have array address in pointer */

ptr = &var[MAX-1];

for ( i = MAX; i > 0; i--) {

printf("Address of var[%d] = %x\n", i-1, ptr );

printf("Value of var[%d] = %d\n", i-1, *ptr );

/* move to the previous location */

ptr--;

}

return 0;

}

what is the output?

What is the output of these programs?

Program 1.

Program 2.

Program 3.

#include <stdio.h>

int main()

{

int *ptr, a = 10;

ptr = &a;

*ptr += 1;

printf("%d,%d\n", *ptr, a);

}

Write a program in C to print the elements of an array in reverse order.

Test Data :

Input the number of elements to store in the array (max 15) : 5

Input 5 number of elements in the array :

element - 1 : 2

element - 2 : 3

element - 3 : 4

element - 4 : 5

element - 5 : 6

#include <stdio.h>

int main()

{

char *p = NULL;

char *q = 0;

if (p)

printf(" p ");

else

printf("nullp");

if (q)

printf("q\n");

else

printf(" nullq\n");

}

C Bit Masking

- A mask is a bit string that "fits over" another bit string and produces a desired result.

- The idea for bit masking is based on boolean logic

4

C Bit Masking

Binary numbers

Binary numbers

program 2

program 1

#include <stdio.h>

int main()

{

int x=0b00001000;

printf("%x ",x);

return 0;

}

#include <stdio.h>

int main()

{

int x=0b00001000;

int y=0b00000100;

int z=x&y;

printf("%x ",x);

printf("%x ",y);

printf("%x ",z);

return 0;

}

Logic operations

We are concerned with the following operations:

1

NOT a - the final value is the opposite of the input value (1 -> 0, 0 -> 1)

2

a AND b - if both values are 1, the final value is 1, otherwise the final value is 0

3

a OR b - if either value is 1, the final value is 1, otherwise the final value is 0

4

a XOR b - if one value is 1 and the other value is 0, the final value is 1, otherwise the final value is 0

Bitwise Left Shift Operator in C

Left shift operator.

- It is denoted by <<

- Bit Pattern of the data can be shifted by specified number of Positions to Left

- When Data is Shifted Left , trailing zero’s are filled with zero

Syntax : Bitwise Left Shift Operator

[variable]<<[number of places]

#include<stdio.h>

int main()

{

int a = 60;

printf("\nNumber is Shifted By 1 Bit : %d",a << 1);

printf("\nNumber is Shifted By 2 Bits : %d",a << 2);

printf("\nNumber is Shifted By 3 Bits : %d",a << 3);

return(0);

}

Masking Operation

Masking Operations:

Clearing bit n

Flipping bit n

Setting bit n

Flipping bit n is the result of XORing the value of the storage variable with 2^n:

storage ^= 1 << n;

01000010 01001010

XOR XOR

00001000 00001000

== ==

01001010 01000010

Clearing bit n is the result of ANDing the value of the storage variable with the inverse (NOT) of the value 2^n:

storage &= ~(1 << n);

Here's the example again:

01001010

AND

11110111

==

01000010

Setting bit n is as simple as ORing the value of the storage variable with the value 2^n.

storage |= 1 << n;

As an example, here is the setting of bit 3 where storage is a char (8 bits):

01000010 OR 00001000 == 01001010

The 2^n logic places the '1' value at the proper bit in the mask itself, allowing access to that same bit in the storage variable.

Some tricks

Some Tricks:

Trick 1:

Trick 2:

Trick 3:

Clear all bits from LSB to ith bit

Upper case English alphabet to lower case

ch |= ' ';

Ex:

ch = ‘A’ (01000001)

mask = ‘ ‘ (00100000)

ch | mask = ‘a’ (01100001)

mask = (1 << i) - 1;

x &= mask;

Ex:

x = 215 (11010111) and we want to clear MSB to 4th bit, total 4 bits

mask -> 1 << 4 -> 16(00010000)

mask -> 16 – 1 -> 15(00001111)

x & mask -> 7(00000111)

mask = ~((1 << i+1 ) - 1);

x &= mask;

Ex:

x = 29 (00011101) and we want to clear LSB to 3rd bit, total 4 bits

mask -> (1 << 4) -> 16(00010000)

mask -> 16 – 1 -> 15(00001111)

mask -> ~mask -> 11110000

x & mask -> 16 (00010000)

What is the embedded systems?

- The Word embedded means inside some thing.

- The other word which is systems means all things that we use

- So embedded Systems inside the whole of our life

5

Embedded Systems

Basic Computer System

Basic Computer system.

system component

System component

Memory

I/O unit

CPU

- Central processing unit

- The brain of the system

- allow arithmetic operations

- and Logic operations

- The way of interfacing with the out door world

- input methods like mouse , keyboard and output methods like screen , printer.

- physical devices used to store data or programs on a temporary or permanent basis for use in an electronic digital computer.

- There are two basic types of Memories (RAM & ROM)

- one permanent and the other is temporary .

Memory types

Memory Types:

Memory types cont..

Memory types:

DRAM

SRAM

EPROM

PROM

- This is the type of memory that can be reprogrammed. We can erase data from it and reprogram it that is erase all the previous data by using high voltage Ultraviolet light.

- It is required to erase each cell in EPROM.

- It is made up of capacitors and transistors

-As compared with other RAM's it is less expensive.

-In DRAM, data is written at the byte-level and it reads data at the multiple-byte page level.

-DRAM requires less power than other RAMs.

- Developers created a type of ROM known as programmable read-only memory (PROM) because Creating ROM chips from scratch are time-consuming and very expensive.

- It can be coded by the user. Once coded, the data and instructions in it cannot be changed.

- It is used to store permanent data in digital electronic devices.

- It can be bought at a low cost as compared to other RAMs.

- It holds data in a static form, that is, as long as the memory has the power as the dynamic RAM, it is not needed to refresh it again and again

-Static RAM provides faster access to data and is more expensive than DRAM as each cell must contain multiple transistors.

-SRAM does not use capacitors.

-SRAM is also highly recommended for use in PCs, peripheral equipment, printers, LCD screens, hard disk buffers, router buffers and buffers in CDROM / CDRW drives

Microcontroller

VS

Microprocessor

6

Microcontroller VS

Microprocessor

MicroController

Microcontroller

A Microcontroller is basically a computer on a chip.

1

2

It differs form normal desktop or laptop computers in that a microcontroller is an application specific computer that usually runsa single program performing dedicated task(s)

3

A microcontroller contains on chip CPU, input/output interface, memory, clock, timer, and an assortment of other peripherals.

Microprocessor

Microprocessor

A Microprocessor on the other hand is just a CPU

1

2

one has to add externally memory, clock, input/output interfaces, timer and all other needed peripheral.

This is the reason a microprocessor has so many pins.

3

Instruction life cycle

Instruction life cycle.

Von Neumann Vs Harvard

7

Microcontroller Architecture

Von Neumann

Von Neumann

1

Earlier microprocessors made use of Von Neumann architecture where the data and instructions (programs) are stored in the same memory.

Even though this architecture is simple, there are many draw backs.

2

3

One of the major drawbacks is that instruction and data cannot be accessed at the same time as they share a single data bus.

Harvard

Harvard

1

Later, Harvard architecture is introduced which makes use of separate program and data memories with separate buses so that both data and

instructions can be accessed at the same time.

CISC & RISC

8

Reduced Set Instruction Set Architecture (RISC)

- The main idea behind is to make hardware simpler by using an instruction set composed of a few basic steps for loading, evaluating and storing operations just like a load command will load data, store command will store the data.

Complex Instruction Set Architecture (CISC)

The main idea is that a single instruction will do all loading, evaluating and storing operations just like a multiplication command will do stuff like loading data, evaluating and storing it, hence it’s complex.

CISC & RISC

CISC

Characteristic of CISC:

Complex instruction, hence complex instruction decoding.

1

Instruction are larger than one word size.

2

Instruction may take more than single clock cycle to get executed.

3

4

Less number of general purpose register as operation get performed in memory itself.

Complex Addressing Modes.

5

RISC

Characteristic of RISC:

Simpler instruction, hence simple instruction decoding.

1

Instruction come under size of one word.

2

Instruction take single clock cycle to get executed.

3

More number of general purpose register.

4

Simple Addressing Modes.

5

Pipeling can be achieved.

6

9

End

Thanks

Learn more about creating dynamic, engaging presentations with Prezi