You are on page 1of 35

Automatics

AutomaticsTrainings
TrainingGroup
Group

Microchip 8 Bits

Basic Gates

C Language Basics

Variables

Loops

Switch

MPLAB IDE

MCC18

Installation
Embedded Electronics
Microchip Microcontroller
Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Microchip 8 Bits

Basic Gates

C Language Basics

Variables

Loops Advanced PIC Micro-controller Programming


Switch

MPLAB IDE Lecture2


MCC18

Installation

Project Wizard

Compile

Watch Window
2
www.automatics.pk
Automatics Trainings Group

Truth Tables and Boolean Notation


Microchip 8 Bits

Basic Gates

C Language Basics
Circuits with one input
Variables

Loops
Buffer P=A A P
A P
Switch 0 0
MPLAB IDE 1 1
MCC18

Installation A P
Not P=A
Project Wizard
0 1 A P
1 0
Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Basic AND / OR
Microchip 8 Bits

Basic Gates
Circuits with two Inputs
C Language Basics

Variables
A B P
Loops
0 0 0
AND P = A.B 0 1 0 A P
Switch 1 0 0 B
1 1 1
MPLAB IDE

MCC18

Installation A B P
0 0 0 A
OR P=A+B 0 1 1 P
Project Wizard B
1 0 1
Compile 1 1 1

Watch Window

www.automatics.pk
Automatics Trainings Group

Basic NAND / NOR


Microchip 8 Bits

Basic Gates
Problems with two Inputs
C Language Basics

Variables
A B P
Loops 0 0 1 A
NAND P = A.B 0 1 1 P
Switch 1 0 1
B
1 1 0
MPLAB IDE

MCC18
A B P
Installation 0 0 1
A
NOR P=A+B 0 1 0 P
Project Wizard 1 0 0
B
1 1 0
Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Basic XOR / XNOR


Microchip 8 Bits

Basic Gates
Circuits with two Inputs:
C Language Basics

Variables XOR P=AB A B P


0 0 0
Loops 0 1 1
A
1 0 1 P
Switch B
1 1 0
MPLAB IDE

MCC18

Installation
XNOR P =AB A B P
0 0 1 A
0 1 0 P
Project Wizard B
1 0 0
Compile 1 1 1

Watch Window

www.automatics.pk
Automatics Trainings Group

Boolean Algebraic Laws


Microchip 8 Bits

Basic Gates A . A = A
Tautology (Idempotent)
A + A = A
C Language Basics

A . A = 0
Variables Complementary
A + A = 1
Loops
Operating with logic 0 A . 0 = 0 A . 1 = A
Switch
and logic 1 A + 0 = A A + 1 = 1
MPLAB IDE
A . B = B . A
Commutative
MCC18 A + B = B + A
Installation
(A.B).C = A.B.C =
Associative
Project Wizard A.(B.C)

Compile A.(B + C) = A.B + A.C


Distributive
Watch Window
A + (B.C) =(A+B).(A+C)

www.automatics.pk
Automatics Trainings Group

Data Types (again)


Microchip 8 Bits

Basic Gates

C Language Basics Type Size Min Max


Variables
char 8 bits -128 127
Loops
unsigned char 8 bits 0 255
Switch
int 16 bits -32,768 32,767
MPLAB IDE
unsigned int 16 bits 0 65, 535
MCC18
short long 24 bits -2^23 2^23-1
Installation
unsigned short long 24 bits 0 2^24-1
Project Wizard
long 32 bits -2^31 2^31-1
Compile
unsigned long 32 bits 0 2^32-1
Watch Window

www.automatics.pk
Automatics Trainings Group

Repetition in Programs
Microchip 8 Bits

Basic Gates
In most software, the statements in the program may need
to repeat for many times.
C Language Basics
e.g., calculate the value of n!.
If n = 10000, its not elegant to write the
Variables

Loops
code as 1*2*3**10000.
Switch Loop is a control structure that repeats a group of steps in a
program.
MPLAB IDE
Loop body stands for the repeated
MCC18
statements.
Installation
There are three C loop control statements:
Project Wizard
while, for, and do-while.
Compile

Watch Window

. 5-9
www.automatics.pk
Automatics Trainings Group

Comparison of Loop Choices


Microchip 8 Bits

Basic Gates

C Language Basics

Variables
Kind When to Use C Structure

Loops Counting loop We know how many loop while, for


repetitions will be needed in
Switch advance.
MPLAB IDE Sentinel-controlled Input of a list of data ended by a while, for
loop special value
MCC18

Installation Endfile-controlled Input of a list of data from a data while, for


loop file
Project Wizard

Compile

Watch Window

- 5-10
www.automatics.pk
Automatics Trainings Group

Comparison of Loop Choices


Microchip 8 Bits

Basic Gates

C Language Basics

Variables
Kind When to Use C Structure
Loops
Input validation Repeated interactive input of a do-while
Switch loop value until a desired value is
entered.
MPLAB IDE
General Repeated processing of data until while, for
MCC18 conditional loop a desired condition is met.
Installation

Project Wizard

Compile

Watch Window
5-11
www.automatics.pk
Automatics Trainings Group

The while Statement in C


Microchip 8 Bits

Basic Gates
The syntax of while statement in C:
while (loop repetition condition)
C Language Basics statement
Variables Loop repetition condition is the condition which controls the
loop.
Loops
The statement is repeated as long as the loop repetition
Switch condition is true.
A loop is called an infinite loop if the loop repetition
MPLAB IDE
condition is always true.
MCC18

Installation

Project Wizard

Compile

Watch Window
5-12
www.automatics.pk
Automatics Trainings Group

An Example of a while Loop


Microchip 8 Bits
Loop repetition condition
Basic Gates

C Language Basics

Variables

Loops
Statement
Switch

MPLAB IDE

MCC18

Installation

Project Wizard
Loop control variable is the variable whose value controls
loop repetition.
Compile
In this example, count_emp is the loop control variable.
Watch Window
5-13
www.automatics.pk
Automatics Trainings Group

Flowchart for a while Loop


Microchip 8 Bits

Basic Gates

C Language Basics

Variables Loop repetition condition


Loops

Switch

MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window
Statement
14
www.automatics.pk
Automatics Trainings Group

The for Statement in C


Microchip 8 Bits

Basic Gates
The syntax of for statement in C:
for (initialization expression;
C Language Basics

Variables
loop repetition condition;
update expression)
Loops
statement
The initialization expression set the initial
Switch

MPLAB IDE
value of the loop control variable.
MCC18
The loop repetition condition test the
Installation
value of the loop control variable.
Project Wizard
The update expression update the loop
Compile
control variable.
Watch Window
5-15
www.automatics.pk
Automatics Trainings Group

An Example of the for Loop


Microchip 8 Bits

Basic Gates Initialization Expression


C Language Basics
Loop repetition condition
Variables

Loops
Update Expression

Switch

MPLAB IDE

MCC18

Installation

Project Wizard
count_emp is set to 0 initially.
Compile
count_emp should not exceed the value of number_emp.
Watch Window
count_emp is increased by one after each iteration.
5-16
www.automatics.pk
Automatics Trainings Group

Increment and Decrement Operators


Microchip 8 Bits

Basic Gates
The statements of increment and decrement are
C Language Basics commonly used in the for loop.
Variables
The increment (i.e., ++) or decrement (i.e., --) operators
are the frequently used operators which take only one
Loops operand.
Switch
The increment/decrement operators increase or decrease
the value of the single operand.
MPLAB IDE
e.g., for(int i = 0; i < 100; i++){ }
MCC18
The variable i increase one after each
Installation
iteration.
Project Wizard

Compile

Watch Window
5-17
www.automatics.pk
Automatics Trainings Group

Nested Loops
Microchip 8 Bits
Nested loops consist of an outer loop with one or more inner
Basic Gates
loops.
C Language Basics e.g.,
for (i=1;i<=100;i++){
Variables
for(j=1;j<=50;j++){
Loops
} Outer loop
Switch
}
MPLAB IDE The above loop will run for 100*50 iterations.
MCC18
Inner loop
Installation

Project Wizard

Compile

Watch Window

Copyright 2004 Pearson Addison- 5-18


www.automatics.pk
esley. All rights reserved.
Automatics Trainings Group

The do-while Statement in C


Microchip 8 Bits

Basic Gates The syntax of do-while statement in C:


do
C Language Basics
statement
Variables while (loop repetition condition);
The statement is first executed.
Loops
If the loop repetition condition is true, the statement is repeated.
Switch Otherwise, the loop is exited.
MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window
5-19
www.automatics.pk
An Example of the do-while
Automatics Trainings Group

Microchip 8 Bits
Loop
Basic Gates
/* Find even number input */
C Language Basics do{
Variables
printf(Enter a value: );
scanf(%d, &num);
Loops }while (num % 2 !=0)
Switch

MPLAB IDE

MCC18

Installation
This loop will repeat if the user inputs
Project Wizard
odd number.
Compile

Watch Window

Copyright 2004 Pearson Addison- 5-20


www.automatics.pk
Wesley. All rights reserved.
Automatics Trainings Group

What is Memory?
Microchip 8 Bits
Memory is like a big table of numbered slots Addr Value
Basic Gates
where bytes can be stored. 0
C Language Basics 1
Variables
The number of a slot is its Address. 2
One byte Value can be stored in each slot. 3
Loops 72?
4 H (72)
Switch Some logical data values span more than 5 e (101)
one slot, like the character string Hello\n
MPLAB IDE 6 l (108)
MCC18 A Type names a logical meaning to a span of 7 l (108)
memory. Some simple types are: 8 o (111)
Installation

a single character (1 slot) 9 \n (10)


Project Wizard char
char [10] an array of 10 characters 10 \0 (0)
Compile int signed 4 byte integer
not always 11
float 4 byte floating point
Watch Window int64_t signed 8 byte integer Signed? 12

21
www.automatics.pk
Automatics Trainings Group

What is a Variable?
symbol table?
Microchip 8 Bits
Symbol Addr Value
A Variable names a place in memory where
Basic Gates you store a Value of a certain Type. 0
1
C Language Basics
2
Variables
You first Define a variable by giving it a
3
name and specifying the type, and
Loops optionally an initial value x 4 ?
declare vs define?
y 5 e (101)
Switch 6
char x; Initial value of
7
MPLAB IDE char y=e; x is undefined
The compiler puts 8
MCC18
Initial them somewhere in 9
value memory. 10
Installation

Nam 11
Project Wizard What names are
e legal?
12
Compile Type is single character
(char) extern? static?
Watch Window
const?

22
www.automatics.pk
Automatics Trainings Group

Comparison and Mathematical Operators


Microchip 8 Bits

== equal to
Basic Gates
< less than
<= less than or equal
C Language Basics
> greater than
>= greater than or equal
Variables
!= not equal
&& logical and
Loops || logical or
! logical not
Switch

+ plus & bitwise and


MPLAB IDE
- minus | bitwise or
* mult ^ bitwise xor
MCC18 / divide ~ bitwise not
% modulo << shift left
Installation >> shift right

Project Wizard

Compile
Dont confuse & and &&..
Watch Window 1 & 2 0 whereas 1 && 2 <true>

23
www.automatics.pk
Automatics Trainings Group

The while loop


Microchip 8 Bits
float pow(float x, uint exp)
{
Basic Gates
int i=0;
float result=1.0;
C Language Basics while (i < exp) {
result = result * x;
Variables i++;
}
return result;
Loops }

Switch int main(int argc, char **argv)


{
float p;
MPLAB IDE p = pow(10.0, 5);
printf(p = %f\n, p);
MCC18 return 0;
}

Installation
Solution: while loop.
Project Wizard
loop:
if (condition) { while (condition) {
Compile statements; statements;
goto loop; }
Watch Window }

24
www.automatics.pk
Automatics Trainings Group

The for loopc


Microchip 8 Bits

Basic Gates The for loop is just shorthand for this while loop structure.
C Language Basics

Variables float pow(float x, uint exp) float pow(float x, uint exp)


{ {
float result=1.0; float result=1.0;
Loops
int i; int i;
i=0; for (i=0; (i < exp); i++) {
Switch while (i < exp) { result = result * x;
result = result * x; }
MPLAB IDE i++; return result;
} }
return result;
MCC18 } int main(int argc, char **argv)
{
Installation int main(int argc, char **argv) float p;
{ p = pow(10.0, 5);
float p; printf(p = %f\n, p);
Project Wizard p = pow(10.0, 5); return 0;
printf(p = %f\n, p); }
Compile return 0;
}

Watch Window

25
www.automatics.pk
Automatics Trainings Group

What are these?


Microchip 8 Bits

Basic Gates
IDE: Integrated Development
C Language Basics

Variables
Environment
Software package that lets you assemble, compile,
Loops
link, export and debug programs.
Switch ICE: In Circuit Emulator:
MPLAB IDE
Hardware that allows you to test the developed
programs on non-real (emulated) hardware. To test
MCC18
ICE you need IDE
Installation Programmer:
Project Wizard Is a piece of hardware, e.g. PICkit 2, PM3 that allow
the export of executable .hex files (load) files to the
Compile
PIC processors memory directly. It is driven by the
Watch Window
IDE.
www.automatics.pk
Automatics Trainings Group

MPLAB IDE Projects


Microchip 8 Bits

Basic Gates A project must be created for implementation


C Language Basics Specify your device
Variables
Create and edityour files
Loops
Compile and link your project
Switch
Program the device
To create a project
ProjectProject Wizard
MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

MPLAB IDE Project Creation (1/4)


Microchip 8 Bits

Basic Gates

C Language Basics
Begin project and specify device
Variables

Loops

Switch

MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

MPLAB IDE Project Creation (2/4)


Microchip 8 Bits

Basic Gates Step 2: Select Microchip MPASM


C Language Basics Toolsuite
Step 3: Create New Project File
Variables

Loops

Switch

MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

MPLAB IDE Project Creation (3/4)


Microchip 8 Bits

Basic Gates
Step 4: Add project files
C Language Basics ASM file
Starting point for
Variables assembly code
<device
Loops
name>tmpo.asm
Switch Linker script
<device name>.lkr
MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

MPLAB IDE Project Creation (4/4)


Microchip 8 Bits

Basic Gates
Finish project creation and return to IDE
C Language Basics

Variables
View project details
Loops
ViewProject
Switch

MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Updating Source Code


Microchip 8 Bits

Basic Gates

C Language Basics
Modify .asm files
Under Source Files
Variables
folder in project
Loops window
Open and edit files to
Switch
implement new
MPLAB IDE functionality
Additional files can
MCC18
be created and added
Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Updating Source Code


Microchip 8 Bits

Basic Gates

C Language Basics
Modify .asm files
Variables Under Source Files

Loops
folder in project
window
Switch Open and edit files to

MPLAB IDE
implement new
functionality
MCC18 Additional files can be
Installation
created and added

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Building a Project
Microchip 8 Bits

Basic Gates
ProjectBuild All (or
C Language Basics Ctrl+F10)
Variables
Output window
indicates success or
Loops failure
Switch
HEX file generated
when project is built
MPLAB IDE
Used to program
MCC18
the device
Installation

Project Wizard

Compile

Watch Window

www.automatics.pk
Automatics Trainings Group

Automatics Training Group


Microchip 8 Bits

Basic Gates

C Language Basics

Variables

Loops

Switch
Thank you
MPLAB IDE

MCC18

Installation

Project Wizard

Compile

Watch Window

www.automatics.pk

You might also like