Looking for someone to help me with the code for uC - Page 1
Forum section: electronics / general

author: Cloud
author_rank: Regular Contributor
timestamp: February 18, 2015, 07:00:53 pm
content: Hello, everybodyI 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

author: elgonzo
author_rank: Unknown
timestamp: February 18, 2015, 07:12:39 pm
content: 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...

author: suicidaleggroll
author_rank: Super Contributor
timestamp: February 18, 2015, 07:16:13 pm
content: 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 Q2actuallydepend on.

author: Cloud
author_rank: Regular Contributor
timestamp: February 18, 2015, 07:44:44 pm
content: @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.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: February 18, 2015, 08:18:32 pm
content: 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).

author: Wilksey
author_rank: Super Contributor
timestamp: February 18, 2015, 08:23:45 pm
content: 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.

author: SL4P
author_rank: Super Contributor
timestamp: February 18, 2015, 08:27:11 pm
content: Also consider what happens when I1 & I2 are conflicting with each other.Which take precedence?

author: Cloud
author_rank: Regular Contributor
timestamp: February 18, 2015, 09:15:52 pm
content: @Wilksey actually not very much, at school we never covered it (at electronics school!).I1 I2 Q1 Q2Q1 Q20 0 0 00 00 0 0 10 10 0 1 0x x0 0 1 11 10 1 0 00 10 1 0 10 00 1 1 0x x0 1 1 10 11 0 0 01 11 0 0 11 11 0 1 0x x1 0 1 10 01 1 0 01 11 1 0 11 11 1 1 0x x1 1 1 10 0I 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

author: SL4P
author_rank: Super Contributor
timestamp: February 18, 2015, 09:22:39 pm
content: OK, I'm confused.If I1 & I2 are inputs - there are only FOUR possible states for Q1 & Q2Am I missing something

author: suicidaleggroll
author_rank: Super Contributor
timestamp: February 18, 2015, 09:27:04 pm
content: 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.

author: SL4P
author_rank: Super Contributor
timestamp: February 18, 2015, 09:40:10 pm
content: ahh ok, so that should be a pretty straightforward state machine to code up.

author: elgonzo
author_rank: Unknown
timestamp: February 18, 2015, 09:45:50 pm
content: Quote from: Cloud on February 18, 2015, 09:15:52 pmYour 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) & Q2P.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

author: Cloud
author_rank: Regular Contributor
timestamp: February 18, 2015, 09:49:55 pm
content: Thank you very muchI'll try to work something out. Thank you for your time

author: elgonzo
author_rank: Unknown
timestamp: February 19, 2015, 01:30:16 am
content: Hehe, in my array example above i said i used hexadecimal notation, but then i wrote it in binary... and nobody complainedI fixed the error in my post above, it should be correct now. Sorry for the mistake

author: zapta
author_rank: Super Contributor
timestamp: February 19, 2015, 02:20:14 am
content: 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 10 1 0 1 0 0Look at the above rules from example, without distinct event detection your Q2 output will go back and forth between zero and one.

author: Cloud
author_rank: Regular Contributor
timestamp: February 20, 2015, 12:11:12 pm
content: Could this be done so that, after the uC receives signal ignores booth inputs for 1-2seconds?

author: elgonzo
author_rank: Unknown
timestamp: February 20, 2015, 12:26:50 pm
content: Quote from: Cloud on February 20, 2015, 12:11:12 pmIs there a problem with your program, or do you just want the user to prevent pressing buttons/switches quickly?

author: Wilksey
author_rank: Super Contributor
timestamp: February 20, 2015, 12:36:38 pm
content: Yes, with timers and flags

author: SL4P
author_rank: Super Contributor
timestamp: February 20, 2015, 08:02:49 pm
content: Quote from: Wilksey on February 20, 2015, 12:36:38 pmAs 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.