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

why can't the Loop states and multi-dimensions arrays run?

Hi,
1. In my programme,there are some loop states such as the following:

int x,x1,x2;
x1=1;x2=100;
for(x=x1;x<=x2;x++)
{
y=k*x+b;
Pixel(x, y, Mode);
}

But in fact,the loop is executed only once,then run the wrong code and the code after the loop is never executed.can you give me some advices?
2.In my programme, there is a multi-dimension array like this--

unsigned char code HZTable[2][16]={....};

But the operation to the array always goes wrong.how can I resolve the problem?
thanks

Liu ***
huihl@163.com

Parents
  • (1) the Loop code is following.Because of the loop state,this programme can run well in Keil UV2,but can't run well in my target board.if the loop state is deleted ,the programme can run well in my target board. can you resolve it?

    #include "absacc.h"
    #include "math.h"
    #include <intrins.h>
    #include <reg51.h>
    #include "stdio.h"
    //------------------------------------------
    void Line( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, bit Mode)
    {
    unsigned char x,y;
    double k,b;


    if( abs(y1-y2) <= abs(x1-x2) ) // |k|<=1
    {k=(float)(y2-y1) / (float)(x2-x1) ;
    b=y1-k*x1;
    if( x1 <= x2 )
    {for(x=x1;x<=x2;x++)
    {
    y=k*x+b;
    // Pixel(x, y, Mode);
    }
    }
    else
    {
    for(x=x2;x<=x1;x++)
    { y=k*x+b;
    // Pixel(x, y, Mode);
    }
    }
    }
    else // abs(y1-y2) > abs(x1-x2) |K|>1
    {
    k=(float)(x2-x1) / (float)(y2-y1) ;
    b=x1-k*y1;
    if( y1 <= y2 )
    {for(y=y1;y<=y2;y++)
    { x=k*y+b;
    // Pixel( x , y,Mode );
    }
    }
    else
    {for(y=y2;y<=y1;y++)
    { x=k*y+b;
    // Pixel( x , y,Mode );
    }
    }
    }
    }

    void main()
    {Line(0,0,23,34,1);
    P1=34;
    jj:goto jj;
    }

Reply
  • (1) the Loop code is following.Because of the loop state,this programme can run well in Keil UV2,but can't run well in my target board.if the loop state is deleted ,the programme can run well in my target board. can you resolve it?

    #include "absacc.h"
    #include "math.h"
    #include <intrins.h>
    #include <reg51.h>
    #include "stdio.h"
    //------------------------------------------
    void Line( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, bit Mode)
    {
    unsigned char x,y;
    double k,b;


    if( abs(y1-y2) <= abs(x1-x2) ) // |k|<=1
    {k=(float)(y2-y1) / (float)(x2-x1) ;
    b=y1-k*x1;
    if( x1 <= x2 )
    {for(x=x1;x<=x2;x++)
    {
    y=k*x+b;
    // Pixel(x, y, Mode);
    }
    }
    else
    {
    for(x=x2;x<=x1;x++)
    { y=k*x+b;
    // Pixel(x, y, Mode);
    }
    }
    }
    else // abs(y1-y2) > abs(x1-x2) |K|>1
    {
    k=(float)(x2-x1) / (float)(y2-y1) ;
    b=x1-k*y1;
    if( y1 <= y2 )
    {for(y=y1;y<=y2;y++)
    { x=k*y+b;
    // Pixel( x , y,Mode );
    }
    }
    else
    {for(y=y2;y<=y1;y++)
    { x=k*y+b;
    // Pixel( x , y,Mode );
    }
    }
    }
    }

    void main()
    {Line(0,0,23,34,1);
    P1=34;
    jj:goto jj;
    }

Children
  • I re-wrote your program (so I could understand it) and it seems to work just fine. Maybe my changes fixed a bug or something.

    #include <math.h>
    #include <stdio.h>
    #include <reg51.h>
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    #define min(x,y) (((x)<(y)) ? (x) : (y))
    #define max(x,y) (((x)<(y)) ? (y) : (x))
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Pixel (
      unsigned char x,
      unsigned char y,
      bit mode)
    {
    mode = mode;
    printf ("%3.3u x %3.3u\n", (unsigned) x, (unsigned) y);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_x (
      unsigned char x,
      unsigned char x_end,
      float k,
      float b,
      bit mode)
    {
    for(; x<=x_end; x++)
      {
      Pixel (x, k*x+b, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_y (
      unsigned char y,
      unsigned char y_end,
      float k,
      float b,
      bit mode)
    {
    for(; y<=y_end; y++)
      {
      Pixel (k*y+b, y, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Line (
      unsigned char x1,
      unsigned char y1,
      unsigned char x2,
      unsigned char y2,
      bit Mode)
    {
    double k,b;
    
    if (abs(y1-y2) <= abs(x1-x2))   // |k|<=1
      {
      k = (float)(y2-y1) / (float)(x2-x1);
      b = y1 - (k * x1);
    
      line_x (min(x1,x2), max(x1,x2), k, b, Mode);
      }
    else                            // |K|>1
      {
      k = (float)(x2-x1) / (float)(y2-y1);
      b = x1 - (k * y1);
    
      line_y (min(y1,y2), max(y1,y2), k, b, Mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void main (void)
    {
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;
    
    Line(0,0,23,34,1);
    
    while (1);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/

    Jon

  • To Jon
    yes,the programme adapted by you can run well in Keil UV2 and my target board. But you just slided over the problem. For example,In my rtos code(e.g. UCOS 51),the "For" state is used many times.if eveytime I should adapt the code as you,it must be terriable.
    many projects which includes "for" loop,can run well in Keil UV2,but can't run well in the target board. by the way,the CPU type in the KEIL UV2 is same to my target. This is the key problem.
    My target board drawings can be reached by the following links:
    <a href="" target="_blank">http://www.redrival.com/huiease/target_1.jpg"&gt;http://www.redrival.com/huiease/target_1.jpg&lt;/a&gt;

    <a href="" target="_blank">http://www.redrival.com/huiease/target_2.jpg"&gt;http://www.redrival.com/huiease/target_2.jpg&lt;/a&gt;
    thanks

  • the following code always goes wrong.

    void ShowHZ(unsigned char lin,unsigned char column,unsigned int hzcode)
    {
    unsigned char i;
    unsigned int StartAddr;

    StartAddr=lin*LineChar + column; //&#23450;&#20301;&#36215;&#22987;&#34892;

    for(i=0;i<16;i++)
    {
    OutPortCom3( (unsigned char)(StartAddr), (unsigned char)(StartAddr>>8), 0x24);
    OutPortCom2( HZTable[hzcode][i*2], 0xc0); //&#24038;&#21322;&#37096; &#22320;&#22336;&#21152;&#19968;
    OutPortCom2( HZTable[hzcode][i*2+1], 0xc4); //&#21491;&#21322;&#37096; &#23383;&#27169;&#22320;&#22336;&#21152;&#19968;

    StartAddr=StartAddr + LineChar;
    }
    }


    But I adapt the array operation HZTable[hzcode][i*2] and HZTable[hzcode][i*2+1] to HZTable[0][i*2] and HZTable[0][i*2]. the programme can run well.(can show the 1st double-byted word).
    why?

  • many projects which includes "for" loop,can run well in Keil UV2,but can't run well in the target board

    Ahhh. Then you just answered your own question. If all FOR loops work as expected in the uVision2 Simulator. And, if MANY of them don't work on your hardware. Then, you most likely have a problem with your hardware. That's where you should spend your time looking.

    If you think the problem is with the object code generated by the compiler then the code would not work in uVision2.

    Jon

    P.S. Please use the pre tags for your source code. It's impossible to read using bold and italics tags.

  • Excuse me but in your schematics 1 and 3 there is no pull up in the P0 port ??

    In the Atmel datasheet page 3 , all port have pull up EXCEPT the P0, so you may encounter sevreal problem il you doesn't put some resistors...

    Sincerely

    C.GRUN