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.

PIC16F877A and UART

Status
Not open for further replies.

notwist

New Member
I am writing a test program that will continuously transmit a byte of data using UART through pin RC6/TX only if RB0 is high. I won't have any access to any test equipment for a couple days so I was wondering, is it possible to simulate this in MPLAB/MPSIM?

Also here is the code, though I haven't tested it in hardware yet.
Code:
databyte	RES	1
count1		RES	1
counta		RES	1
countb		RES	1

main
start
	call	Init
	call	InitSer
	movlw	b'10010110'
	movwf	databyte
loop1
	btfss	PORTB,	0
	goto	loop1
	
	bsf	STATUS,	RP0
	bsf	TXSTA,	TXEN	;enable Transmit
	bcf	STATUS,	RP0
	movf	databyte,	W
	movwf	TXREG			;put data in register TXREG
	
	call	Delay255
	call	Delay255
	call	Delay255
	call	Delay255
	goto	loop1
	
;subroutines
Init
	clrf	PORTB			
	clrf	PORTC
	bsf		STATUS,	RP0
	movlw	0x80
	movwf	TRISC			;RC7 - input,	RC6 - output
	movlw	0xFF
	movwf	TRISB			;PORTB - inputs
	bcf		STATUS,	RP0
	return

InitSer
	bsf		RCSTA,	7		;enable RC6, RC7 for UART
	bsf		STATUS,	RP0
	movlw	0xC0
	movwf	TRISC			;enable RC6, RC7 for UART
	movlw	d'25'
	movwf	SPBRG			;2.4KBAUD, 0.17% ERROR
	bcf		TXSTA,	BRGH	;Asynchronous mode: low speed
	bcf		TXSTA,	SYNC	;USART mode: asynchronous 
	bcf		TXSTA,	TX9		;eight bit transmission
	bcf		STATUS,	RP0		
	
	return

Delay255
....
....
.... etc.

	END                       ; directive 'end of program'
 
Last edited:
connecting PIC16F877A to PC thru UART

I need some help trying to get the UART to work on the PIC16F877A. I don't know how to connect the PIC to the COM port of my computer. Right now, I have RC6 and RC7 as the serial input and output. Can anyone tell me if this is correct? How do I connect it to my PC?

Code:
databyte	RES	1
count1		RES	1
counta		RES	1
countb		RES	1

main
start
	call	Init
	call	InitSer
	movlw	b'10010110'
	movwf	databyte
loop1
	btfss	PORTB,	0
	goto	loop1
	
	bsf	STATUS,	RP0
	bsf	TXSTA,	TXEN	;enable Transmit
	bcf	STATUS,	RP0
	movf	databyte,	W
	movwf	TXREG			;put data in register TXREG
	
	call	Delay255
	call	Delay255
	call	Delay255
	call	Delay255
	goto	loop1
	
;subroutines
Init
	clrf	PORTB			
	clrf	PORTC
	bsf		STATUS,	RP0
	movlw	0x80
	movwf	TRISC			;RC7 - input,	RC6 - output
	movlw	0xFF
	movwf	TRISB			;PORTB - inputs
	bcf		STATUS,	RP0
	return

InitSer
	bsf		RCSTA,	7		;enable RC6, RC7 for UART
	bsf		STATUS,	RP0
	movlw	0xC0
	movwf	TRISC			;enable RC6, RC7 for UART
	movlw	d'25'
	movwf	SPBRG			;2.4KBAUD, 0.17% ERROR
	bcf		TXSTA,	BRGH	;Asynchronous mode: low speed
	bcf		TXSTA,	SYNC	;USART mode: asynchronous 
	bcf		TXSTA,	TX9		;eight bit transmission
	bcf		STATUS,	RP0		
	
	return

Delay255
....
....
.... etc.

	END                       ; directive 'end of program'
 
I need some help trying to get the UART to work on the PIC16F877A. I don't know how to connect the PIC to the COM port of my computer. Right now, I have RC6 and RC7 as the serial input and output. Can anyone tell me if this is correct? How do I connect it to my PC?

You must have a line driver/receiver ic, MAX232 or equivalent, connected to the RX/TX pins of the PIC.

Is your PC serial port a 25 or 9 pin 'D' connector.?
 
ericgibbs said:
You must have a line driver/receiver ic, MAX232 or equivalent, connected to the RX/TX pins of the PIC.

Is your PC serial port a 25 or 9 pin 'D' connector.?
9 pin D connector.

is it possible to so it without the aid of this extra chip?
 
notwist said:
9 pin D connector.

is it possible to so it without the aid of this extra chip?

hi,
Do you want to TX and RX from the PIC to the PC.

There are transistor circuits that work over a short distances, say upto 5 mtrs or so.

EDIT:
This is a version I use, its the section inside the box, the remainder of the circuit is one I did for a friend.

The RX at the PIC can be simple transistor, do you want the circuit for that.?
 
Last edited:
hi,
Added the RX circuit.
 
Last edited:
notwist said:
is it possible to so it without the aid of this extra chip?

You can do it with just a single resistor (as the BASIC STAMP does), but you can't use the hardware USART, you need to use software RS232 routines.
 
Nigel Goodwin said:
You can do it with just a single resistor (as the BASIC STAMP does), but you can't use the hardware USART, you need to use software RS232 routines.

When you say software RS232 routines, you mean the code in your tutorial? Also, If I use your code how do I modify the transfer rate (BAUD)?
 
Last edited:
notwist said:
When you say software RS232 routines, you mean the code in your tutorial? Also, If I use your code how do I modify the transfer rate (BAUD)?

Yes, code similar to my tutorial (apart from the last one which uses the USART), but you would need to invert the operation of the routines to avoid using an RS232.

To modify the speed you need to modify the delay loops.

How fast are you wanting to use?.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top