|
|
View previous topic :: View next topic |
Author |
Message |
Rocky
Joined: 26 Apr 2009 Posts: 22 Location: USA
|
problem diff between PIC18F and PIC24F |
Posted: Sat Sep 26, 2009 3:42 pm |
|
|
I wrote a graphic application that run perfectly on the PIC24 using
primitive routine like the one below which is driving a line.
On the PIC18F I replaced the 'int' with 'int16'.
All my functions are working fine on the pic24 but don't on the PIC18.
A vertical or horizontal line on the PIC18 draws as a 45 degree line.
I tried simpler algorithms but they all show problems.
Code: |
// Note: taken verbatim from Professor McMillan's presentation:
// http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
//
// ************************************************************
void LCDSetLine(int x1, int y1, int x2, int y2, int color)
{
int dy, dx, stepx, stepy, fraction;
dy = y2 - y1;
dx = x2 - x1;
if (dy < 0)
{
dy = - dy;
stepy = - 1;
}
else
{
stepy = 1;
}
if (dx < 0)
{
dx = - dx;
stepx = - 1;
}
else
{
stepx = 1;
}
dy <<= 1; // dy is now 2 * dy
dx <<= 1; // dx is now 2 * dx
LCDSetPixel (x1, y1, color);
if (dx > dy)
{
fraction = dy - (dx >> 1); // same as 2 * dy - dx
while (x1 != x2)
{
if (fraction >= 0)
{
y1 += stepy;
fraction -= dx; // same as fraction -= 2 * dx
}
x1 += stepx;
fraction += dy; // same as fraction -= 2 * dy
LCDSetPixel (x1, y1, color);
}
}
else
{
fraction = dx - (dy >> 1);
while (y1 != y2)
{
if (fraction >= 0)
{
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
LCDSetPixel (x1, y1, color);
}
}
}
|
with this simpler algorithm the problem appears if y1>y2 or x1>x2.
Code: | void lineSimple(int16 x0, int16 y0, int16 x1, int16 y1, int8 color)
{
int16 dx,dy,i;
float m,b;
dx = x1 - x0; //PROBLEM IS IF X0>X1 OR Y0>Y1
dy = y1 - y0;
LCDSetPixel(x0, y0,color);
if (dx > dy) {
m = (float)dy / (float) dx;
b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1) {
x0 = x0 + dx;
LCDSetPixel(x0, (int16)(m*x0 + b),color);
}
}
else
if (dy != 0) {
m = (float)dx / (float) dy;
b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1) {
y0 = y0 + dy;
LCDSetPixel( (int16)(m*x0 + b), y0,color);
}
}
} | Again, everything works fine on the PIC24F.
Thanks for any help. _________________ PERSEVERANCE IS THE KEY TO SUCCESS |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 26, 2009 4:05 pm |
|
|
Note that in the CCS PCH compiler (and PCB and PCM), integer data types
are 'unsigned' by default. If you want a signed value, you need to
declare it explicitly. Example:
|
|
|
Rocky
Joined: 26 Apr 2009 Posts: 22 Location: USA
|
problem diff between PIC18F and PIC24F |
Posted: Sun Sep 27, 2009 12:02 am |
|
|
Thank you PCM that was the problem.
PCM programmer wrote: | Note that in the CCS PCH compiler (and PCB and PCM), integer data types
are 'unsigned' by default. If you want a signed value, you need to
declare it explicitly. Example:
|
_________________ PERSEVERANCE IS THE KEY TO SUCCESS |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|