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

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

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

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

Добавлен: 14.06.2025

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

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

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

Communications

381

RCREG is double-buffered, meaning that it is possible for two bytes of data to be started simultaneously while a third byte begins shifting to RSR. If the stop bit is detected while RCREG is not empty, then the overrun error bit (OERR) is set in

RCSTA. RCREG operates in first-in-first-out order. When it is read twice the two bytes are retrieved in this order.

The overrun error bit (OERR) inhibits transfer from RSR into RCREG; therefore, it is important to clear this bit once the error is detected. The framing error bit (FERR) in the RCSTA register is set if a stop bit is not detected.

The following steps are followed in initializing and executing asynchronous reception:

1.The SPBRG register is set up for the selected baud rate.

2.Asynchronous reception is enabled by clearing the SYNC bit in the TXSTA register and setting the SPEN bit in the RCSTA register.

3.To enable the receive data interrupt, the RCIE, GIE, and PEIE bits must be set.

4.Reception is activated by setting the CREN bit in RCSTA.

5.When reception has concluded, the RCIF bit in the PIE1 register is set. At that time, an interrupt is generated if the RCIE bit was set.

6.Received data is retrieved by reading RCREG.

7.If any error occurred the CREN bit must be cleared.

PIC-to-PC RS-232-C Communications Circuit

To demonstrate serial communications with the RS-232-C protocol we developed a circuit consisting of a 4-by-4 keypad and a 2-line by 20-character LCD display. Characters typed on the keypad are converted to ASCII codes for the hexadecimal digit set, that is, the numeral digits and the letters A through F. When a key is pressed, the corresponding ASCII code is displayed in the LCD and transmitted through the serial port to a PC application. Characters received through the serial line are displayed on the LCD. Figure 14-18 (in the following page) is a wiring diagram of the circuit.

The program SerComLCD demonstrates the circuit in Figure 14-18:

16F877 PIC Initialization Code

The following code fragment shows the initialization of the UART module in the 16F877 PIC for 2400 baud, 8 bits, no parity, and one stop bit. No interrupts are used in this example.

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

;

USART initialization procedure

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

;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

382

Note:

MAX202 IC requires

components not shown

in this circuit diagram.

See device data sheet.

R=270

X 4

RESET

+5v

R=10K

Chapter 14

KEYPAD 4 x 4

SW1

SW2

SW3

SW4

SW5

SW6

SW7

SW8

SW9

SW10

SW11

SW12

SW13

SW14

SW15

SW16

1

RB7/PGD

40

2

!MCLR/VPP 16F877

39

RA0/AN0

RG6/PGC

3

RB5

38

RA1/AN1

4

RB4

37

RA2/AN2.VREF-

5

RB3/PGM

36

RA3/AN3/VREF+

LCD

6

RB2

35

RA4/TOCKI

7

34

RB1

2 rows x 20

RA5/AN4/SS

8

RB0/INT

33

RE0/!RD/AN5

9

32

RE1/!WR/AN6

VDD

14

10

31

RE2/!CS/AN7

VSS

+5v

11

30

VDD

RD7/PSP7

12

29

VSS

RD6/PSP6

13

28

OSC1/CLKIN

RD5/PSP5

14

27

OS2/CLKOUT

RD4/PSP4

4 MHz

15

26

RC0/T1OSO/T1CKI

RC7/RX/DT

Osc

16

25

RC1/T1OSI/CCP2

RC6/TX/CK

17

24

RS

RC2/CCP1

RC5/SD0

18

23

RC3/SCK/SCL

RC4/SDI/SDA

19

22

E

RD0/PSP0

RD3/PSP3

20

21

RD1/PSP1

RD2/PSP2

R/W

+5 V

+5 V

DB-9

1

1

MAX202

16

(female)

C1+

+5v

2

15

GND

V+

5

4

3

2

1

3

14

C1-

T1out

9

8

7

6

4

R1in

13

C2+

HD44780

C2-

R1out

12

5

6

T1in

11

V-

7

T2in

10

T2out

8

R2out

9

R2in

Figure 14-18 USART Communications Circuit with PIC 16F877

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


Communications

383

;Where x is the 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 = syncrhonous 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)

; Setup value: 0010 0000 = 0x20

movlw

0x20

; Enable transmission and high

baud rate

movwf

TXSTA

Bank0

; Bank 0


384

Chapter 14

; 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

;

clrf

errorFlags ; Clear local error flags register

Return

USART Receive and Transmit Routines

The transmit data routine is quite simple. Code checks the TXIF bit in PIR1. If the bit is set, data is transmitted by storing the data byte in TXREG. The following procedure performs the required operations.

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

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

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

Bank0

; Select bank 0

busyWait:


Communications

385

btfss

PIR1,TXIF

;

check if transmitter busy

goto

busyWait ; wait until transmitter is not busy

movwf

TXREG

;

and transmit the data

return

Receiving data is more complicated than transmitting it. One of the reasons is that code must test for and handle several possible errors that can occur during reception. The following code fragment shows the local variables and processing required for simple data reception.

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

; variables in PIC RAM

;===================================================== ; Local variables

cblock 0x20

; Start of block

.

.

.

; Communications variables

newData

; not 0 if new data received

ascVal

errorFlags

endc

;=========================================================== ; USART receive data procedure ;===========================================================

;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


386

Chapter 14

; 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

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

;error handlers ;==========================

;Overrun error detected OverErr:

bsf

errorFlags,0

; Bit 0 is overrun

error

; Reset system

errExit:

bcf

RCSTA,CREN

; Clear continuous

receive bit

bsf

RCSTA,CREN

; Set to re-enable

reception

return

; Error. FERR framing error bit is set

FrameErr:

bsf

errorFlags,1

; Bit 1 is framing

error

movf

RCREG,W

; Read and throw away bad data

goto

errExit

The procedures listed previously are from the program SerComLCD in the book’s online software. The applicable circuit is shown in Figure 14-18.

The USART Receive Interrupt

Polled routines for serial communications are adequate when the application does little else but check transmission lines. If the application has other tasks to perform, polled routines can waste processing time and even lose data. In this sense, the send function is usually less critical. An application can typically determine when to send data and have available all the data when the send operation activates. This is often not the case in receiving data, especially in applications that execute full-duplex.

A practical solution is to use interrupts for receiving characters through the serial line. The 60F87x includes facilities for implementing interrupt routines by both the send and the receive functions. To enable interrupts for the USART receive operation the following preparatory steps are necessary:

1.Peripheral and global interrupts must be enabled by setting bits 6 and 7 of the INTCON register.

2.The receive interrupt must be enabled by setting the RCIF bit in the PIE1 register.

The handler for the serial reception interrupt usually performs the following functions: