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.

RS-232 with 16F877

Status
Not open for further replies.

MikeyG79

New Member
Starting out a new project and just trying to write some bits for my computer to read (just using cat /dev/ttyS1)- here is part of what I have so far, not sure why it doesn't work

Main:
call Print_Message
movlw 90H
movf TXREG
call Delay1sec
goto Main

Print_Message:
call LCD_Init
call Delay10ms

movlw MSG1
;movwf TXREG
call LCD_PrintString
call LCD_NewLine
movlw MSG2
call LCD_PrintString

call Delay1sec

call LCD_Init
call Delay10ms
movlw MSG3
call LCD_PrintString
call LCD_NewLine
;call Voltage
movlw MSG4
call LCD_PrintString
call Delay1sec
return
 
Do you setup the uart somewhere else in your code?. The pic's uart requires some setup code before you can start copying things to TXREG
Secondly, you should check bit TXIF of register PIR1 to see you are allowed to write new data to TXREG. This flag indicates the usart is ready for transmission.
 
Ooops, guess I missed copying some code-

LIST p=16F877
#include "p16f877.inc"
errorlevel 1, -302 ;Suppress bank 0 messages

; Memory Declarations
cblock 0x20 ; Start of general purpose memory
endc

ORG 0x000
Start:
goto Init ; Execute code

ORG 0x004
Interrupt:
goto Init ; Ignore interrupts

Init:
BANKSEL TRISA
movlw b'11111111'
movwf TRISA

BANKSEL TRISB
movlw b'11111111' ; All inputs including user buttons
movwf TRISB

BANKSEL TRISC
movlw b'11110000' ; LED lines as outputs
movwf TRISC

BANKSEL TRISD
movlw b'00000000' ; LCD lines all are outputs
movwf TRISD

BANKSEL OPTION_REG
movlw b'00000111' ; 0=PortB weak<7>,(Intedg<6>),(T0CS<5>),(T0SE<4>),(PSA<3>)256<210>
movwf OPTION_REG

;;;; Initialize UART
BANKSEL SPBRG
; Set up baud rate (9600)
movlw .129
movwf SPBRG
; Set up serial port
movlw 0x24
movwf TXSTA
BANKSEL RCSTA
; Enable port
movlw 0x90
movwf RCSTA

BANKSEL PORTA ; Normal bank again
 
Can't see anything wrong with the setup code at first glance

But you'll need to check TXIF before transmitting

Code:
Transmits W over the serial port

SerialTransmit
		
	   BANKSEL TXREG
		BTFSS	PIR1, TXIF		;Buffer empty?
		GOTO 	$-1				;no? then wait until it is
		MOVWF	TXREG			;Transmit W
		RETURN
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top