Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8594
Скачиваний: 0
320 |
Chapter 13 |
|||||
; |
| | | |
1 = 5x10 in 1/8 or 1/11 |
||||
; |
| | | |
0 = 1/16 dc |
||||
; |
| | |___ Duty cycle select |
|||||
; |
| | |
1 = 1/8 or 1/11 |
||||
; |
| | |
0 = 1/16 |
||||
; |
| |___ Interface width |
|||||
; |
| |
0 = 4 bits |
||||
; |
| |
1 = 8 bits |
||||
; |
|___ FUNCTION SET COMMAND |
|||||
movwf |
PORTB |
;0011 1000 |
||||
call |
pulseE |
;pulseE and |
delay |
|||
;***********************| |
||||||
; |
DISPLAY OFF |
| |
||||
;***********************| |
||||||
movlw |
0x08 |
; 0 0 0 0 1 |
0 0 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 |
|||
;***********************| |
||||||
; 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 |
|||||
LCD Interfacing and Programming |
321 |
|||||
; |
| |
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 |
||||
; |
| |
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 |
busyTest ; |
Test for busy |
||||
return |
||||||
;========================
;busy flag test routine ;========================
;Procedure to test the HD44780 busy flag
;Execution returns when flag is clear busyTest:
movlw |
b’11111111’ |
; All lines |
to input |
|
tris |
PORTB |
; in port B |
||
bcf |
PORTA,RS_line |
; RS line low |
for control |
|
bsf |
PORTA,RW_line |
; Read mode |
||
bsf |
PORTA,E_line |
; E line high |
||
movf |
PORTB,w |
; Read port |
B |
into W |
; Port B bit 7 is busy flag |
||||
bcf |
PORTA,E_line |
; E line low |
||
322 |
Chapter 13 |
|||
andlw |
0x80 |
; Test bit 7, high is busy |
||
btfss |
STATUS,Z ; Test zero |
bit |
in STATUS |
|
goto |
busyTest ; Repeat if |
set |
||
;At this point busy flag is clear
;Reset R/W line and port B to output
bcf |
PORTA,RW_line |
; Clear R/W |
line |
|
movlw |
b’00000000’ |
; |
All lines |
to output |
tris |
PORTB |
; |
in port B |
|
return |
||||
;=======================
;Procedure to delay
;42 microseconds ;======================= delay_125:
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_5:
movlw |
D’41’ |
; Counter = 41 |
|
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 |
PORTA,E_line |
; Pulse E line |
|
nop |
; Delay |
||
bcf |
PORTA,E_line |
||
call |
delay_5 |
; Wait |
|
return |
|||
;=============================
;long delay sub-routine
;(for debugging) ;=============================
LCD Interfacing and Programming |
323 |
|
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
call |
busyTest ; 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 |
|||
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 |
||
;========================
;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 |
324 |
Chapter 13 |
|
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 |
busyTest ; busy? |
|
; 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 |
busyTest ; Busy? |
|
return |
||
;========================
;Set Address register
;to LCD line 2 ;========================
;ON ENTRY:
;Address of LCD line 2 in constant LCD_2
line2:
bcf |
PORTA,E_line |
; E line low |
bcf |
PORTA,RS_line |
; RS line low, set up for |
control |
||
call |
busyTest |
; Busy? |
; Set to second display line |
||
movlw |
LCD_2 |
; Address with high-bit set |
movwf |
PORTB |
|