Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8635
Скачиваний: 0
498 |
Chapter 15 |
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
call |
delay_5 |
; Make sure not busy |
; Set up for data |
||
bcf |
PORTA,E_line |
; E line low |
bsf |
PORTA,RS_line |
; RS line high for data |
; 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 |
||
call |
send8 |
; 4-bit interface routine |
; Test for 16 characters displayed |
||
decfsz |
count3,f |
; Decrement counter |
goto |
nextchar ; Skipped if done |
|
return |
||
nextchar: |
||
incf |
FSR,f |
; Bump pointer |
goto |
getchar |
|
;========================
;send 2 nibbles in
;4-bit mode ;========================
;Procedure to send two 4-bit values to Port-B lines
;7, 6, 5, and 4. High-order nibble is sent first
;ON ENTRY:
;w register holds 8-bit value to send
send8:
movwf |
store1 |
; Save |
original value |
call |
merge4 |
; Merge with Port-B |
|
; Now w has merged byte |
|||
movwf |
PORTB |
; w to |
Port-B |
call |
pulseE |
; Send |
data to LCD |
; High nibble is sent |
|||
movf |
store1,w ; Recover byte into w |
||
swapf |
store1,w ; Swap nibbles in w |
||
Data EEPROM Programming |
499 |
|
call |
merge4 |
|
movwf |
PORTB |
|
call |
pulseE |
; Send data to LCD |
call |
delay_125 |
|
return |
||
;=================
;merge bits ;=================
;Routine to merge the 4 high-order bits of the
;value to send with the contents of Port-B
;so as to preserve the 4 low-bits in Port-B
;Logic:
;AND value with 1111 0000 mask
;AND Port-B with 0000 1111 mask
;Now low nibble in value and high nibble in
;Port-B are all 0 bits:
;value = vvvv 0000
;Port-B = 0000 bbbb
;OR value and Port-B resulting in:
; |
vvvv bbbb |
;ON ENTRY:
;w contains value bits
;ON EXIT:
;w contains merged bits
merge4:
andlw |
b’11110000’ |
; ANDing with |
0 clears |
the |
; bit. ANDing |
with 1 preserves |
|||
; the original value |
||||
movwf |
store2 |
; Save result |
in variable |
|
movf |
PORTB,w |
; Port-B to w |
register |
|
andlw |
b’00001111’ |
; Clear high nibble in |
Port-b |
|
; and preserve low nibble |
||||
iorwf |
store2,w |
; OR two operands in w |
||
return |
||||
;========================
;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’ |
; Setup counter |
||
movwf |
count1 |
|||
movf |
pic_ad,w |
; First |
PIC RAM address |
|
movwf |
FSR |
; |
Indexed addressing |
|
movlw |
0x20 |
; |
ASCII |
space character |
storeit
Data EEPROM Programming |
501 |
|
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 |
|
goto |
endstr1 |
; End of string |
;ASSERT: valid string character in w
;store character in text buffer (by FSR)
movwf |
INDF |
; store in buffer by FSR |
|
incf |
FSR,f |
; increment buffer pointer |
|
; Restore table character counter from variable |
|||
movf |
index,w |
; Get value into w |
|
addlw |
1 |
; Bump to next character |
|
movwf |
index |
; Store table index in variable |
|
goto |
get_msg_char |
; Continue |
|
endstr1: |
|||
return |
|||
;Routine for returning message stored in program area
;Message has 10 characters
msg1:
addwf |
PCL,f |
; Access table |
retlw |
‘I’ |
|
retlw |
‘t’ |
|
retlw |
‘e’ |
|
retlw |
‘r’ |
|
retlw |
‘.’ |
|
retlw |
0x20 |
|
retlw |
‘N’ |
|
retlw |
‘o’ |
|
retlw |
‘.’ |
|
retlw |
0x20 |
|
retlw |
0 |
;==============================
;binary to ASCII decimal
;conversion ;==============================
;ON ENTRY:
502 |
Chapter 15 |
;w register has binary value in range 0 to 255
;ON EXIT:
;output variables asc100, asc10, and asc1 have
;three ASCII decimal digits
;Routine logic:
;The value 100 is subtracted from the source operand
;until the remainder is < 0 (carry cleared). The number
;of subtractions is the decimal hundreds result. 100 is
;then added back to the subtrahend to compensate
;for the last subtraction. Now 10 is subracted in the
;same manner to determine the decimal tenths result.
;The final remainder is the decimal units result.
;Variables:
; |
inNum |
storage for source operand |
;asc100 storage for hundreds position result
; |
asc10 |
storage for tenth position result |
|
; |
asc1 |
storage for unit position result |
|
; |
thisDig |
Digit counter |
|
bin2asc: |
|||
movwf |
inNum |
; Save copy of source value |
|
clrf |
asc100 ; Clear hundreds storage |
||
clrf |
asc10 |
; Tens |
|
clrf |
asc1 |
; Units |
|
clrf |
thisDig |
||
sub100: |
|||
movlw |
.100 |
||
subwf |
inNum,f |
; Subtract 100 |
|
btfsc |
STATUS,C |
; Did subtract overflow? |
|
goto |
bump100 |
; No. Count subtraction |
|
goto |
end100 |
||
bump100: |
|||
incf |
thisDig,f |
;increment digit counter |
|
goto |
sub100 |
||
; Store 100th digit |
|||
end100: |
|||
movf |
thisDig,w |
; Adjusted digit counter |
|
addlw |
0x30 |
; Convert to ASCII |
|
movwf |
asc100 |
; Store it |
|
; Calculate tenth position value |
|||
clrf |
thisDig |
||
; Adjust minuend |
|||
movlw |
.100 |
; Minuend |
|
addwf |
inNum,f |
; Add value to minuend to |
|
; compensate |
|||
for last operation |
|||
sub10: |
|||
movlw |
.10 |
||
subwf |
inNum,f |
; Subtract 10 |
|