Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8563
Скачиваний: 0
Communications |
451 |
;ON EXIT:
;w contains merged bits
merge4:
andlw |
b’11110000’ |
; ANDing with |
0 clears |
the |
; bit. ANDing |
with 1 preserves |
|||
; the original value |
||||
movwf |
store2 |
; Save result |
in variable |
|
movf |
PORTD,w |
; Port-B to w |
register |
|
andlw |
b’00001111’ |
; Clear high nibble in |
Port-B |
|
; and preserve low nibble |
||||
iorwf |
store2,w |
; OR two operands in w |
||
return |
||||
;==========================
;Set address register
;to LCD line 2 ;==========================
;ON ENTRY:
;Address of LCD line 2 in constant LCD_2
line2:
bcf |
PORTE,E_line |
; E line low |
bcf |
PORTE,RS_line |
; RS line low, setup for |
control |
||
call |
delay_5 |
; Busy? |
; Set to second display line |
||
movlw |
LCD_2 |
; Address with high-bit set |
call |
send8 |
|
; Set RS line for data |
||
bsf |
PORTE,RS_line |
; RS = 1 for data |
call |
delay_5 |
; Busy? |
return |
||
;==========================
;Set address register
;to LCD line 1 ;==========================
;ON ENTRY:
;Address of LCD line 1 in constant LCD_1
line1:
bcf |
PORTE,E_line |
; E line low |
bcf |
PORTE,RS_line |
; RS line low, set up for |
; control |
||
call |
delay_5 |
; busy? |
; Set to second display line |
||
movlw |
LCD_1 |
; Address and command bit |
call |
send8 |
; 4-bit routine |
; Set RS line for data |
||
bsf |
PORTE,RS_line |
; Setup for data |
452 |
Chapter 14 |
|
call |
delay_5 |
; Busy? |
return |
;==========================
;scroll to LCD line 2 ;==========================
;Procedure to count the number of characters displayed on
;each LCD line. If the number reaches the value in the
;constant LCDlimit, then display is scrolled to the second
;LCD line. If at the end of the second line, then LCD is
;reset to the first line.
LCDscroll: |
||
incf |
LCDcount,f |
; Bump counter |
; Test for line limit |
||
movf |
LCDcount,w |
|
sublw |
LCDlimit |
; Count minus limit |
btfss |
STATUS,Z |
; Is count - limit = 0 |
goto |
scrollExit |
; Go if not at end of line |
;At this point the end of the LCD line was reached
;Test if this is also the end of the second line movf LCDline,w
sublw |
0x01 |
; Is it line 1? |
btfsc |
STATUS,Z |
; Is LCDline minus 1 = 0? |
goto |
line2End |
; Go if end of second line |
; At this point it is the end of the top LCD line |
||
call |
line2 |
; Scroll to second line |
clrf |
LCDcount ; Reset counter |
|
incf |
LCDline,f |
; Bump line counter |
goto |
scrollExit |
|
; End of second LCD line |
||
line2End: |
||
call |
initLCD |
; Reset |
clrf |
LCDcount ; Clear counters |
|
clrf |
LCDline |
|
call |
line1 |
; Display to first line |
scrollExit: |
||
return |
||
;============================================================== ; 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
Communications |
453 |
|
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 9600 baud
;using a 4Mhs oscillator at a high-speed baud rate the formula
;is:
; |
4,000,000 |
4,000,000 |
;--------- = --------- = 9,615 baud (0.16% error)
; |
16*(25+1) |
416 |
;
; At slow speed (BRGH = 0)
; |
4,000,000 |
4,000,000 |
;--------- = ---------- = 2,403.85 (0.16% error)
; |
64*(25+1) |
1,664 |
|||||||||
; |
|||||||||||
movlw |
spbrgVal ; Value in spbrgVal = 25 |
||||||||||
movwf |
SPBRG |
; Place in baud rate generator |
|||||||||
; TXSTA (Transmit Status |
and Control Register) bit map: |
||||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<== |
bits |
|
; |
| |
| |
| |
| |
| |
| |
| |
|______ |
TX9D 9nth data bit on |
||
; |
| |
| |
| |
| |
| |
| |
| |
? |
(used for parity) |
||
; |
| |
| |
| |
| |
| |
| |
|_________ |
TRMT Transmit Shift Register |
|||
; |
| |
| |
| |
| |
| |
| |
1 = TSR empty |
||||
; |
| |
| |
| |
| |
| |
| |
* |
0 = TSR full |
|||
; |
| |
| |
| |
| |
| |
|____________ |
BRGH High Speed Baud Rate |
||||
; |
| |
| |
| |
| |
| |
(Asynchronous mode only) |
|||||
; |
| |
| |
| |
| |
| |
1 = high speed (* 4) |
|||||
; |
| |
| |
| |
| |
| |
* |
0 = low speed |
||||
; |
| |
| |
| |
| |
|__________ |
NOT |
USED |
||||
; |
| |
| |
| |
|_____________ |
SYNC |
USART Mode Select |
|||||
; |
| |
| |
| |
1 |
= |
synchronous mode |
|||||
; |
| |
| |
| |
* |
0 |
= |
asynchronous mode |
||||
; |
| |
| |
|________________ |
TXEN |
Transmit Enable |
||||||
; |
| |
| |
* |
1 |
= |
transmit enabled |
|||||
; |
| |
| |
0 |
= |
transmit disabled |
||||||
; |
| |
|___________________ |
TX9 |
Enable 9-bit Transmit |
|||||||
; |
| |
1 |
= |
9-bit transmission mode |
|||||||
; |
| |
* |
0 |
= |
8-bit mode |
||||||
;|______________________ CSRC Clock Source Select
; |
Not |
used in |
asynchronous mode |
|
; |
Synchronous |
mode: |
||
; |
1 |
= |
Master Mode (internal clock) |
|
; |
* 0 |
= |
Slave |
mode (external clock) |
454 |
Chapter 14 |
||||||||||
; Setup value: 0010 0000 = 0x20 |
|||||||||||
movlw |
0x20 |
; Enable transmission and low baud rate |
|||||||||
movwf |
TXSTA |
||||||||||
Bank0 |
; Bank 0 |
||||||||||
; RCSTA (Receive Status and |
Control Register) bit map: |
||||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<== |
bits |
|
; |
| |
| |
| |
| |
| |
| |
| |
|______ RX9D 9th data bit received |
|||
; |
| |
| |
| |
| |
| |
| |
| |
? |
(can be parity bit) |
||
; |
| |
| |
| |
| |
| |
| |
|_________ |
OERR Overrun errror |
|||
; |
| |
| |
| |
| |
| |
| |
? |
1 = error (cleared by software) |
|||
; |
| |
| |
| |
| |
| |
|____________ |
FERR Framing Error |
||||
; |
| |
| |
| |
| |
| |
? |
1 = error |
||||
; |
| |
| |
| |
| |
|_______________ NOT USED |
||||||
; |
| |
| |
| |
|____________ CREN |
Continuous Receive Enable |
||||||
; |
| |
| |
| |
Asynchronous mode: |
|||||||
; |
| |
| |
| |
* |
1 |
= Enable continuous receive |
|||||
; |
| |
| |
| |
0 |
= Disables continuous receive |
||||||
; |
| |
| |
| |
Synchronous mode: |
|||||||
; |
| |
| |
| |
1 |
= Enables until CREN cleared |
||||||
; |
| |
| |
| |
0 |
= Disables continuous receive |
||||||
; |
| |
| |
|_______________ SREN |
Single Receive Enable |
|||||||
; |
| |
| |
? |
Asynchronous mode = don’t care |
|||||||
; |
| |
| |
Synchronous master mode: |
||||||||
; |
| |
| |
1 |
= Enable single receive |
|||||||
; |
| |
| |
0 |
= Disable single receive |
|||||||
; |
| |
|__________________ RX9 9th-bit Receive Enable |
|||||||||
; |
| |
1 = 9-bit reception |
|||||||||
; |
| |
* 0 = 8-bit reception |
|||||||||
;|_____________________ SPEN Serial Port Enable
; |
* |
1 |
= |
RX/DT and TX/CK are serial pins |
; |
0 |
= |
Serial port disabled |
|
; Setup value: 1001 0000 = |
0x90 |
|||
movlw |
0x90 |
; Enable serial port and |
|||||||
; continuous reception |
|||||||||
movwf |
RCSTA |
||||||||
; Enable |
glocal and peripheral interrupts |
||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<= INTCON bitmap |
; |
| |
| |
— unrelated —- |
||||||
; |
| |
|___________________ |
Peripheral interrupts enable |
||||||
; |
|______________________ |
Global interrupts enable |
|||||||
movlw |
b’11000000’ |
||||||||
movwf |
INTCON |
||||||||
; Enable |
receive interrupt |
in PIE1 register |
|||||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<= PIE1 bitmap |
; |
|________________ |
USART receive interrupt enable |
|||||||
Bank1 |
|||||||||
movlw |
b’00100000’ |
||||||||
Communications |
455 |
movwf |
PIE1 |
; Clear error flags register |
|
Bank0 |
|
clrf |
errorFlags |
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 |
||||
;============================================================
;============================================================ ; interrupt handler for received characters ;============================================================ ;============================================================ IntServ:
movwf |
tempW |
; Save W |
movf |
STATUS,W ; Store STATUS in W |
|
clrf |
STATUS |
; Select bank0 |
movwf |
tempStatus |
; Save STATUS |
movf |
PCLATH,W ; Store PCLATH in W |
|
movwf |
tempPclath |
; Save PCLATH |
clrf |
PCLATH |
; Select program memory page 0 |
movf |
FSR,W |
; Store FSR in W |
movwf |
tempFsr |
; Save FSR value |
; Test for received data interrupt |
||
Bank0 |
; select bank0 |
|
; 7 6 5 4 3 |
2 1 0 |
<= PIR1 |
;|__________________ (RCIF) USART receive interrupt
; |
flag |
|||
btfsc |
PIR1,RCIF |
; Test bit 5 |
||
bsf |
STATUS,RP0 |
; Bank 1 if RCIF set |
||
; |
7 6 5 4 3 |
2 1 0 |
<= PIE1 |
|
;|__________________ (RCIE) Receive interrupt enable
; |
bit |
||
btfss |
PIE1,RCIE |
; Test if interrupt is enabled |
|
goto |
IntExit |
; Go if not enabled |
|
;============================== |
|||
; |
received data |
||
;==============================