View previous topic :: View next topic |
Author |
Message |
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
C question |
Posted: Mon Jan 16, 2017 6:21 pm |
|
|
My apology for the question if it is not CCS related
I have to fill two bytes with data that are individual bits
What I am doing is much assembler like:
Code: | int byte0=0;
short bit0=0;
if(bit0==1)
{
bit_set(byte0,0);
}
else
{
bit_clear(byte0,0);
} |
And so on for two bytes 8 bit each
To fill in this way the 8 bits of two bytes takes many program lines and a lot of comments to remember what is what
Can somebody show me (teach me) how to do it in more "C" like way?
Best wishes
Joe |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Jan 17, 2017 4:06 am |
|
|
Depends on the exact form of your bits.
You could try:-
1) A union.
2) A loop if the bits are arranged as an array.
Mike |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Tue Jan 17, 2017 10:37 pm |
|
|
Thank you for the answer Mike
Will try to learn the subject
Best wishes
Joe |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Wed Jan 18, 2017 3:13 am |
|
|
As Mike says, a union would be the standard way to do this. Since CCS allows bit arrays (int1), you can do it like:
Code: |
union bit_access
{
int1 bits[16];
unsigned int16 word;
unsigned int8 bytes[2];
} val;
void main(void)
{
int8 ctr;
int8 abit;
const int8 fill_with[16] = {1,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1};
//16 values to putinto the bits as a demo
val.word=0; //clear the memory to start.
//demonstrate filling 16 bit value
for (ctr=0;ctr<16;ctr++)
{
abit=fill_with[ctr];
val.bits[ctr]=abit;
}
//Now see what is held
printf("byte 0: %2x, byte 1: %2x\n\r",val.bytes[0],val.bytes[1]);
while(TRUE)
;
}
|
This should give 05 E1
Remember bits in the 'source' array here are being read least significant bit first.
This demonstrates byte, and word access to the same memory (word to clear it at the start, and byte to print it). |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Wed Jan 18, 2017 7:23 pm |
|
|
Thanks for the details Ttelmah
I will implement and work with
Best wishes
Joe |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 18, 2017 9:23 pm |
|
|
One thing you never told us is where these 16 bits come from.
Are they declared variables (int1) ? Or are they port pins ? |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Thu Jan 19, 2017 4:57 pm |
|
|
Hi PCM programmer
They are all defined as (short).
Activated by switches that connects VSS as they have pull-up resistors of 10K to VDD.
All read with a 1 to 16 MUX with 4 pins as MUX address and 1 pin for the data in.
Best wishes
Joe |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Fri Jan 20, 2017 12:47 am |
|
|
You are doing more transfers than are needed. Don't fiddle with reading the bits into separate places. Bring them in directly.
So:
Code: |
int16 mask;
int16 val=0;
int8 mult_addr;
mult_addr=WhateverBitYouAreReading; //however you are
//generating this address
mask=1<<mult_addr;
//Then put 'mult_addr' out to your multiplexer
if (data_from_input) //where this is your data input
val = val | mask;
else
val = val & (mask ^ 0xFFFF);
|
This way the required bit is read directly into the 'val' variable.
Even better if you are looping and doing the read, just load 'mask' with 1 at the start of the loop, and rotate it once each time round the loop. |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Fri Jan 20, 2017 5:29 am |
|
|
Thanks Ttelmah
Said the truth, I don't understand how the program is setting/clearing the address bits to the MUX.
I will try to implement and see how is going
Best wishes
Joe |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Jan 20, 2017 6:38 am |
|
|
perhaps this may help ?
given: mux adrs lines are on portA,bit3...0
mux data lins is tied to portB, bit1
something like...
Code: |
for(mux_adrs=0;mux_adrs<16,mux_adrs++){
output_a(mux_adrs); // sets mux address
delay_ms(1); // allow mux to 'settle' before reading
mux_data=input(PIN_B1); // read the mux data
switch_array[mux_adrs]= mux_data; // rem store mux data into array
} |
Now I haven't tried it, it's still dark here, only has 2 coffees but...
it's a simple loop that
1) sets the muxes address
2) wait's a bit
3) read the mux data
4) stores the data
for all 16 addresses of the mux.
I think this is what you want to do.
switch_array[] is an unsigned 16 bit integer
the delay probably can be shorter,the mux device datasheet will say what the minimum is, I'd x2 that , just to be sure.
There's a LOT of ways to do what you want, to me , this is simple( one step at a time) and easy to read. Since you're reading switches it does NOT have to be fast and fancy.
Hope this helps
Jay |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sat Jan 21, 2017 11:13 pm |
|
|
Thanks Jay
Your program is very clear, except I have a question:
Code: | output_a(mux_adrs); |
How I can use the other pins of port A?
I mean they will be output also together with pin 3...0, no?
Yes, you are right, I don't need a fast reading for the switches
Best wishes
Joe |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Sun Jan 22, 2017 2:55 am |
|
|
There are a number of different answers to how to use the other pins:
1) Have a 'byte to send to port A' variable, and mask in the three bits, then send the whole variable.
2) Look at PCM_Programmer's 'Flex_lcd'. Here he allows any pins on a port to be used to drive the LCD, by outputting the data 'bit at a time', using a test, and bit output. Can be adapted for any combination of pins.
3) If you are happy to use the other bits as inputs, then switch the port to fast_IO, and only set the bottom three bits as outputs. Then you can write a 'byte' to the port, and only the three output bist will be used.
Loads of other similar ways of doing this..... |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sun Jan 22, 2017 4:49 pm |
|
|
Thanks Ttelmah
I am working on the subject, will report after implemented.
Best wishes
Joe |
|
|
|