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

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

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

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

Добавлен: 14.06.2025

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

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

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

660

Appendix D

processor 16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

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

;

PIC register equates

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

porta

equ

0x05

portb

equ

0x06

status

equ

0x03

z

equ

0x02

c

equ

0x00

tmr0

equ

0x01

; countL

equ

0x01

; Alias for tmr0

;

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

;

variables in PIC RAM

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

; Local variables

cblock

0x0d

; Start of block

J

; counter J

K

; counter K

;3-byte auxiliary counter. Low order byte is kept

;in the timer0 register

countM

; Medium byte

countH

; High byte

endc

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

; m a i n p r o g r a m

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

org

0

; start at address 0

goto

main

;

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

;

interrupt handler

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

org

0x04

;goto IntServ ;=============================

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

;Clear the watchdog timer and reset prescaler clrf tmr0

clrwdt

;Set up the OPTION register bit map


Supplementary Programs

661

movlw

b'11011000'

;

7

6

5

4

3

2 1 0 <= OPTION bits

;

|

|

|

|

|

|__|__|_____

PS2-PS0 (prescaler bits)

;

|

|

|

|

|

Values for Timer0

;

|

|

|

|

|

*000

= 1:2

001 = 1:4

;

|

|

|

|

|

010

= 1:8

011 = 1:16

;

|

|

|

|

|

100

= 1:32

101 = 1:64

;

|

|

|

|

|

110

= 1:128 *111 = 1:256

;

|

|

|

|

|______________

PSA

(prescaler assign)

;

|

|

|

|

1

=

to WDT

;

|

|

|

|

*0

=

to Timer0

;

|

|

|

|_________________

TOSE (Timer0 edge select)

;

|

|

|

0

=

increment on low-to-high

;

|

|

|

*1

=

increment in high-to-low

;

|

|

|____________________

TOCS (TMR0 clock source)

;

|

|

*0

=

internal clock

;

|

|

1

=

RA4/TOCKI bit source

;

|

|_______________________

INTEDG (Edge select)

;

|

*0

=

falling edge

;|__________________________ RBPU (Pullup enable)

;

0 = enabled

;

*1 = disabled

option

; Setup ports

movlw

0x00

; Set port B to output

tris

portb

clrf

portb

; All port B to 0

; Port A is not used in this program

mloop:

bsf

portb,0

call

TM0delay

bcf

portb,0

call

TM0delay

goto

mloop

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

;one second delay sub-routine

;using Timer0 ;*********************************

;Routine logic:

;The prescaler is assigned to timer0 and setup so

;that the timer runs at 1:2 rate. This means that

;every time the counter reaches 128 (0x80) a total

;of 256 machine cycles have elapsed. The value 0x80

;is detected by testing bit 7 of the counter

;register. This method gives the routine a total of

;128 machine cycles before the next counter beat must

;be acknowledged.

TM0delay:


662

Appendix D

;Timer is designed to count from 0 to 1,000,000

;1,000,000 = 0x0f 0x42 0x40

;

---- ---- ----

;

|

|

|___

(see note)

;

|

|________

countM

;

|_____________

countH

;Note:

;The initial count of 0x40 (64 decimal) is ensured

;by initializing the tmr0 register to count 32 timer

;beats at the 1:2 prescaler rate. 128 - 32 = 96 = 0x60

;Initialize the counters:

movlw 0x0f

movwf countH

movlw 0x42

movwf countM

movlw 0x60

movwf tmr0

;Routine tests timer overflow by testing bit 7 of

;the tmr0 register.

cycle:

movlw 3

subwf tmr0,w

btfsc status,c

goto cycle

;Subtract 256 from beat counter by decrementing the

;mid-order byte

decfsz countM,f

goto

cycle

; Continue if mid-byte not

zero

;At this point the mid-order byte has overflowed.

;High-order byte must be decremented.

decfsz countH,f

goto cycle

; At this point one second has elapsed

return

end

Supplementary Programs

663

; File name: SevenSeg.asm

;Date: April 19, 2006

;Author: Julio Sanchez

;Reference: SevenSeg Circuit and Board

;Description:

;Test program for reading four toggle switches and

;displaying the represented hex number on seven-segment

;LED. Also contains a pushbutton switch to activate a

;piezo buzzer. Switches are wired active low.

;

; 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

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

;setup and configuration ;=========================

processor 16f84A include <p16f84A.inc>

__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;

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

;

constant definitions

;

(per circuit wiring diagram)

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

#define Pb_sw 4 ; Port A line 4 to push button switch

;

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

; PIC register equates

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

Porta

equ

0x05

Portb

equ

0x06

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


664

Appendix D

;

local variables

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

cblock

0x0c

; Start of block

J

; counter J

K

; counter K

endc

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

;

program

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

org

0

; start at address 0

goto

main

;

; Space for interrupt handlers

org

0x08

main:

; Port A. Five low-order lines set for input

movlw

B'00011111'

; w = 00011111 binary

tris

porta

; port

A (lines

0 to 4) to

input

; Port B. All eight 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 port A bit 4

;Switch logic is active low

btfss

porta,Pb_sw

; Test and skip if switch bit

; set

goto

buzzit

; Buzz 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 monitoring ;============================ readdip:

;Read port A switches

movf

porta,w

; Port A bits to w

;Since board is wired 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

; Mask off 4 high-order bits


Supplementary Programs

665

andlw

b'00001111'

; And with mask

;At this point the w register contains a 4-bit value

;in the range 0 to 0xf. Use this value (in w) to

;obtain seven-segment display code

call

segment

movwf

portb

; Display switch bits

goto

pbutton

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

; routine to returns 7-segment

;

codes

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

segment:

addwf

PCL,f

; PCL is program counter latch

retlw

0x3f

; 0 code

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

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

; long delay sub-routine