|
|
View previous topic :: View next topic |
Author |
Message |
as702ecs
Joined: 26 Jun 2006 Posts: 6
|
Newbie - creating a custom port? |
Posted: Mon Jun 26, 2006 3:44 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 4:06 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 4:19 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 4:37 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 7:14 am |
|
|
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
|
Re: Newbie - creating a custom port? |
Posted: Mon Jun 26, 2006 8:19 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 8:34 am |
|
|
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
|
|
Posted: Mon Jun 26, 2006 8:37 am |
|
|
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
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 8:48 am |
|
|
Table 4-1 on page 47. |
|
|
as702ecs
Joined: 26 Jun 2006 Posts: 6
|
|
Posted: Mon Jun 26, 2006 8:52 am |
|
|
Thanks, Mark. I've been staring at the computer screen for too long! I'll give it a shot. |
|
|
|
|
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
|