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
  • 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.

Reply
  • 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.

Children
No data