Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8547

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Programming Essentials: Input and Output

197

The counter routine demonstrates the creation of a procedure in PIC programming. In fact, a procedure is nothing more than a routine called by a label at its entry point and terminated with a return statement. The procedure is executed by a call statement to its initial label, as follows:

call

delay

; Call to procedure

.

.

.

; Elsewhere in the program

delay:

; procedure instructions go here

return

; End of procedure

The simplest delay loop consists of wasting processor time. Since each instruction takes four clock cycles, the delay can be calculated by multiplying the number of instructions in the loop by the device’s clock speed divided by four. The details of delay loops are discussed in Chapter 12, on timers and counters. Here we just present a double-counter loop without entering into timing details.

The timer loop requires two counters, since the maximum value that can be stored in a register file is 255 and a delay of 255 machine cycles is very short. In this example, we get around this limitation by creating double counters: an inner loop counts down 200 cycles and an outer loop repeats the inner loop 200 times. The result is that the routine repeats 200 multiplied by 200 times, or 40,000 iterations, which is sufficient for the purpose at hand. Code is as follows:

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

Code assumes that two variables were created in the processor’s GPR space, as follows:

; Declare variables at 2 memory locations

j

equ

0x0c

k

equ

0x0d

The listing for the entire LEDFlash program, contained in the book’s online software, is as follows:


198

Chapter 10

;File: LEDFlash.asm

;Date: June 2, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;

;Description:

;Turn on and off 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)

;

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 ;=============================

org 0x04 ;=============================

;main program ;============================= main:


Programming Essentials: Input and Output

199

;

Initialize all line in Port-B for output

movlw

B’00000000’

;

w =

00000000 binary

tris

PORTB

;

Set

up Port-B for output

;

;Program loop to turn LED on and off LEDonoff:

;Turn on line 0 in Port-B. All others remain off

movlw

B’00000001’

; LED ON

movwf

PORTB

call

delay

; Local delay routine

; Turn off line 0 in Port-B.

movlw

B’00000000’

; LED OFF

movwf

PORTB

call

delay

goto

LEDonoff

;================================

;

delay subroutine

;================================

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.2.2 LED/Pushbutton Circuit

A slightly more complex circuit contains a pushbutton switch. In this case, the program monitors the state of the pushbutton and lights the LED accordingly. Figure 10-4 (in the following page) shows one possible wiring for the LED/pushbutton circuit.

If a switch reports a zero bit when active, it is described as active-low. A switch that reports a one-bit when pressed is said to be active-high. The pushbutton switch on the preceding figure is active-low. In the same manner, an output device can be wired so that it is turned on with a logic 0 and off with logic 1 on the port pin. A device turned on by the port current is said to be a source current device. When the device is turned on when the port reports logic 0 the line is said to sink the current. PICs and other CMOS devices operate better sinking than sourcing current. Table 10.2 shows the maximum sink and source currents for the 16F84 ports.


200

Chapter 10

+5 V

4 MHz

R=4.7K Ohm

R=330 Ohm

Osc

LED

+5 V

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1 OSC2

Vdd

RB7

RB6

RB5

RB4

16F84

RA2

RA3

T0Tkl

MCLR Vss

RB0/INT RB1

RB2

RB3

+5 V

1

2

3

4

5

6

7

8

9

10K Ohms

Figure 10-4 LED/pushbutton Experimental Circuit

Table 10.2

Sink and Source Current for 16F84 Ports

SOURCE

ANY I/O PIN

PORT A

PORT-B

sink current

25 mA

80 mA

150 mA

source current

20 mA

50 mA

100 mA

The 4.7K Ohm resistor in the circuit of Figure 10-4 keeps RA0 high until the switch is pressed. This switch action determines that RA0 reads binary one when the switch is released and binary zero (low) when the switch is pressed (active).

To test if the switch in the circuit of Figure 10-4 is closed, the application can read RA0. If the value in the port is 1, then the switch is open (released). If 0, then the switch is closed. The following program, named LEDandPb, exercises the circuit in Figure 10-4:

;File: LEDandPb.asm

;Date: June 2, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Circuit with LED wired to RB0 and pushbutton switch,

;active low, wired to RA0. Pushbutton action turns LED

;OFF when pressed and ON when released.


Programming Essentials: Input and Output

201

;===========================

;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 (simplest, 20%

error)

;|

;|_____ * indicates setup values

processor

16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;=====================================================

;

variables in PIC RAM

;===================================================== ; Not used in this program

;========================================================

; 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 lines in Port-B for output

movlw

B’00000000’

; w

=

00000000

binary

tris

PORTB

; Set

up

Port-B for output

; Initialize Port-A, line 0, for input

movlw

B’00000001’

;

w

=

00000001

binary

tris

PORTA

;

Set

up

RA0 for input

; Program loop to test state of pushbutton switch ;==============================