Hi all,
I am trying to interface the serial port of the PIC16F877A to
communicate with the PC.
But I am not able to do so. I have listed below the connections that
I have made.
Any help will be of great help.
Thanks,
With Regards,
Alok
- Rx (Pin 26) of PIC16F877A connected to Pin9 (R2Out) of Max232.
R2in (Pin8) of Max232 connected to TxD (Pin3) of DB9 female
connector.
- Tx (Pin 25) of PIC16F877A connected to T2in (Pin10) of Max232.
T2out (Pin7) of Max232 connected to RxD (Pin2) of DB9 female
connector.
- Pin7&8 of the DB9 connector have been wired together.
Pin1, 4, 6 of the DB9 connector have been wired together.
- Max232 has been wired up with 1uF caps. When powered up, I can see
that
Pin2 has ~+10V and Pin6 has ~-10V.
- On the PC front, I have written a small program in VB which reads
the serial port.
I have tried the same using Realterm also.
- The program on the PIC receives the charater from the PC and sends
the character back
to the PC adding 1 to it.
;------------------------------------------------------
LIST P=16F877A, F=INHX8M
ERRORLEVEL -302
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF &
_CP_OFF & _CPD_OFF & _DEBUG_OFF
INCLUDE "COMMON.H"
;-------------------------------------------------------
; Program start
;-------------------------------------------------------
ORG 0x00 ; RESET vector location
RESET clrf PCLATH
GOTO MAIN
;-------------------------------------------------------
; This is the Periperal Interrupt routine.
;-------------------------------------------------------
ORG 0x03 ; Interrupt vector location
INTERRUPT
GOTO INTERRUPT_SERVICE
;-------------------------------------------------------
; Interrupt Service Routine
;-------------------------------------------------------
INTERRUPT_SERVICE
movwf WREG_TEMP ;save WREG
movf STATUS,W ;store STATUS in WREG
clrf STATUS ;select file register bank0
movwf STATUS_TEMP ;save STATUS value
movf PCLATH,W ;store PCLATH in WREG
movwf PCLATH_TEMP ;save PCLATH value
clrf PCLATH ;select program memory page0
movf FSR,W ;store FSR in WREG
movwf FSR_TEMP ;save FSR value
;Check for what caused the interrupt
Bank0
btfsc PIR1,RCIF ;test RCIF receive interrupt
bsf STATUS,RP0 ;change to bank1 if RCIF set
btfsc PIE1,RCIE ;test if interrupt enabled if RCIF set
goto RECEIVE ;if RCIF and RCIE set, do receive
; Restore STATUS and W and return from interrupt
EndInt: Bank0 ;select bank 0
movf FSR_TEMP,W ;get saved FSR value
movwf FSR ;restore FSR
movf PCLATH_TEMP,W ;get saved PCLATH value
movwf PCLATH ;restore PCLATH
movf STATUS_TEMP,W ;get saved STATUS value
movwf STATUS ;restore STATUS
swapf WREG_TEMP,F ;prepare WREG to be restored
swapf WREG_TEMP,W ;restore WREG without affecting STATUS
retfie
;--------------------------------------------------------------------
; Use receive routine as a command to send data to PC
;--------------------------------------------------------------------
RECEIVE
Bank0
btfsc RCSTA,OERR ;test overrun error flag
goto ErrOERR ;handle it if error
btfsc RCSTA,FERR ;test framing error flag
goto ErrFERR ;handle it if error
movf RCREG, W ; move received data to working register, THIS
CLEARS RCIF
movwf TEMP
movf TEMP,w
addlw 0x01
movwf TXREG
goto EndInt
;error because OERR overrun error bit is set
;can do special error handling here - this code simply clears and
continues
ErrOERR: bcf RCSTA,CREN ;reset the receiver logic
bsf RCSTA,CREN ;enable reception again
goto EndInt
;error because FERR framing error bit is set
;can do special error handling here - this code simply clears and
continues
ErrFERR: movf RCREG,W ;discard received data that has error
goto EndInt
;-------------------------------------------------------------------
; Serial Initialisation Starts
;-------------------------------------------------------------------
MAIN
Bank1
movlw 0xc0 ;set tris bits for TX and RX
iorwf TRISC,F
movlw 0x19 ; load value baud rate generation
movwf SPBRG
movlw 0x24 ;enable transmission and high baud rate
movwf TXSTA
Bank0
movlw 0x90 ;enable serial port and reception
movwf RCSTA
; configures RC7/RX/DT and RC6/TX/CK pins as serial port pins
; Disables address detection, all bytes are received, and ninth
bit can be used as parity bit
; Select 8-bit reception
; Enable 8-bit reception
Bank1
movlw 0x20
movwf PIE1
Bank0
movlw 0xC0 ;enable global and peripheral ints
movwf INTCON
movlw 0x35 ; Debugging info
movwf TXREG
HERE goto HERE
------------------------------------
to unsubscribe, go to http://www.yahoogroups.com and follow the instructions
______________________________
controlSUITE software. Comprehensive. Intuitive. Optimized.
Real-world software for real-time control. Details Here!

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )