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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

747

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

;end of keypad? ;=========================

;Test for last key row (maximum count is 4)

decfsz rowCount,f ; Decrement counter goto keyLoop

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

;display scan code ;=========================

;At this point all keys have been tested

;variable scanCode holds scan code

movf

scanCode,w

movwf

PORTD

call

long_delay

;

Debounce

goto

keyScan

;

Continue

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

;calculate scan code ;==========================

;The column position is added to the row code (stored

;in rowCode register). Sum is the scan code

col0:

movf

rowCode,w

; Row code to w

addlw

0x00

; Add 0 (clearly not necessary)

movwf

scanCode ; Final value

return

col1:m

movf

rowCode,w

; Row code to w

addlw

0x01

; Add 1

movwf

scanCode

return

col2:

movf

rowCode,w

; Row code to w

addlw

0x02

; Add 2

movwf

scanCode

return

col3:

movf

rowCode,w

; Row code to w

addlw

0x03

; Add 3

movwf

scanCode

return

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

;

long delay sub-routine

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


748

Appendix D

long_delay

movlw

D'200'

; w delay count

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

Supplementary Programs

749

;File name: RamDemo.asm

;Last revision: May 27, 2006

;Author: Julio Sanchez

;PIC: 16F877

;Description:

;Program to demonstrate access to General Purpose Register

;located in different banks. 16F877 GPR space is as

;follows:

; BANK 0

BANK 1

BANK 2

BANK 3

;

0x20

0xa0

0x110

0x190

|

;

--

--

--

--

| bank

;

--

--

--

--

| related

;

--

0xef

0x16f

0x1ef |

;

--

0xf0

0x170

0x1f0 |

;

0x70

<== access registers at 0x70-0x7f

;

0x7f

these GPRs are common to all banks

;

;Program is designed to be used with a debugger so that

;access to different registers can be tested

;GPR registers are named according to the following format:

;

REGx_yyy

;

|

|||

;

|

|||_________

hex address

(up to 3

digits)

;

|_____________

bank number

(0 to 3)

;

;CONCLUSIONS:

;GPRs located between 0x70 and 0x7f can be accessed from

;any bank. GPRs at other addresses require previous bank

;selection

;

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

;16F877 switches ;===========================

;Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_BODEN_ON

Brown-out reset enable ON/OFF

; *

_BODEN_OFF

;

*

_PWRTE_ON

Power-up timer enable ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer

ON/OFF

; * _WDT_OFF

;

_LPV_ON

Low voltage IC

programming enable ON/OFF

; * _LPV_OFF


750

Appendix D

;

_CPD_ON

Data EE memory code protection ON/OFF

;* _CPD_OFF

;OSCILLATOR CONFIGURATIONS:

;

_LP_OSC

Low power crystal

occilator

;

_XT_OSC

External parallel

resonator/crystal oscillator

;* _HS_OSC

;_RC_OSC

;|

High speed crystal resonator Resistor/capacitor oscillator (simplest, 20% error)

;|

;|_____ * indicates setup values presently selected

processor 16f877 ; Define processor

#include <p16f877.inc>

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF

;__CONFIG directive is used to embed configuration data

;within the source file. The labels following the directive

;are located in the corresponding .inc file.

errorlevel -302

; Supress bank-related warning

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

; M A C R O S

;============================================================ ; Macros to select the register banks

Bank0

MACRO

; Select RAM bank 0

bcf

STATUS,RP0

bcf

STATUS,RP1

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0

bcf

STATUS,RP1

ENDM

Bank2

MACRO

; Select RAM bank 2

bcf

STATUS,RP0

bsf

STATUS,RP1

ENDM

Bank3

MACRO

; Select RAM bank 3

bsf

STATUS,RP0

bsf

STATUS,RP1

ENDM

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

; PIC register equates

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

reg0_20 equ

0x20


Supplementary Programs

751

reg0_50

equ

0x50

reg0_7e

equ

0x7e

reg1_a0

equ

0xa0

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

;

P R O G R A M

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

Bank0

nop

movlw

0xee

; Test value

movwf

reg0_20

movwf

reg0_7e

;

Bank1

; Register at address 0x20 in bank 0 cannot be accessed

nop

movf

reg0_20,w

nop

; However, the register at 0x7e CAN be accessed

movf

reg0_7e,w

nop

Bank0

movlw

0x0

movf

reg0_7e,w

nop

; How about from bank 3

Bank3

movlw

0x0

movf

reg0_7e,w

nop

loopHere:

goto

loopHere

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

end ; END OF PROGRAM

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


752

Appendix D

;File name: RTC6355.asm

;Last revision: June 4, 2006

;Author: Julio Sanchez

;PIC: 16F877

;Description:

;Program to demonstrate programming of the NJU6355ED

;realtime clock IC.

;

;Operation:

;WARNING:

;Code assumes 10Mhz clock. Delay routines must be

;edited for faster clock. Clock speed also determines

;values for baud rate setting (see spbrgVal constant).

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

;16F877 switches

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

; Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_BODEN_ON

Brown-out reset enable ON/OFF

; *

_BODEN_OFF

;

*

_PWRTE_ON

Power-up timer enable ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer

ON/OFF

; * _WDT_OFF

;

_LPV_ON

Low voltage IC

programming enable ON/OFF

; * _LPV_OFF

;

_CPD_ON

Data EE memory

code protection ON/OFF

;* _CPD_OFF

;OSCILLATOR CONFIGURATIONS:

;

_LP_OSC

Low power crystal

occilator

;

_XT_OSC

External parallel

resonator/crystal oscillator

;* _HS_OSC

;_RC_OSC

;|

High speed crystal resonator Resistor/capacitor oscillator (simplest, 20% error)

;|

;|_____ * indicates setup values presently selected

processor 16f877 ; Define processor

#include <p16f877.inc>

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF