0% found this document useful (0 votes)
36 views28 pages

Lab1 Cce Adv C

embedded systems course labs, advanced C, by Eng. Omar Wahba, Mansoura University

Uploaded by

danabelal
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)
36 views28 pages

Lab1 Cce Adv C

embedded systems course labs, advanced C, by Eng. Omar Wahba, Mansoura University

Uploaded by

danabelal
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/ 28

Faculty of Engineering

Mansoura university
Computer Control Engineering department

Embedded Systems
Lab (1)

Advanced C programming

By / Omar Mahmoud Wahba


TA at CSED – FOE - MU
1
Course Overview

2
what’s embedded system.

Indoor Outdoor Medical


Entertainment Security field
So Embedded system is:
It’s a Combination of software and hardware that performs a
specific task.
3
Course Objectives
• Understanding microprocessors/microcontroller
concepts.
Hardware
• Basic micro controllers architecture.
• Microcontrollers internal peripherals.
• Basic electronics needed to communicate with
the environment.

4
Course Objectives
• Understanding embedded system software Software
limitations.
• Adapt C language to be used in embedded
context.
• Embedded C techniques.
• Writing peripherals drivers.
• Learn basic embedded software architecture.

5
Course overview Embedded
Concept

• Learn more advanced • Learn embedded C


• Interfacing with PIC:
tools in C language: technique:
• GPIO.
• Pointers. • Preprocessing.
• 7 segment + LCD
• Functions. • Compilation process.
• Keypad.
• C file system. • Optimization of
• Interrupts
speed and memory.
• Peripherals drivers.
6
Course Grading
• You will be graded for

Assignments
micro projects for each lab.

• Assignment description will be in lab


material.
• Each assignment will be uploaded to MU Final
model.
project
• Each assignment will have a deadline.
7
AGAIN !!
DEADLINES..

8
Lab ( 1 )

9
Lab - Outlines
• Pointers.

• Pointer arithmetic.

• Null pointer.

• Void pointer.

• Pass by reference:

• Function overhead.

• Status-return 10
C Pointers
❑ C is very powerful allowing its user to directly access memory address
using pointers.
❑ Pointers are a data type that store the memory locations.

❑ Pointer can indirectly access value unlike, variables name which access the
value directly.
C Pointers
int y = 5 ;

int * yptr = & y;

Pointer to Address Operator


Return the memory address of the
integer variable
C Pointers
int y = 1309 ;
int * ptr = &y; OUTPUT:
printf("y = %d \n",y); 1309
*ptr = 150; 150
printf("y = %d \n",y);
deference a pointer
The unary * operator
returns the value of the object to which
its operand (pointer) points.
Size of pointers
what is the output on 32-bit architecture?

Int main(){
char * c;
char y;
printf(“%d \n%d”,sizeof(c),sizeof(y)); OUTPUT:
return 0; 4
} 1
Size of pointer depend on computer Really?
Arch.
C Pointers
Pointers make C more powerful allowing a wide variety
of tasks to be accomplished. Pointers enable
programmers to:
• Change values of actual arguments passed to function “call
by reference”.
• More concisely and efficiently deal with arrays.
• Represent strings in a different manner.
• Call functions from different levels.
• manipulate memory specially those is dynamically allocated
• Effectively represent sophisticated data structures such
as linked lists, queues, stacks and trees.
Pointers and Arrays
The array name is self is a constant pointer to the
first element in the array?
Int a[ ] = {1, 2, 3, 4};
Int * ptr;
Ptr = a;
Printf(“ %d “ , *ptr); OUTPUT:
Ptr = &a[0]; 1
Printf(“ %d “ , *ptr); 1
Ptr = &a[3]; 4
Printf(“ %d “ , *ptr);
Pointers and Arrays
The array name it self is a constant pointer to the
first element in the array?

Int a[ ] = {1, 2, 3, 4};


OUTPUT:
Int * ptr; ERROR
Ptr = a++ ;
Printf(“ %d “ , *ptr);
Pointers Arithmetic
A limited set of arithmetic operations may be performed on pointers:
❑ increment ( + ).
❑ Decrement ( - ).
❑ Postfix (ptr ++).
❑ Prefix (++ptr).

Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;
Pointers Arithmetic
Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;

vptr += 2; // ???
Pointers Arithmetic
Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;

vptr += 2; // ???

Note
When adding value to pointer we don’t add ( 1 , 2 or 3 …n)
but we actually add a memory location.
New_add = old_add + ( n * sizeof (pointing data type) )
Pointers Arithmetic
Example :
#include <stdio.h> arr[0] = 5, arr[1] = 2, arr[2] = 3
int main(){ arr[0] = 5, arr[1] = 10, arr[2] = 3
int arr[] = {1, 2, 3}; 0[arr] = 5, 1[arr] = 10, 2[arr] = 15
*arr= 5;
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",arr[0], arr[1], arr[2]);
*(arr+ 1) = 10;
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",arr[0], arr[1], arr[2]);
2[arr] = 15;
printf("0[arr] = %d, 1[arr] = %d, 2[arr] = %d\n",0[arr], 1[arr], 2[arr]);
return 0;
}
Pointers Arithmetic
Example :
#include <stdio.h> 1 2 3 000000000061FE0C 1
Int main(){ -1 2 3 000000000061FE10 2
Int arr[] = {1,2,3}; -1 2 -2 000000000061FE14 -2
int*ptr= arr; -1 2 -1 000000000061FE14 -1
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
*ptr++=-1;
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
*++ptr=-2;
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
++(*ptr);
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
return 0;}
Pointer to char
Access a chunk of memory
Int main()
{
int a = 0xFFAACCDD; 0xDD
unsigned char * ptr = (char *)
&a;
0xCC
0xAA
printf("0x%x \n",*(ptr));
printf("0x%x \n",*(ptr+1));
printf("0x%x \n",*(ptr+2));
}
Side note:
This example assume little-Indian (search for it :D).
Assignment (1)
This assignment aims to make you remember C basic syntax

37
Assignment (1)
Implement an X,O game with a grid 3 x 3, the X and O players take turn to play.
(X) will always start first
your program should do the following: 1 2 3
• display a matrix as POINTED to the user 4 5 6
• promote to user which turn ( X or Y), then the user will input the
index in which he wants to play in. 7 8 9
• The program should update the matrix and print it again to the next
player to select where to play
⚬ for example x will enter (5) then the program should display the
1 2 3
following 4 X 6
• The program should check if any player wins, if so should print when
player won and end the game. if it is a draw the program should
7 8 9
start another grid until someone win
Assignment (1)
• The winning condition for either X or O is like the following
• any horizontal row contains (x or O)
• any vertical column contains(x,o)
• the two diagonal of the matrix
X
X X
X X X
• you code must have
⚬ char check_board_for_winner()-> the function takes the 3x3 2D array and check if X
wins return ‘x’ or O wins return ‘O’ other returns (255) indicating draw. you need to
search how give 2D array as argument to a function.
Assignment (1)
How to upload your answer:

• make a PDF file that contains :


⚬ Your code (make sure that it has all the requirements)
⚬ Text logs from terminal when the program is working in the following condition
■ (X) win the game
■ (O) win the game
■ (X) win the game after 2 draw condition.
■ (O) wins the game after 1 draw condition.
Thank You!!

41

You might also like