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

Newbie - creating a custom port?

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



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

Newbie - creating a custom port?
PostPosted: Mon Jun 26, 2006 3:44 am     Reply with quote

forgive me since i'm still trying to get to grips with C and PIC programming. I have a PIC18F452 and am trying to configure a 9-bit output using portB<0:7> and portC<0>. How do i create a single variable to define these 2 ports? what i want to do is loop a variable output of values to this port. e.g.

#define TABLE_SIZE 81 //9-bit ouput

void main() {

int table[TABLE_SIZE] =
{
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10,11,
12,13,14,15, ...79, 80
};

int i;

for (i=0; i<TABLE_SIZE; i++) {
CUSTOM_PORT = table[i];
delay_us(20);
}
}


i.e. how do i define portB<0:7> and portC<0> to the variable CUSTOM_PORT? Any help anyone could provide would be appreciated. Thanks
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 4:06 am     Reply with quote

You can do this with a struct/union combination, but most flexible is a macro. For examples see: I/O pin management
as702ecs



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 4:19 am     Reply with quote

ok, would something like this work?

--------------------------------------------------------
#define TABLE_SIZE 16

int pin;
int16 PORTDATA;
int16 pin_table[] = {PIN_C0, PIN_B7, PIN_B6, PIN_B5, PIN_B4,
PIN_B3, PIN_B2, PIN_B1, PIN_B0 };

PORTDATA = pin_table[pin];

int table[TABLE_SIZE] =
{
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 54, 11,
12, 13, 14, 511
};

int i;

for (i=0; i<TABLE_SIZE; i++) {
PORTDATA = table[i];
delay_us(20);
}

}

or should i use structs?
as702ecs



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 4:37 am     Reply with quote

ckielstra, do you mean something like this?


extern volatile near unsigned char PORTDATA;
extern volatile near union {
struct {
unsigned RB0:1;
unsigned RB1:1;
unsigned RB2:1;
unsigned RB3:1;
unsigned RB4:1;
unsigned RB5:1;
unsigned RB6:1;
unsigned RB7:1;
unsigned RC0:1;
};
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 7:14 am     Reply with quote

The keywords 'volatile' and 'near' are not supported in CCS.
For example code check the link I provided above (click the blue colered text).
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Newbie - creating a custom port?
PostPosted: Mon Jun 26, 2006 8:19 am     Reply with quote

as702ecs wrote:
forgive me since i'm still trying to get to grips with C and PIC programming. I have a PIC18F452 and am trying to configure a 9-bit output using portB<0:7> and portC<0>. How do i create a single variable to define these 2 ports? what i want to do is loop a variable output of values to this port. e.g.

#define TABLE_SIZE 81 //9-bit ouput

void main() {

int table[TABLE_SIZE] =
{
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10,11,
12,13,14,15, ...79, 80
};

int i;

for (i=0; i<TABLE_SIZE; i++) {
CUSTOM_PORT = table[i];
delay_us(20);
}
}


i.e. how do i define portB<0:7> and portC<0> to the variable CUSTOM_PORT? Any help anyone could provide would be appreciated. Thanks

The PIC is an 8 bit machine. You can't really do it with a single assigment. The simpliest approach would be to write the lower eight bits to port b and the msb to port c:0.
as702ecs



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 8:34 am     Reply with quote

i tried setting it up using macros as suggested in http://www.ccsinfo.com/forum/viewtopic.php?t=18949 by PCW Programmer. Unfortunately the memory addresses associated with each output pin have been excluded from the datasheet for the PIC18F452 (PCW Programmer was using a 16F877). Anyway i can find these?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 8:37 am     Reply with quote

There are not individual addresses for pins on a pic. Only the ports are addressable. The addresses of the ports are in the datasheet.
as702ecs



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 8:46 am     Reply with quote

my mistake. in anycase i still cannot find them on the datasheet:
http://ww1.microchip.com/downloads/en/devicedoc/39564b.pdf

section 9.0, I/O Ports

:(
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 26, 2006 8:48 am     Reply with quote

Table 4-1 on page 47.
as702ecs



Joined: 26 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Mon Jun 26, 2006 8:52 am     Reply with quote

Thanks, Mark. I've been staring at the computer screen for too long! I'll give it a shot.
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