Professional Documents
Culture Documents
Software listing
As said, identical software is used in
both parts of the circuit. There are two
parts to the software, one associated with
the transmission and the other with the
reception and display. When the software
is run, it first checks the status of pin RA2.
If the pin is low the software routes to the
transmission part, if it is high it routes to
the reception part. The pin connections are
shown in Fig.8.1.
48
Microchip state that it may be advantageous to use BRGH = 1 even for slower
Baud clocks, because its equation can
reduce Baud rate error in some cases.
Transposing the formula when BRGH =
1, we get:
Listing 8.1
SETBAUD
BANK1
movlw 103
movwf SPBRG
movlw b00100100
movwf TXSTA
bcf STATUS,RP0
movlw b10010000
movwf RCSTA
call FlushRXBuffer
return
FlushRXBuffer
movf RCREG,W
movf RCREG,W
movf RCREG,W
return
Listing 8.3
Baud = Fosc / (16 x (X + 1) for BRGH = 1
Baud = Fosc / (64 x (X + 1) for BRGH = 0
49
Listing 8.2
Public Sub Calculate_Click()
= (XtalIndex / (BaudIndex * 64)) 1
=0
=C
= XtalIndex / (64 * (CInt(BRGH0) + 1))
= Int(actualbaud0 * 1000) / 1000
= ((actualbaud0 - BaudIndex) /
BaudIndex) * 100
= Int(error0 * 1000) / 1000
= "SPBRG = " & CInt(BRGH0)
= "Baud = " & actualbaud0
= "Error " & error0 & "%"
= "NOT POSSIBLE"
= ""
= ""
=C
= XtalIndex / (16 * (CInt(BRGH1) + 1))
= Int(actualbaud1 * 1000) / 1000
= ((actualbaud1 - BaudIndex) /
BaudIndex) * 100
= Int(error1 * 1000) / 1000
= "SPBRG = " & CInt(BRGH1)
= "Baud = " & actualbaud1
= "Error " & error1 & "%"
Listing 8.4
MAIN
clrf TMR1L
clrf TMR1H
bsf PORTA,1
bsf T1CON,0
M2
btfss PORTA,4
goto M2
bcf T1CON,0
bcf PORTA,1
movlw S
call TXBYTE
movf TMR1H,W
call TXBYTE
movf TMR1L,W
call TXBYTE
call PAUSIT
call PAUSIT
goto MAIN
Listing 8.5
TxByte nop
btfss PIR1,TXIF
goto TxByte
movwf TXREG
return
50
The timers MSB and LSB are then similarly transmitted, followed by a brief
pause, and the process repeats, until the
power is switched off.
At the TXBYTE routine a check is first
made to see if the transmission buffer is
empty:
Once a value is within the buffer, no further program action is needed for transmission, the PIC automatically takes care of it.
First, the PICs RCSTA and TRISB registers are set for reception. Then, at R1, a
check to see if any data has been received,
looping if it has not:
R1 btfss PIR1,RCIF ; Check for any RXd
data
goto R1
; Nothing RXd
When it has been received, the data is
loaded into W, via movf RCREG,W, and
a check made to see if the data is S, the
sync command transmitted. If not, the loop
repeats.
If the data is correct, Port A is toggled to
signal to you, via a multimeter if desired,
that correct reception has taken place.
In theory, of course, it is possible that
counter value data could also have the
same 8-bit value as the ASCII for letter
S, but the technique reduces the
chances of data being received in the
wrong order. A better technique using
checksums, was discussed by Mike
Hibbett in PIC n Mix Jan 08.
Now the two counter bytes are awaited,
MSB and LSB, and loaded into W when
received. Next, the RCSTA and TRISB
registers are set to allow Port B to be used
to send data to the LCD:
movlw 0
movwf RCSTA
BANK1
movlw b00000000 ; set TRISB for LCD
output
movwf TRISB
BANK0
The received two-byte value is now digitised and output to the LCD on its Line 1.
Each time a fresh batch of data is received,
an 8-bit counter is incremented and its
value is shown on Line 1 following the TX
value. The process then repeats for as long
as data is transmitted until the power is
switched off in this demo.
Data reception is slightly complicated by
the fact that both the LCD and the RS232
pins are within Port B.
Consequently, a few housekeeping commands are required to ensure correct functioning of the TRISB register, study the
main program.
Time-out
In a real program, rather than just a
demo, it is possible that TX/RX could be
interrupted by a fault, such as the TX PIC
going off-line or the connecting wires
being broken. If this were possible, the use
of a time-out would be beneficial.
A time-out option can be easily inserted
into the reception routines, resetting a
timer each time reception is required and
incrementing it until data is received. If
data is not received within a given period,
a termination routine would be entered.
Timer 1
We are also introducing here the use of
the PIC16F628s Timer 1. Unlike the 8bit Timer 0 previously discussed, Timer 1
is a 2-byte (16-bit) counter, with its MSB
and LSB held in TMR1H and TMR1L
respectively.
The PIC on the breadboard uses a software routine which is somewhat similar to
analogue-to-digital conversion (ADC).
The PICs Timer 1 is used to monitor a
variable voltage on its pin RA4. It is not a
true ADC, as the PIC16F628 does not have
the facility, unlike some PICs. It is controlled by register T1CON.
The timer can have any one of eight
prescale values set to be active via its bits
5 and 4:
11
10
01
00
1:8
1:4
1:2
1:1
ADC techniques
51
Reaction Timer
Theres room this month to provide you
with a Reaction Timer. Its circuit is shown
in Fig.8.5, and the breadboard layout in
Fig.8.6. Assemble it and load the PIC with
TEACHINH02.hex.
When the program is run, the message
REACTION TIMER is shown on LCD
line 2 and LED D2 is turned on. Now press
switch S2 to start a random delay. At the
end of the delay, LED D1 comes on. As
soon as it does, LED D2 is turned off and
a timer based on Timer 1 is started. Press
switch S1 as soon as you see LED D1 lit.
The time between D1 coming on and you
pressing S1 is then displayed on line 1, in
seconds, to three decimal places.
During the time between D1 coming on
and you pressing S1, Timer 1 counts at the
PICs clock rate divided by four every
microsecond for a 4MHz clock rate. Each
time Timer 1 rolls over, a counter is incremented. When S1 is pressed, The counter
value is multiplied by 63356 and added to
the timers value. The answer is divided by
one million and displayed as a seconds
count.
In this instance, the division is done by
simply placing a decimal point in the display at the appropriate position, and omitting the final three digits of the converted
decimal value. The delay value is updated
in a semi-random fashion while the wait
for S1 to be pressed is in progress.
Should S1 not be pressed before the
counter rolls over, an overflow condition
exists and is diplayed on screen; LED D2
comes on again. S2 must be pressed again
to restart the process from the beginning.
Study the ASM files code to see how the
whole program operates.
EPE BINDERS
KEEP YOUR MAGAZINES SAFE RING US NOW!
This ring binder uses a special system to allow the issues to be easily removed and re-inserted without any damage.
A nylon strip slips over each issue and this passes over the four rings in the binder, thus holding the magazine in place.
The binders are finished in hard-wearing royal blue p.v.c. with the magazine logo in gold on the spine. They will keep
your issues neat and tidy but allow you to remove them for use easily.
The price is 7.95 plus 3.50 post and packing. If you order more than one binder add 1 postage for each binder
after the initial 3.50 postage charge (overseas readers the postage is 6.00 each to everywhere except Australia
and Papua New Guinea which costs 10.50 each).
Send your payment in s sterling cheque or PO (Overseas readers send sterling bank draft, or cheque drawn
on a UK bank or pay by card), to Everyday Practical Electronics, Wimborne Publishing Ltd, Sequoia House,
398a Ringwood Road, Ferndown, Dorset BH22 9AU.
Tel: 01202 873872. Fax: 01202 874562.
E-mail: editorial@epemag.wimborne.co.uk.
Web site: http://www.epemag.co.uk
Order on-line from: www.epemag.wimborne.co.uk/shopdoor.htm
We also accept card payments. Mastercard, Visa or Maestro. Send your card number and card valid from and expiry date and
the card security code (the last 3 digits on the signature strip) plus Maestro Issue No. with your order.
52