Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8533
Скачиваний: 0
Data EEPROM Programming |
493 |
||
; * _XT_OSC |
External parallel resonator/crystal oscillator |
||
; |
_HS_OSC |
High speed crystal resonator (8 to |
10 MHz) |
; |
Resonator: Murate Erie CSA8.00MG = |
8 MHz |
|
; |
_RC_OSC |
Resistor/capacitor oscillator |
|
; | |
(simplest, 20% error) |
||
;|
;|_____ * indicates setup values presently selected
;=========================
; setup |
and configuration |
|
;========================= |
||
processor 16f84A |
||
include |
<p16f84A.inc> |
|
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
|
;===================================================== |
||
; |
constant definitions |
|
; for PIC-to-LCD pin wiring and LCD line addresses |
||
;===================================================== |
||
#define |
E_line 1 |
;| |
#define |
RS_line 2 |
;| — from wiring diagram |
#define |
RW_line 3 |
;| |
; LCD line addresses (from LCD data sheet) |
||
#define |
LCD_1 0x80 |
; First LCD line constant |
#define |
LCD_2 0xc0 |
; Second LCD line constant |
;Note: The constants that define the LCD display line
;addresses have the high-order bit set in
;order to facilitate the controller command
;
;===================================================== ; variables in PIC RAM ;=====================================================
;Reserve 16 bytes for string buffer cblock 0x0c
strData endc
;Reserve three bytes for ASCII digits cblock 0x1d
asc100
asc10
asc1 endc
;Continue with local variables
cblock 0x20 count1 count2 count3
494 |
Chapter 15 |
||
pic_ad |
; Storage for start of text area |
||
; (labeled strData) in PIC RAM |
|||
J |
; counter J |
||
K |
; counter K |
||
index |
; Index into text table (also used |
||
; for auxiliary storage) |
|||
store1 |
; Local temporary storage |
||
store2 |
; Storage # 2 |
||
; EEPROM-related variables |
|||
EEMemAdd ; EEPROM address to access |
|||
EEByte |
; Data byte to write |
||
; Storage for ASCII decimal conversion and digits |
|||
inNum |
; Source operand |
||
thisDig |
; Digit counter |
||
endc |
|||
;========================================================= |
|||
; |
program |
||
;========================================================= |
|||
org |
0 |
; start at address |
|
goto |
main |
||
; Space for interrupt handlers |
|||
org |
0x08 |
||
main: |
|||
movlw |
b’00000000’ |
; All lines to output |
|
tris |
PORTA |
; in Port-A |
|
tris |
PORTB |
; and Port-B |
|
movlw |
b’00000000’ |
; All outputs ports low |
|
movwf |
PORTA |
||
movwf |
PORTB |
||
; Wait and initialize HD44780 |
|||
call |
delay_5 |
; Allow LCD time to initialize |
|
; itself |
|||
call |
delay_5 |
||
call |
initLCD |
; Then do forced |
|
initialization |
|||
call |
delay_5 |
; Wait again |
|
; Store base address of text buffer in PIC RAM |
|||
movlw |
0x0c |
; Start address for buffer |
|
movwf |
pic_ad |
; to local variable |
|
; Initialize EEPROM data to 0x0 |
|||
clrf |
EEMemAdd ; Set address to 0 |
||
;====================== |
|||
;first LCD line ;======================
;Store 16 blanks in PIC RAM, starting at address stored
Data EEPROM Programming |
495 |
|
; in variable pic_ad |
||
call |
blank16 |
|
;Call procedure to store ASCII characters for message
;in text buffer
movlw |
d’0’ |
; Offset into buffer |
call storeMS1
;=======================
;Read EEPROM memory ;=======================
;EEPROM memory address to use is at 10 (0x0a). Variable
;EEMemAdd is already initialized.
;Fill data for EEPROM is 0xff. This value indicates
;the first iteration
call |
EERead |
; |
Local procedure. Value in w |
movwf |
EEByte |
; |
Save result |
; EEPROM data still in w
incf EEByte,f
call EEWrite
;At this point iteration inNum is stored in EEByte
;This value must be displayed on the LCD at offset 11
;of the first line. This means it must be stored at offset
;11 in the buffer. Since the buffer starts at 0x0c the
;iteration digit must be stored at offset 0x0c+11=0x17 ShowEEData:
;Binary data in EEByte
movf |
EEByte,w ; Value to w |
|
call |
bin2asc |
; Conversion routine |
;At this point three ASCII digits are stored in local
;variables. Move digits to display area
movf |
asc1,w |
; Unit digit |
movwf |
0x18 |
; Store in buffer |
movf |
asc10,w |
; same with other digits |
movwf |
0x17 |
|
movf |
asc100,w |
|
movwf |
0x16 |
;Display line
;Set DDRAM address to start of first line showLine:
call line1
; Call procedure to display 16 characters in LCD call display16
loopHere:
goto loopHere ;done
;============================================================ ; initialize LCD for 4-bit mode ;============================================================ initLCD:
496 |
Chapter 15 |
;Initialization for Densitron LCD module as follows:
;4-bit interface
;2 display lines of 16 characters each
;cursor on
;left-to-right increment
;cursor shift right
;no display shift
;=======================| |
||||
; |
set command mode |
| |
||
;=======================| |
||||
bcf |
PORTA,E_line |
; E line low |
||
bcf |
PORTA,RS_line |
; RS line low |
||
bcf |
PORTA,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 |
||
Data EEPROM Programming |
497 |
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 |
|||
bcf |
PORTA,E_line |
||
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 |