using interrupts on at80c51ed2

Help
2009-03-21
2013-03-12
  • Curtis Been
    Curtis Been
    2009-03-21

    Hello Everyone<
        I have a question about using interuppts. I have written the following code and can't seem to allter the time of the counter. I can set it to 0x00 TH0 and 0x00 TL0 or write 0xFF into both but the time doesn'y get longer or shorter. Please review my code and tell what I'm dont wrong.
          Thanks in Advance

    It was comiled using

    /home/project
    $ sdcc -mmcs51 --out-fmt-ihx --stack-auto --int-long-reent test12.c

    packihx test12.ihx>test.hex

    --There are no error messages generated by the compiler.

    The source code:

    /*-------------------------------------------------------------------------
    Date:           3/13/2009
    Project:        Multi-State timed system
    I/O:            This system will use 3 multicolor led

    Processor:      Atmel at89c51ed2 to be compiled on SDCC
    ---------------------------------------------------------------------------
    #include <at89c51ed2.h>
    /* Global Declarations */

    #define Led1 P1_7
    #define Led2 P1_6
    #define Led3 P1_5

    #define Led1 P1_7
    #define Led2 P1_6
    #define Led3 P1_5
    #define Switch P1_3

    #define ON (bit) 1
    #define OFF (bit) 0

    #define SwitchNotPressed (bit) 1
    #define SwitchPressed (bit) 0

    #define Stop_Duration 10

    #define Caution_Duration 10

    #define Go_Duration 10

    #define Crash_Duration 10

    #define Freeze_Duration 210

    /* Prototypes */
    void Led_Init(const eLight_state);
    bit Switch_Inp(void);
    void Led_Update(void);
    void Timer_0(void);
    void it_timer0(void) __interrupt (1);
    /* State Indicators and definitions */
    static TimeoutLoop, StateNow;
    volatile unsigned char Time_in_state;

    typedef enum eLight_state {Stop, Caution, Go, Crash, Freeze};

    void main(void)
    {
        Timer_0();
        Led_Init(Stop);
        while(1);
    }

    void Led_Init(const eLight_state)
    {
        StateNow = eLight_state;
    }

    bit Switch_Inp(void)
    {

    bit ReturnValue = SwitchNotPressed;

        if(Switch == SwitchPressed)
        {
            Timer_0();
            if(Switch == SwitchPressed)
            {

           while(Switch == SwitchPressed)

                if(Switch == SwitchPressed)
                {
                    ReturnValue=SwitchPressed;
                }
                else
                {
                    ReturnValue= SwitchNotPressed;
                }
            }
        } return ReturnValue;
    }

    void Led_Update(void)
    {
        switch(StateNow)
        {
        case Stop:

            {
            Led1 = ON;
            Led2 = OFF;
            Led3 = OFF;

            if(Time_in_state == Stop_Duration)
                {
                    StateNow = Caution;
                    Time_in_state = 0;
                   StateNow = Caution;
                   Time_in_state = 0;
               }
           break;
           }
       case Caution:

           {
           Led1 = OFF;
           Led2 = ON;
           Led3 = OFF;

           if(Time_in_state == Caution_Duration)
               {
                   StateNow = Go;
                   Time_in_state = 0;
               }
           break;
           }
       case Go:

           {
           Led1 = ON;
           Led2 = OFF;
           Led3 = OFF;

           if(Time_in_state == Go_Duration)
               {
                   StateNow = Crash;
                   Time_in_state = 0;
               }
           break;
           }
       case Crash:

           {
           Led1 = OFF;
           Led2 = ON;
           Led3 = ON;

           if(Time_in_state == Crash_Duration)
               {
                   StateNow = Freeze;
                   Time_in_state = 0;
               }
           break;
           }

       case Freeze:

           {
           Led1 = ON;
           Led2 = OFF;
           Led3 = ON;
            Led2 = OFF;
            Led3 = ON;

    if((Time_in_state == Freeze_Duration)||(Switch_Inp()==SwitchPressed))
                {
                    StateNow = Stop;
                    Time_in_state = 0;
                }

            break;
            }
        }

    }

    void Timer_0(void)
    {
            TMOD &= 0xF0;              /* Timer 0 mode 1 with software gate */
            TMOD |= 0x01;              /* GATE0=0; C/T0#=0; M10=0; M00=1; */
            TH0 = 0xff;                /* init values */
            TL0 = 0x00;
            ET0=1;                     /* enable timer0 interrupt */
            EA=1;                      /* enable interrupts */
            TR0=1;                     /* timer0 run */
            while(TF0 != 0);
    }

    void it_timer0(void) interrupt 1  /* interrupt address is 0x000b */
    {
            Led_Update();
            TF0 = 0;
            ++Time_in_state;
    }

     
    • John Connell
      John Connell
      2009-03-23

      You left off the "*/" at the end of your heading. 

       
    • Curtis Been
      Curtis Been
      2009-03-23

      Actually, I just copied it down wrong. Excuse me, I find that my email riddled with typos. The above program has a few copy typos 1)(this one I didn't notice) the */ is missing (but not on the actual c program) 2) You will notice that the define statements (first 3) are repeated (but not in the program). I apologize (didn't realise untill after the program was uploaded. I am so confused at this point, not to mention I'm a C beginner. Please bear with me guys and girls.

       
    • aozima
      aozima
      2009-03-24

      You code is very difficult to recognize
      I have no time test you code !

      so! I write a test code like:

      /************* main.c ********************/
      #include <at89c51ed2.h>

      unsigned char test = 0xAA;

      void Timer_0(void)
      {
      TMOD &= 0xF0; /* Timer 0 mode 1 with software gate */
      TMOD |= 0x01; /* GATE0=0; C/T0#=0; M10=0; M00=1; */
      TH0 = 0xff; /* init values */
      TL0 = 0x00;
      ET0=1; /* enable timer0 interrupt */
      EA=1; /* enable interrupts */
      TR0=1; /* timer0 run */
      while(TF0 != 0);
      }

      void it_timer0(void) interrupt 1 /* interrupt address is 0x000b */
      {
        test = ~test;
      }

      void main(void)
      {
         Timer_0();
         while(1)
         {
            P1 = test;
         }
      }
      /************** end code*******************/

      comiled using :
      sdcc -mmcs51 --out-fmt-ihx --stack-auto --int-long-reent main.c -o main.hex

      test pass on my board !
      please use my code test you board and then add you Function !

      good luck fou you !

       
    • Maarten Brock
      Maarten Brock
      2009-03-26

      Curtis,

      ET0=1; /* enable timer0 interrupt */
      EA=1; /* enable interrupts */
      TR0=1; /* timer0 run */
      while(TF0 != 0);

      This code waits for TF0 to become 1 but it will never see it because the enabled interrupt switches it off before this code can see it. Remove the while-line or enable the interrupts after it.

      Maarten

       
      • Curtis Been
        Curtis Been
        2009-03-26

        Thank you guys I really appreciate the help, now this prompts another question. Do I need to use an interrupt in order to increment the time in state or can I do it without interrupts? If so how? If not, why not.
        Excuse me if this C question versus a Sdcc specific question.