|
|
View previous topic :: View next topic |
Author |
Message |
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
best way to define a 48bit data structure |
Posted: Sun Sep 04, 2005 3:51 am |
|
|
I have 48 LED's, and want the ability to rotate a bit around the "variable", also directly manipulate each individual bit to turn an LED ON or OFF.
whats the best way ?, is it a union/structure, and could some guru help me with the code.
Mike |
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 04, 2005 5:48 am |
|
|
Code: |
structure bit48 {
int8 b[6];
};
#define RR48(x) rotate_right(&x,6)
#define RL48(x) rotate_left(&x,6)
int1 is_set(struct bit48 val,int8 bit_no) {
int8 locn;
locn=bit_no>>3;
bitno&=7;
return (bit_test(val.b[locn],bitno);
}
void set_bit(struct bit48 *val,int8 bit_no) {
int8 locn;
locn=bit_no>>3;
bitno&=7;
bit_set(val->b[locn],bitno);
}
void clear_bit(struct bit48 *val,int8 bit_no) {
int8 locn;
locn=bit_no>>3;
bitno&=7;
bit_clear(val->b[locn],bitno);
}
void clear_all(struct bit48 *val) {
int8 ctr;
for (ctr=0;;ctr<6;ctr++) val->b[ctr]=0;
}
main {
struct bit48 LEDstore;
//initialise as needed here
//Clears the varaible
clear_all(&LEDstore);
//Set a specific bit
set_bit(&LEDstore,2);
//Rotate the variable right
RR48(LEDstore);
//Rotate the variable left
RL48(LEDstore);
//Test a bit
if (is_set(LEDstore,2)) {
}
}
|
Remember you can also access the individual bytes as LEDstrore.b[0..5], so if for example these need to be multiplexed out of a single port, this is the easiest way to access the data.
There are dozens of ways to do this, and the best will depend to some extent, on how the data needs to be accessed.
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Sep 04, 2005 9:09 am |
|
|
This looks a bit funny to me
Code: | #define RR48(x) rotate_right(&x,6)
|
I would think that you would have either used 1 or 8. What did you use 6 or was it a typo? |
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 04, 2005 10:12 am |
|
|
Yes, and in the 'for' loop as well....
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Sep 04, 2005 10:17 am |
|
|
Ttelmah wrote: | Yes, and in the 'for' loop as well....
Best Wishes |
You've got the for loop correct since you are clearing the array containing 6 elements. |
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 04, 2005 10:24 am |
|
|
The other 6 is also right.
The variables for CCS's rotate functions, are the address to rotate, and the numer of bytes to rotate. Six bytes. I went away, and found myself thinking: '6*8=48'. I was right. :-)
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Sep 04, 2005 8:43 pm |
|
|
Ttelmah wrote: | The other 6 is also right.
The variables for CCS's rotate functions, are the address to rotate, and the numer of bytes to rotate. Six bytes. I went away, and found myself thinking: '6*8=48'. I was right. :-)
Best Wishes |
The rotate and shift functions rotate bits, not bytes. |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
thank you for your help |
Posted: Mon Sep 05, 2005 2:14 am |
|
|
many thanks, i will try it today.
I am sitting in a forest in france, and my internet connection is very dodgy, usually 33Kb/s, (sometimes just for a few minutes !!!).
I spent a while looking at old posts, and came up with this, which may help others, but Ttelmah's code looks good.
int16 PCF7575_Register_1;
int16 PCF7575_Register_2;
int16 PCF7575_Register_3;
#bit LED0 = PCF7575_Register_1.0
#bit LED1 = PCF7575_Register_1.1
#bit LED2 = PCF7575_Register_1.2
#bit LED3 = PCF7575_Register_1.3
#bit LED4 = PCF7575_Register_1.4
#bit LED5 = PCF7575_Register_1.5
#bit LED6 = PCF7575_Register_1.6
#bit LED7 = PCF7575_Register_1.7
#bit LED8 = PCF7575_Register_1.8
#bit LED9 = PCF7575_Register_1.9
#bit LED11 = PCF7575_Register_1.10
#bit LED12 = PCF7575_Register_1.11
#bit LED13 = PCF7575_Register_1.12
#bit LED14 = PCF7575_Register_1.13
#bit LED15 = PCF7575_Register_1.14
#bit LED16 = PCF7575_Register_1.15
#bit LED17 = PCF7575_Register_2.0
#bit LED18 = PCF7575_Register_2.1
#bit LED19 = PCF7575_Register_2.2
#bit LED20 = PCF7575_Register_2.3
#bit LED21 = PCF7575_Register_2.4
#bit LED22 = PCF7575_Register_2.5
#bit LED23 = PCF7575_Register_2.6
#bit LED24 = PCF7575_Register_2.7
#bit LED25 = PCF7575_Register_2.8
#bit LED26 = PCF7575_Register_2.9
#bit LED27 = PCF7575_Register_2.10
#bit LED28 = PCF7575_Register_2.11
#bit LED29 = PCF7575_Register_2.12
#bit LED30 = PCF7575_Register_2.13
#bit LED31 = PCF7575_Register_2.14
#bit LED32 = PCF7575_Register_2.15
#bit LED33 = PCF7575_Register_3.0
#bit LED34 = PCF7575_Register_3.1
#bit LED35 = PCF7575_Register_3.2
#bit LED36 = PCF7575_Register_3.3
#bit LED37 = PCF7575_Register_3.4
#bit LED38 = PCF7575_Register_3.5
#bit LED39 = PCF7575_Register_3.6
#bit LED40 = PCF7575_Register_3.7
#bit LED41 = PCF7575_Register_3.8
#bit LED42 = PCF7575_Register_3.9
#bit LED43 = PCF7575_Register_3.10
#bit LED44 = PCF7575_Register_3.11
#bit LED45 = PCF7575_Register_3.12
#bit LED46 = PCF7575_Register_3.13
#bit LED47 = PCF7575_Register_3.14
#bit LED48 = PCF7575_Register_3.15
then LED0=ON;
Update_LEDS();
delay_ms(250);
LED0=OFF;
Update_LEDS();
delay_ms(2500);
nicely flashes the LED, having defined ON to be 1 etc
I also had a play with this
int16 PCF7575_Register_Array[3];
void Ripple_LEDS()
{
if ((PCF7575_Register_Array[0]==0) && (PCF7575_Register_Array[1]==0) && (PCF7575_Register_Array[2]==0)) PCF7575_Register_Array[0]=1;
write_PCF8575(0, PCF7575_Register_Array[0]);
write_PCF8575(1, PCF7575_Register_Array[1]);
write_PCF8575(2, PCF7575_Register_Array[2]);
rotate_left(PCF7575_Register_Array,6);
} // end of function
and it works
I havent done any coding for 2 years, so am very rusty :-(
Mike |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 05, 2005 2:55 am |
|
|
Mark wrote: | Ttelmah wrote: | The other 6 is also right.
The variables for CCS's rotate functions, are the address to rotate, and the numer of bytes to rotate. Six bytes. I went away, and found myself thinking: '6*8=48'. I was right. :-)
Best Wishes |
The rotate and shift functions rotate bits, not bytes. |
Er. Yes.
They rotate bits _in_ bytes.
They are called with the address of the first byte in a block, and the number of _bytes_ to be handled in the rotation. They only rotate by one bit at a time, but can be called with a block of six bytes, and will rotate the entire _block_ by one bit...
Best Wishes |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
oh bugger |
Posted: Mon Sep 05, 2005 7:32 am |
|
|
Sorry to be a pain, but the compiler throws up an error immediately.
error[48] : Expecting a (
this is at the end of the structure bit48 {
line. I am using V3.233, but i get the same error on v3.230.
the only line above the error line, is an include header file, which works with my original working code (see above)
Mike |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
a small step forward |
Posted: Mon Sep 05, 2005 8:10 am |
|
|
OK, making progress,
#define structure struct
now lets the compiler know what "stucture" means
next problem, what does "bitno " do, and where is it defined.
Mike |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
nearly there now !! |
Posted: Mon Sep 05, 2005 8:23 am |
|
|
nearly there now
#include "Version#2.h" // header file
#define structure struct // mikes added line
int8 bitno ; // mikes added line
structure bit48
{
int8 b[6];
};
#define RR48(x) rotate_right(&x,6)
#define RL48(x) rotate_left(&x,6)
int1 is_set(struct bit48 val,int8 bit_no)
{
int8 locn;
locn=bit_no>>3;
bitno&=7;
return (bit_test(val.b[locn],bitno));
}
void set_bit(struct bit48 *val,int8 bit_no)
{
int8 locn;
locn=bit_no>>3;
bitno&=7;
bit_set(val->b[locn],bitno);
}
void clear_bit(struct bit48 *val,int8 bit_no)
{
int8 locn;
locn=bit_no>>3;
bitno&=7;
bit_clear(val->b[locn],bitno);
}
void clear_all(struct bit48 *val)
{
int8 ctr;
for (ctr=0;ctr<6;ctr++) val->b[ctr]=0;
}
void main()
{
struct bit48 LEDstore;
//initialise as needed here
//Clears the varaible
clear_all(&LEDstore);
//Set a specific bit
set_bit(&LEDstore,2);
//Rotate the variable right
RR48(LEDstore);
//Rotate the variable left
RL48(LEDstore);
//Test a bit
if (is_set(LEDstore,2)) { //***** gives an error "Structures and Unions can not be parameters (use* or &) rotate_left
}
}
last problem seems to be with test a bit function
Mike |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 06, 2005 6:21 am |
|
|
don't use a #define for structure. It should just be struct
Code: | struct bit48
{
int8 b[6];
};
|
|
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 06, 2005 7:47 am |
|
|
Change 'is_set' to:
Code: |
int1 is_set(struct bit48 *val,int8 bit_no) {
int8 locn;
locn=bit_no>>3;
bitno&=7;
return (bit_test(val->b[locn],bitno);
}
|
Note the change both in the declaration, and the return line.
And call it with the address as:
if (is_set(&LEDstore,2))
Best Wishes |
|
|
|
|
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
|