View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
truth table |
Posted: Tue Jul 21, 2009 6:57 pm |
|
|
Hi
What is better way to make a truth table?
I have one PIC for make this decoder, X0 to X4 is inputs and output is other pins...
I make some like that:
Code: |
#include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#define X0 PIN_C6
#define X1 PIN_C7
#define X2 PIN_B0
#define X3 PIN_B1
#define X4 PIN_B2
void main()
{
while (TRUE)
{
//###################### relay OFF #######################
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_B6); //R1
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_B5); //R2
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==1)) output_low(PIN_B4); //R3
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==0)) output_low(PIN_B3); //R4
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_C5); //R5
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_C4); //R6
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==1)) output_low(PIN_C3); //R7
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) output_low(PIN_C2); //R8
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_C1); //R9
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_C0); //R10
//###################### relay ON #######################
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_B6); //R1
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_B5); //R2
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==1)) output_high(PIN_B4); //R3
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==0)) output_high(PIN_B3); //R4
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_C5); //R5
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_C4); //R6
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==1)) output_high(PIN_C3); //R7
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) output_high(PIN_C2); //R8
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_C1); //R9
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_C0); //R10
//###################### relay ALL OFF #######################
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) //ALL OFF
{
output_low(PIN_B6); //R1
output_low(PIN_B5); //R2
output_low(PIN_B4); //R3
output_low(PIN_B3); //R4
output_low(PIN_C5); //R5
output_low(PIN_C4); //R6
output_low(PIN_C3); //R7
output_low(PIN_C2); //R8
output_low(PIN_C1); //R9
output_low(PIN_C0); //R10
}
}
}
|
what you think?
kind regards |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Tue Jul 21, 2009 9:02 pm |
|
|
One way of doing this, instead of writing tons of code (which could be host to several errors) is to use an 'overlay' approach.
You can create a 'structure' of sorts, in which different pins are mapped to bits in a byte, and input/output to pins in that structure by modifying that byte.
With this method, instead of Code: | if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_B6); //R1 | you'd be able to do something like this Code: | if (my_byte==0x03)
{some_other_byte=0x08;} | where the bits in *my_byte* are mapped to pins.
Read this http://ccsinfo.com/forum/viewtopic.php?t=35816 and http://www.ccsinfo.com/forum/viewtopic.php?t=18949&start=6
Rohit |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed Jul 22, 2009 8:06 pm |
|
|
Hi
I try make with 16F876 an decoder and put combination with 18F452.
on 18F452 I have:
Code: |
void RELAY1ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY2ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_high(PIN_C3);
output_low (PIN_C4);
}
void RELAY3ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_high (PIN_C3);
output_high (PIN_C4);
}
void RELAY4ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_low (PIN_C3);
output_low (PIN_C4);
}
void RELAY5ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY6ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_high (PIN_C3);
output_low (PIN_C4);
}
void RELAY7ON ()
{
output_high(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_high (PIN_C3);
output_high (PIN_C4);
}
void RELAY8ON ()
{
output_high(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_low (PIN_C4);
}
void RELAY9ON ()
{
output_high(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY10ON ()
{
output_high(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_high (PIN_C3);
output_low (PIN_C4);
}
//##################### RELAY OFF #####################
void RELAY1OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY2OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_high(PIN_C3);
output_low (PIN_C4);
}
void RELAY3OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_low (PIN_C2);
output_high (PIN_C3);
output_high (PIN_C4);
}
void RELAY4OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_low (PIN_C3);
output_low (PIN_C4);
}
void RELAY5OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY6OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_high (PIN_C3);
output_low (PIN_C4);
}
void RELAY7OFF ()
{
output_low(PIN_C0);
output_low (PIN_C1);
output_high (PIN_C2);
output_high (PIN_C3);
output_high (PIN_C4);
}
void RELAY8OFF ()
{
output_low(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_low (PIN_C4);
}
void RELAY9OFF ()
{
output_low(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_low (PIN_C3);
output_high (PIN_C4);
}
void RELAY10OFF ()
{
output_high(PIN_C0);
output_high (PIN_C1);
output_low (PIN_C2);
output_high (PIN_C3);
output_low (PIN_C4);
}
void RELAYALLOFF ()
{
output_high (PIN_C0);
output_high (PIN_C1);
output_high (PIN_C2);
output_high (PIN_C3);
output_high (PIN_C4);
}
|
and on 16F876 I have
Code: |
void main()
{
while (TRUE)
{
//###################### relay OFF #######################
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_B6); //R1
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_B5); //R2
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==1)) output_low(PIN_B4); //R3
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==0)) output_low(PIN_B3); //R4
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_C5); //R5
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_C4); //R6
if ((input(X4)==0) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==1)) output_low(PIN_C3); //R7
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) output_low(PIN_C2); //R8
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_low(PIN_C1); //R9
if ((input(X4)==0) && (input(X3)==1) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_low(PIN_C0); //R10
//###################### relay ON #######################
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_B6); //R1
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_B5); //R2
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==1) && (input(X0)==1)) output_high(PIN_B4); //R3
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==0)) output_high(PIN_B3); //R4
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_C5); //R5
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_C4); //R6
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==1) && (input(X1)==1) && (input(X0)==1)) output_high(PIN_C3); //R7
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) output_high(PIN_C2); //R8
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==0) && (input(X0)==1)) output_high(PIN_C1); //R9
if ((input(X4)==1) && (input(X3)==1) && (input(X2)==0) && (input(X1)==1) && (input(X0)==0)) output_high(PIN_C0); //R10
//###################### relay ALL OFF #######################
if ((input(X4)==1) && (input(X3)==0) && (input(X2)==0) && (input(X1)==0) && (input(X0)==0)) //ALL OFF
{
output_low(PIN_B6); //R1
output_low(PIN_B5); //R2
output_low(PIN_B4); //R3
output_low(PIN_B3); //R4
output_low(PIN_C5); //R5
output_low(PIN_C4); //R6
output_low(PIN_C3); //R7
output_low(PIN_C2); //R8
output_low(PIN_C1); //R9
output_low(PIN_C0); //R10
}
}
}
|
but it don't work
if for example I call RELAY1ON(); on 18F542
it turn on and turn off all relays very quickly
some one can help me for make this decoder?
kind regards |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Wed Jul 22, 2009 9:46 pm |
|
|
It is possible that the relays are causing glitches in the supply.
-How are you powering/activating the relays? Separate supply with a driver? Or is the supply shared between the relays and microcontroller?
-Are you using a snubber diode across your relays?
-Instead of relays use LEDs, and see if you face the same problem.
-Have you got a pull up on the MCLR pins of the microcontrollers?
Rohit |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed Jul 22, 2009 10:08 pm |
|
|
Hi
Thanks for opinions...
Problem stay on hardware...
Now all stay fine. Thanks |
|
|
|