Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8557
Скачиваний: 0
Data EEPROM Programming |
513 |
;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 |
PORTD |
; w to Port-D |
call |
pulseE |
; Send data to LCD |
; High nibble is sent |
||
movf |
store1,w ; Recover byte into w |
|
swapf |
store1,w ; Swap nibbles in w |
|
call |
merge4 |
|
movwf |
PORTD |
|
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 contain 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 |
PORTD,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 |
|||
;==========================
514 |
Chapter 15 |
;Set address register
;to LCD line 2 ;==========================
;ON ENTRY:
;Address of LCD line 2 in constant LCD_2
line2:
bcf |
PORTE,E_line |
; E line low |
bcf |
PORTE,RS_line |
; RS line low, setup for |
; control |
||
call |
delay_5 |
; Busy? |
; Set to second display line |
||
movlw |
LCD_2 |
; Address with high-bit set |
call |
send8 |
|
; Set RS line for data |
||
bsf |
PORTE,RS_line |
; RS = 1 for data |
call |
delay_5 |
; Busy? |
return |
||
;==========================
;Set address register
;to LCD line 1 ;==========================
;ON ENTRY:
;Address of LCD line 1 in constant LCD_1
line1:
bcf |
PORTE,E_line |
; E line low |
bcf |
PORTE,RS_line |
; RS line low, set up for |
; control |
||
call |
delay_5 |
; busy? |
; Set to second display line |
||
movlw |
LCD_1 |
; Address and command bit |
call |
send8 |
; 4-bit routine |
; Set RS line for data |
||
bsf |
PORTE,RS_line |
; Setup for data |
call |
delay_5 |
; Busy? |
return |
||
;==========================
;scroll to LCD line 2 ;==========================
;Procedure to count the number of characters displayed on
;each LCD line. If the number reaches the value in the
;constant LCDlimit, then display is scrolled to the second
;LCD line. If at the end of the second line, then LCD is
;reset to the first line.
LCDscroll:
incf |
LCDcount,f |
; Bump counter |
; Test for line limit
movf LCDcount,w
Data EEPROM Programming |
515 |
|
sublw |
LCDlimit ; Count minus limit |
|
btfss |
STATUS,Z ; Is count - limit = 0 |
|
goto |
scrollExit |
; Go if not at end of line |
;At this point the end of the LCD line was reached
;Test if this is also the end of the second line movf LCDline,w
sublw |
0x01 |
; Is it line 1? |
btfsc |
STATUS,Z ; Is LCDline minus 1 = 0? |
|
goto |
line2End ; Go if end of second line |
|
; At this point it is the end of the top LCD line |
||
call |
line2 |
; Scroll to second line |
clrf |
LCDcount ; Reset counter |
|
incf |
LCDline,f |
; Bump line counter |
goto |
scrollExit |
|
; End of second LCD line |
||
line2End: |
||
call |
initLCD |
; Reset |
clrf |
LCDcount ; Clear counters |
|
clrf |
LCDline |
|
call |
line1 |
; Display to first line |
scrollExit: |
||
return |
||
;=============================
;LCD display procedure ;=============================
;Sends 20 characters from PIC buffer with address stored
;in variable bufAdd to LCD line previously selected display20:
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 20 characters |
|||
movlw |
D’20’ |
||
movwf |
count3 |
||
; Get display address from local variable bufAdd |
|||
movf |
bufAdd,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 20 characters displayed |
|||
decfsz |
count3,f |
; Decrement counter |
|
goto |
nextchar ; Skipped if done |
||
return |
|||
516 |
Chapter 15 |
|
nextchar: |
||
incf |
FSR,f |
; Bump pointer |
goto |
getchar |
;===============================
;first text string procedure ;=============================== storeMS1:
;Procedure to store in PIC RAM buffer the message
;contained in the code area labeled msg1
;ON ENTRY:
;variable bufAdd 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 holds 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 |
bufAdd,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 |
|
Data EEPROM Programming |
517 |
endstr1:
return
;Routine for returning message stored in program area
;Message has 10 characters
msg1:
addwf |
PCL,f |
; Access table |
retlw |
‘R’ |
|
retlw |
‘e’ |
|
retlw |
‘C’ |
|
retlw |
‘e’ |
|
retlw |
‘i’ |
|
retlw |
‘v’ |
|
retlw |
‘i’ |
|
retlw |
‘n’ |
|
retlw |
‘g’ |
|
retlw |
‘:’ |
|
retlw |
0 |
;========================
;blank buffer ;========================
;Procedure to store 20 blank characters in PIC RAM
;buffer starting at address stored in the variable
;bufAdd
blank20: |
|||
movlw |
D’20’ |
; Setup counter |
|
movwf |
count1 |
||
movf |
bufAdd,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 |
||
;============================================================== ; communications procedures ;==============================================================
;Initialize serial Port-for 2400 baud, 8 bits, no parity,
;1 stop
InitSerial:
Bank1 |
; Macro to select bank1 |