Professional Documents
Culture Documents
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
Installation
Project Wizard
Compile
Watch Window
2
www.automatics.pk
Automatics Trainings Group
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 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 Gates
Circuits with two Inputs:
C Language Basics
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
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)
www.automatics.pk
Automatics Trainings Group
Basic Gates
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
Basic Gates
C Language Basics
Variables
Kind When to Use C Structure
Compile
Watch Window
- 5-10
www.automatics.pk
Automatics Trainings Group
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
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
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
Basic Gates
C Language Basics
Switch
MPLAB IDE
MCC18
Installation
Project Wizard
Compile
Watch Window
Statement
14
www.automatics.pk
Automatics Trainings Group
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
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
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
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
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
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
== 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
Project Wizard
Compile
Dont confuse & and &&..
Watch Window 1 & 2 0 whereas 1 && 2 <true>
23
www.automatics.pk
Automatics Trainings Group
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
Basic Gates The for loop is just shorthand for this while loop structure.
C Language Basics
Watch Window
25
www.automatics.pk
Automatics Trainings Group
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
MCC18
Installation
Project Wizard
Compile
Watch Window
www.automatics.pk
Automatics Trainings Group
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
Loops
Switch
MPLAB IDE
MCC18
Installation
Project Wizard
Compile
Watch Window
www.automatics.pk
Automatics Trainings Group
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
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
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
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
Basic Gates
C Language Basics
Variables
Loops
Switch
Thank you
MPLAB IDE
MCC18
Installation
Project Wizard
Compile
Watch Window
www.automatics.pk