1

I've been happily using a PIC 16F628A UART for some time now. I've switched up to a PIC 18F4431 chip recently but having some problems with the USART. Initially I couldn't get the transmit function to work, it was transmitting but at only 0.8V for high condition. Strangely I then put in these lines:

 movlw   0FFh
 movwf   PORTB 

and suddenly it started transmitting correctly and I can receive data OK via the UART on Pickit2. Unfortunately I now cannot receive anything via the UART. I've tried multiple chips and they all exhibit the same behaviour.

I've attached my code below, any ideas??

include <P18f4431.INC>


CONFIG     OSC = IRC
CONFIG     FCMEN = OFF
CONFIG     IESO = OFF
CONFIG     PWRTEN = OFF
CONFIG     BOREN = OFF
CONFIG     BORV = 20
CONFIG     WDTEN = OFF
CONFIG     WDPS = 1
CONFIG     WINEN = OFF
CONFIG     PWMPIN = OFF
CONFIG     LPOL = LOW
CONFIG     T1OSCMX = OFF
CONFIG     FLTAMX = RC1
CONFIG     SSPMX = RD1
CONFIG     PWM4MX = RB5
CONFIG     EXCLKMX = RC3
CONFIG     MCLRE = ON
CONFIG     CP0 = OFF
CONFIG     CP1 = OFF
CONFIG     CP2 = OFF
CONFIG     CP3 = OFF
CONFIG     CPD = OFF
CONFIG     WRT1 = OFF
CONFIG     WRT2 = OFF
CONFIG     WRT3 = OFF
CONFIG     WRTC = OFF
CONFIG     WRTB = OFF
CONFIG     WRTD = OFF
CONFIG     EBTR0 = OFF
CONFIG     EBTR1 = OFF
CONFIG     EBTR2 = OFF
CONFIG     EBTR3 = OFF
CONFIG     EBTRB = OFF
CONFIG     HPOL = LOW
CONFIG     STVREN = OFF
CONFIG     DEBUG = OFF
CONFIG     CPB = OFF
CONFIG     WRT0 = OFF
CONFIG     LVP = OFF


    ;***********VARIABLE DEFINITIONS******************

Timer   res 1
Timer2  res 1
DataTemp1  res 1
DataTemp2  res 1
dataL   res 1

 ;*******************************************************************************
 ; Reset Vector
 ;*******************************************************************************

 STARTUP  CODE    0x00           ; processor reset vector
 GOTO    START                   ; go to beginning of program

org    0x0008
 GOTO    HIGH_ISR

 ;*******************************************************************************
 ; MAIN PROGRAM
 ;******************************************************************************

 PROG1 code

 START

 clrf PORTD
 clrf TRISD
 clrf LATB   ; clear portB output latches
 clrf TRISB  ; set all portB as outputs
 clrf TRISC   ;set all port C as outputs

 ;***********SET UP USART ***********************
 bsf TRISC, 7    ; Make RC7 an input
 bsf TRISC, 6
 bsf TRISC, 1
 movlw   d'12'     ;9600 baud at 8MHz
 movwf   SPBRG
 bsf TXSTA, TXEN     ; enable transmit
 bcf TXSTA, BRGH     ;de Select high baud rate
 bsf RCSTA, SPEN     ; enable serial port
 bsf RCSTA, CREN     ; Enable continuous reception
 bcf PIR1, RCIF      ; clear RCIF Interrupt Flag
 bsf PIE1, RCIE      ; Set RCIE Interrupt Enable
 bsf INTCON, PEIE    ; Enable peripheral interrupts
 bsf INTCON, GIE     ; Enable global interrupts

 movlw   0FFh
 movwf   PORTB  ; I don't know why I need to do this but this seems to be
               ; needed to kick start the USART !!!!!


 ;***********SET UP OSCILLATOR**************

 bsf   OSCCON, IRCF0
 bsf   OSCCON, IRCF1
 bsf   OSCCON, IRCF2

 ;***********MAIN ROUTINE **********************************

 ;********SETTLING TIME *************
 clrf    dataL
 settle: decfsz dataL, F
 goto settle
 ;*******flush the receive buffer**************
 movf RCREG, w
 movf RCREG, w
 movf RCREG, w

 ;turn on lamp to show power
  bsf PORTD, 1


 Loop:
  call Delay
  GOTO Loop                         ; loop forever


Delay:

 movlw   d'255'
 movwf   Timer
 movwf   Timer2

Loop2:
 nop
Loop1:
 nop
 decfsz Timer, f
 goto Loop1
 decfsz Timer2, f
 goto Loop2
 return


 HIGH_ISR

btfss   PIR1, RCIF  ;check if USART caused interrupt
goto OtherInt
btg     PORTD, 0
movlw   06h     ; mask unwanted bits
andwf   RCSTA,w    ;check for errors
btfss   STATUS,Z   ;was error status bit set?
goto    RCVError    ; Found error goto handler
movf    RCREG, w    ; get input data and stick it on w
movwf   LATB        ; Display on LEDs
movwf   TXREG       ; Echo character back
goto    ISREnd      ; go to end of ISR, restore context, return


RCVError
bcf RCSTA, CREN     ; clear receiver status
bsf RCSTA, CREN
movlw   0FFh    ; light all the LEDs
movwf   PORTB
goto ISREnd ; goto end of ISR

OtherInt
ISREnd

RETFIE

END
PeterJ
  • 17,131
  • 37
  • 56
  • 91
tommo2000
  • 21
  • 2
  • In my experience the more features a PIC chip has the more setting up it requires. So I go through every feature (UARTS, Timers, INterupts etc) and enable/disable as required to get to a known state. From the loading of PORTB with 0FFh I would guess the output pull ups needed to be enabled other wise you would only have open collectors being turned on ... hence the 0.8V. try adding a pull up to the Tx pin and you may not need to load PORTB with 0FFh. However it has been a long time since I used a UART on a PIC. – Spoon Jul 05 '13 at 11:26
  • When I say every feature.... I mean EVERY feature in the chip.... – Spoon Jul 05 '13 at 11:27
  • I've been through the data sheet in detail on this topic and I'm struggling to find anyhthing not set correctly. Adding the pull up hasn't changed the behaviour. – tommo2000 Jul 15 '13 at 19:46

0 Answers0