Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

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:

206

 

Chapter 10

getcode:

 

 

addwf

PC,f

; Add value in w register to program

counter

 

 

retlw

0x3f

; code for number 0

retlw

0x06

; code for number 1

retlw

0x5b

; code for number 2

...

 

 

retlw

0x6f

; code for number 9

The calling routine places in the w register the numeric value whose code is desired, and then calls the table lookup, as follows:

movlw

0x03

;

Code for number 3 desired

call

getcode

 

 

movwf

PORTB

;

Display 3 in 7-segment display

10.4 A Demonstration Board

A demonstration board, also known as a demo board, is a useful tool in mastering PIC programming. Many are available commercially; like programmers, there is a cottage industry of PIC demo boards on the internet. Constructing your own demo boards and circuits is not difficult. The components can be placed on a breadboard, or wire-wrapped onto a special circuit board, or a printed circuit board can be homemade, or ordered through the internet. These options have been previously discussed and Appendix B contains instructions on how to build your own PCBs.

Figure 10-7 shows a simple 16F84A-based demo board with a seven-segment LED, buzzer, pushbutton switch, and a bank of four toggle switches.

10.4.1 PCB Images for Demo Board

Some PCBs contain circuit etchings on both sides. In this case two circuit board images are required. In addition, most boards contain a top-side image of the components, company logos, model numbers, and other information. Commercially, this image is silk-screened onto the board.

The homemade board (see Appendix B) usually contains a single etched image and a top-side image with informational text and graphics. Both images can be created with a conventional drawing program, such as Corel Draw, Adobe Illustrator, or Windows Paint, or with a specialized application, several of which are available free and for purchase on the Web. Figure 10-7 shows the images used for making the PCB for the circuit in Figure 10-8.

Note that the top-side (text) image has been mirrored on the horizontal plane. This is necessary so that the text and graphics coincide with the circuit etchings once the images are transferred to the board. The process for making your own PCBs is described in Appendix B.

Programming Essentials: Input and Output

207

PB SW

R=10K

R=10K

1

2

3

4

5

6

7

8

9

+5v

10k R

X 4

DIP SW

 

 

 

 

 

 

Piezo

 

 

 

 

 

 

 

Buzzer

 

 

 

 

 

18

 

 

 

 

 

RA2

 

RA1

 

 

 

 

 

 

RA3

 

RA0

17

Osc

 

 

 

 

 

 

 

16

 

 

 

 

 

RA4/TOCKI

OSC1

 

 

 

 

 

 

 

 

 

15

 

 

 

 

 

MCLR

16F84A

OSC2

 

 

 

 

 

 

 

 

14

+5v

 

 

 

 

Vss

 

Vdd

 

 

 

 

 

 

 

 

13

 

 

7-segment

 

RB0/INT

 

RB7

 

 

 

 

 

 

 

12

 

 

 

LED

 

RB1

 

RB6

 

 

f

 

 

a

 

 

 

 

 

 

 

 

 

 

11

 

 

 

 

 

RB2

 

RB5

 

 

 

 

 

 

 

 

 

 

 

g

 

a

b

 

 

 

10

 

 

 

 

RB3

 

RB4

 

 

 

 

 

 

 

 

 

 

 

 

f

b

 

 

 

 

 

 

 

 

g

 

 

 

 

 

220 R

 

 

 

PWR

 

 

 

 

X 7

e

e

c

 

 

 

 

ON

 

 

 

 

 

 

 

 

 

 

 

 

 

d

 

d

c

Figure 10-7 PIC 16F87A Demo Board

Figure 10-8 Bottomand Top-side images of a PCB.

208

Chapter 10

10.4.2 TestDemo1 Program

The following program exercises some of the experiments that can be implemented on the demo boards in Figure 10-7:

;File: TestDemo1.asm

;Date: June 2, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Program to exercise the demonstration circuit and board

;number 1

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

;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

oscillator

 

; * _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

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

 

cblock

0x0c

; Start of block

 

count1

 

; Counter # 1

 

j

 

; counter J

 

k

 

; counter K

 

endc

 

 

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

; P R O G R A M

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

org

0

; start at address 0

goto

main

 

;

 

 

Programming Essentials: Input and Output

 

209

; Space for interrupt handlers

 

 

org

0x08

 

 

main:

 

 

 

; Port A (5 lob) for input

 

 

movlw

B’00011111’

; w := 00001111

binary

tris

PORTA

; Port-A (lines

0 to 4) to

input

 

 

 

; Port-Bit (8 lines) for output

 

 

movlw

B’00000000’

; w := 00000000

binary

tris

PORTB

; Port-B to output

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

;Pushbutton switch processing ;============================== pbutton:

;Push button switch on demo board is wired to RA4

;Switch logic is active low

btfss

PORTA,4

; Test and skip if bit is set

goto

buzzit

; Buz if switch ON

; At this point Port-A bit 4 is set (switch is off)

call

buzoff

; Buzzer off

goto

readdip

; Read DIP switches

buzzit:

 

 

call

buzon

; Turn on buzzer

goto

pbutton

 

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

;DIP switch processing ;==============================

;Read all bits of Port-A readdip:

movf

PORTA,w

; Port A bits to w

;If board uses active low then all switch bits must be negated

;This is done by XORing with 1-bits

xorlw

b’11111111’

; Invert all bits in w

; Eliminate all 4 high order bits

 

andlw

b’00001111’

; And with mask

; Get digit into w

 

call

segment

; get digit code

movwf

PORTB

; Display digit

call

delay

; Give time

; Update digit and loop counter

 

goto

pbutton

 

;******************************* ; 7-segment table of hex codes ;******************************* segment:

addwf

PCL,f

;

PCL is

program counter latch

retlw

0x3f

;

0 code

 

210

 

Chapter 10

retlw

0x06

; 1

retlw

0x5b

; 2

retlw

0x4f

; 3

retlw

0x66

; 4

retlw

0x6d

; 5

retlw

0x7d

; 6

retlw

0x07

; 7

retlw

0x7f

; 8

retlw

0x6f

; 9

retlw

0x77

; A

retlw

0x7c

; B

retlw

0x39

; C

retlw

0x5b

; D

retlw

0x79

; E

retlw

0x71

; F

retlw

0x7f

; Just in case all on

;****************************

;piezo buzzer ON ;****************************

;Routine to turn on piezo buzzer on Port-B bit 7 buzon:

bsf

PORTB,7

; Tune on bit 7, Port-B

return

;****************************

;piezo buzzer OFF ;****************************

;Routine to turn off piezo buzzer on Port-B bit 7 buzoff:

 

bcf

PORTB,7

; Bit 7 Port-B clear

 

return

 

 

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

;

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