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);
                }
        }

  • I see you are a member of the "I hate software comments" club. These members are also often members of the "My code doesn't work" or "Help me with my code" clubs.

  • Thank you for the reply.

    No one knows everything by there own. They get the knowledge from the book or from others.

    I learned something from this forum and i thank who guided me.

    am i done any mistake for asking what mistake i done in my code?

    still your comment is not helpful but thank you for 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.

  • Most of the time, when people help other people on forums, they do it by comparing the code "what is happening" with the comments "what the programmer wanted to happen".

    To some part, code can be self-documenting. But only to some part. Good naming of variables, data types and functions can tell what the code does - or intends to do. But only comments will tell why the programmer wants to do it. And the "why" is one of the most important things to always focus on when coding.

    That is a big reason why many forum readers skips posts where the code doesn't contain any comments, or if the poster have forgotten the proper tags around the code making original indenting and line breaks to be lost.

    As already noted, your ISR should be as quick as possible to do what needs to be done and then exit. This reduces problems with large percentages of the CPU capacity being stolen from the main loop. And it reduces problems when multiple devices have interrupts that needs to be serviced. And it reduces problems where the same device may actually need servicing multiple times but only have one interrupt flag available.

  • Sorry for not commenting in my code. and thank you for reply's.

    And understands the mistakes i done in the coding.

    Regards
    arun raj

  • still your comment is not helpful
    comments serve two purposes
    a) they make YOU better see your code
    b) they make people willing to help you

    Erik