Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8578
Скачиваний: 0
192 |
Chapter 10 |
||
Table 10.1 |
|||
Common Dry Cell Alkaline Battery Types |
|||
DESIGNATION |
VOLTAGE |
LENGTH |
DIAMETER |
MM. |
MM. |
||
D |
1.5 |
61.5 |
34.2 |
C |
1.5 |
50 |
26.2 |
AA |
1.5 |
50 |
14.2 |
AAA |
1.5 |
44.5 |
10.4 |
AAAA |
1.5 |
42.5 |
8.3 |
All of the batteries in Table 10.1 produce 1.5V. A PIC with a supply voltage of 2 to 6 volts uses two to four batteries. Note that in selecting the battery power source for a PIC-based circuit, other elements beside the microcontroller itself must be considered, such as the oscillator. Holders for several interconnected batteries are available at electronic supply sources.
Alternatively, the power supply can be a transformer with 120VAC input and 3 to 12VDC called AC/DC adapters. The most useful type for the experimenter are the ones with an ON/OFF switch and several selectable output voltages. Color-coded alligator clips at the output wires are convenient.
Voltage Regulator
A useful device for a typical PIC-based power source is a voltage regulator IC. The 7805 voltage regulator is ubiquitous in most PIC-based boards with AC/DC adapter sources. The IC is a three-pin device whose purpose is to ensure a stable voltage source which does not exceed the device rating. The 7805 is rated for 5V and produces this output from any input source in the range 8 to 35V. Since the excess voltage is dissipated as heat the 7805 is equipped with a metallic plate intended for attaching a heat sink. The heat sink is not required in a typical PIC application but it is a good idea to maintain the supply voltage closer to the device minimum rather than its maximum.
The voltage regulator circuit requires two capacitors: one electrolytic and the other one not. Figure 10-2 shows a power source circuit using the 7805.
+5v DC |
9 -35v DC |
|||||||||||||||||||
output |
input |
|||||||||||||||||||
OUT |
IN |
|||||||||||||||||||
EC=100mF |
+ |
78L05 |
C=0.1mF |
|||||||||||||||||
Figure 10-2 Voltage Stabilizer Circuit
Programming Essentials: Input and Output |
193 |
10.1.3 Comparisons in PIC Programming
The power and usefulness of programs is due, in great measure, to their deci- sion-making ability, and decisions are based on comparison. In a comparison code, it is able to make decisions based on the relative values of two operands. For example, compare the values a and b. If a is greater than b execute a certain code routine. If b is greater than a, execute another one. If both operands have the same value then proceed to a third code branch.
CISC and even some RISC microprocessors contain a compare operator in their instruction set. However, the compare can be substituted, with some inconvenience, by a subtraction. Since there is no compare operation in the PIC instruction set, we have to simulate the comparison by subtracting the w register from a literal value or from a file register. The sublw and subwf instructions can be used. After the subtraction takes place, code can make decisions based on the state of the zero and the carry flags. For example, the following code fragment compares the value in the two registers, labeled OP1 and OP2 respectively, and directs execution to three possible routines:
; Declare variables at 2 memory locations
OP1 |
equ |
0x0c |
; First operand |
|
OP2 |
equ |
0x0d |
; second operand |
|
. |
||||
. |
||||
. |
||||
main: |
||||
movlw |
0x30 |
; First operand |
||
movwf |
OP1 |
; to OP1 register |
||
movlw |
0x50 |
; Second operand |
||
movwf |
OP2 |
; To OP2 register |
||
movf |
OP2,w |
; OP2 to w register (not really |
||
; necessary) |
||||
subwf |
OP1,w |
; Subtract w (OP2) from OP1 |
||
btfsc |
STATUS,2 ; 2 is zero bit. Test zero flag. |
|||
; Skip next instruction if Z bit = 0, |
||||
; that is if both numbers are not the |
||||
; same |
||||
goto |
ops_are_eq |
; OP2 = w routine |
||
;At this point the zero flag is not set. Therefore the two operands
;are not equal
;Now test the carry flag for OP1 < OP2, in this case C = 1
btfss |
STATUS,0 ; 0 is carry bit. Test carry flag |
|
; and skip next instruction if |
||
; C bit = 1 |
||
goto |
op2big |
; OP2 > w routine |
; Processing for the case OP1 > OP2 |
||
nop |
||
goto |
done |
|
194 Chapter 10
ops_are_eq:
; Processing for the case OP1 = OP2l nop
nop
goto done
op2big:
; Processing for the case OP1 < OP2 nop
nop
done:
goto |
done |
end |
The Infamous PIC Carry Flag
In PIC programming, the effects on the carry flag are different in addition than in subtraction. During addition (addwf and addlw) the carry flag indicates a carry-out of the most significant bit of the result. In this case, C = 1 if there was a carry out, and C = 0 otherwise. However, in subtraction the carry flag is described in the Microchip documentation as behaving as an inverted borrow. This means that when two numbers are subtracted and the result is too big to fit in the destination operand, then the carry flag is clear. What this amounts to is that in PIC subtraction (sublw and subwf operations) the carry bit is set if there is no carry-out of the high-order bit. This unusual behavior is shown in the preceding code fragment.
10.2 Simple Circuits and Programs
In the following sections we describe very simple PIC-based circuits that can be assembled with few components on a breadboard. The corresponding programs exercise the circuit components. The beginner should not skip building these circuits and coding the programs since they demonstrate essential hardware and software elements.
As a learning experience, it is a good idea to reverse engineer the code in these sample programs. With the processor’s instruction set at hand, listed in Appendix C, proceed to follow the code one instruction at a time until you can understand every processing detail.
10.2.1 A Single LED Circuit
One of the simplest circuits consists of a single LED lamp wired to Port-B, line 0, of a 16F84A PIC, as shown in Figure 10-3.
The power source for the circuit in Figure 10-3 is not shown in the diagram. Typically, a battery source or an AC/DC converter and a voltage stabilizer circuit as in the one in Figure 10-2 are used.
A program to turn on the LED on Port-B, line 0, requires a few but essential processing operations. Code must perform the following operations:
Programming Essentials: Input and Output |
195 |
+5v |
1
2
R=10K
3
4
5
6
7
8
9
LED R=330Ohm
RA2
RA3
RA4/TOCKI
MCLR
Vss
16F84A
RB0/INT
RB1
RB2
RB3
18
RA1
Osc
17
RA0
16
OSC1
15
OSC2
14
Vdd +5v
13
RB7
12
RB6
11
RB5
10
RB4
Figure 10-3 Simple LED Circuit
1.Define and select processor (in this case 16F84A).
2.Link-in the corresponding include file (p16f84A.inc).
3.Select the oscillator type (here external resonator, _XT type).
4.Direct execution to the main label.
5.Initialize Port-B for output.
6.Set line 0 in Port-B high.
The entire program is as follows:
;File: LEDOn.asm
;Date: June 1, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;
;Description:
;Turn on LED wired to Port-B, line 0 ;===========================
;switches ;===========================
;Switches used in __config directive:
; |
_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 |
occilator |
|
; * _XT_OSC |
External |
parallel |
resonator/crystal oscillator |
|
; |
_HS_OSC |
High speed crystal resonator (8 to 10 MHz) |
196 |
Chapter 10 |
|
; |
Resonator: Murate Erie CSA8.00MG = 8 MHz |
|
; |
_RC_OSC |
Resistor/capacitor oscillator |
;|
;|_____ * indicates setup values
processor |
16f84A |
include |
<p16f84A.inc> |
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
;========================================================
; variables in PIC RAM
;======================================================== ; None used ;========================================================
; m a i n p r o g r a m
;========================================================
org |
0 |
; start at address 0 |
goto |
main |
;=============================
;space for interrupt handler ;=============================
org 0x04 ;=============================
;main program ;============================= main:
;Initialize all line in Port-B for output
movlw |
B’00000000’ |
; w = 00000000 binary |
||
tris |
PORTB |
; Set up Port-B for output |
||
; Turn on line 0 in Port-B. All others remain off |
||||
movlw |
B’00000001’ |
|||
; ———-| |
||||
; |
| |
|____ Line 0 ON |
||
;|________ All others off
movwf PORTB
; Endless loop intentionally hangs up program wait:
goto wait
end
The preceding program, named LEDOn, can be found in the book’s online software.
LED Flasher Program
A different program makes the LED in the circuit in Figure 10-3 flash on and off. All that is necessary is a delay loop using a file register counter. The logic turns on the LED and counts down to zero. Then it turns the LED off and counts down again.