Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Program hangs in printf().

C
void UART0_ISR(void) interrupt 4
{
    if (RI == 1)
    {
            RI = 0;
            TI = 1;
            Count = 444;
            datau[temp] = SBUF;             // Read receive data
            SBUF = 0;// datau[temp];           // Send back same data on uart
            Count=1;
            if(temp < 10)
            {
                temp =temp+1;
            }
            if(temp >10)
            {
                temp=0;
            }
            uartreceive=1;
            Count = 444;
        }
    else TI = 0;
}

void main() {

        AUXR1|=0x00;
        PCON&=0x7F;
        SCON  = 0x50;         // SCON: mode 1, 8-bit UART, enable rcvr
        TMOD&=0xAF;
            TMOD |= 0x20;               // TMOD: timer 1, mode 2, 8-bit reload
        TH1   = 0xFA ;                // TH1:  reload value for 1200 baud @ 16MHz
            TR1   = 1;                  // TR1:  timer 1 run
            TI    = 1;                  // TI:   set TI to send first char of UART
        ES = 1;                             // Enable serial interrupt
        EA = 1;
        Count=123;
        temp=0;
    while(1)
    {
        Delay_ms(100);
        Count=102;
        printf("sss\n %u",Count); //My program is getting hangs here

        if(uartreceive==1)
        {
            uartreceive=0;
            Count=4;
            printf("Entire array is %s  %u\n",datau,temp);
            Count=105;
            printf("\nindex  %u %c  %c\n",temp,datau[0],datau[1]);
            Count=106;
            sprintf(datau," ");
            Count=107;
            temp=0;
            uartreceive=0;
            Count=104;
        }

        }
     }


What I have tried:

I am trying to communicate from PC using UART. Its is working but stuck at the printf() function inside the while loop. I don't know what is the mistake I have done.
Posted
Updated 6-Jun-23 20:07pm
v2
Comments
barneyman 20-Jul-16 5:52am    
you're in the middle of an interrupt service routine - you have to get out as quickly as possible,and make no blocking calls

push the data into a pre-allocated buffer, and let another, normal, thread do something with it
S.Soundar 21-Jul-16 1:45am    
I checked that sir. But interrupt is working fine. If I take out the Delay_ms(100) function its working fine. But if I am using delay program hangs on printf().

Here is my delay function code:

void Delay_us(unsigned int us_count)
{
while(us_count!=0)
{
us_count--;

}
}

void Delay_ms(unsigned int ms_count)
{
while(ms_count!=0)
{
Delay_us(550); //delay_us is called to generate 1ms delay
ms_count--;
Count=ms_count;
}

}
barneyman 21-Jul-16 2:52am    
you need some form of read/write locking on that data buffer - currently it has the potential to change while printf is traversing it


[no name] 25-Jul-16 15:26pm    
Does the printf use the UART to communicate to the host PC?
S.Soundar 26-Jul-16 5:12am    
Yes bling.

1 solution

UART interrupt routiing set TI = 0 will cause printf crashed, I use sprintf and below uart_sendstring to replace printf to avoid crash. If didn't set TI = 0 in UART interrupt routing, It will cause the UART interrupt to continually occur at the frequency of the UART baud rate, causing the CPU's computing power to be wasted in handling a large number of UART interrupts.

uint8_t xdata pbf[100];
sprintf(pbf, "\033[2J\033[H");
uart_sendstring(pbf, strlen(pbf));


//---------------------------------------------------------------
void Send_Data_To_UART0 (UINT8 c)
{
TI = 0;
SBUF = c;
while(TI==0);
}


//---------------------------------------------------------------
void uart_sendstring(uint8_t* s, uint8_t cont)
{
while (cont) {
Send_Data_To_UART0(*s++);
cont--;
}
}
 
Share this answer
 
Comments
Rick York 7-Jun-23 4:03am    
Was it actually necessary to resurrect a nearly six-year-old thread?
OriginalGriff 7-Jun-23 4:19am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 7 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900