Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8554
Скачиваний: 0
186 |
Chapter 9 |
;=====================================================================
;
; Configuration Bits
;
;=====================================================================
_CP_ON |
EQU |
H’000F’ |
_CP_OFF |
EQU |
H’3FFF’ |
_PWRTE_ON |
EQU |
H’3FF7’ |
_PWRTE_OFF |
EQU |
H’3FFF’ |
_WDT_ON |
EQU |
H’3FFF’ |
_WDT_OFF |
EQU |
H’3FFB’ |
_LP_OSC |
EQU |
H’3FFC’ |
_XT_OSC |
EQU |
H’3FFD’ |
_HS_OSC |
EQU |
H’3FFE’ |
_RC_OSC |
EQU |
H’3FFF’ |
Names in the include file are defined in all-capital letters. It is probably a good idea to adhere to this style instead of creating alternate names in lower case. The C-like #include directive is used to refer the .inc files at assembly time, for example:
#include <p16f84a.inc>
9.4.6 Errorlevel Directive
This directive allows controlling the warning and error messages produced at assembly and link times. One particular type of warning can be disturbing: those that refer to bank changes. Applications often turn off bank change related warning with the following line:
errorlevel -302
9.5 Pseudo Instructions
Sometimes a code listing contains instructions that are not part of the standard set for the particular device. The reason this happens is that MPLAB includes a set of p s e u d o - i n s t r u c t i o n s f o r 1 2 - a n d 1 4 - b i t d e v i c e s . Ta b l e 9 . 2 l i s t s t h e s e pseudo-instructions and their standard equivalents:
Table 9.2
PIC Pseudo Instructions
MNEMONIC |
DESCRIPTION |
EQUIVALENT |
STATUS BIT |
|
OPERATION(S) |
CHANGED |
|||
ADDCF f,d |
Add Carry to File |
BTFSC 3,0 |
Z |
|
Register |
INCF f,d |
|||
ADDDCF f,d |
Add Digit Carry |
|||
to File Register |
BTFSC 3,1 |
Z |
||
INCF f,d |
||||
B |
k |
Branch |
GOTO k |
- |
BC |
k |
Branch on Carry |
BTFSC 3,0 |
|
GOTO k |
- |
|||
(continues)
PIC Programming: Tools and Techniques |
187 |
||||||
Table 9.2 |
|||||||
PIC Pseudo Instructions |
|||||||
MNEMONIC |
DESCRIPTION |
EQUIVALENT |
STATUS BIT |
||||
OPERATION(S) |
CHANGED |
||||||
BDC k |
Branch on Digit |
||||||
Carry |
BTFSC |
3,1 |
|||||
GOTO k |
- |
||||||
BNC k |
Branch on No Carry BTFSS 3,0 |
||||||
BNDC |
k |
Branch on No Digit |
GOTO k |
- |
|||
Carry |
BTFSS |
3,1 |
|||||
GOTO k |
- |
||||||
BNZ k |
Branch on No Zero |
BTFSS |
3,2 |
||||
GOTO k, 2 |
- |
||||||
BZ k |
Branch on Zero |
BTFSC 3,2 |
|||||
GOTO |
k |
- |
|||||
CLRC |
Clear Carry |
BCF |
3,0 |
- |
|||
CLRDC |
Clear Digit Carry |
BCF |
3,1 |
- |
|||
CLRZ |
Clear Zero |
BCF |
3,2 |
- |
|||
LCALL |
k |
Long Call |
BCF/BSF 0x0a,3 |
||||
BCF/BSF 0x0a,4 |
|||||||
CALL k |
|||||||
LGOTO k |
Long GOTO |
BCF/BSF 0x0a,3 |
|||||
BCF/BSF 0x0a,4 |
|||||||
GOTO k |
|||||||
MOVFW f |
Move File to W |
MOVF f,0 |
Z |
||||
NEGF |
f,d |
Negate File |
COMF |
f,1 |
|||
INCF |
f,d |
- |
|||||
SETC |
Set Carry |
BSF |
3,0 |
- |
|||
SETDC |
Set Digit Carry |
BSF |
3,1 |
- |
|||
SETZ |
Set Zero |
BSF |
3,2 |
- |
|||
SKPC |
Skip on Carry |
BTFSS |
3,0 |
- |
|||
SKPDC |
Skip on Digit Carry |
BTFSS |
3,1 |
- |
|||
SKPNC |
Skip on No Carry |
BTFSC 3,0 |
- |
||||
SKPNDC |
Skip on No Digit |
||||||
Carry |
BTFSC |
3,1 |
- |
||||
SKPNZ |
Skip on Non Zero |
BTFSC 3,2 |
- |
||||
SKPZ |
Skip on Zero |
BTFSS |
3,2 |
- |
|||
SUBCF |
f,d |
Subtract Carry from |
|||||
File |
BTFSC |
3,0 |
|||||
SUBDCF f,d |
Subtract Digit Carry |
DECF |
f,d |
Z |
|||
from File |
BTFSC |
3,1 |
|||||
DECF |
f,d |
Z |
|||||
TSTF f |
Test File |
MOVF |
f,1 |
Z |
|||
We have listed the PIC pseudo-instructions to provide a reference. In our programming we prefer to stay away from using them since they tend to make code less readable. Microchip recommends not using the pseudo-instructions.
Chapter 10
Programming Essentials: Input and Output
In this chapter, we discuss the simplest circuits and programming operations. Using a PIC to control an LED or read a switch is as elementary as it gets. However, neither of these operations is trivial, since there is more to it than a few lines of code. Other input/output devices that are also considered are seven-segment LED displays and multiple switches, sometimes called toggle switches. A bank of multiple LEDs can also function as a binary output device.
10.0 16F84A Programming Template
We have found that program development can be simplified considerably by using code templates. A code template is a program devoid of functionality that serves to implement the most common and typical features of an application. The template not only saves the effort of redoing the same tasks, but reminds the programmer of program elements that could otherwise be forgotten. A professional developer will have collected many different templates over the years for different types of applications on various processors. The following template is for the 16F84A PIC:
;============================================================
;File name:
;Date:
;Author:
;Processor:
;Reference circuit: ;============================================================
;Copyright notice: ;============================================================
;Program Description:
;
;===========================
;configuration switches ;===========================
;Switches used in __config directive:
189
190 |
Chapter 10 |
|
; |
_CP_ON |
Code protection ON/OFF |
; * _CP_OFF |
||
; |
* _PWRTE_ON |
Power-up timer ON/OFF |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog timer ON/OFF |
; * _WDT_OFF |
||
; |
_LP_OSC |
Low power crystal osccillator |
; * _XT_OSC |
External parallel resonator |
|
; |
_HS_OSC |
High speed crystal resonator (8 to 10 MHz) |
; |
_RC_OSC |
Resistor/capacitor oscillator |
; |
(simplest, 20% error) |
|
;|
;|_____ * indicates setup values
;========================= ; setup and configuration ;=========================
processor 16f84A |
||
include |
<p16f84A.inc> |
|
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
|
;===================================================== |
||
; |
constant definitions |
|
;===================================================== |
||
;===================================================== |
||
; |
PIC register equates |
|
;===================================================== |
||
;===================================================== |
||
; |
variables in PIC RAM |
|
;===================================================== |
||
cblock |
0x0c |
|
endc |
||
;============================================================
; program
;============================================================
org |
0 |
; start at address |
goto |
main |
|
; Space for interrupt handlers |
||
org |
0x08 |
|
main:
;============================================================ end ; END OF PROGRAM
;============================================================
Programming Essentials: Input and Output |
191 |
In addition to the template file the program developer should keep at hand the necessary include files. In this case, p16f84a.inc.
10.1 Introducing the 16F84A
The circuits and programs in this chapter use the 16F84A, probably the most popular of all mid-range PIC microcontrollers. Although we have discussed the mid-range architecture, we start with a review of this processor in order to establish a base for the material that follows.
10.1.1 Template Circuit for 16F84A
Like the programmer uses a programming template for developing 16F84A code, the circuit designer uses a template circuit. This circuit contains the components that most 16F84A boards require. The elements include a diagram of the PIC itself with the pin-out, as well as the wiring of the standard components, including the power source, ground, the reset pin (MCLR), and the most commonly used oscillator. Figure 10-1 shows a circuit template for the 16F84A.
+5v
1 |
18 |
|||
RA2 |
RA1 |
|||
2 |
17 |
Osc |
||||
R=10K |
RA3 |
RA0 |
||||
3 |
16F84A |
16 |
|||||||
RA4/TOCKI |
OSC1 |
||||||||
4 |
15 |
||||||||
5 |
MCLR |
OSC2 |
|||||||
14 |
|||||||||
+5v |
|||||||||
Vss |
Vdd |
||||||||
6 |
13 |
||||||||
RB0/INT |
RB7 |
||||||||
7 |
12 |
||||||||
RB1 |
RB6 |
||||||||
8 |
11 |
||||||||
RB2 |
RB5 |
||||||||
9 |
10 |
||||||||
RB3 |
RB4 |
||||||||
Figure 10-1 16F84A Circuit Template
The circuit template in Figure 10-1 does not suit every possible circuit. Even the simplest components must sometimes be configured differently; for example, the reset line could be wired to a pushbutton switch, or a different oscillator may be used. In any case, it is always easier to make modifications to an existing base than to start from scratch every time.
10.1.2 Power Supplies
Every PIC-based circuit board requires a +5V power source. A possible source of power is one or more batteries. There is an enormous selection of battery types, sizes, and qualities. The most common ones for use in experimental circuits are listed in Table 10.1.