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.

soft RS232 code

Status
Not open for further replies.
I am running a PIC18F2620 at 115,200 and talking to a SIM20 rf module, with Fosc = 14.7456MHz.

I need to send 4-bytes of data to another device at 9,600, but without using the existing USART.

My thoughts were to use say RA0+RA1 as the RXD2 and TXD2 pins but I am battling getting the ASM code that I found somewhere on the net (for PIC16F84) to work.

I don't know whether it is my delay routines or something inherently wrong with the code.

Has anyone got any archived snippets of a soft UART, or point me in the right direction.?
 
Hello, I haven't had the pleasure to work with the 18f series yet, but Nigels tutorials use a bit banged rs232 on a 16f628a.

Porting the code over should be somewhat simple right? I was successful porting some of the other tutorials over to a 16f88.

I'm aware that the 18f chips have additional instructions and perhaps some that are different, but should be fairly simple once you have the concepts down. Post the code that you have so far and it will be easier for us to help.

Edit: Check out tutorial twelve in the link I posted above. RS232 is generally not compatible with the rf modules.
 
Last edited:
Soft Rs232

Sig239 - many thanks for your prompt reply and for your input.

I am looking at Nigels's RS232 tut now in module 7 (not 12).

OOI - my 115,200 with the SIMCOM SIM20 rf module is working 100% with 14.7456MHz. I just need to talk to another PIC and not have to use the same USART as I am waiting for RXD over the rf channel.

I think I have got my head around soft RS232 as one seems to have to wait for the start bit (LOW), wait 52uS to get to the centre of that bit, and then sample the port every 104uS (9,600 baud) and set/clear the data bit accordingly.

I will contnue .....
 
hi Graham
Post the program code for the 16F84 that you are trying to convert.
Use the '#' symbol for CODE, shown on the 'Advanced' reply Menu, to enclose the code in order to keep the formatting.
 
Soft RS232

Eric - thanks again for a quick reply. Gosh this forum is fast eh !

Code:
;=================================================================
; SOFTWARE USART ROUTINE FOR 9600 BAUD
; tested, using MPLAB SIM, at 03/10/201 to be 3.626mS for all 4-bytes
;=================================================================
RXD_RS232:
	; we neeed to receive 4-bytes at FSR0 = 0x0020 -> 0x0023
	lfsr	0,0x0020
	movlw	.4
	movwf	ByteCounter
ReceiveNextByte:
     	movlw     .8
	movwf	CounterA     	; 8-data bits
ReceiveWait:
	movf     SoftRS232Port,w ; CHECK FOR A START BIT (LOW)
	btfsc     RXD2      	 ; currently PORTA:0
         	bra	$-4	 ;  -//-
         	call      Delay052       ; wait until centre of start bit
; we should be at the centre of the start bit by now
ReceiveBit:
     	bcf       STATUS,C
         	rrcf      RCVREG,F
	movf	SoftRS232Port,w
         	btfsc     RXD2
         	bsf       RCVREG,7
         	call      Delay104       ; ONE PERIOD = 104uS
         	decfsz    CounterA,F
         	bra	ReceiveBit
         	movf      RCVREG,w
	decfsz	ByteCounter,F
	bra	RXD_RS232_Next		
         	return                   ; data now at 0x0020 -> 0x0023
RXD_RS232_Next:
	; save incoming byte in RAM at 0x0020 -> 0x0023
	movff	WREG,POSTINC0
	bra	ReceiveNextByte
				
;====================================================================
; Originally coded for 10MHz, recoded for 14.7456MHz (0.27uS/instruction)
;	DELAY ROUTINES 9600 BAUD HAS:
;	104uSEC PERIOD (1/9600)
;	HALF PERIOD IS 52uSEC (to centre of start bit)
;	1.25 PERIOD IS 130uSEC
;====================================================================
Delay104:	movlw     .94	; was 0x3E = .62
	movwf	DelayCounter    ;ADJUST FOR 9600 HZ
D104:    	NOP
         	DECFSZ    DelayCounter,F
         	GOTO      D104
         	NOP
         	NOP
         	RETURN
         
Delay052:
               movlw     .62	; was 0x27 = .39
	movwf	DelayCounter
D052:	DECFSZ    DelayCounter,F
         	GOTO      D052
         	NOP
         	NOP
         	RETURN
 
Last edited:
hi Graham,
Added that code to a 18F2620 template.
Can you post your 18F2620 program, so that I can check the config etc,,
 
PIC18F2620 code

Eric - The rest works OK but I have attached it for your perusal.
 

Attachments

  • SB001T_LED_DisplayMaster.asm
    47.9 KB · Views: 123
Last edited:
Eric - The rest works OK but I have attached it for your perusal.

hi,
Running the program in the Oshonsoft Sim and driving PORT.0 with a data input, the program never responds and so never calls the SoftRXD subr.
The Sim is running at 14.746mHz and the Soft RS232 input for 9600baud.
 
Eric - your code will not run as it is waiting for an RS232 interrupt from the SIM20 TX before it tries to run the soft RS232 transmission.

After trying the soft code I gave you I think I have given up as, despite checking and double checking the timing, I still get rubbish coming through.

Tomorrow is another day as they say. Perhaps with an opto-isolator to separate the two devices would help.
 
Eric - your code will not run as it is waiting for an RS232 interrupt from the SIM20 TX before it tries to run the soft RS232 transmission.

After trying the soft code I gave you I think I have given up as, despite checking and double checking the timing, I still get rubbish coming through.

Tomorrow is another day as they say. Perhaps with an opto-isolator to separate the two devices would help.

hi,
The closest I could get with the crystal in Oshonsoft is 15mHz, this is a 'asm' listing from a Basic program for RS232 reception only
It may give an idea regarding the timing.

I will re-run your asm file tomorrow and use a Sim TXD interrupt in order to call the RXD subr.
 

Attachments

  • SoftUartTest1.asm
    2.1 KB · Views: 114
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top