Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16...

75
Dr M. Abd El Ghany Eng. Yasmin Adel Microelectronics Lab ELCT 708 Session 1 Introduction to Embedded System & AVR Microcontroller & C Programming Language

Transcript of Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16...

Page 1: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Microelectronics Lab

ELCT 708

Session 1

Introduction to Embedded System

&

AVR Microcontroller

&

C Programming Language

Page 2: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

What is an embedded system

• It is a system that has software embedded in computer hardware.

• The system consists of a small computer that performs a specific task, inputs( sensors/buttons), and outputs(actuators). Depending on these inputs, the small computer decides how the output should be.

Page 3: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Examples

1-calculators ( inputs : buttons, small computer that evaluates mathematical operation results, output : number on LCD screen)

2-coffee machine ( inputs: buttons [type of coffee, sugar, shots], computer that based on the given inputs decided how to control the mechanical part (how long coffee will be injected based on the given number of shots)

3-cars [ input: obstacle detecting sensors, a small computer that if an obstacle if detected stops the car]

Page 4: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Embedded Systems

Characteristics

1. Reliability

2. Efficiency

3. Tightly-constrained

4. Maintainability

Page 5: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Reliability

• An error in an Embedded system application such as a TV remote control or compact disc player will result in a product that does not work and consequently does not sell.

• An error in Embedded system application such as an Antilock Braking System(ABS) or Breaking Assistant(BA) or autopilot could be fatal.

Page 6: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Efficiency

• Considered in real time applications.

• A real time application must be able to act at a speed corresponding with the occurrence of an actual process.

• It must compute certain results in certain real-time without delay.

Page 7: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Tightly-constrained

• Embedded Systems normally come with constraints in hardware resources:

1. Processing(speed)

2. Memory(Data)

3. Storage(Code Size)

• Most of the time it targets real time objectives, this means,

It needs to be fast and efficient(Response Time).

It needs to be predictable (execution time known ahead, and almost constant)

• Power limited(battery operated devices).

• Cost

• Size

Page 8: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Maintainability

• The ability to modify the system after its initial release and enhance its performance like execution time, code and memory size.

Page 9: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Can Personal Computer be considered as an

Embedded System as it integrates hardware

and software to perform functions? Why?

• PC cannot be considered as an embedded system because :

1.It uses a General-Purpose Processor.

2.The system is built independently from the software runs on it.

Page 10: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Micro Processor

• A microcontroller is not the same as a microprocessor.

• A microcontroller is itself a single chip computer system.

• It is a processor along with other several supporting components on one chip .

Microcontroller

• A microprocessor is a single chip CPU used within other

computer systems. • CPU = ALU + Registers + Control unit

Page 11: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

A microcontroller has seven main

components:

1.Central Processing Unit (CPU).

2.Memory Units.

3.Input and Output Ports (GPIO or DIO).

4.Timers.

5.Watch-Dog Timer.

6.Buses.

7.Interrupt Unit.

Page 12: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Microcontroller Vs. Microprocessors

Microcontroller Microprocessor

A processor along with other components on chip(ADC/MEMORY)

A processor on a single chip

Used for a specific task (Coffee machine) Used for General Purpose (Laptops and PCs)

Supplied with pins along each side. The pins presented by a microcontroller are used for power, ground, oscillator, I/O ports, interrupt request signals, reset and control

Pins are most often memory bus signals (rather than I/O ports).

Limited memory

Configurable memory

Small Design Area, consequently less power. Low cost

Larger Design area, power consumption, and higher cost

Slow clock rates(MHz) Fast clock rates (GHz)

Page 13: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Why do we have different

microcontrollers in the market?

• Differences in requirements, make the manufacturers produce different microcontrollers with different memory sizes, number of I/O lines and number of integrated peripheral devices. Other wise they are all similar to use.

Page 14: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

AVR

• AVR developed by Amtel

• AVR derives its name from its developers and stands for

Alf-Eigel Bogen Vegard Wollan RISC microcontroller.

• Also known as Advanced Virtual RISC.

Page 15: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

What is special about AVR?

• They have fast execution time since most of the instructions execute in one clock cycle.

• AVR are about 4 times faster than PICs.

• They consume less power.

Page 16: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Naming Convention

Page 17: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

1. CPU

• CPU is like a brain of the controller which helps in executing a number of instructions. It can handle interrupts, perform calculations and control peripherals with the help of registers. Atmega16 comes with two buses called instruction bus and data bus.

• The CPU reads the instructions in the instruction bus while data bus is used to read or write the corresponding data. The CPU mainly consists of the program counter, general purpose registers, stack pointer, instruction register and an instruction decoder.

Page 18: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

2. ROM

• The controller program is stored in ROM, also known as non-volatile programmable flash memory. The flash memory comes with a resolution of at least 10,000 write/erase cycles. Flash memory is mainly divided into two parts known as Application flash section and booth flash section.

• Program of the controller is stored in the applications flash section. While booth flash section is optimized to work directly when the controller is powered up.

Page 19: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

3. RAM

• The SRAM (static random access memory) is used for storing information temporarily and comes with 8-bit registers. This is just like a regular computer RAM which is used to supply data through the runtime.

Page 20: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

4. Interrupt

• The interrupt is used for an emergency which puts the main function on hold and executes the necessary instructions at that time. Once the interrupt is called and executed the code switches back to the main program.

Page 21: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

5. I/O modules

• Digital I/O modules are used to set a digital communication between the controller and external devices. While analog I/O modules are used for transferring analog information. Analog comparators and ADC fall under the category of analog I/O modules.

Page 22: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

6. Timers

• Timers are used for calculating the internal signal within the controller. Atmega16 comes with two 8-bit timers and one 16-bit timer. All these timers work as a counter when they are optimized for external signals.

7. Watchdog timer

The watchdog timer is a remarkable addition in this controller which is used to generate the interrupt and reset the timer. It comes with 128kHz distinct CLK source.

Page 23: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

8. Serial Communication

• Atmega16 comes with USART and SPI units that are used for developing serial communication with the external devices.

9. Analog Comparator

• It compares the input values of two analog signals.

Page 24: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

ATmega16 Architecture

10. Internal Calibrated Oscillator

• It is equipped with an internal oscillator for driving its clock. By default ATmega16 is set to operate at internal calibrated oscillator of 1Mhz.

• The maximum frequency of internal oscillator is 8 Mhz.

• ATmega16 can be operated using an external crystal oscillator with a maximum frequency of 16Mhz.

Page 25: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pin Configuration

Page 26: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

AVR ATmega16

Page 27: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Assembly Language

Page 28: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Why do use C ?

• Each microcontroller has an individual instruction set and assembly language.

Modifying assembly language code so a program written for one

microcontroller will run on a different microcontroller is very time

consuming and effort intensive.

• Writing C code that supports general microcontroller features helps to avoid

portability problems.

• Using C library functions and header files ensures that application source code can be recompiled for different microcontroller targets.

Page 29: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Why do use C ?

• C is a high level language. You will be able to program your applications quickly.

• C’s breadth of expression is concise and powerful; therefore,

each line of code written in C can replace many lines of assembly

language.

• Debugging and maintaining code written in C is much easier than in assembly language code.

Page 31: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Page 32: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Compiler

• The compiler converts code written in a human-readable programming language into assembly code.

• GNU compiler collection or GCC is the most widely used C compiler.

• The linker is a computer program that takes one or more object files

generated by a compiler and combines them into one, executable program.

Linker

Assembler

• The assembler converts the assembly code into a machine code representation which is understood by your processor. This step creates object files.

Page 33: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Compiler vs Assembler vs Linker

Page 34: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Linker VS Compiler

Page 35: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Executable File

• This is a set of instructions (machine code).

• It is used by the microcontroller to run its specific function.

Page 36: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Page 37: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Types Bits Bytes Range

Signed Char 8 1 -128 to +127

Unsigned char 8 1 0 to 255

Int 32 4 -2,147,483,648 to 2,147,483,647

Long 32 4 -2,147,483,648 to 2,147,483,647

Unsigned long 32 4 0 to 4,294,697,295

Long long 64 8 -(2^63) to (2^63)-1

Unsigned long long 64 8 0 to 2*((2^63)-1)

Short int 16 2 -32,768 to 32,767

Unsigned short 16 2 0 to 65,535

Float 32 4 3.4E-38 to 3.4 E+38 (fraction)

Double 64 8 1.7E-308 to 1.7E+308 (fraction)

Long double 80 10 3.4E-4932 to 1.1 E+4932 (fraction)

Page 38: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Printf

Page 39: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Scanf

Page 40: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Array

1. Array declaration by specifying size:

• Int arr1[10];

2. Array declaration by initializing elements:

• Int arr2[]={10,20,30};

3. Array declaration by specifying size and initializing elements:

• Int arr3[2]={10,20};

Page 41: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Accessing Array Elements:

Page 42: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Types of Operators

1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increment and decrement Operators 6. Conditional Operators 7. Bitwise Operators 8. Special Operators

Page 43: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

1. Arithmetic Operators

Page 44: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

2. Relational Operators

Page 45: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

3. Logical Operators

Page 46: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

4. Assignment Operators

Page 47: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

5.Increment and decrement

Operators

Increment prefix ++i

Increment postfix i++

Decrement prefix --i

Decrement postfix i--

Page 48: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

6. Conditional Operators

Page 49: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

7. Bitwise Operators

Operator Use Example

& Bitwise AND 101&001=001

| Bitwise OR 110 |101=111

<< Left shift 110<<1=100

>> Right shift 110>>2=001

~ Ones complement ~110=001

Page 50: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

8. Special Operator

Page 51: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

if-else-if ladder

Page 52: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

For loop

Page 53: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

While

Page 54: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Do while

Page 55: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Switch

Page 56: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Function

Name Definition Example in code

Parameter Names inside function definition

x,y

Argument Names inside function call

a,b

Page 57: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Arguments

• Arguments are passed by value.

• Any changes inside the parameters doesn’t effect the arguments.

Page 58: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Question

What will happen to the values of i and d ?

Page 59: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Memory

• Main Memory is divided into bytes, with each byte capable of storing eight bits of information.

• If there are n bytes in memory, we can

think of addresses as numbers that range from 0 to n-1.

Page 60: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Memory

• Each variable occupies one or more bytes of the memory ; the address of the first byte is said to be the address of the variable.

• The variable I occupies the bytes at addresses 2000 and 2001, so I’s address is 2000:

Page 61: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pointers

• Pointer Variable is a variable that store an address.

• Pointer variable P stores the address of variable i.

Page 62: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pointer Declaration

• A pointer variable must be preceded by an asterisk:

int *p; (it points nowhere)

• This declaration means that p is a pointer variable capable of pointing to objects of type int.

Page 63: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pointers Operators

• Address Operator (&)

• Indirection Operator (*)

Page 64: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Address Operator (&)

1. int i,*p; p=&i;

2. int i;

int *p=&i;

3. int i, *p=&i;

Page 65: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Indirection Operator(*)

• Once a pointer variable points to an object, we can use (*) to access what is stored in the object.

• If p points to I we can print the value of i as

printf(“%d\n”,*p) ;

• Changing the value of p changes the value of I

• Changing the value of I changes the value of p

Page 66: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Indirection Operator(*)

Page 67: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Wrong Assignments

1. Int *p;

Printf(“%d”,*p);

2. Int *p;

*p=1;

Page 68: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pointer Assignment

Int i, j, *p, *q

p=&i;

q = p;

Page 69: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pointer Assignment

• The first lines of code are pointer assignment while the second is not.

• *q=*p means that copy the value of p to q.

Page 70: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Question

int a=5;

int *b=&a;

int c=*b;

a=7;

Printf(“%d\n%d\n”,*b,c);

Page 71: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Results

• *b=7

• C=5

Page 72: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Pass by Reference

Page 73: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Assignment 1

• You have to complete the function void update(int *a, int *b), which reads two integers as argument, and sets a with the sum of them, and b with the absolute difference of them.

• A=a+b

• B=|a-b|

• Input Format

• Input will contain two integers a, and b , separated by a newline.

• Output Format

• You have to print the updated value of a and b, on two different lines. You only have to complete the void update(int *a,int *b) function.

Page 74: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Assignment 2

• Write a c function for swapping of two arrays of the same dimensions.

• In the main function test your code by declaring 2 arrays of size 5 and print the output of each array on the console.

Page 75: Microelectronics Lab ELCT 708 - German University in Cairo Microelectronic… · ATmega16 Architecture 8. Serial Communication •Atmega16 comes with USART and SPI units that are

Dr M. Abd El Ghany Eng. Yasmin Adel

Assignment 3

• Write a function to print half pyramid using *. The user give the number of rows use scanf and print the output on the console.

*

**

***

****

*****