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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

753

;__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

; Supress 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

; Defines from real-time clock wiring diagram

#define

CLK

PORTC,1

#define

DAT

PORTC,3

#define

IO

PORTC,5

; input/output select

#define

CE

PORTA,2

; chip enable bit


754

Appendix D

;===================================================== ; variables in PIC RAM ;=====================================================

;Local variables

;Reserve 20 bytes for string buffer cblock 0x20

strData endc

;Reserve three bytes for ASCII digits cblock 0x34

asc100

asc10

asc1 endc

;Data

cblock

0x37

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

pic_ad

J

; counter J

K

; counter K

index

store1

; Local storage

store2

; For LCDscroll procedure

LCDcount ; Counter for characters per line

LCDline

; Current display line (0 or 1)

; Communications variables

newData

; not 0 if new data received

ascVal

errorFlags

; Variables for Real-Time Clock

year

; Year (00h-99h)

month

; Month (01h-12h)

day

; Day of Month (01h-31h)

dow

; Day of Week (01h-07h)

hour

; Hour (00h-23h)

min

; Minute (00h-59h)

sec

; Second (00h-59h)

T0

; Temporary storage

T1

; Temporary storage

endc

;EEPROM-related variables are placed in common area so

;they may be accessed from any bank

cblock 0x70

EEMemAdd ; EEPROM address to access


Supplementary Programs

755

EEByte

; Data byte to write

; Storage for ASCII decimal conversion and digits

inNum

; Source operand

thisDig

; Digit counter

endc

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

;

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

;Data memory bank selection bits:

; RP1:RP0

Bank

;

0:0

0

Ports A,B,C,D, and E

;

0:1

1

Tris A,B,C,D, and E

;

1:0

2

;

1:1

3

; 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


756

Appendix D

; Tris

port E for output

movlw

B'00000000'

movwf

TRISE

; Tris port E

; NJU6355

Interface:

; CLK

PORTC,1

Output

; DAT

PORTC,3

Output

; IO

PORTC,5

Output

; CE

PORTA,2

Output

movlw

b'00000000'

movwf

TRISC

movlw

TRISA

; Enable port B pullups for switches in OPTION register

;

7

6

5

4

3

2 1 0 <= OPTION bits

;

|

|

|

|

|

|__|__|_____

PS2-PS0 (prescaler bits)

;

|

|

|

|

|

Values for Timer0

;

|

|

|

|

|

000

= 1:2

001 = 1:4

;

|

|

|

|

|

010

= 1:8

011 = 1:16

;

|

|

|

|

|

100

= 1:32

101 = 1:64

;

|

|

|

|

|

110

= 1:128 *111 = 1:256

;

|

|

|

|

|______________

PSA

(prescaler assign)

;

|

|

|

|

*1

=

to WDT

;

|

|

|

|

0

=

to Timer0

;

|

|

|

|_________________

TOSE (Timer0 edge select)

;

|

|

|

*0

=

increment on low-to-high

;

|

|

|

1

=

increment in high-to-low

;

|

|

|____________________

TOCS (TMR0 clock source)

;

|

|

*0

=

internal clock

;

|

|

1

=

RA4/TOCKI bit source

;

|

|_______________________

INTEDG (Edge select)

;

|

*0

=

falling edge

;|__________________________ RBPU (Pullup enable)

;

*0

=

enabled

;

1

=

disabled

movlw

b'00001000'

movwf

OPTION_REG

; Clear the write error flag (WRERR) in EECON1 Bank3

bcf EECON1,WRERR

;Back to bank 0 Bank0

;Clear all output lines movlw b'00000000'

movwf

PORTD

movwf

PORTE

movwf

PORTA

movwf

PORTC

; Wait and initialize HD44780

call

delay_5 ; Allow LCD time to initialize itself


Supplementary Programs

757

call

initLCD

;

Then do forced initialization

call

delay_5

;

(Wait probably not necessary)

; Clear character counter and line counter variables

clrf

LCDcount

clrf

LCDline

; Store base address of text buffer in PIC RAM

movlw

0x20

;

Start address for buffer

movwf

pic_ad

;

to local variable

; Initialize EEPROM address and data

clrf

EEMemAdd ;

Set address to 0

clrf

EEByte

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

;first LCD line ;======================

;Store 20 blanks in PIC RAM, starting at address stored

;in variable pic_ad

call blank20

;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

;

Store

value

;At this point w must be 0

;EEPROM data still in w

clrf

EEMemAdd ; Address 0

incf

EEByte,f

call

EEWrite

;At this point iteration number 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 0x20 the

;iteration digit must be stored at offset 0x20+11=0x2b 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

0x2b

;

Store in buffer