Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need help with UART on PIC16F877A

Status
Not open for further replies.

notwist

New Member
I'm having trouble receiving through the UART on the PIC16F877A. I am able to send from the PIC to the PC but I cannot send data from the PC to the PIC. I am using a MAX232 IC level shifter. And a Linx QS USB module connected to the PC. (**broken link removed**) I am able to receive data from the PIC with and without the MAX232 but the PIC is not able to receive anything.

Also, the MAX232 has 2 TTL-to-RS232 converters and 2 RS232-to-TTL converters. Which ones am i supposed to use from the PIC to PC and PC to PIC?

Here is the code I am using
Code:
   ;
     ; Author  Claudio RACHIELE IW0DZG
     ;
     ; Working  20000318
     ;
     list p=16f877,c=140    ; processor type
     errorlevel          1, -(305)
     #include "p16f877.inc"
     __CONFIG  _PWRTE_ON&_HS_OSC&_LVP_OFF&_WDT_OFF

temp  equ   7Fh               ;
;
; -----------
; DESCRIPTION
; -----------
;
; Baud Rate = 9600, No Parity, 8 bits & 1 Stop Bit
;
; -------------
; PROGRAM START
; -------------
;
     org  0         ; startup = 0000H
     clrf STATUS         ; bank 0
     goto BootStart
     org  4
ISR  goto 5         ;
     org  5
;
; --------------------------------------------------
; SET UP THE PORTS TO SUIT YOUR CIRCUIT REQUIREMENTS
; --------------------------------------------------
;

BootStart
     banksel PORTA       ; bank0
     movlw     b'00000000'
     movwf     PORTA
     movlw     b'00000000'
     movwf     PORTB
     movlw     b'01000000'
     movwf     PORTC
     movlw     b'00000000'
     movwf     PORTD
     movlw     b'00000000'
     movwf     PORTE

     banksel TRISA       ; bank1
     movlw     b'00000001'
     movwf     TRISA
     movlw     b'00000000'
     movwf     TRISB
     movlw     b'11000000'    ; set RC6 & RC7 as input
     movwf     TRISC
     movlw     b'00000000'
     movwf     TRISD
     movlw     b'11101000'
     movwf     TRISE
     movlw     b'00000111'
     movwf     ADCON1         ; porta inputs = digital not analog
;
; BAUD RATE SETTINGS
;
                    ;  Baud Values with BRGH = 0
                    ;  ((20000000/9600)/64)-1 = 32
;    movlw     d'207'    ;  1200 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'103'          ;  2400 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'25'           ;  9600 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'12'           ; 19200 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'255'          ;  1200 baud @ 20 Mhz Fosc +1.73 err
;    movlw     d'129'          ;  2400 baud @ 20 Mhz Fosc +0.16 err
;    movlw     d'32'          ;  9600 baud @ 20 Mhz Fosc -1.36 err
;    movlw     d'15'          ; 19200 baud @ 20 Mhz Fosc +1.73 err

                    ;  Baud Values with BRGH = 1
                    ;  ((20000000/9600)/16)-1 = 32
     movlw     d'129'         ;  9600 baud @ 20 Mhz Fosc +0.16 err
     movwf     SPBRG
     movlw     b'00100100'    ; brgh = 1
     movwf     TXSTA           ; enable Async Transmission, set brgh
     banksel RCSTA       ; bank0
     movlw     b'10010000'
     movwf     RCSTA          ; enable Async Reception
     movf RCREG,w
     movf RCREG,w
     movf RCREG,w        ; flush receive buffer
;
;
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
     clrf temp
Settle
     decfsz  temp
     goto Settle

LWaitCom
     call      RecLoop         ; wait and read from RS232
     movwf     PORTB          ; show value for diagnostic purpose
TxLoop
     nop
     btfss     PIR1,TXIF ;xmit buffer empty?
     goto TxLoop         ;no, wait

     movwf     TXREG

     goto LWaitCom  ; no
;
; ----------------------------
; RECEIVE CHARACTER FROM RS232
; ----------------------------
; This routine does not return until a character is received.

RecLoop
     nop
     btfss     PIR1,RCIF ; check for received data
     goto RecLoop
     movf RCREG,w
     return
     end

anyone have any idea what i may be doing wrong?
 
Also, the MAX232 has 2 TTL-to-RS232 converters and 2 RS232-to-TTL converters. Which ones am i supposed to use from the PIC to PC and PC to PIC?

Hi,
The MAX232...
At the PIC end the PIC drives the TTL TX input of the MAX and receives data on the TTT RX output of the MAX.

Do you follow that.?

I'll look thru your program.
 
hi,
Your program runs OK in the Oshonsoft Simulator, ref pixs.:)

The test string was "0123456789", 'echo' displayed in the Uart window, also the last character "9", 0x39 in PORTB.

It suggests a problem in the hardware and/or connections.
 
Last edited:
weird.. I am using the same connections you used and still the PIC is unable to receive anything. I may have to end up scrapping the hardware USART in favor of software. Any ideas what the problem could be? I tried receiving from the PC straight through the COM port without the USB module and still cannot receive anything. I checked the baud rate, no parity bit, no hardware control. Hmmm
 
notwist said:
weird.. I am using the same connections you used and still the PIC is unable to receive anything. I may have to end up scrapping the hardware USART in favor of software. Any ideas what the problem could be? I tried receiving from the PC straight through the COM port without the USB module and still cannot receive anything. I checked the baud rate, no parity bit, no hardware control. Hmmm
hi,
What you could try is do, is to link pins 2 and 3 on a spare 'D' 9 way connector and plug it into the PC's serial port.

From the web there a number of free RS232/serial test programs.

Using this type of program with the linked 2 and 3 pins, the PC should be able to talk back to itself.

Lets know what you find.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top