CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

problem diff between PIC18F and PIC24F

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

problem diff between PIC18F and PIC24F
PostPosted: Sat Sep 26, 2009 3:42 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Sep 26, 2009 4:05 pm     Reply with quote

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:
Code:
signed int16 dy;
Rocky



Joined: 26 Apr 2009
Posts: 22
Location: USA

View user's profile Send private message

problem diff between PIC18F and PIC24F
PostPosted: Sun Sep 27, 2009 12:02 am     Reply with quote

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:
Code:
signed int16 dy;

_________________
PERSEVERANCE IS THE KEY TO SUCCESS
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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