Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8577
Скачиваний: 0
Data EEPROM Programming |
523 |
|
processor |
16f877 |
; Define processor |
#include <p16f877.inc>
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF
;__CONFIG directive is used to embed configuration data
;within the source file. The labels following the directive
;are located in the corresponding .inc file.
errorlevel -302
; Suppress bank-related warning
;============================================================
; M A C R O S
;============================================================ ; Macros to select the register banks
Bank0 |
MACRO |
; Select RAM bank 0 |
bcf |
STATUS,RP0 |
|
bcf |
STATUS,RP1 |
|
ENDM |
||
Bank1 |
MACRO |
; Select RAM bank 1 |
bsf |
STATUS,RP0 |
|
bcf |
STATUS,RP1 |
|
ENDM |
||
Bank2 |
MACRO |
; Select RAM bank 2 |
bcf |
STATUS,RP0 |
|
bsf |
STATUS,RP1 |
|
ENDM |
||
Bank3 |
MACRO |
; Select RAM bank 3 |
bsf |
STATUS,RP0 |
|
bsf |
STATUS,RP1 |
|
ENDM |
;=====================================================
; constant definitions
; for PIC-to-LCD pin wiring and LCD line addresses ;=====================================================
#define E_line 1 |
;| |
|
#define RS_line 0 |
;| — from wiring diagram |
|
#define RW_line 2 |
;| |
|
; LCD line addresses (from LCD data sheet) |
||
#define LCD_1 |
0x80 |
; First LCD line constant |
#define LCD_2 |
0xc0 |
; Second LCD line constant |
#define LCDlimit .20; Number of characters per line #define spbrgVal .64; For 2400 baud on 10Mhz clock
;Note: The constants that define the LCD display
;line addresses have the high-order bit set
;so as to meet the requirements of controller
524 |
Chapter 15 |
;commands.
;==========================================================
;constants for I2C initialization ;==========================================================
;I2C connected to 24LC04B EEPROM.
;The MSSP module is in I2C MASTER mode.
#define LC04READ 0xa0 ; I2C value for read control byte #define LC04WRITE 0xa1 ; I2C value for write control byte
;========================================================== ; General Purpose Variables ;==========================================================
;Local variables
;Reserve 20 bytes for string buffer cblock 0x20
strData endc
;Other data
cblock 0x34 |
; Start of block |
count1 |
; Counter # 1 |
count2 |
; Counter # 2 |
count3 |
; Counter # 3 |
J |
; counter J |
K |
; counter K |
bufAdd |
|
index |
|
store1 |
; Local storage |
store2 |
|
; For LCDscroll procedure |
|
LCDcount ; Counter for characters per line |
|
LCDline |
; Current display line (0 or 1) |
Endc |
|
;
;==============================
;Common RAM area ;==============================
;These GPRs can be accessed from any bank.
;15 bytes are available, from 0x70 to 0x7f
cblock |
0x70 |
; Communications variables |
|
newData |
; not 0 if new data received |
ascVal |
|
errorFlags
; EEPROM-related variables
EEMemAdd ; EEPROM address to access
EEByte |
; Data byte to write |
endc |
Data EEPROM Programming |
525 |
||
;============================================================ |
|||
; |
P R O G R A M |
||
;============================================================ |
|||
org |
0 |
; start at address |
|
goto |
main |
||
; Space for interrupt handlers |
|||
org |
0x08 |
||
main: |
|||
;Wiring:
;LCD data to Port-D, lines 0 to 7
;E line -> Port-E, 1
;RW line -> Port-E, 2
;RS line -> Port-E, 0
;Set PORTE D and E for output
;First, initialize Port-B by clearing latches clrf STATUS
clrf PORTB
;Select bank 1 to TRIS Port-D for output Bank1
;TRIS Port-D for output. Port-D lines 4 to 7 are wired
;to LCD data lines. Port-D lines 0 to 4 are wired to LEDs.
movlw B’00000000’
movwf |
TRISD |
; and Port-D |
;By default Port-A lines are analog. To configure them
;as digital code must set bits 1 and 2 of the ADCON1
;register (in bank 1)
movlw |
0x06 |
; binary 0000 0110 is code to |
; make all Port-A lines digital
movwf ADCON1
;Port-B, lines are wired to keypad switches, as follows:
;7 6 5 4 3 2 1 0
;| | | | |_|_|_|_____ switch rows (output)
;|_|_|_|_____________ switch columns (input)
;rows must be defined as output and columns as input
movlw |
b’11110000’ |
|
movwf |
TRISB |
|
; TRIS Port-E for output |
||
movlw |
B’00000000’ |
|
movwf |
TRISE |
; TRIS Port-E |
; Enable Port-B pullups for switches in OPTION register movlw b’00001000’
movwf OPTION_REG
;Back to bank 0 Bank0
;Initialize serial port for 2400 baud, 8 bits, no parity
;1 stop
call InitSerial
; Test serial transmission by sending “RDY-”
526 |
Chapter 15 |
||
movlw |
‘R’ |
||
call |
SerialSend |
||
movlw |
‘D’ |
||
call |
SerialSend |
||
movlw |
‘Y’ |
||
call |
SerialSend |
||
movlw |
‘-’ |
||
call |
SerialSend |
||
movlw |
0x20 |
||
call |
SerialSend |
||
; Clear all output lines |
|||
movlw |
b’00000000’ |
||
movwf |
PORTD |
||
movwf |
PORTE |
||
; Wait and initialize HD44780 |
|||
call |
delay_5 |
; Allow LCD time to initialize itself |
|
call |
initLCD |
; Then do forced initialization |
|
call |
delay_5 |
||
; Clear character counter and line counter variables |
|||
clrf |
LCDcount |
||
clrf |
LCDline |
||
; Set display address to start of first LCD line |
|||
call |
line1 |
||
; Store address of display buffer |
|||
movlw |
0x20 |
||
movwf |
bufAdd |
||
; Display “Receiving:” message prompt |
|||
call |
blank20 |
; Clear buffer |
|
movlw |
0x00 |
; Offset in buffer |
|
call |
storeMS1 ; Store message at offset |
||
call |
display20 |
; Display message |
|
; Start address of EEPROM |
|||
clrf |
EEMemAdd |
||
; Setup for display in second line |
|||
call |
line2 |
||
clrf |
LCDline |
||
incf |
LCDline,f ; Set scroll control for line 2 |
||
; Initialize I2C EEPROM operation |
|||
call |
SetupI2C ; Local procedure |
||
;============================================================
;receive serial data, store, and display ;============================================================ receive:
;Call serial receive procedure
call SerialRcv
;HOB of newData register is set if new data
;received
btfss newData,7
Data EEPROM Programming |
527 |
|
goto |
scanExit |
|
; At this point new data was received. |
||
movwf |
EEByte |
; Save received character |
; Display character on LCD |
||
movf |
EEByte,w ; Recover character |
|
call |
send8 |
; Display in LCD |
call |
LCDscroll |
; Scroll at end of line |
; Store character in EEPROM at location in EEMemAdd |
||
call |
WriteI2C ; Local procedure |
|
incf |
EEMemAdd,f |
; Bump to next EEPROM |
; Check for <Enter> key (0x0d) and execute display function |
||
movf |
EEByte,w ; Recover last received |
|
sublw |
0x0d |
|
btfsc |
STATUS,Z ; Test if <Enter> key |
|
goto |
isEnter |
; Go if <Enter> |
; Not <Enter> key, continue processing |
||
scanExit: |
||
goto |
receive |
; Continue |
;============================
;display EEPROM data ;============================
;This routine receives control when the <Enter> key is
;received.
;Action:
;1. Clear LCD
;2. Output is set to top LCD line
;3. Characters stored in EEPROM are displayed
;until 0x0d code is detected
isEnter:
call clearLCD
; Clear character counter and line counter variables clrf LCDcount
clrf LCDline
;Read data from EEPROM memory, starting at address 0
;and display on LCD until 0x0d terminator
call |
line1 |
|
clrf |
EEMemAdd ; Start at EEPROM 0 |
|
readOne: |
||
call |
ReadI2C |
; Get character |
; Store character |
||
movwf |
EEByte |
; Save character |
; Test for terminator |
||
sublw |
0x0d |
|
btfsc |
STATUS,Z ; Test if 0x0d |
|
goto |
atEnd |
; Go if 0x0d |
;At this point character read is not 0x0d
;Display on LCD
movf |
EEByte,w ; Recover character |