This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem in using the timer 0 for delay in serial interrupt

The code is below, it run perfectly when i run it step by step during debugging. But when i
run it continuously it stuck somewhere after executing the ISR.

Is there any mistake i done?

please give suggestion if i done any.

Thank you.

#include <REGX51.H>

sbit motof = P2^0;
sbit motob = P2^1;


void timer2()
        {
        TH0 = 0xff;
        TL0 = 0xfd;

        TR0 = 1;

        while(TF0 == 0);
        TR0 = 0;
        TF0 = 0;
        }



void msdelay(unsigned char ms)
        {
        int i;
        for(i=0;i<=ms;i++)
        timer2();
        }

void ser() interrupt 4
        {

        char j;
        while(RI == 0);
        j = SBUF;
        RI = 0;

        if(j=='f')
                {
                motof = 0;

                motob = 0;
                msdelay(10);
                motob = 1;
                msdelay(10);
                }
        if(j=='r')
                {
                motob = 0;

                motof = 1;
                msdelay(10);
                motof = 0;
                msdelay(10);
                }

        }


void main()
        {

        SCON = 0x50;
        TMOD = 0x21;
        TH1 = 0xFD;
        TR1 = 1;
        IE = 0x90;
        while(1)
                {
                motob = 0;
                motof = 0;
                msdelay(10);

                motof = 1;
                msdelay(10);
                }
        }

Parents
  • The code is below, it run perfectly when i run it step by step during debugging.

    That doesn't mean anything, because your code doesn't contain any hints what "run perfectly" might be supposed to mean. Those hints would usually be in the comments.

    But when i run it continuously it stuck somewhere after executing the ISR.

    I rather doubt the correctness of that assessment. If you really knew it's stuck, you would also know where, and how.

    Is there any mistake i done?

    Plenty. For starters, you're doing not just too much, but also entirely the wrong kind of things in your ISRs. Delay loops do not belong into ISRs.

Reply
  • The code is below, it run perfectly when i run it step by step during debugging.

    That doesn't mean anything, because your code doesn't contain any hints what "run perfectly" might be supposed to mean. Those hints would usually be in the comments.

    But when i run it continuously it stuck somewhere after executing the ISR.

    I rather doubt the correctness of that assessment. If you really knew it's stuck, you would also know where, and how.

    Is there any mistake i done?

    Plenty. For starters, you're doing not just too much, but also entirely the wrong kind of things in your ISRs. Delay loops do not belong into ISRs.

Children