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 CCS Technical Support

PCD: Converting 18F code to PIC24
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ramsey
Guest







PCD: Converting 18F code to PIC24
PostPosted: Sat Oct 03, 2009 1:31 pm     Reply with quote

Hello,
I hope someone can help me to convert this code into PIC24 compatible.
I am using PIC24FJ256GB.
Code:

#include <18F452.h>
#fuses EC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=============================

//Keypad connection:
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3

// Keypad layout:
char const KEYS[4][4] =
{{'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}};


#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second

void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true);
}

short int ALL_ROWS (void)
{
if(input (row0) & input (row1) & input (row2) & input (row3))
   return (0);
else
   return (1);
}

char kbd_getc()
{
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;

byte kchar;
byte row;

kchar='\0';

if(++kbd_call_count>KBD_DEBOUNCE_FACTOR)
  {
   switch (col)
     {
      case 0:
        output_low(col0);
        output_high(col1);
        output_high(col2);
        output_high(col3);
        break;
   
      case 1:
        output_high(col0);
        output_low(col1);
        output_high(col2);
        output_high(col3);
        break;

      case 2:
        output_high(col0);
        output_high(col1);
        output_low(col2);
        output_high(col3);
        break;

      case 3:
        output_high(col0);
        output_high(col1);
        output_high(col2);
        output_low(col3);
        break;
      }

   if(kbd_down)
     {
      if(!ALL_ROWS())
        {
         kbd_down=false;
         kchar=last_key;
         last_key='\0';
        }
     }
   else
     {
      if(ALL_ROWS())
        {
         if(!input (row0))
            row=0;
         else if(!input (row1))
            row=1;
         else if(!input (row2))
            row=2;
         else if(!input (row3))
            row=3;

         last_key =KEYS[row][col];
         kbd_down = true;
        }
      else
        {
         ++col;
         if(col==4)
            col=0;
        }
     }
   kbd_call_count=0;
  }
return(kchar);
}

//===========================
void main()
{
char k;

kbd_init();

printf("\r\Starting ...");

while(TRUE)
  {
   k=kbd_getc();
   if(k!=0)
     {
      if(k=='*')
         printf("%c", '*');
      else
         printf("%c", k);
     }
  }

}
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Oct 03, 2009 10:31 pm     Reply with quote

Have you even tried yet?

You need to change the #device .H file, the clock speed and your general use of 8bit vs 16bit INTs.

Have you read the datasheet for the PIC24 you are using?

Have you read the conversion PDF in the PICC install directory covering differences between PCH and PCD?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 12:46 am     Reply with quote

He will also have to map the UART pins....
_________________
Google and Forum Search are some of your best tools!!!!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 1:01 am     Reply with quote

The only explicite difference is with port_b_pullups() that has to be replaced with set_pullup() for individual pins (or another construct operating on PIC24 SFR directly).

#pin_select statements for UART pins are needed to specify the usage of a hardware UART with PIC24FJ256GB. The xmit=PIN_xx, rcv=PIN_xx syntax creates a software UART. If you started with a "Hello world" application for your target hardware, you should have already used the UART.

There should be no problem to continue using 8-bit data.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 1:15 am     Reply with quote

dyeatman wrote:
He will also have to map the UART pins....


not if he just calls out:

Code:
#use rs232 (UART1, baud=9600, yada yada)

_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 6:02 am     Reply with quote

Quote:
#use rs232 (UART1, baud=9600, yada yada)

Which specification you're referring to?
Ramsey
Guest







PostPosted: Sun Oct 04, 2009 10:49 am     Reply with quote

BKamen:
I set the configuration bit as follows:
Code:
#include <24FJ256GB110.h>
#device ADC=10
#device PASS_STRINGS=IN_RAM

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                 //No Debug mode for ICD
#FUSES ICSP1                      //ICD uses PGC1/PGD1 pins
#FUSES NOWINDIS                 //Watch Dog Timer in Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO                        //Internal External Switch mode enabled
#FUSES NOCKSFSM                 //Clock Switching is disabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES HS                           //High speed Osc
#FUSES XT
#FUSES NOIOL1WAY               //multiple reconfigurations of pins
#FUSES WPEND_LOW             
#FUSES NOWPCFG               
#FUSES NOWPDIS               
#FUSES WPFP0   
//#FUSES PR_PLL                   //Enable PLL for primary oscillator?
#FUSES PLL3                     //Divide By 3(12MHz oscillator input)
#FUSES RESERVED                 //Used to set the reserved FUSE bits
#FUSES DISUVREG       
#use delay(clock=24000000)
-----------------------------------------------------------

FVM:
void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
set_pullup(true, pin_B4);
set_pullup(true, pin_B5);
set_pullup(true, pin_B6);
set_pullup(true, pin_B7);
set_pullup(true, pin_C1); // on c port
set_pullup(true, pin_C2);
set_pullup(true, pin_C3);
set_pullup(true, pin_C4);
}


The result:
------------------------------------------------------------
*** Error 28 "main.c" Line 62(11,14): Expecting an identifier
- short int ALL_ROWS (void)
*** Error 43 "main.c" Line 62(20,21): Expecting a declaration
*** Error 81 "main.c" Line 63(1,2): Expecting a basic type
*** Error 43 "main.c" Line 64(1,3): Expecting a declaration
*** Error 43 "main.c" Line 64(3,4): Expecting a declaration

--------------------------------------------------------------
Quote:
your general use of 8bit vs 16bit INTs.
or
#pin_select XXX = PIN_B2


Could this be a problem ?
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 1:40 pm     Reply with quote

Your error sits in main. Could you post main() for us?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Oct 04, 2009 1:49 pm     Reply with quote

I can't do anything than you can: Identifying the error locations and determine the real or PCD imagined cause.

I found two problems:
- PCD is actually rejecting the type short int, although specified as legal in the manual. Just replace it with int8.
- PCD apparently doesn't use a complete table of CN pins that have a pulldown option for set_pullup().
So you have to operate on CNPUx SFRs instead.
Ramsey
Guest







PostPosted: Mon Oct 05, 2009 5:43 am     Reply with quote

Hi guys,
i changed to int8 and this part is working. Thank you.

All that is now left is this error:
Quote:
*** Error 27 "main.c" Line 61(24,25): Expression must evaluate to a constant :: Not a CN pin
*** Error 27 "main.c" Line 62(24,25): Expression must evaluate to a constant :: Not a CN pin
*** Error 27 "main.c" Line 63(24,25): Expression must evaluate to a constant :: Not a CN pin
*** Error 27 "main.c" Line 64(24,25): Expression must evaluate to a constant :: Not a CN pin
*** Error 27 "main.c" Line 65(24,25): Expression must evaluate to a constant :: Not a CN pin
*** Error 27 "main.c" Line 66(24,25): Expression must evaluate to a constant :: Not a CN pin


line 27 is this : set_pullup(true, pin_B6);

CN settings:
CNPU1 - 006C
CNPU2 - 006E
CNPU3 - 0070
CNPU4 - 0072
CNPU5 - 0074
CNPU6 - 0076

#byte CNPU1 = 0x6C

-------------------------------------------------
What's the right way to define SFR settings ?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Oct 05, 2009 7:10 am     Reply with quote

I didn't mention explicitely, but you should report the set_pullup() bug to CCS.

First, you have to find out the CN numbers of the respective pins. RB4=CN6, RB5=CN7, RB6=CN24, RB7=CN25, C1-4=CN45-48. The number defines the bit position in CNPU registers. You can either use bit, byte or word accesses to set them. I suggest bit accesses according set_pullup();

Code:
#bit CNPU6E = 0x6c.6
#bit CNPU24E = 0x6e.8
#bit CNPU25E = 0x6e.9
#bit CNPU45E = 0x70.13
//...
CNPU6E = 1;
CNPU24E = 1;
CNPU25E = 1;
Ramsey
Guest







PostPosted: Mon Oct 05, 2009 2:07 pm     Reply with quote

OK. I will report it to CCS.
btw. it's 4.093 version.
Code:

#bit CN6PUE = 0x6C.6     // RB4
#bit CN24PUE = 0x6E.8   // RB3
#bit CN25PUE = 0x6E.9   // RB2
#bit CN45PUE = 0x70.13 // RB1

#bit CN48PUE = 0x72.0     // RC4
#bit CN47PUE = 0x70.15   // RC3
#bit CN46PUE = 0x70.14   // RC2
#bit CN45PUE = 0x70.13  //  RC1

main void()
{
------------------------------
CN6PUE = 1;
CN24PUE = 1;
CN25PUE = 1;
CN45PUE = 1;

CN48PUE = 1;
CN47PUE = 1;
CN46PUE = 1;
//CN45PUE = 1;
------------------------------
settings for the LCD

setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);

}

I removed the set_pullups part and the code is compiled without errors.
I'm gonna try it on the demo board to see if this is working.

If I miss something, let me know.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 05, 2009 2:16 pm     Reply with quote

Quote:

setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);

The spi modules are not disabled in that way.

This is how it should be done:
Code:

setup_spi(FALSE);
setup_spi2(FALSE);
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Oct 05, 2009 3:07 pm     Reply with quote

Quote:
The spi modules are not disabled in that way
Yes, important to mention. But with this PIC, an SPI module without #pin_select in effect doesn't show at all.
Ramsey
Guest







PostPosted: Tue Oct 06, 2009 10:33 am     Reply with quote

I compiled the code without errors but I can't get any result.
LCD is working but I can't get any keypress output.

Btw. I tested the keypad on 18F and picdem2 plus, and it worked just fine.
So it looks like we missed something in the code conversion part...

R.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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