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

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

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

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

Добавлен: 14.06.2025

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

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

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

690

Appendix D

;

M A C R O S

;============================================================ ; Macros to select the register banks

Bank0

MACRO

; Select RAM bank 0

bcf

STATUS,RP0

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0

ENDM

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

;

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

#define LCDlimit .16; Number of characters per line

;4800 baud clock countdown values

;Code reduces rate to 2400 baud by entering a minimal

;prescaler to TRM0

#define halfBaud .152 ; For one-half bit time

#define fullBaud .48 ; For one full bit time

;

;Note: The constants that define the LCD display line

;addresses have the high-order bit set in

;order to facilitate the controller command

;

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

;

PIC register and flag equates

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

z

equ

2

;

Zero flag

c

equ

0

;

Carry flag

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

;buffer and variables in PIC RAM ;=====================================================

;Create a 16-byte storage area

cblock 0x0c

; Start of first data block

lineBuf

; buffer for text storage

endc

; Leave 16 bytes and Continue with local variables

cblock 0x1c

; Second data block

count1

;

Counter

#

1

count2

;

Counter

#

2

J

; counter J


Supplementary Programs

691

K

; counter K

store1

; Local temporary storage

store2

; Storage # 2

; For LCDscroll procedure

LCDcount ; Counter for characters per line

LCDline

; Current display line (0 or 1)

bufPtr

; Buffer pointer

; Variables for serial communications

tempData ; Temporary storage for bit manipulations

rcvData

; Final storage for received character

bitCount ; Bit counter

sendData ; Character to send

endc

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

;

m a i n

p r o g r a m

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

Bank1

movlw

b'00010001' ; Port A lines I/O setup

; RA0 = RS232 input (R2out)

; RA4 = Pushbutton SW # 1

movwf

TRISA

movlw

b'00000000' ; Port B lines as follow:

;RB4-RB7 ===> LCD data lines 4 to 7 (output)

;RB0 =======> MAX202 T2in line (output)

;RB0 =

movwf

TRISB

Bank0

; Clear bits in port A output lines

bcf

PORTA,1

bcf

PORTA,2

bcf

PORTA,3

movlw

b'00000000' ; All outputs ports low

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

; Set port B, line 0 high so start bit is detected

bsf

PORTB,0


692

Appendix D

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

;wait for start command ;============================

;Program waits until pushbutton number 1 is pressed

;to continue execution. Pushbutton 1 is active low

;and wired to RA4

pb1Wait:

btfsc

PORTA,4

;

Test

port A, line

4

goto

pb1Wait

;

Loop

if not clear

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

;display and send "Ready-" ;============================

;Set LCD base address

call line1

; Initialize system for UART emulation at 2400 baud call initTTY

;Display on LCD and test serial transmission by sending

;the string "Ready-"

movlw

'R'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

movlw

'e'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

movlw

'a'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

movlw

'd'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

movlw

'y'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

movlw

'-'

movwf

sendData ; Store in send register

call

send8

; Local LCD display procedure

call

sendTTY

; Local send procedure

;

Init character

counter and line counter variables for

;

LCD line scroll

procedure

movlw

0x06

;

6 characters already

displayed

movwf

LCDcount

clrf

LCDline

;

LCD line counter


Supplementary Programs

693

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

; monitor RS232 line

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

nextChar:

call

rcvTTY

; Receive character

;Store character in local line buffer using indirect

;addressing

;16-byte buffer named lineBuf starts at address 0x0c

;Register variable bufPtr holds offset into buffer

movlw

0x0c

; Buffer base address

addwf

bufPtr,w

; Add pointer in

w

movwf

FSR

; Value to index

register

movf

rcvData,

; Character into

w

movwf

INDF

; Store w in [FSR]

incf

bufPtr,f

; Bump pointer

; Send character (still in w)

call

send8

; Display it

call

LCDscroll

; Scroll display

lines

goto

nextChar

; Continue

;============================================================ ; initialize LCD for 4-bit mode ;============================================================ initLCD:

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

;

| | | |__ font select:

;

| | |

1 = 5x10 in 1/8 or 1/11

;

| | |

0 = 1/16 dc

;

| | |___ Duty cycle select


694

Appendix D

;

| |

0 = 1/8 or 1/11

;

| |

1 = 1/16

;

| |___ Interface width

;

|

0 = 4 bits

;

|

1 = 8 bits

;

|___ FUNCTION SET COMMAND

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)

;

| | | |___ Blink character

;

| | |

1 = on, 0 = off

;

| | |___ Cursor on/off

;

| |

1 = on, 0 = off

;

| |____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

call

send8

;***********************|

;

set entry mode

|

;***********************|

movlw

0x06

; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)

;

| | |___ display shift

;

| |

1 = shift

;

| |

0 = no shift

;

| |____ increment mode

;

|

1 = left-to-right

;

|

0 = right-to-left

;

|___ COMMAND BIT

call

send8

;***********************|

; 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