Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Programming
1. Programming
1. Introduction
Programming is important and fruitful only when both the programmer and interpreter
know the same language. In a microcontroller, the language that the man and
microcontroller use to communicate is assembly language. Assembly language and the
assembler are two different notations. Assembly language is the language that the
programmer uses to write a programme for a microcontroller whereas assembler
translates the assembly language into zeros and one on a computer. This language of
zeros and ones is called as machine language.
2. Assembly language elements
Basic elements of assembly language are the following:
Labels
A label is a textual designation for a line in a programme or a section of a programme
where the microcontroller can jump to. A label should always start with an alphabet
or an underline and should always start in the first column. It should never start with a
number. The length of the label can be up to 32 characters.
Instructions
The way we write an instruction is also called as instruction syntax.
The pic microcontroller does not recognise the instructions movlp or gotto
Operands
2. Operands are the instruction elements for which the instruction is being executed.
They are usually stored in registers, variables, or constants.
Directives
It is similar to an instruction but unlike instruction, it is independent of a
microcontroller and represents the characteristics of the assembly language itself.
Comments
Comment is a series of words that a programmer writes to make the programme more
clear and legible. It is placed after an instruction and must start with a semi-colon (;)
3. Writing a simple programme
In order to function properly, some microcontroller parameters must be defined such as
Type of oscillator
Whether watchdog timer is turned on or
Whether internal reset circuit is enabled or not
All this is defined by the following directive:
_CONFIG_CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
3. When all the needed elements have been defined, we can start writing a programme.
4. Control directives
Control
directives
Syntax Description Example
define #define <text> [<another text>]
(exchange one part of text for another)
Each time <text>
appears in the
programme, it will
be exchanged for
<another text>
#define turned_on 1
#define turned_off 0
include #include <file_name>
#include "file_name"
(include an additional file in a
programme)
An application of
this directive has the
effect as though the
entire file was
copied to a place
where the include
directive was found
#include <regs.h>
#include "subprog.asm"
constant constant <name> = <value>
gives a constant numeric value to a
textual designation
Each time <name>
appears in the
programme, it will
be replaced by
<value>
constant MAXIMUM = 100
constant LENGTH = 30
variable Variable <name> = <value>
Gives a variable numeric value to a
textual designation
Textual designation
changes with
particular value.
variable level = 30
variable time = 13
set <name_variable> set <value>
Defining assembler variable
Name of a variable
can be redefined
following a
definition
level set 0
length set 12
level set 45
equ <name_constant> set <value>
Defining assembler constant
To the name of the
constant
<name_constant> is
added value <value>
five equ 5
six equ 6
seven equ 7
org <label> org <value>
Defines an address from which the
programme is stored in microcontroller
memory
We define where
some part of the
programme will start
on the programme
memory
start org 0x00
movlw 0xFF
movwf PORTB
end end
end of programme
At the end of the
programme it is
necessary to place
end
.
.
.
movlw 0xFF
movwf PORTB
end
if If <conditional_term>
Conditional programming term
If condition in
<conditional_term>
if level = 100
goto FILL
4. is met, instructions
following are
executed others not
else
goto DISCHARGE
endif
else else
the alternative to IF programme block
with conditional term
Used with IF
directive as an
alternative if
conditional term is
incorrect
If time < 50
goto SPEED UP
else
goto SLOW DOWN
endif
endif endif
end the conditional programme section
This directive is
written at the end of
conditional block
if level =100
goto LOADS
else
goto UNLOADS
endif
while While <condition>
.
.
endw
execution of programme section as long
as condition is met
Programme between
while and endw will
be executed as long
as condition is met
while I < 10
i = i + 1
endw
endw endw
end of conditional part of the
programme
This instruction is
written at the end of
conditional while
block
while i< 10
i = i +1
endw
ifdef ifdef <designation>
execution of a part of a programme if
symbol was defined
If designation
<designation> was
previously defined,
instruction which
follow would be
executed until ELSE
or ENDIF directive
are not reached
#define test
.
.
ifdef test ; how the test was defined
…. ; instruction from these lines would be
executed
Endif
ifndef ifndef <designation>
execution of a part of a programme if
symbol was defined
#define test
……..
#undefine test
……..
ifndef test ; how the test was defined
…. ; instruction from these lines would be
executed
endif
cblock cblock [<term>]
<label> [:<increment>],
<label> [:<increment>],…
endc
This directive is
used to give values
to named constant.
Each following term
receives a value
greater than its
precursor. If
<increment >
parameter is also
given, then value
cblock 0x02
first, second, third ; first =
0x02, second = 0x03, third =
0x04
endc
cblock 0x02
first: 4; second : 2; third; first =
0x06, second = 0x08, third =
0x09
5. given in
<increment>
parameter is added
to the following
constant. Value of
the <term>
parameter is the
starting value. If it is
not given, it is
considered to be
zero.
endc
endc endc
end of constant block
Directive was used
at the end of
definition of a block
of constants so
assembly translator
could know that
there are no more
constants.
cblock 0x02
first, second, third ; first =
0x02, second = 0x03, third =
0x04
endc
db [<label>]db<term>[<term>,….,<term>]
Defining one byte data
Similar with dt and de
This directive
reserves a one byte
in programme
memory
Db ‘t’, 0x0f, ‘e’, ‘s’, 0x12
CONFIG _ _config<term> or _
_config<address>,<term>
Setting the configuration bits
Oscillator, watchdog
timer application
and internal reset
circuit is defined
before using this
directive the
processor must be
defined using
PROCESSOR
directive
_CONFIG _CP _OFF &
_WDT_OFF & _PWRTE_ON
& _XT_OSC
Processor Processor <microcontroller_type>
Defining microcontroller model
Instruction sets the
type of
microcontroller
where programming
is done
Processor 16F84
6. Advantages of PIC Microcontroller:
o PIC microcontrollers are consistent and faulty of PIC percentage is very less. The
performance of the PIC microcontroller is very fast because of using RISC
architecture.
o When comparing to other microcontrollers, power consumption is very less and
programming is also very easy.
o Interfacing of an analog device is easy without any extra circuitry
Disadvantages of PIC Microcontroller:
o The length of the program is high due to using RISC architecture (35 instructions)
o One single accumulator is present and program memory is not accessible