Lab3 Cce Intro To Pic
Lab3 Cce Intro To Pic
Mansoura university
Computer Control Engineering department
Embedded Systems
Lab (3)
Intro to PIC
#define log
int main(){
#ifndef log OUTPUT:
printf("Hello World\n"); Hello Earth
#else
• #ifndef checks if a macro is not
printf("Hello Earth!\n"); defined so it includes the code to
#endif be compiled.
return 0;
}
C preprocessor – Header File guard
• A very common use for the #ifndef is to guard the .h files from recopy the
code when nested calling happens.
common.h
Int power(int,int);
main.i
main.c After preprocessing int power(int,int);
#include “common.h” int power(int,int);
#include “timer.h”
Timer.h
#include “common.h”
Compiler ERROR.
power is defined twice.
C preprocessor – Header File guard
• A very common use for the #ifndef is to guard the .h files from recopy the
code when nested calling happens.
common.h
#ifndef _COMM_H
main.i
#define _COMM_H main.c After preprocessing int power(int,int);
#include “common.h”
Int power(int,int); #include “timer.h”
#endif
Timer.h
#include “common.h”
Solved using #ifnded
C preprocessor – #error
• It is used to stop the compilation process if something is not right or there
is missing data.
6
Lab - Outlines
• Variable scope (extern).
• Micro controller vs micro processor.
• Harvard architecture.
• PIC16f877a essential circuits.
• getting started with MPLAB.
• MPLAB + Proteus.
7
Variable scopes & lifetime
8
Variable scope.
It’s the space in which the variable is visible and can be used
in it
❑Local variable. ❑Global variable
Void foo() Int var1 =5;
{ Void foo()
int Var1; • Var1 is visible to
{ all functions in
} Var1 = 5; the file :D
Void main(){ }
Var1 =5; //error Void main(){
} printf(“%d”,var1);
• Var1 is visible only in foo function }
Variable scope.
What’s the output of this code?
int x;
Void foo(){
x++;
OUTPUT:
}
15
Void main(){
int x = 15; Local definition overcomes
Foo(); global one.
printf(“%d”,x);}
Variable scope.
• What’s the output of this code?
int x=10;
void use_global(){
printf("%d\n",x++);
}
void use_local(){ OUTPUT:
int x =15;
printf("%d\n",x++); 10
} 10
void main(){
use_global(); 15
{ int x =10; 11
printf("%d\n",x++);}
use_local();
printf("%d\n",x);}
Extern variables
What about variable file scopes when using multiple file?
12
Extern
• Extern keyword is used with variables when:
• A source file wanted to use a variable that is defined in another file.
Main.c
math.c
extern float pi;
float pi = 3.14; compiler int main()
ERROR {
printf(“%f”,pi);
}
Main.c • Extern tells the compiler that this value will be taken
#include “math.h” from linker it is not available right now.
Int main()
{
• if pi is not defined in any other file this a linker error.
printf(“%f”,pi); • by default, any global variable is external linkage.
} • if two global have the same name in two different
files a linker error will raise (redefinition).
Extern
• Extern introduce a new scope called software scope where variable is available for all
the source file to use.
• To make our global file restricted to the file scope only we can use static keyword.
math.c
15
Embedded system brain
Embedded system tasks:
• Capture input signals from input device (sensor).
• Analyze inputs and take decisions.
• Take actions by controlling actuators.
in1
in2 Controller
in3
Micro controller vs Micro processor
Micro Controller Micro processor
Micro controller vs Micro processor
Micro Controller Micro processor
It consists of a CPU unit and memory and It is just stand alone CPU that can never
more peripherals that are all inside the work alone as it needs external memory,
same chip (SoC) so it can work alone external peripheral,,..etc.
without the need of external parts.
Embedded system
Micro Controller Micro processor
• Slower (64 Mhz) • Faster (can work in high frequency 1 GHz)
• fixed specification : • more flexible (we can choose memory
⚬ static limited memory. size, .. etc)
⚬ static peripherals • suitable for large & complex applications.
• suitable for small applications • Example:
• Example Raspberry Pi 3 Model B (ARM processor)
arduino (based on AVR)
Micro-controller basic structure
20
MC Basic Structure
as we described before MC have a system on chip that consists of:
Program Data
memory
CPU Memory
program or instructions memory or data or execution memory or RAM:
ROM: • it’s volatile memory (lose its
• often called flash. content if electricity is out)
• it’s non-volatile memory • high speed.
• easier to read, hard to write. • expensive.
• We burn the code into the flash to • can Read / Write from it.
save it.
• most flashes have cycles to burn
or delete data from it.
PIC 16F877A
23
PIC 16F877A
Key features of pic16f877a.
26
PIC Essential circuits
❑ For any MC to work we need to provide 3 main circuits.
RESET
Micro Controller
responsible for resetting
MC when a button is
pushed. Clock
This provide the essential
POWER clock for MC CPU and other
Provide essential +Ve and peripherals.
GND for the chip. • Can be external or
internal clock.
Voltage regulator
The VDD and VSS terminals are the heart of the PIC as without them no
electricity will be provided inside the PIC
The maximum volt that the PIC can handle is 5V so we need to use 5V voltage
regulator <7805>
Master Clear –Reset Circuit
• The reset is very essential
configuration when
dealing with micro
controllers
32
MPLAB – PIC toolchain
• we will use MBLAP ide (provided from micro chip) to generate the
executable file to be flashed into PIC.
40
MPLAB – First project
start a new project from file > new project , a pop-up will show.
choose
Microchip embedded
MPLAB – First project
choose
• family PIC16
• write down device
name (16f877a) then
hit next
MPLAB – First project
choose:
• project name
• location or path to
store you project in
hit finish :D
MPLAB – First project
48
MPLAB – First project
52
Assignment (3)
• You Will be given a file “english_words.h” that contains
an C string array that has a 100 English words.
• we want to implement Hangman Game where user tries to
guess a random English word by trying English letters
until he got the answer.
• The user has 10 trials, or the hangman will be drawn and
will be executed.
• The win condition occurs if user guessed the right word
in less than 10 trials to save the man.
Implement a C code to:
• get a random word from the array given in .h file by
including it.
• implement the game logic described above. 53
Assignment (3)
Example of input
• suppose we choose word “summer”
1. At the beginning the code should hide the word like this
( _ _ _ _ _ _ ) to indicate how many letter is has.
2. code should display how many trials left in each iteration.
3. if user enters ‘s’ the code should update the displayed word like
this ( S _ _ _ _ _ ).
4. if user enters ‘m’ it should show any ‘m’ in word like this
( S _ m m _ _ ).
5. if user enters ‘z’ (z is not in “summer”) the code should display
the same word as before -> ( S _ m m _ _ ) but reduce trials = 9.
6. The program should keep going.
7. if the user guessed the word “summer” right under 10 trials it
should print “WIIIIIN”
8. if 10 trials passed and user didn’t guess the word it should
display the actual random word and print “Man is Executed ”
54
Assignment (3)
How to upload your answer:
55
Thank You!!
56