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.

Unexpected USART results

Status
Not open for further replies.

dantonk

New Member
Hello group,

I have an interesting (read frustrating) situation where I am sending some known data via USART from 1 PIC16F887 device to another, but the data received is corrupted. As this is a test set-up I am processing no other instructions. Both devices share the exact same initialization code (below) one is set to TX only and the other RX only. Baud=9600, Internal 4Mhz clk,

An example of the corrupted data is below:
Sending (in hex) AA, 55, AA, 00
Receiving (hex) D6, A9, D6, 80
(Bin) 10101010 .01010101 .10101010 .00000000
(Bin) 11010110 .10101001 .11010110 .10000000

Other data sent is corrupted in a similar way (MSB=1 & Lower nibble damaged). other "new" devices have been tested and I can observe the correct data being sent on the oscilloscope. There is some redundant code with bank switching - please ignore -


any help appreciated,
Dan.


; 1) ENABLE THE TRANSMITTER
BANKSEL TXSTA ; Select Bank 1
BSF TXSTA,TXEN ; enables TX of the EUSART.
BCF TXSTA,SYNC ; configures EUSART for async
BANKSEL RCSTA ; Select Bank 0
BSF RCSTA,SPEN ; auto config TX & RX I/O pins

; 2) ENABLE THE RECEIVER
BANKSEL RCSTA ; Select Bank 1
BSF RCSTA,CREN ; enable the RX of the EUSART

; 3) Set the Baude Rate
BANKSEL SPBRG ; Select Bank1
MOVLW D'25' ; Load Baud register with d25 (19hex)
MOVWF SPBRG ; Register in BANK1
BANKSEL TXSTA ; Bank1 Selected
BSF TXSTA,BRGH ; Set BRGH = H.Speed Baud

BANKSEL PORTA ; Set back to Bank0 for main:
RETURN ; before exiting the set-up routine

**** sample of RX test code ****
BTFSS PIR1,RCIF ; Poll RX flag for character in buffer
GOTO $-1 ; Continue polling for the RX data
MOVF RCREG,W ; Extract char from buffer to WREG
MOVWF CharRX ; Move to variable location

**** sample of RX test code ****
MOVLW H'55'
MOVWF Var1 ; Load data for TX into variable
BTFSS PIR1,TXIF; TXIF=1 [ready to accept data] TXIF=0 [busy]
GOTO $-1 ; Branch always to previous cmd line [poll flag]
MOVF Var1,W ; Move variable into the WREG [Var1=55]
MOVWF TXREG
 
Last edited:
I've not checked your code but I recently had a similar problem with a 16F886. I was using the internal oscillator at 8MHz and getting corrupt data. It turned out that the int osc was running at ~5.3Mhz. Try swapping the two chips and see if the error changes.

Mike.
 
dantonk, the initialization of the ESUART module is correct.
You might run a known delay routine which toggles a pin and check the frequency with your scope. This will tell you whether the PIC is running at 4 MHz or not (BTW that's the default frequency, have you changed the OSCCON settings?).
Then, I would probably write simple programs to test the serial interface between the two PICs: one would transmit only, the other would receive and display data.
 
Update...

Thanks for the input Mike and Eng1.

I was waiting till today to have a look at my output clock signal on a decent scope (slow USB unit at home). When I initialize the _CONFIG to give me _INTRC_OSC_CLKOUT and then scope the RA6/OSC2/CLKOUT pin on the device I get a nice clean 1Mhz signal.

This has left me a little confused because I was expecting to see my OSC clock there (4Mhz) not one that ticks over at the instruction speed (OSC/4). When I look into the data sheet, it says I should get "a clock output" which is not too clear. [IRCF=110 in OSCCON register = 4Mhz]

Should I get a 4Mhz (system clock) at the CLKOUT pin if correctly selected?

Cheers,
Dan.
 
The 0sc/4 output is normal. Looks like the oscillator isn't the problem.

For comparison, here is my code for the UART setup,

Code:
;Setup RS232 for 9600,n,8,1 
		bsf	STATUS,RP0	;#1
		movfw	low .103
		movwf	SPBRG
		movfw	high .103
		movwf	SPBRGH
		movlw	(0<<TX9|1<<TXEN|1<<BRGH)
		movwf	TXSTA
		bsf	STATUS,RP1	;#3
		movlw	(1<<BRG16)
		movwf	BAUDCTL
		clrf	STATUS		;#0
		movlw	(1<<SPEN|0<<RX9|1<<CREN|0<<ADDEN)
		movwf	RCSTA

The most notable difference is that I setup BAUDCTL and SPBRGH.

The equivalent setup with BRG16=0 would be,
Code:
;Setup RS232 for 9600,n,8,1 
		bsf	STATUS,RP0	;#1
		movfw	low .25
		movwf	SPBRG
		movfw	high .25
		movwf	SPBRGH
		movlw	(0<<TX9|1<<TXEN|1<<BRGH)
		movwf	TXSTA
		bsf	STATUS,RP1	;#3
		movlw	(0<<BRG16)
		movwf	BAUDCTL
		clrf	STATUS		;#0
		movlw	(1<<SPEN|0<<RX9|1<<CREN|0<<ADDEN)
		movwf	RCSTA

I can't see why clearing SPBRGH would make any difference if BRG16 is clear, but it may be some silly quirk with the new hardware. The 16 bit baudrate generator is new to the 887.

Mike.
 
Last edited:
Hi dantonk, I've just tried your routines with my PIC16F887 + MAX232. I copied & pasted them into a 'serial echo' program and it works as expected.

In my programs I also configure the BAUDCTL register and clear SPBRGH even if BRG16=0, as suggested by Pommie, just in case...

Since you observed the correct data being sent on the oscilloscope and after trying you routines with my hardware, I suspect that each PIC would do its job separately but for some reasons the communication between them fails.
 
Last edited:
any solution to for uart communication between 16f887

Hello all,
I am trying to communicate between to 16f887 and using 1200 baud rate with BRG16 = 1. But I am getting errors same as you are getting in bits. So can you please tell me is there is any issue with the IC or something else have to be set? I have checked the Oscillator frequency and it is fine.
Please respond.
Thanks
Regards
 
Hi, Mike,
Following is the config macro, I have set on transmitter and receiver side.

Both are using 16f887 and this ICs are mounted on Microchip's touch sense demo board



__CONFIG(INTIO & WDTDIS & PWRTEN & BORDIS & IESODIS & FCMDIS & LVPDIS);


Following is the initialization on transmitter side


IRCF0 = 1; // 8Mhz Oscillator Frequency
SCS = 1;
IOCB1 = 1; // using RB1 as input switch, and I am using change in input flag //in my code
ANS10 = 0;
SPEN = 1;
BRG16 = 1;
SPBRG = 0xA0; // I am using 1200 baud rate . took the value from the //datasheet tables
SPBRGH = 0x01;
TXEN = 1;


Following is the intialization code on RF side

IRCF0 = 1;
SCS = 1;
SPEN = 1;
CREN = 1;
BRG16 = 1;
SPBRG = 0xA0;
SPBRGH = 0x01;


The other bits which are not set is going to be set to their initial values on reset.
My application is like that when I press the switch it transmits one code and in another press it transmits other code.
On receiver side ideally it should detect the code and turn led on or off.
I had connected RF module with this but it did not work so I traced down that it is controller issue then I directly connected TX pin of one 16f887 to RX of another 16f887 and it still did not work then I just connected Receiver side 16f887 to serial port and tried to send the data through terminal application from PC but the data is still not valid.

I am using MPLAB IDE with its additional third party compiler (HI-TECH C compiler) for this

Please provide me your input on this.
Thanks
Regards
 
Last edited:
If you only set IRCF0 to 1 then your oscillator is running at 125kHz. Try changing it to OSCCON=0x70 for 8MHz.

Mike.
 
Hi,
Mike,
Its the same result even if I changed that way. but it should not matter as by default IRCF1 and IRCF2 is initialized to 0 anyway.

Again the data is coming in regular currupted way like suppose if I transmit C8 in hex through serial terminal then I get E8 in RCREG. If I transmit 43 then I get A3. every time first four bits are getting currupted.

Do you see any other errors in intialization.

Thanks
Regards
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top