Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8658
Скачиваний: 0
358 |
Chapter 14 |
The code comments explain the routine’s operation.
The receiving program, named SerialRcv, runs in the receiver PIC. In this case, the serial line is RA0. Input from the sender program is received through this line. The program performs the following initialization operations:
1.Lines RA0 and RA2 are initialized for input since the pushbutton switch is located on RA2 and RA0 is the serial input line. Lines RB0 to RB7 are output since they are wired to the eight LEDs.
2.The prescaler is assigned to the Watchdog Timer so that channel TMR0 runs at full processor speed.
3.Interrupts are disabled.
Once initialized, code performs the following functions:
1.The SEND READY LED is turned on.
2.Code monitors the RECEIVE READY pushbutton switch.
3.Once the switch is pressed, the program turns on the RECEIVE READY LED.
4.Code then monitors the serial line for the first low that indicates the leading edge of the start bit.
5.Once the start bit is detected, code waits for 128 clock cycles to locate the center of the start bit. This synchronizes the receiver with the sender and accommodates small timing errors.
6.The eight data bits are then received and stored.
7.After waiting for the stop bit, code turns off the RECEIVE READY LED and sets the eight LEDs according to the data received through the serial line.
The following code fragment is the procedure rcvData from the SerialRcv program:
;============================================================
; |
procedure to receive serial data |
;============================================================
;ON ENTRY:
;local variable dataReg is used to store 8-bit value
;received through port (labeled serialLN)
;OPERATION:
;1. The timer at register TMR0 is set to run at
;maximum clock speed, that is, 256 clock beats.
;The timer overflow flag in the INTCON register
;is set when the timer cycles from 0xff to 0x00.
;2. When the START signal is received, the code
;waits for 128 timer beats so as to read data in
;the middle of the send period.
;3. Each bit (start, data, and stop bits) is read
;at intervals of 256 timer beats.
;4. The procedure tests the timer overflow flag
;(tmrOVF) to determine when the timer cycle has
Communications |
359 |
; ended, that is when 256 clock beats have passed.
;=============================================================
rcvData:
clrf |
TMR0 |
; |
Reset timer |
movlw |
0x08 |
; |
Initialize bit counter |
movwf |
bitCount |
;=========================
; wait for START bit
;=========================
startWait: |
||
btfsc |
PORTA,0 ; Is port A0 low? |
|
goto |
startWait |
; No. Wait for mark |
;=========================
;offset 128 clock beats ;=========================
;At this point the receiver has found the falling
;edge of the start bit. It must now wait 128 timer
;beats to synchronize in the middle of the sender’s
;data rate, as follows:
; |
|<========= |
falling edge of START bit |
|
; |
| |
||
; |
|-----|<====== 128 clock beats offset |
||
; |
-----------. |
| |
.------- |
; |
| |
| <== SIGNAL |
|
; |
----------- |
||
; |
|<---256--->| |
||
; |
|||
movlw |
0x80 |
; 128 clock beats offset |
|
movwf |
TMR0 |
; to TMR0 counter |
|
bcf |
INTCON,tmrOVF |
; Clear overflow flag |
|
offsetWait: |
|||
btfss |
INTCON,tmrOVF |
; Timer overflow? |
|
goto |
offsetWait |
; Wait until |
|
btfsc |
PORTA,0 |
; Test start bit for error |
|
goto |
offsetWait |
; Recycle if a false |
|
start |
|||
;========================== |
|||
; |
receive data |
||
;========================== |
|||
clrf |
TMR0 |
; Restart timer |
|
bcf |
INTCON,tmrOVF |
; Clear overflow flag |
|
; Wait for 256 timer cycles for first/next data bit |
|||
bitWait: |
|||
btfss |
INTCON,tmrOVF |
; Timer cycle end? |
|
goto |
bitWait |
; Keep waiting |
|
; Timer has counter 256 beats |
|||
bcf |
INTCON,tmrOVF |
; Reset overflow flag |
|
movf |
PORTA,w |
; Read Port-A into w |
|
360 |
Chapter 14 |
|||
movwf |
temp |
; Store value read |
||
rrf |
temp,f |
; Rotate bit 0 into carry flag |
||
rlf |
rcvReg,f ; Rotate carry into rcvReg bit 0 |
|||
decfsz |
bitCount,f |
; 8 bits received |
||
goto |
bitWait |
; Next bit |
||
; Wait for one time cycle at end of reception |
||||
markWait: |
||||
btfss |
INTCON,tmrOVF |
; Timer overflow flag |
||
goto |
markWait |
; keep waiting |
||
;======================== |
||||
; |
end of reception |
|||
;======================== |
||||
return |
||||
Neither the SerialRcv nor the SerialSnd programs contain any handshake signal. The programs rely on the user turning-on the receiver before the send function is activated. If this is not the case, the programs fail to communicate. But looking at the circuit diagram in Figure 14-7, we notice that there are available ports in both receiver and sender circuits. The circuit designer could interconnect two ports, one in the receiver and one in the sender, so as to provide a handshake signal.
For example, lines RA4 in both circuits can be interconnected. Then Port-A, line 4, in the sender circuit is defined as input and the same line as output in the receiver. The receiver could then set the handshake line high to indicate that it is ready to receive. The sender monitors this same port and does not start the transmission of each character until it reads that the handshake line is high. In this manner, the receiver can suspend transmission at any time and prevent data from being lost. At the same time, the “receiver ready” and “send ready” LEDs can be eliminated.
14.3.2 Program Using Shift Register ICs
The problem of handling multiple input and output lines, which was resolved in the previous example by using two PICs, can also be tackled by means of special-purpose integrated circuits. The term shift register refers to the fact that register input and output are connected in a way that data is shifted-down a set of flip-flops when the circuits are activated. Many variations of shift registers ICs are available, the most popular ones being serial-in to serial-out, parallel-in to parallel-out, serial-in to parallel-out, and parallel-in to serial-out. In shift register terminology the in and out terms refer to the function in the registers themselves, and are not related to the functions that these elements perform in a particular circuit. Figure 14-9 shows an input/output circuit using shift registers.
The circuit in Figure 14-9 shows the use of a parallel-to-serial IC (74HC165) that reads the state of eight input switches, and a serial-to-parallel IC (74HC164) that outputs data to eight LEDs. Without the shift register ICs, the circuit would require sixteen ports, more than those available in the 16F84. Using the shift registers, only six PIC ports are required, leaving eight ports available on the PIC. The demonstration program for the circuit in Figure 14-9 is named Serial6465.