|
In an effort to expand our serial communication capabilities, we will get a couple of PIC16F84 to talk to each other. Actually we’ll do part of the job by getting one PIC16F84 to talk while the other listens.
We’ll see if the listener understood what the talker said. We will set this up so you can continue on your own by sending more than one word and by interchanging the talk/listen roles ( two-way communication ).
Both PIC16F84 with 4 MHz clock oscillators. For the transmitting chip, port A, bit 1 is used to transmit. The receiver uses port A, bit 0 to receive. We will choose the bit time interval as 256 internal clock ( 1 MHz ) cycle. Both transmitter and receiver will use TMR0 for timing.
When the transmit data ( TD ) line is high, the condition is known as “mark”. When TD is low , the condition is known as “Space”. The terminology comes from the old teletype days.
When one word ( 8 bits ) is sent, the TD line output vs time will look like this :
;=======P2PSEND.ASM=================================4/29/97==
list p=16f84a
__config h'3ff1'
radix hex
;------------------------------------------------------------
; cpu equates (memory map)
tmr0 equ 0x01
status equ 0x03
porta equ 0x05
intcon equ 0x0b
sendreg equ 0x0c
count equ 0x0d
optreg equ 0x81
trisa equ 0x85
;------------------------------------------------------------
; bit equates
c equ 0
rp0 equ 5
;------------------------------------------------------------
org 0x000
;
start bsf status,rp0 ;switch to bank 1
movlw b'00000100' ;port A inputs/outputs
movwf trisa
bcf status,rp0 ;switch back to bank 0
bsf porta,1 ;output mark, bit 1
movlw 0x80 ;number to be sent
movwf sendreg ;store
switch btfsc porta,2 ;start send?
goto switch ;not yet
call ser_out ;to serial out subroutine
circle goto circle ;done
;------------------------------------------------------------
ser_out bcf intcon,5 ;disable tmr0 interrupts
bcf intcon,7 ;disable global interrupts
clrf tmr0 ;clear timer/counter
clrwdt ;clear wdt prep prescaler assign
bsf status,rp0 ;to bank 1
movlw b'11011000' ;set up timer/counter
movwf optreg
bcf status,rp0 ;back to bank 0
movlw 0x08 ;init shift counter
movwf count
bcf porta,1 ;start bit
clrf tmr0 ;start timer/counter
bcf intcon,2 ;clear tmr0 overflow flag
time1 btfss intcon,2 ;timer overflow?
goto time1 ;no
bcf intcon,2 ;yes, clear overflow flag
nxtbit rlf sendreg,f ;rotate msb into carry flag
bcf porta,1 ;clear port A, bit 1
btfsc status,c ;test carry flag
bsf porta,1 ;bit is set
time2 btfss intcon,2 ;timer overflow?
goto time2 ;no
bcf intcon,2 ;clear overflow flag
decfsz count,f ;shifted 8?
goto nxtbit ;no
bsf porta,1 ;yes, output mark
time3 btfss intcon,2 ;timer overflow?
goto time3 ;no
return ;done
;------------------------------------------------------------
end
;------------------------------------------------------------
;at device program time, select:
; code protection off
; watchdog timer disabled (default is enabled)
; standard crystal XT (using 4 MHz osc for test)
; power-up timer on
;============================================================
source : pic'n up the pace
No comments:
Post a Comment