Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8573

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Data EEPROM Programming

533

; 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

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 hold 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


534

Chapter 15

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

‘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


Data EEPROM Programming

535

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

;Bits 6 and 7 of Port-C are multiplexed as TX/CK and RX/DT

;for USART operation. These bits must be set to input in the

;TRISC register

movlw

b’11000000’

; Bits for TX and RX

iorwf

TRISC,f

; OR into TRISc register

; The asynchronous baud rate is calculated as follows:

;

Fosc

;

ABR = -------

;

S*(x+1)

;where x is value in the SPBRG register and S is 64 if the high

;baud rate select bit (BRGH) in the TXSTA control register is

;clear, and 16 if the BRGH bit is set. For setting to 2400 baud

;using a 10Mhs oscillator at a slow baud rate the formula

;is:

;At slow speed (BRGH = 0)

;

10,000,000

10,000,000

;---------- = ----------- = 2,403.84 (0.16% error)

;

64*(64+1)

4160

;

movlw

spbrgVal ; Value in spbrgVal = 64

movwf

SPBRG

; Place in baud rate generator

; Setup value: 0010 0000 = 0x20

movlw

0x20

; Enable transmission and high

; baud rate

movwf

TXSTA

Bank0

; Bank 0

; Setup value: 1001 0000 = 0x90

movlw

0x90

; Enable serial port and


536

Chapter 15

; continuous reception

movwf

RCSTA

;

clrf

errorFlags ; Clear local error flags register

return ;==============================

;transmit data ;==============================

;Test for Transmit Register Empty and transmit data in w SerialSend:

Bank0

; Select bank 0

btfss

PIR1,TXIF

; check if transmitter busy

goto

$-1

; wait until transmitter is not busy

movwf

TXREG

; and transmit the data

return

;==============================

;receive data ;==============================

;Procedure to test line for data received and return value

;in w. Overrun and framing errors are detected and

;remembered in the variable errorFlags, as follows:

;

7

6

5 4

3

2

1 0

<== errorFlags

;

not

used

——

|

|___

overrun error

;

|______ framing error

SerialRcv:

clrf

newData ;

Clear new data received register

Bank0

;

Select bank 0

;Bit 5 (RCIF) of the PIR1 Register is clear if the USART

;receive buffer is empty. If so, no data has been received

btfss

PIR1,RCIF

;

Check for received data

return

;

Exit if no data

;At this point data has been received. First eliminate

;possible errors: overrun and framing.

;Bit 1 (OERR) of the RCSTA register detects overrun

;Bit 2 (FERR( of the RCSTA register detects framing error

btfsc

RCSTA,OERR

; Test

for overrun error

goto

OverErr

;

Error handler

btfsc

RCSTA,FERR

;

Test

for framing error

goto

FrameErr ; Error handler

;At this point no error was detected

;Received data is in the USART RCREG register

movf

RCREG,w

;

get

received

data

bsf

newData,7

;

Set

bit 7 to

indicate new

data

; Clear error flags

clrf errorFlags return


Data EEPROM Programming

537

;==========================

;

error handlers

;==========================

OverErr:

bsf

errorFlags,0

; Bit 0 is overrun error

; Reset system

bcf

RCSTA,CREN

; Clear continuous receive bit

bsf

RCSTA,CREN

; Set to re-enable reception

return

;error because FERR framing error bit is set

;can do special error handling here - this code simply clears

; and continues

FrameErr:

bsf

errorFlags,1

;

Bit 1 is

framing error

movf

RCREG,W

;

Read and

throw away bad data

return

;============================================================

; I2C EEPROM data procedures

;============================================================

;GPRs used in EEPROM-related code are placed in the common

;RAM area (from 0x70 to 0x7f). This makes the registers

;accessible from any bank.

;============================

;

LIST OF PROCEDURES

;============================

; SetupI2C

—-

Initialize MSSP module for I2C mode

;

in hardware master mode

;

Configure I2C lines

;

Set slew rate for 100kbps

;

Set baud rate for 10Mhz

; WriteI2C

—-

Write byte to I2C EEPROM device

;

Data is stored in EEByte variable

;

Address is stored in EEMemAdd

; ReadI2C

—-

Read byte from I2C EEPROM device

;

Address stored in EEMemAdd

;

Read data returned in w register

;============================

;

I2C setup procedure

;============================

SetupI2C:

Bank1

movlw

b’00011000’

iorwf

TRISC,f

; OR into TRISC

; Setup MSSP module for Master Mode operation

Bank0

movlw

B’00101000’; Enables MSSP and uses appropriate

;

0

0

1

0

1

0

0

0

Value to install

;

7

6

5

4

3

2

1

0

<== SSPCON bits in this operation