We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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); } /*--------------------------------------------------------- ---------------------------------------------------------*/
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">http://www.redrival.com/huiease/target_1.jpg</a> <a href="" target="_blank">http://www.redrival.com/huiease/target_2.jpg">http://www.redrival.com/huiease/target_2.jpg</a> thanks
target board drawings: http://www.redrival.com/huiease/target_1.jpg http://www.redrival.com/huiease/target2.jpg http://www.redrival.com/huiease/target3.jpg
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; //定位起始行 for(i=0;i<16;i++) { OutPortCom3( (unsigned char)(StartAddr), (unsigned char)(StartAddr>>8), 0x24); OutPortCom2( HZTable[hzcode][i*2], 0xc0); //左半部 地址加一 OutPortCom2( HZTable[hzcode][i*2+1], 0xc4); //右半部 字模地址加一 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