Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8598
Скачиваний: 0
202 |
Chapter 10 |
;read PB switch state ;============================== LEDctrl:
;Push button switch on demo board is wired to Port-A bit 0
;Switch logic is active low
btfss |
PORTA,0 |
; Test. Skip |
next line |
if |
|
; |
bit is set |
||||
goto |
turnOFF |
; |
Turn LED off routine |
||
;At this point Port-A bit 0 is not set
;Switch is pressed (active low action)
;Turn ON line 0 in Port-B
bsf |
PORTB,0 |
; RB0 high |
goto |
LEDctrl |
|
turnOFF: |
||
; Routine to turn OFF LED |
||
bcf |
PORTB,0 |
; RB0 low |
goto |
LEDctrl |
|
End
10.2.3 Multiple LED Circuit
The following circuit allows a few more programming complications since it contains a battery of eight LEDs, all wired to Port-B.
+5v
1 |
16F84 |
18 |
|||||||||
2 |
RA2 |
RA1 |
Osc |
||||||||
17 |
|||||||||||
R=10K |
RA3 |
RA0 |
|||||||||
3 |
RA4/TOCKI |
OSC1 |
16 |
||||||||
4 |
15 |
||||||||||
5 |
MCLR |
OSC2 |
|||||||||
Vss |
Vdd |
14 |
+5v |
||||||||
6 |
|||||||||||
13 |
|||||||||||
RB0/INT |
RB7 |
||||||||||
7 |
12 |
||||||||||
RB1 |
RB6 |
||||||||||
8 |
11 |
||||||||||
RB2 |
RB5 |
||||||||||
9 |
10 |
||||||||||
RB3 |
RB4 |
||||||||||
R=330x8 Ohm
Figure 10-5 Multiple LED Circuit
Programming Essentials: Input and Output |
203 |
The circuit in Figure 10-5 can be programmed to do different functions. For example, the eight LEDs can be visualized as representing an 8-bit binary number and the circuit can be programmed to count in binary from 0 to 255. Since the eight LEDs are all wired to Port-B, the binary count can be directly echoed on the port. The following program, named LEDCount, performs this operation:
;File: LEDCount.asm
;Date: June 3, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Circuit with eight LEDs wired to RB0 to RB7.
;Program displays a binary count from 0 to 255 on
;LEDs.
;===========================
;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) |
|
; |
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
;===================================================== ; Declare variables at 2 memory locations
j |
equ |
0x0c |
k |
equ |
0x0d |
;========================================================
; m a i n p r o g r a m
;========================================================
org |
0 |
; start at address 0 |
goto |
main |
;============================= ; space for interrupt handler ;=============================
204 |
Chapter 10 |
org 0x04 ;=============================
;main program ;============================= main:
;Initialize all lines in Port-B for output
movlw |
B’00000000’ |
; w = 00000000 binary |
||
tris |
PORTB |
; Set up Port-B for output |
||
; Set Port-B bit 0 ON |
||||
movlw |
B’00000000’ |
; w := 0 binary |
||
movwf |
PORTB |
; Port-B itself := w |
||
; Clear the carry bit |
||||
bcf |
STATUS,C |
|||
mloop: |
||||
incf |
PORTB,f |
; Add 1 to register value |
||
call |
delay |
|||
goto |
mloop |
|||
;================================ |
||||
; |
delay sub-routine |
|||
;================================ |
||||
delay: |
||||
movlw |
.200 |
; w = 200 decimal |
||
movwf |
j |
; j = w |
||
jloop: |
||||
movwf |
k |
; k = w |
||
kloop: |
||||
decfsz |
k,f |
; k = k-1, skip next if zero |
||
goto |
kloop |
|||
decfsz |
j,f |
; j = j-1, skip next if zero |
||
goto |
jloop |
|||
return |
||||
end
10.3 Programming the Seven-segment LED
A 7-segment display can be connected to output ports on the PIC and used to display numbers and some digits. The circuit in Figure 10-6 shows one possible wiring scheme.
As the name indicates, the seven-segment display has seven linear LEDs that allow forming all the decimal and hex digits and some symbols and letters. Once the mapping of the individual bars of the display to the PIC ports has been established, digits and letters are shown by selecting which port lines are set and which are not. For example, in the seven-segment LED of Figure 10-5, the digit 2 is displayed by setting segments a, b, g, e, and d. In this particular wiring, these segments correspond to Port-B lines 0, 1, 6, 4, and 5.
Programming Essentials: Input and Output |
205 |
R=10K
+5v
1 |
16F84 |
18 |
||||||
RA2 |
RA1 |
|||||||
2 |
17 |
Osc |
||||||
RA3 |
RA0 |
|||||||
3 |
16 |
|||||||
RA4/TOCKI |
OSC1 |
|||||||
4 |
15 |
|||||||
MCLR |
OSC2 |
|||||||
5 |
14 |
+5v |
+5v |
|||||
Vss |
Vdd |
|||||||
6 |
13 |
7-segment |
||||||
RB0/INT |
RB7 |
|||||||
7 |
12 |
LED |
||||||
RB1 |
RB6 |
f |
a |
|||||
8 |
11 |
|||||||
RB2 |
RB5 |
|||||||
9 |
g |
a |
b |
|||||
10 |
||||||||
RB3 |
RB4 |
|||||||
f |
b |
|||||||
g |
||||||||
220 R |
PWR |
|||||||
X 7 |
e |
e |
c |
|||||
ON |
||||||||
d |
d |
c |
||||||
Figure 10-6 Seven-segment LED Circuit
As the name indicates, the seven-segment display has seven linear LEDs that allow forming all the decimal and hex digits and some symbols and letters. Once the mapping of the individual bars of the display to the PIC ports has been established, digits and letters are shown by selecting which port lines are set and which are not. For example, in the seven-segment LED of Figure 10-6, the digit 2 is displayed by setting segments a, b, g, e, and d. In this particular wiring, these segments correspond to Port-B lines 0, 1, 6, 4, and 5.
Conversion of the individual digits to port display codes is easily accomplished by means of a lookup table. The processing depends on three special features of PIC assembly language:
•The program counter file register (labeled PC and located at offset 0x02) holds the address in memory of the current instruction. Since each PIC instruction takes up a single byte (except for those that modify the PC), one can jump to consecutive entries in a table by adding an integer value to the program counter.
•The addwf instruction is used to add a value in the w register to the program counter.
•The retlw instruction returns to the caller a literal value stored in the w register. In the case of retlw the literal value is the instruction operand.
If the lookup table is located at a subroutine called getcode, then the processing can be implemented as follows: