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.

18f452 usart (rs232) and pic

Status
Not open for further replies.

BOBKA

New Member
Hi,

I am just trying to transmit something from the pic to the rs232 port of the computer. Although I am using null modem cable, instead of the normal female/female, I did re-reouted my pins. The problem is that on the PC side, I am getting some garbage.

Here is the code of my program:

Code:
#include "p18f452.inc"
#define BAUD d'9600'
#define XTAL d'40'
#define X ((XTAL*d'1000000')/(d'64'*BAUD))-1
#define DATA_OUT	0x30    ; corresponds to 0 in ascii
			
             org		h'0'
             goto	MAIN

             org 	h'8'
             btfss 	PIR1, TXIF	; TX buffer is full?
             retfie			; return from interrupt
TX_INT1  movf	DATA_OUT, w	
             movwf 	TXREG		; put the data into the TX buf reg
             retfie
			
MAIN
	movlw 	X		; X is the calculated constant
	movwf	SPBRG		; set baud rate generator appropriately

	bsf	RCSTA, SPEN	; enable the serial port
	bcf	TRISC, 6		; set pin 6 of port c as TX
	bsf	TRISC, 7		; set pin 7 of port c as RX

	bcf	TXSTA, SYNC	; set the mode to asyhncronous
	bsf 	TXSTA, TXEN	; enable the TX

	; we want to enable to interrupts associated with TX and RX, so
	bsf	PIE1, TXIE	; enable the TX interrupts
	bsf	INTCON, GIE	; enable all unmasked interrupts
	bsf	INTCON, PEIE	; enable all unmasked peripheral ints

	movf	DATA_OUT
	movwf	TXREG

INF_LOOP	goto INF_LOOP

END

I thought that I will be getting 0s in the tera term terminal on the PC side, but instead of that I am getting some wierd symbols. Any advice on how to fix this issue?
Also, st232 (pin-to-pin analog to max232) was used w/ capacitors of 10uF attached as in Nigel's tutorial. I didn't use any of the 47 / 22 uF caps, instead I just used 10uF everywhere. The voltage on pin 2 of rs232 was observed to be 9.5 volts (using voltmeter).

Also, I am using external 40MHz oscillator, is it correct to drive 18f452 w/ such an oscillator, because in the spec microchip used 20 MHz max as an example.
 
Alright, I fixed my error. I was accessing some garbage information at location DATA_OUT, instead a literal :) All fixed now ... finally I will be able to use serial port. Didn't think it would be that easy :)
 
Also, I am using external 40MHz oscillator, is it correct to drive 18f452 w/ such an oscillator, because in the spec microchip used 20 MHz max as an example.
Only 10MHz is needed. You can multiply to 40MHz by setting config fuses.

PS when using the USART set both the TX & RX pins to inputs.
 
When I set the TX pin to input, nothing changed :) According to the datasheed it should be set to Output though.

Anyway, now I am encountering another problem:

I rewrote a program a little bit such that, when it starts, it sends '0' to rs232, and then goes into infinite loop and sends '3' from this infinite loop. The problem is that I don't see '0' and then constant 3's, but I see 0's and 3's are mixed.
Sample output at BAUD=4800 is:
"033333333333333333333303333333333330303333330333033330333333333333333333333303333333333303333333333333333333303033033333333333333333333033333333333333333333333333333333333303333333333033333333033333333333303333333333333333333033033333333333333033333333333333333333333333333333333333033333333333333333333333333333333303333333333330333333333333033333333333333333333333333033330333333333333333333333333333333333333333333333333333333033330333333333333333333333333333333333"

Provided this output, I assume that the pic restarts itself. I checked the config bits in MPLAB, and all the timers (and everything that has disable option) is disabled.

What can be a cause for such a problem?
 
It seems that I solved this problem now.

It had someting to do with the power rails of the icd2.

Originally I power my pic from 4x1.2V rechargable AAA battaries. Frankly, instead of the promissed 4*1.2=4.8V I am getting 5.42 on the voltemeter. Since this is abit high for the pic, I decided to put a diode that will reduce it to around 4.8V.
Initially with this configuration I also had my ICD2 power rails (which are 5V)connected to pic. Using the above mentioned configuration, I had my pic restarted every now and then, thus I was getting errors.

Now, when I disconnected power rails from ICD2, everything works fine.

Note: I always had the "Power target ckt from MPLAB ICD2 (5V Vdd)" unchecked.

Thank You :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top