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

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

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

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

Добавлен: 14.06.2025

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

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

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

Analog to Digital and Realtime Clocks

583

J

; counter J

K

; counter K

index

store1

; Local storage

store2

; For LCDscroll procedure

LCDcount ; Counter for characters per line

LCDline

; Current display line (0 or 1)

endc

; Common RAM area for most critical variables

cblock

0x70

; Storage for ASCII decimal conversion and digits

inNum

; Source operand

thisDig

; Digit counter

endc

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

;

P R O G R A M

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

;Wiring:

;LCD data to Port-D, lines 0 to 7

;E line -> Port-E, 1

;RW line -> Port-E, 2

;RS line -> Port-E, 0

;Set PORTE D and E for output

;First, initialize Port-B by clearing latches clrf STATUS

clrf PORTB

;Select bank 1 to tris Port-D for output Bank1

;Tris Port-D for output. Port-D lines 4 to 7 are wired

;to LCD data lines. Port-D lines 0 to 4 are wired to LEDs. movlw B’00000000’

movwf

TRISD

; and Port-D

;By default Port-A lines are analog. To configure them

;as digital code must set bits 1 and 2 of the ADCON1

;register (in bank 1)

movlw

0x06

; binary 0000 0110 is code to

; make all Port-A lines

digital

movwf

ADCON1

; Port-B, lines are not used by this application. Init


584

Chapter 16

; to output

movlw

b’00000000’

movwf

TRISB

; Tris

Port-E for output. LCD lines

are in Port-E

movwf

TRISE

; Tris Port-E

; Enable Port-B pullups for switches in OPTION register

;

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

movlw

b’00001000’

movwf

OPTION_REG

;Back to bank 0 Bank0

;Clear all output lines

movlw

b’00000000’

movwf

PORTD

movwf

PORTE

; Wait and initialize HD44780

call

delay_5

; Allow LCD time to initialize

itself

call

initLCD

; Then do forced

initialization

call

delay_5

; (Wait probably not

necessary)

; Clear character counter and line counter variables

clrf

LCDcount

clrf

LCDline

; Initialize A/D conversion lines

call

InitA2D

; Local procedure

; Store base address of text buffer in PIC RAM


; Conversion routine

Analog to Digital and Realtime Clocks

585

movlw

0x20

;

Start address for

buffer

movwf

pic_ad

;

to local variable

;Store 20 blanks in PIC RAM, starting at address stored

;in variable pic_ad

call blank20

;Call procedure to store ASCII characters for message

;in text buffer

movlw

d’0’

; Offset into buffer

call

storeMS1

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

;

read POT digital value

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

readPOT:

call

ReadA2D

; Local procedure

;w has digital value read from analog line RA0

;Display result

call bin2asc

;At this point three ASCII digits are stored in local

;variables. Move digits to display area

movf

asc1,w

; Unit digit

movwf

0x2e

; Store in buffer

movf

asc10,w

; same with other digits

movwf

0x2d

movf

asc100,w

movwf

0x2c

;Display line

;Set DDRAM address to start of first line showLine:

call line1

; Call procedure to display 16 characters in LCD call display20

goto readPOT

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

;============================================================ ; L O C A L P R O C E D U R E S ;============================================================ ;============================================================ ;==========================

;init LCD for 4-bit mode ;========================== initLCD:

;Initialization for Densitron LCD module as follows:

;4-bit interface

;2 display lines of 20 characters each

;cursor on

;left-to-right increment

;cursor shift right


586

Chapter 16

;

no display shift

;=======================|

;

set command mode

|

;=======================|

bcf

PORTE,E_line

; E line low

bcf

PORTE,RS_line

; RS line low

bcf

PORTE,RW_line

; Write mode

call

delay_125

; delay 125 microseconds

;***********************|

;

FUNCTION SET

|

;***********************|

movlw

0x28

; 0 0 1 0 1 0 0 0 (FUNCTION SET)

call

send8

; 4-bit send routine

; Set 4-bit mode command must be repeated

movlw

0x28

call

send8

;***********************|

; DISPLAY AND CURSOR ON |

;***********************|

movlw

0x0e

; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF)

call

send8

;***********************|

;

set entry mode

|

;***********************|

movlw

0x06

; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)

call

send8

;***********************|

; cursor/display shift

|

;***********************|

movlw

0x14

; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY

SHIFT)

call

send8

;***********************|

;

clear display

|

;***********************|

movlw

0x01

; 0 0 0 0 0 0 0 1 (CLEAR DISPLAY)

call

send8

; Per documentation

call

delay_5

; Test for busy

return

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

;Procedure to delay

;125ms. at 10Mhz ;======================= delay_125:

movlw

.110

; Repeat 110 machine cycles


Analog to Digital and Realtime Clocks

587

movwf

count1

; Store value in counter

repeat:

decfsz

count1,f

; Decrement counter

goto

repeat

; Continue if not 0

return

; End of delay

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

;Procedure to delay

;5 milliseconds ;======================= delay_5:

movlw

.110

; Counter = 110

movwf

count2

; Store in variable

delay:

call

delay_125

; Delay

decfsz

count2,f

; 40 times = 5 milliseconds

goto

delay

return

; End of delay

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

;

pulse E line

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

pulseE:

bsf

PORTE,E_line

; Pulse E line

nop

bcf

PORTE,E_line

return

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

;

long delay sub-routine

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

long_delay:

movlw

.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

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

;display buffer on LCD ;=============================

;Sends 20 characters from PIC buffer with address stored

;in variable pic_ad to LCD line previously selected display20: