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.

Alternately Missing Data in Serial Communication

Status
Not open for further replies.

PICMICRO

New Member
I have written a test program for serial communication (PIC16F877A) Everytime a button is pressed the data 'ABCDEFG' is to be sent to the computer.
But, I am recieving only 'ACEG'
Here is the Hi-Tech C code
Code:
#include<htc.h>
__CONFIG(HS & LVPDIS & BORDIS & PWRTEN & WDTDIS);

#define _XTAL_FREQ 20000000

void main()
{
        //	Serial_dir=Out;
		TRISC6=0;
		TRISC7=1;
		SYNC=0;	 // no synch	
		SPBRG=129;	// 9600 baud at 20Mhz
		BRGH=1;	// high baud rate
		SPEN=1; //enable the serial port
		TX9=0;	// no pairity bit
		TXEN=1;	// Start Transmission
		CREN=1; //recieve data too
		TXIE=0;	// no intrpt
       
		TRISB5=0;
		RB5=1; // indicator LED. If it glows, PIC is working :)

		TRISD1=1; // button is input

		while(1)
        {
			if(RD1==1){
				for(char i=0;i<=20;i++)__delay_ms(10);
				while(!TXIF);	//wait until previous transmit ends
				TXREG = 'A';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='B';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='C';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='D';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='E';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='F';
				while(!TXIF);  //wait until previous tansmit ends
				TXREG='G';
			}
			
			
			
		}
}

So after fiddling through the datasheet, I felt, 'Maybe "while(!TXMT)" should work' , and it did.
So, whats the matter going here?

Code that works.
Code:
#include<htc.h>
__CONFIG(HS & LVPDIS & BORDIS & PWRTEN & WDTDIS);

#define _XTAL_FREQ 20000000

void main()
{
        //	Serial_dir=Out;
		TRISC6=0;
		TRISC7=1;
		SYNC=0;	 // no synch	
		SPBRG=129;	// 9600 baud at 20Mhz
		BRGH=1;	// high baud rate
		SPEN=1; //enable the serial port
		TX9=0;	// no pairity bit
		TXEN=1;	// Start Transmission
		CREN=1; //recieve data too
		TXIE=0;	// no intrpt
       
		TRISB5=0;
		RB5=1; // indicator LED. If it glows, PIC is working :)

		TRISD1=1; // button is input

		while(1)
        {
			if(RD1==1){
				for(char i=0;i<=20;i++)__delay_ms(10);
				while(!TRMT);	//wait until previous transmit ends
				TXREG = 'A';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='B';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='C';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='D';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='E';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='F';
				while(!TRMT);  //wait until previous tansmit ends
				TXREG='G';
			}
			
			
			
		}
}
 
TXIF is for checking interrupts, Its high as soon as transmission is initiated, ie.. TSR is loaded... TRMT is low until transmission ends and TSR is empty.
TXIF is set as soon as TXREG is empty. Its reset as soon as TXREG is filled. Thats what the datasheet says, BTW, IMHO.
 
Yep.. but after the TXREG is emptied (transfered to the TSR.. transmit shift register) you cannot load TXREG again until TSR is empty.. The TXIF flag isn't valid here only the TRMT.
 
Last edited:
Can anyone help- UART Commn

I used microC for pgmming 18f4520. the pgm aim is to transmit the same data back to terminal.But what i get is different.
send data:hi dere
received data:ø8øø
Code goes here:

void main() {

unsigned char Temp;
trisb=0x00;
Uart1_Init(9600);
Delay_ms(25);
latb.b1=1;
Delay_ms(50);
UART1_Write_Text("Start");
UART1_Write_Text(" ");
latb.b1=0;
for(; ;)
{
while (!UART1_Data_Ready());
Temp = UART1_Read();
latb.b1 = ~latb.b1;
Delay_ms(50);
UART1_Write(Temp);
latb.b1 = ~latb.b1;
Delay_ms(50);

}
 
Yep.. but after the TXREG is emptied (transfered to the TSR.. transmit shift register) you cannot load TXREG again until TSR is empty.. The TXIF flag isn't valid here only the TRMT.

That's not exactly correct Ian. It's true that TXIF is set after the contents of TXREG have been copied to TSR, or in other words, when TXREG is "empty". At that point you can load TXREG with another character. There's no need to wait for TSR to empty. That said, I'm not sure why the OP is having a problem with TXIF. Perhaps whatever he is connecting to requires more than one stop bit and the little bit of extra delay from using TRMT instead of TXIF is helping in that respect?
 
Last edited:
I support the idea to put some time between data sent and I mean between each load TXREG register. Then try to just have a millisecond to lose.
 
Status
Not open for further replies.

Latest threads

Back
Top