Author Topic: Looking for someone to help me with the code for uC  (Read 4877 times)

0 Members and 1 Guest are viewing this topic.

Offline CloudTopic starter

  • Regular Contributor
  • *
  • Posts: 194
  • Country: si
Looking for someone to help me with the code for uC
« on: February 18, 2015, 07:00:53 pm »
Hello, everybody :)
I tried to make this with the logic gates, but it would be too big and too expensive, so I decided to use uC preferably AtTiny. But the problem comes at the program because I was playing around with arduino for a few days but I still don't understand anything. So if anyone would be kind to write the code or at least give me some guidelines, I would be so happy.
So I need two inputs (I1 & I2) and two outputs (Q1 & Q2), both are 1 or 0. For better understanding I draw a timing diagram (I have no idea how this thing is called in english), the parts, that are coloured with yellow, are the ones that are 1. If you don't understand please ask me and I'll try to explain.




Thank you :)
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: Looking for someone to help me with the code for uC
« Reply #1 on: February 18, 2015, 07:10:10 pm »
 :-// :palm: :scared:
 :wtf: :wtf: :wtf: :wtf:
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline elgonzo

  • Supporter
  • ****
  • Posts: 688
  • Country: 00
Re: Looking for someone to help me with the code for uC
« Reply #2 on: February 18, 2015, 07:12:39 pm »
Your timing diagram is somewhat ambiguous about how the inputs affect the outputs.
Perhaps it is better to write it down as a truth table.

Also, it is unknown how tight the timing tolerances between input and output signal needs to be. Remember that a program executed by a MCU cannot immediately change an output based on an changing input signal. Since an MCU executes assembly instructions, the mininum delay between an output changing as response to a change of input is dependent on the clock speed of the MCU and the the amount clock cycles used by the instructions used to read the input and set the output...
« Last Edit: February 18, 2015, 07:15:48 pm by elgonzo »
 

Offline suicidaleggroll

  • Super Contributor
  • ***
  • Posts: 1453
  • Country: us
Re: Looking for someone to help me with the code for uC
« Reply #3 on: February 18, 2015, 07:16:13 pm »
It looks like you're trying to draw a truth table, but it's inconsistent.

In the 1st and 5th frames, I1 is high, I2 is low, and both Q1 and Q2 are high.  In the 7th frame, I1 is high, I2 is low, and both Q1 and Q1 are low.
In the 2nd and 6th frames, I1 and I2 are low, and Q1 and Q2 are high, but in the 4th frame I1 and I2 are low and Q1 is low.

If it's supposed to be a simple truth table, then you need to be consistent.  If it's not a simple truth table, then you need to describe what, exactly, Q1 and Q2 actually depend on.
 

Offline CloudTopic starter

  • Regular Contributor
  • *
  • Posts: 194
  • Country: si
Re: Looking for someone to help me with the code for uC
« Reply #4 on: February 18, 2015, 07:44:44 pm »
@elgonzo the delay isn't problem for me, because it will be used for switching lights.

The outputs will be relying on the previous state of the outputs. I'll try to explain it in the words.

If you press I1 while Q1 and Q2 are 0, the Q1 and Q2 will be set to 1.
If you press I1 while outputs are 1, they'll be set to 0.
If you press I2 while outputs are 0, Q2 will be 1.
If you press I2 while Q2 is 1, it'll be set to 0.
If you press l2 while Q1 and Q2 are 1, Q1 will be set to 0 and Q2 will stay 1.
If you press I1 while Q2 is 1, Q1 will be set to 1 and Q2 will stay 1.

 

Offline suicidaleggroll

  • Super Contributor
  • ***
  • Posts: 1453
  • Country: us
Re: Looking for someone to help me with the code for uC
« Reply #5 on: February 18, 2015, 08:18:32 pm »
You need to generate a truth table.

16 rows, with all combinations of I1, I2, Q1, Q2, and the outputs Q1' and Q2' (meaning those will be the updated outputs on the next clock cycle).
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Looking for someone to help me with the code for uC
« Reply #6 on: February 18, 2015, 08:23:45 pm »
Are you familiar with a truth table?

Your best bet is to probably use something like an Arduino.

If this is going to be for anything other than hobby type stuff then you will probably want to look at switch debouncing also.
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: Looking for someone to help me with the code for uC
« Reply #7 on: February 18, 2015, 08:27:11 pm »
Also consider what happens when I1 & I2 are conflicting with each other.
Which take precedence? 
Don't ask a question if you aren't willing to listen to the answer.
 

Offline CloudTopic starter

  • Regular Contributor
  • *
  • Posts: 194
  • Country: si
Re: Looking for someone to help me with the code for uC
« Reply #8 on: February 18, 2015, 09:15:52 pm »
@Wilksey actually not very much, at school we never covered it (at electronics school!  |O ).

I1   I2   Q1   Q2    Q1   Q2
0   0   0   0    0   0
0   0   0   1    0   1
0   0   1   0    x   x
0   0   1   1    1   1
0   1   0   0    0   1
0   1   0   1    0   0
0   1   1   0    x   x
0   1   1   1    0   1
1   0   0   0    1   1
1   0   0   1    1   1
1   0   1   0    x   x
1   0   1   1    0   0
1   1   0   0    1   1
1   1   0   1    1   1
1   1   1   0    x   x
1   1   1   1    0   0

I hope this is correct, the red is output and where is x there is impossible thing, and again thank you for your time and your patience  :)
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: Looking for someone to help me with the code for uC
« Reply #9 on: February 18, 2015, 09:22:39 pm »
OK, I'm confused.
If I1 & I2 are inputs - there are only FOUR possible states for Q1 & Q2
Am I missing something


Don't ask a question if you aren't willing to listen to the answer.
 

Offline suicidaleggroll

  • Super Contributor
  • ***
  • Posts: 1453
  • Country: us
Re: Looking for someone to help me with the code for uC
« Reply #10 on: February 18, 2015, 09:27:04 pm »
The previous state of Q1 and Q2 are also inputs in his truth table.

It looks like a decent start, now just code it up and put it on a 1 second (or whatever cadence you want) loop.  You should also have entries for your "impossible" states, just in case.  Even though they will probably never happen, you don't want a fluke to send your code into an infinite loop from which it can never recover.  Maybe if the Q1 and Q2 states are "illegal" or "impossible", you just set them both to 0 as sort of a soft reset.
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: Looking for someone to help me with the code for uC
« Reply #11 on: February 18, 2015, 09:40:10 pm »
ahh ok, so that should be a pretty straightforward state machine to code up.
Don't ask a question if you aren't willing to listen to the answer.
 

Offline elgonzo

  • Supporter
  • ****
  • Posts: 688
  • Country: 00
Re: Looking for someone to help me with the code for uC
« Reply #12 on: February 18, 2015, 09:45:50 pm »
@Wilksey actually not very much, at school we never covered it (at electronics school!  |O ).

Your program should follow this very simple structure (basically a state machine):

1. Read the port bits associated with I1 and I2.
2. Read the current values of the output port bits associated with Q1 and Q2.
3. Check the values of I1, I2, Q1, and Q2 you just read, and set the output ports for Q1 and Q2 to the their new values according to your truth table. (See below for more detail)
4. Continue and with step #1. Rinse and repeat (essentially what is being called a polling loop)...

How would you check the I1, I2, Q1, Q2?
Note that I1, I2, Q1, Q2 are essentially 4 bits - you could see them as a 4-bit number.
So you could create an array in your program with 16 values (a 4-bit number can have 16 distinct values: 0...15).
The index of the array would be the 4-bit number formed by I1,I2,Q1,Q2.
The value at a particular array position would be the two new bits/values for Q1 and Q2.

From your truth table, the array could look like this (in binary notation, with bit 1 being the new value for Q1 and bit 0 being the new value for Q2):
[b00, b01, b00, b11, b01, b00, ... and so on]

The array index would be formed from the bit values read from the respective input/output ports for I1, I2, Q1 and Q2:
index = (I1 << 3) & (I2 << 2) & (Q1 << 1) & Q2

P.S.: If you write code, it is advised you document it properly. Otherwise you will forget over a few weeks time how your own code works :)
« Last Edit: February 19, 2015, 01:32:49 am by elgonzo »
 

Offline CloudTopic starter

  • Regular Contributor
  • *
  • Posts: 194
  • Country: si
Re: Looking for someone to help me with the code for uC
« Reply #13 on: February 18, 2015, 09:49:55 pm »
Thank you very much :) I'll try to work something out. Thank you for your time  :-+
 

Offline elgonzo

  • Supporter
  • ****
  • Posts: 688
  • Country: 00
Re: Looking for someone to help me with the code for uC
« Reply #14 on: February 19, 2015, 01:30:16 am »
Hehe, in my array example above i said i used hexadecimal notation, but then i wrote it in binary... and nobody complained   :P
I fixed the error in my post above, it should be correct now. Sorry for the mistake  :-[
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: Looking for someone to help me with the code for uC
« Reply #15 on: February 19, 2015, 02:20:14 am »
Cloud, what you describe here is called state machine because it moves between states whenever some event happens. To have it working you first need to detect the events and then update the state base on the current state and the new data.

In your case the event is when one of the input changes. Only then you want to update the state once. If the state does not change, you don't want to update the state.

If your inputs come from switches then you will need to filter changes (also called debouncing). Otherwise you will have multiple state changes per one actual switch change. This filtering can be cone in hardware or in software. I would suggest starting with that filtering.

I1 I2  Q1 Q2  Q1'  Q2'
0   1   0   0     0     1
0   1   0   1     0     0

Look at the above rules from example, without distinct event detection your Q2 output will go back and forth between zero and one.
 

Offline CloudTopic starter

  • Regular Contributor
  • *
  • Posts: 194
  • Country: si
Re: Looking for someone to help me with the code for uC
« Reply #16 on: February 20, 2015, 12:11:12 pm »
Could this be done so that, after the uC receives signal ignores booth inputs for 1-2seconds?
 

Offline elgonzo

  • Supporter
  • ****
  • Posts: 688
  • Country: 00
Re: Looking for someone to help me with the code for uC
« Reply #17 on: February 20, 2015, 12:26:50 pm »
Could this be done so that, after the uC receives signal ignores booth inputs for 1-2seconds?
Is there a problem with your program, or do you just want the user to prevent pressing buttons/switches quickly?
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Looking for someone to help me with the code for uC
« Reply #18 on: February 20, 2015, 12:36:38 pm »
Yes, with timers and flags
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: Looking for someone to help me with the code for uC
« Reply #19 on: February 20, 2015, 08:02:49 pm »
Yes, with timers and flags
As Wilksey said - there are several ways to achieve this - even a simple delay - extending the power-on reset period may be enough for your requirements.
Don't ask a question if you aren't willing to listen to the answer.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf