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

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

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

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

Добавлен: 14.06.2025

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

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

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

310

Chapter 13

;

|

|

1 = on, 0 = off

;

|

|____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

movwf

PORTB

call

pulseE

;pulseE and

delay

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

; DISPLAY AND CURSOR ON |

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

movlw

0x0e

; 0 0 0 0

1

1 1 0 (DISPLAY ON/OFF)

;

|

| | |___ Blink character

;

|

| |

1 = on, 0 = off

;

|

| |___ Cursor on/off

;

|

|

1 = on, 0 = off

;

|

|____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

movwf

PORTB

call

pulseE

;pulseE and

delay

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

;

ENTRY MODE SET

|

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

movlw

0x06

; 0 0 0 0

0

1 1 0 (ENTRY MODE SET)

;

| | |___ display shift

;

| |

1 = shift

;

| |

0 = no shift

;

| |____ increment mode

;

|

1 = left-to-right

;

|

0 = right-to-left

;

|___ COMMAND BIT

movwf

PORTB

;00000110

call

pulseE

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

; CURSOR/DISPLAY SHIFT

|

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

movlw

0x14

; 0 0 0 1

0

1 0 0 (CURSOR/DISPLAY

;

|

|

| | |

SHIFT)

;

|

|

| |_|___ don’t care

;

|

|_|__ cursor/display shift

;

|

00 = cursor shift left

;

|

01 = cursor shift right

;

|

10 = cursor and display

;

|

shifted left

;

|

11 = cursor and display


LCD Interfacing and Programming

311

;

|

shifted right

;

|___ COMMAND BIT

movwf

PORTB

;0001

1111

call

pulseE

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

;

CLEAR DISPLAY

|

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

movlw

0x01

; 0 0

0 0 0 0 0 1 (CLEAR DISPLAY)

;

|___ COMMAND BIT

movwf

PORTB

;0000

0001

;

call

pulseE

call

delay_5ms

;delay 5 milliseconds after

init

return ;************************************************************ ; DELAY AND PULSE PROCEDURES ;************************************************************ ;=======================

;Procedure to delay

;42 microseconds ;======================= delay_125mcs

movlw

D’42’

; Repeat 42 machine cycles

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_5ms

movlw

D’41’

; Counter = 41

movwf

count2

; Store in variable

delay

call

delay_125mcs

; Delay

decfsz

count2,f

; 40 times = 5 milliseconds

goto

delay

return

; End of delay

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

;

pulse E line

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

pulseE

bsf

PORTA,E_line

;pulse E line

bcf

PORTA,E_line


312

Chapter 13

call

delay_125mcs

;delay 125 microseconds

return

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

;long delay sub-routine

;(for debugging) ;============================= long_delay

movlw

D’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

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

;LCD display procedure ;=============================

;Sends 16 characters from PIC buffer with address stored

;in variable pic_ad to LCD line previously selected display16:

;Set up for data

bcf

PORTA,E_line

; E line low

bsf

PORTA,RS_line

; RS line low for control

call

delay_125mcs

; Delay

; Set up counter for 16 characters

movlw

D’16’

;

Counter = 16

movwf

count3

; Get display address from local variable pic_ad

movf

pic_ad,w ;

First display RAM address to W

movwf

FSR

;

W to FSR

getchar:

movf

INDF,w

;

get character from display RAM

;

location pointed to by file select

;

register

movwf

PORTB

call

pulseE

;send data to display

; Test for 16 characters displayed

decfsz

count3,f

; Decrement counter

goto

nextchar ;

Skipped if done

return

nextchar:

incf

FSR,f

;

Bump pointer

goto

getchar

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


LCD Interfacing and Programming

313

;blank buffer ;========================

;Procedure to store 16 blank characters in PIC RAM

;buffer starting at address stored in the variable

;pic_ad

blank16:

movlw

D’16’

; Set up counter

movwf

count1

movf

pic_ad,w ; First PIC RAM address

movwf

FSR

; Indexed addressing

movlw

0x20

; ASCII space character

storeit:

movwf

INDF

; Store blank character in PIC

RAM

; buffer using FSR register

decfsz

count1,f

; Done?

goto

incfsr

; no

return

; yes

incfsr:

incf

FSR,f

; Bump FSR to next buffer

space

goto

storeit

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

;Set Address register

;to LCD line 1 ;========================

;ON ENTRY:

;Address of LCD line 1 in constant LCD_1

line1:

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

; RS line low, set up for

control

call

delay_125mcs

; delay 125 microseconds

; Set to second display line

movlw

LCD_1

; Address and command bit

movwf

PORTB

call

pulseE

; Pulse and delay

; Set RS line for data

bsf

PORTA,RS_line

; Set up for data

call

delay_125mcs

; Delay

return

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

;Set Address register

;to LCD line 2 ;========================

;ON ENTRY:

;Address of LCD line 2 in constant LCD_2


314

line2:

bcf

PORTA,E_line

bcf

PORTA,RS_line

call

delay_125mcs

; Set to second display line

movlw

LCD_2

movwf

PORTB

call

pulseE

; Set RS line for data

bsf

PORTA,RS_line

call

delay_125mcs

return

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

Chapter 13

;E line low

;RS line low, set up for

;control

;delay

;Address with high-bit set

;Pulse and delay

;RS = 1 for data

;delay

;first text string procedure ;=============================== storeMN:

;Procedure to store in PIC RAM buffer the message

;contained in the code area labeled msg1

;ON ENTRY:

;variable pic_ad holds address of text buffer

;in PIC RAM

;w register hold offset into storage area

;msg1 is routine that returns the string characters

;and a zero terminator

;index is local variable that hold offset into

;text table. This variable is also used for

;temporary storage of offset into buffer

;ON EXIT:

;Text message stored in buffer

;

;Store offset into text buffer (passed in the w register)

;in temporary variable

movwf

index

; Store w in index

; Store base address of text buffer in FSR

movf

pic_ad,w ; first display RAM address to W

addwf

index,w

; Add offset to address

movwf

FSR

; W to FSR

; Initialize index for text string access

movlw

0

; Start at 0

movwf

index

; Store index in variable

; w still = 0

get_msg_char:

call

msg1

; Get character from table

; Test for zero terminator

andlw

0x0ff

btfsc

STATUS,Z ; Test zero flag