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

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

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

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

Добавлен: 14.06.2025

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

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

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

416

Chapter 14

movf

INDF,w

;

get character from display RAM

;

location pointed to by file select

;

register

call

send8

;

4-bit interface routine

; Test for 16 characters displayed

decfsz

count2,f

; Decrement counter

goto

nextchar ;

Skipped if done

; At this point scroll operation has concluded

clrf

LCDcount ;

Clear counters

; Stay at line 2

clrf

LCDline

incf

LCDline,f

call

line2

;

Set for second line

scrollExit:

return

nextchar:

incf

FSR,f

; Bump pointer

goto

getchar

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

;clear line buffer ;============================

;Use indirect addressing to store 16 blanks in the

;buffer located at 0x0c

blankBuf:

Bank0

movlw

0x0c

; Pointer to RAM

movwf

FSR

; To index register

blank16:

clrf

INDF

; Clear memory pointed at by FSR

incf

FSR,f

; Bump pointer

btfss

FSR,4

; 000x0000 when bit 4 is set

; count reached 16

goto

blank16

return

;============================================================ ; initialize for TTY ;============================================================

;Procedure to initialize RS232 reception

;Assumes:

;

2400 baud

;

8 data bits

;

no parity

;

one stop bit

initTTY:

;First initialize receiver to RS-232-C line parameters

;Disable global and peripheral interrupts


Communications

417

;

7

6

5

4

3

2

1

0

<=

INTCON bitmap

;

|

?

|

?

?

?

?

?

(?

= unrelated bits)

;

|

|________________

Timer0 interrupt

on overflow

;|______________________ Global interrupts

bcf

INTCON,5

; Disable TMR0 interrupts

bcf

INTCON,7

; Disable global interrupts

clrf

TMR0

; Reset timer

clrwdt

; Clear WDT for prescaler

assign

Bank1

; Set up the

OPTION register bit map

;

7

6

5

4

3

2

1

0 <= OPTION bits

;

1

1

0

1

1

0

0

0 <= setup

;

|

|

|

|

|

|__|__|_____

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

;

|

*1

=

raising edge

;|__________________________ RBPU (Pullup enable)

;

0

= enabled

;

*1

= disabled

movlw

b’11010000’ ; set up timer/counter

movwf

OPTION_REG

Bank0

return

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

;

receive character

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

;Receive a single character through the serial port.

;Assumes: 4800 baud, 8 data bits, no parity, 1 stop bit.

;Receiving line is Port-A, line 0

rcvTTY:

movlw

0x08

; Counter for 8 bits

movwf

bitCount

; The start of character transmission is signaled by


418

Chapter 14

; the sender by setting the line low

startBit:

btfsc

PORTA,0

; Test for low on line

goto

startBit ; Go if not low

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

;offset to data bit ;=========================

;At this point the receiver has found the falling

;edge of the start bit. It must now wait one and

;one-half the baud rate to synchronize in the center

;of the sender’s first data bit, as follows:

;|<========= falling edge of START bit

;

|

|<========== center of start bit

;

|

|

|<====== center of data bit

;|-----|-----|

;_____

___________

__________

;

|

|

|

|

<== SIGNAL

;

-----------

----------

;

|<-- 208 -->|h| <====== ms. for 4800

baud

;

; Clock start count for one-half bit

= 255

- 104

= 151

; Clock start count for one full bit =

255

- 208

= 47

;One clock cycle is added for the movwf instruction:

;clkHalf = 152 (for one-half bit countdown)

;clkFull = 48 (for one full bit countdown)

movlw

halfBaud

; Skip one-half bit

movwf

TMR0

; Initialize tmr0 and start count

bcf

INTCON,2

; Clear overflow flag

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

;

start bit

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

wait1:

btfss

INTCON,2

; Timer count overflow?

goto

wait1

; No, keep waiting

; At this point we are at the center of the start bit

btfsc

PORTA,0

; Check to see it is still low

goto

startBit ; No, it is high. False start

;At this point the clock is at the center of the start

;bit. The first data bit must be read one full baud

;period later

movlw

fullBaud

; One full bit delay

movwf

TMR0

; Start timer

bcf

INTCON,2

; clear tmr0 overflow flag

wait2:

btfss

INTCON,2

; End of one full baud period?

goto

wait2

; Wait if not end of period

;Timer is now at the center of the first/next data bit

;Timer must be reset immediately so that code will not


Communications

419

; lose synchronization with sender

movlw

fullBaud ; Skip to next data bit

movwf

TMR0

; Restart timer

bcf

INTCON,2

; Reset overflow flag

; Now the data bit can be read and and stored

movf

PORTA,w

; Read Port-B

movwf

tempData ; Store in temporary variable

rrf

tempData,f

; Rotate bit 0 into carry flag

rrf

rcvData,f

; Rotate carry flag into

; storage register high-order

; bit

decfsz

bitCount,f

; End of data?

goto

wait2

; Continue until 8 bits received

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

;

stop bit

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

stopWait:

btfss

INTCON,2

; Test time

goto

stopWait ; Wait

return

; Exit

;============================================================ ; send character ;============================================================

;Procedure to send one character through the RS232 line.

;Assumes: 2400 baud, 8 data bits, no parity, one stop bit

;Sending line is Port-B, line 0

;ON ENTRY:

;variable sendData holds character to send

sendTTY:

movlw

0x08

; Init bit counter

movwf

bitCount

bcf

PORTB,0

; Low for start bit

movlw

fullBaud ; For one baud space

movwf

TMR0

; Start timer

bcf

INTCON,2

; Clear timer flag

start2snd:

btfss

INTCON,2

; Full baud done?

goto

start2snd

; No

movlw

fullBaud ; Reset for one full bit period

movwf

TMR0

; Start timer

bcf

INTCON,2

; Clear flag

;At this point the start bit has been sent

;Data follows

sendOut:

rrf

sendData,f

; Rotate

bit into

carry

bcf

PORTB,0

;

Assume

data bit

is 0

btfsc

STATUS,c

;

Test if carry set


420

Chapter 14

bsf

PORTB,0 ; Change bit to 1 if clear

; Hold bit for 1 baud period

timeBit:

btfss

INTCON,2

; Wait for baud period to end

goto

timeBit

; Loop if not yet

movlw

fullBaud

; Reset timer

movwf

TMR0

; Start timer

bcf

INTCON,2

; Clear flag

; Test for last bit

decfsz

bitCount,f

; Count this bit

goto

sendOut

; Continue if not last bit

; Done. Send stop bit

bsf

PORTB,0

; High for stop bit

stopBit:

btfss

INTCON,2

; Timer done?

goto

stopBit

; No

; Set Port-B line 0 high back again

bsf

PORTB,0

call

delay_5

; And hold

return

End

14.5.5 SerComLCD Program

;File name: SerComLCD.asm

;Last revision: May 14, 2006

;Author: Julio Sanchez

;Processor: 16F877

;Description:

;Decode 4 x 4 keypad, display scan code in LCD, and send

;ASCII character through the serial port. Also receive

;data through serial port and display on LCD. LCD lines

;are scrolled by program.

;Default serial line setting:

;

2400 baud

;

no parity

;

1

stop bit

;

8

character bits

;

;Program uses 4-bit PIC-to-LCD interface.

;Code assumes that LCD is driven by Hitachi HD44780

;controller and PIC 16F977. Display supports two lines

;each one with 20 characters. The length, wiring and base

;address of each display line is stored in #define

;statements. These statements can be edited to accommodate

;a different set-up.