|
|
View previous topic :: View next topic |
Author |
Message |
j_gatz
Joined: 29 Oct 2023 Posts: 2 Location: Mexico
|
for cicle doesn't work and binary parameter using variable |
Posted: Sun Oct 29, 2023 12:59 am |
|
|
hi, im new to ccs, im trying to get the logic values from portb through a 4 buttons switch to make a binary number, im using these values to turn on some leds in a display of 7 segments using as output portd, the lit leds represent the number in hexadecimal. The thing is I need to do it with logical operations like OR AND.
Code: |
int1 x[7];
int i;
void main()
{
set_tris_b(0xFF);
set_tris_d(0x00);
while(true){
x[0] = !PIN_B0&&!PIN_B2 || PIN_B1&&PIN_B2 || PIN_B3&&!PIN_B2 || PIN_B0&&PIN_B1&&!PIN_B3 || !PIN_B0&&!PIN_B1&&!PIN_B3 || PIN_B0&&PIN_B2&&PIN_B3;
//i defined until x[6] but didn't show it here
for(i=0 ; i=7; ++i){
if(x[i] == 0) { output_LOW(64+i); }
else { output_HIGH(64+i); }
}
}
}
|
the problem is that this doesn't do anything. another thing that I would like to do but i cannot is using output_d(0b0abcdefg) using as parameters my logic operations initialized as int1 a,b,c...f, but it says that I should use numerical expression.
I really hope someone can help me with these questions. THANK YOU. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Sun Oct 29, 2023 9:17 am |
|
|
As coded if any statement is TRUE, you will get a result of 'TRUE' (1).
You don't show how you have coded the pins. Presumably #bit
statements?.
I'd suggest you change the approach. If you have four buttons, then
all you need is an array of the bit patterns you want for the LED's.
16 entries.
Then just read the whole of port B. Take the low 4 bits.
So:
val=input_b() & 0xF; //val is now the low four bits of b.
Then output this to the led's:
array[val] is then the value you want to sent to the LED's.
Now you don't say whether your LED's are wired to come on with a 0,
or a 1?.
Your current posted code, for the 16 possible input combinations gives:
Code: |
0000 1
0001 0
0010 0
0011 1
0100 1
0101 0
0110 1
0111 1
1000 1
1001 1
1010 1
1011 1
1100 1
1101 1
1110 1
1111 1
|
Only three states when the result will be 0.
If this isn't right you need to rresearch a Kernaugh map, and how
to solve the logic from this. However remember you have to invert
the logic if the LED needs a 0 to turn it on, and also invert the diagram
if the buttons are pulling down to 0.
On your output, you have to recombine the results into a numeric value
to put out. Each of the value you get from the functions will just be a
1 or 0. You need to take a[0], add a[1]*2, then a[2]*4, a[3]*8 etc.,
to build a numeric value to put out the port.
This way you are building a binary number containing
a6 a5 a4 a3 a2 a1 a0
as the bits
This is why the array approach is so much simpler, since the value is
already correctly formatted in the array. You just store in the array the
bit patterns you want. |
|
|
j_gatz
Joined: 29 Oct 2023 Posts: 2 Location: Mexico
|
|
Posted: Sun Oct 29, 2023 5:37 pm |
|
|
thank you so much, building a binary number worked, I didn't know that output function could receive decimals, and thanks for the advice, certainly an array would be simpler but this time I was asked for doing it with logical operations.
so i convert every logic operation into a binary expression which essentially is a decimal expression.
by the way i don't really understand why the for cycle didn't work, I tried to use it to name each pin input but didn't work, I had to make it manually. so I couldnt use for cycle in any way. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Mon Oct 30, 2023 2:17 am |
|
|
Several bits to possibly why the loop didn't work.
First, what chip???. The actual binary values for pins depend totally on this.
Second, what compiler version???. This latter is vital. The early compilers
(V3 and before) would not accept a variable to access a pin. In the forum,
dating from these early compilers, is a function from PCM_Programmer
to give variable access.
One other thought is that instead of the array you currently use, you could
use a structure containing these elements as bit fields. Have this in a
union with a byte, and the byte is the required result without the overhead
of the multiplication. |
|
|
rjenkinsgb
Joined: 03 Apr 2018 Posts: 4
|
|
Posted: Wed Nov 01, 2023 3:49 am |
|
|
To clarify the problem with your first try, the "PIN_B0" etc. references are just numbers, internally - you can see them in each device header file.
To use those to access the device ports/pins, you have to use such as
input(PIN_B0);
or
output_bit(PIN_C2, HIGH); |
|
|
|
|
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
|