# problem with TI flag in 89c5x microcontrollers

Status
Not open for further replies.

#### faraj

##### Member level 1
hi everybody.
I'm writing a program in C language for 89c52. it's collects data from an isa ported device and sends the data with rs232 to the PC. and beside this, the uc recieves commands through rs232 and must respond to this commands immediately. I use serial interrupt to get data from PC. As known, the serial interrupt vector is common for RI & TI flags, and for sending data from uc to pc, TI falg needs to be set and in this case uc always goes to serial interrupt vector and this makes uc to work very slowly. so i decided to keep the TI flag cleared and just set it when i want to send data to PC. But this time, the data are missing or completely lost in the PC side. and another thing is that the TI flag gets set without sending any data to PC that seems wrong to me, because the TI flag just set when a data transfer from uc has finished. can anyone help me with this communication problem? thanks in advance.

#### Medtronic

##### Full Member level 5
Hi my friend

you can control TI flag by forcing (Set) it but it is not recomended ...
TI flag will set by hardware if your code i.e
mov sbuf,a
that will happen after the data is tranmitted (the last bit out)

Regards
MedTronic

### faraj

Points: 2

#### faraj

##### Member level 1
Medtronic said:
Hi my friend

you can control TI flag by forcing (Set) it but it is not recomended ...
TI flag will set by hardware if your code i.e
mov sbuf,a
that will happen after the data is tranmitted (the last bit out)

Regards
MedTronic
yes that is write that the TI flag is set by hardware, but in my code is set without sending any data and that makes me confused. is there another source that sets the TI flag? i don't know another source to set the TI flag. and even when i simulate the code in keil the TI flag gets set after a while eithout transmitting any data through serial buffer.

#### laktronics

##### Banned
Hi,
Have you taken care of the extra TX interrupt generated at the end of sending the character, since you are initiating transmission with a forced TI bit set? I suppose you clear the intrrupt flag at the entry of interrupt routine. There is no other source to set TI interrupt flag, somewhere you are not clearing it.
Regards,
Laktronics

### faraj

Points: 2

#### faraj

##### Member level 1
laktronics said:
Hi,
Have you taken care of the extra TX interrupt generated at the end of sending the character, since you are initiating transmission with a forced TI bit set? I suppose you clear the intrrupt flag at the entry of interrupt routine. There is no other source to set TI interrupt flag, somewhere you are not clearing it.
Regards,
Laktronics
I use "printf ();" in my programe and it is automaticlly clears the TI flag, but after a while the TI flag gets set without sending any character through serial. I was wonder, is there another source that makes TI set except the serial and you say no. this is more confusing me. if you want i can put my code here for your test.

and I must add that the TI flag gets set every 2.523 seconds no matter i do send anything through serial (printf) or not.

#### vietdung79

##### Junior Member level 3
What compiler do you use?

If you use Keil, you should re-write the putchar() function.

### faraj

Points: 2

#### laktronics

##### Banned
Hi,
While I am not sure as to why you are observing this problem, please clarify why you are setting the TI flag at all, if you initiate a transfer with printf?, and does printf really clear TI bit set by sending the last byte from the buffer, assuming you have a buffer. Your magic number of 2.523sec is really confusing, is it send buffer length x Tbyte@(4800baud) by any chance?
Regards,
Laktronics

#### faraj

##### Member level 1
vietdung79 said:
What compiler do you use?

If you use Keil, you should re-write the putchar() function.
yes i use keil, and why should i er-write the putchar function and what changes is needed?

laktronics said:
Hi,
While I am not sure as to why you are observing this problem, please clarify why you are setting the TI flag at all, if you initiate a transfer with printf?, and does printf really clear TI bit set by sending the last byte from the buffer, assuming you have a buffer. Your magic number of 2.523sec is really confusing, is it send buffer length x Tbyte@(4800baud) by any chance?
Regards,
Laktronics
i just set the TI flag at the beginning of the program, because it is needed for sending the first character. but after that the printf() instruction automatically send characters and clears the TI flag. and my in my code until here everything is normal but sometime and somewhere the TI flag gets set and this is the confusing point. and i must add that I use 9600 bud.
now as vietdung79 says maybe there is something wrong with putchar() function.
thanks to all of you. now i should go and check the function to find the problem.

### faraj

Points: 2

#### vietdung79

##### Junior Member level 3
The printf() function use the putchar() function to interface with peripheral device. You can find the putchar() function in somewhere in the Keil folder (Maybe the Lib folder). Choose version which is suitable with you and copy it to your code. Now you have to set TI flag only one time in your program.
BTW maybe you can post your code here and I may correct it for you.
Regards.

### faraj

Points: 2

#### faraj

##### Member level 1
wek said:
NEVER MIX interrupt-based reception with "main-based" transmission (and vice versa) in '51!

JW
yes, that topic in the link is exactly my problem.

vietdung79 said:
The printf() function use the putchar() function to interface with peripheral device. You can find the putchar() function in somewhere in the Keil folder (Maybe the Lib folder). Choose version which is suitable with you and copy it to your code. Now you have to set TI flag only one time in your program.
BTW maybe you can post your code here and I may correct it for you.
Regards.
ok, i put my code here consisting of a C file and a H file. I don't know how to thank you, beacause this problem was confusing me for 2 weeks.

#### aupa

##### Member level 3
please, can you post the source code?
thx

#### faraj

##### Member level 1
aupa said:
please, can you post the source code?
thx

#### Help

Hi faraj,

Which MCU do you use it? AT89c52? AT89C52 don't have external ram. So, you have to use internal ram.

Good Luck..

#### faraj

##### Member level 1
Help said:
Hi faraj,

Which MCU do you use it? AT89c52? AT89C52 don't have external ram. So, you have to use internal ram.

Good Luck..
hi. yes i have used AT89c52. but i don't have any problem with ram. the pointer used for reading external data in my code is not for reading external ram. it is for reading isa ported device. thanks for your interest.

#### vietdung79

##### Junior Member level 3
Maybe a error in the interupt function.

Code:
if (TI) TI=0;

in this.

and I think you don't have to add the #ifndef in the init function if you don't need it.

Regards.

#### faraj

##### Member level 1
vietdung79 said:
Maybe a error in the interupt function.

Code:
if (TI) TI=0;

in this.

and I think you don't have to add the #ifndef in the init function if you don't need it.

Regards.
I tried this. in the interrupt routine i placed

Code:
if (RI)
{...}
else
TI = 0;
but in this case I have to set TI before each printf function manually and that cause the loss of transmitted data to the PC.

#### vietdung79

##### Junior Member level 3
Did you try this yet?

I think you are wrong fundamentally in your theory. In sending data, TI is set by hardware and need clear by software for next time when finish sending data. So you have to clear TI in the init function.

Regards.

### faraj

Points: 2

#### faraj

##### Member level 1
vietdung79 said:
Did you try this yet?

I think you are wrong fundamentally in your theory. In sending data, TI is set by hardware and need clear by software for next time when finish sending data. So you have to clear TI in the init function.

Regards.
yes I have tried this and it is not sending data. I have read from Mazidi's 8051 book that you must set the TI flag for sending the first character. and that is right. when you start with cleared TI flag sending data could not begin.
but I think about your suggession on rewriting the putchar() function. may be it could solve my problem, but I am not so proffessinal to do this.

#### vietdung79

##### Junior Member level 3
So if you clear ES (don't use UART interrupt) could you send data?

Points: 2