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

Lab3 Cce Intro To Pic

embedded systems course labs, introduction to pic microcontroller, 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 views56 pages

Lab3 Cce Intro To Pic

embedded systems course labs, introduction to pic microcontroller, 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/ 56

Faculty of Engineering

Mansoura university
Computer Control Engineering department

Embedded Systems
Lab (3)

Intro to PIC

By / Omar Mahmoud Wahba


TA at CSED – FOE - MU
1
C preprocessor - #ifndef
• it is used to not to include some code based on certain defined macro:

#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.

#define OPERATING_MODE (7) • When user select an invalid


#if OPERATING_MODE == 5 mode, we stop the building
//some code if mode =5 process printing a message like
this.
#elif OPERATING_MODE == 6
//some code if mode = 3
#else
#error invalid operating
mode
#endif
Lab ( 3 )

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

static float pi = 3.14; Linker


ERROR

Main.c • static made pi scope “file scope” so no other c file


extern float pi; can use it.
Int main()
{
• common practice : define any global variable with
printf(“%f”,pi); static inside the file unless you will extern it
} somewhere else to avoid any redefinition.
Micro controller vs Micro processor

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:

• basic computer (CPU +


Memory)
• other HW peripherals
like (timers,
interrupts, serial
ports).
• Buses that connects
everything together.
Harvard Architecture.
Embedded controllers usually selects different types of memory

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.

it has 3 types of memory: • 10-bit ADC. • SPI-UART-I2C.


• PROG MEM : 14kB • 3-Timers. • 2 PWM channel.
• static RAM : 368 byte • 33 Input out pin.
• EEPROM : 256 to store data • BOR.
• Watch Dog Timer.
PIC 16F877A - Pinout
Key features of pic16f877a.
PIC Essential circuits

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

• If MCLR is connected to ground


the PIC will be always in reset
handler

• We use pull up configuration to


make MCLR connected to +dv
then if we press the button the
MCLR will be connected to
ground and a reset will happen
Oscillator – MC clock
• As we said clock pulses is essential for MC or any processor to work.
• The more the frequency of the clock the greater number of instructions
that can be executed in 1s and the more power consumption.
• Clock pulses must be very stable and accurate to get high performance
from the micro controller.
• The clock pulse source can be either external or internal clock.
Oscillator – MC clock
We will use external
oscillator ( crystal ) for
more stability.
The following configuration is
needed in order for the pic to
work
PIC Firmware - MBLAP

32
MPLAB – PIC toolchain
• we will use MBLAP ide (provided from micro chip) to generate the
executable file to be flashed into PIC.

• you can download it from their official website:


MPLAB – installation
After download start the setups – Hit next > next :D
Make sure you are connected to internet
MPLAB – installation
Stop here and select the following only
MPLAB – installation
wait for installation to finish.
MPLAB – installation
wait for installation to finish.
MPLAB – installation
select compiler then finish a popup will show up to download the
compiler.
MPLAB – installation
select the free licence.
MBLAP First project

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

select the compiler we


downloaded before then,
hit next.
MPLAB – First project

choose:
• project name
• location or path to
store you project in
hit finish :D
MPLAB – First project

In the left-upper corner:


• right click on source files.
• choose new > main.c
• an auto generated main.c
file will appear.
MPLAB – First project
In the main.c file copy the
following code:

#define _XTAL_FREQ 8000000


#include <xc.h>
void main(void) {
TRISB = 0;
while(1){
PORTB = 0x1;
__delay_ms(1000);
PORTB = 0x0;
__delay_ms(1000);}
}
MPLAB – First project
• In the upper bar click on the
small arrow beside the
building icon > Build Main
Project
• wait build process to finish if
everything is good you will
find “BUILD SUCCESSFUL”
message.
Connect with proteus

48
MPLAB – First project

• Draw a circuit like this in


proteus.
• Then double click on the
PIC16f877a on the circuit
MPLAB – First project
when a pop-up appears:
• choose the .hex file path
from the small folder icon
(you will find it in directory
\dist\default\production\pr
oj_name.X\example.hex

• choose 8Mhz clock


frequency.
MPLAB – First project
Hit run on proteus, you should see the led blink
Assignment (3)
Hangman Game
This assignment aims to improve your C string
manipulation skills

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:

• make a PDF file that contains :


⚬ Your final code.
⚬ Screenshots for :
⚬ win condition (just show the win and final word no need to show steps).
⚬ Another win condition like above just to verify that your code randomly select from
words array.
⚬ A lose condition (just show the lose message and the actual word that the user failed
to guess, no need to show steps).

55
Thank You!!

56

You might also like