Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: February 05, 2013, 10:52:30 am » |
So i got the binary counter working. Now I just have to take and input (Load = HIGH) and if i turn on count it will continue counting from there..,
So i was thinking that i was thinking that I would take the 4 bit input then turn it to decimal and give the value to count.. is there anyway to do this?
I was thinking putting the HIGH or LOW values in an array or concatenate each input value and convert it to decimal.
If so how to convert from int to string, string to bit and then from string to int.
|
|
|
|
« Last Edit: February 05, 2013, 04:42:59 pm by iwanna4get »
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 14
Posts: 716
Big things come in large packages
|
 |
« Reply #1 on: February 05, 2013, 09:14:02 pm » |
Maybe it is me, but I have no idea what you are talking about.
Can you post your circuit and any code you may have so we can see what you are trying to do. I have a feeling that you are overcomplicating something here.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25566
Solder is electric glue
|
 |
« Reply #2 on: February 06, 2013, 03:21:53 am » |
No it is not just you, no idea what the problem is. I do know you should not be using a string. I suspect it might be a four digit counter and not a four bit one. Please supply more information about what you have and what you want to do with it and why.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #3 on: February 06, 2013, 05:51:16 am » |
Ok first I got a binary counter program in arduino (with the help of some arduino topics), The Code(I'm pretty sure your familiar with this): const int led[4] = {2,3,4,5};
int pin; int counting;
void setup()
{ for (pin =0; pin < 4; pin++ ) { pinMode (led[pin], OUTPUT); } }
void loop()
{ for ( counting = 0; counting < 16; counting++) { LEDview(); delay (1000); // The One second delay } }
void LEDview()
{ digitalWrite (led[3], boolean ((counting & 8)>>3)); digitalWrite (led[2], boolean ((counting & 4)>>2)); digitalWrite (led[1], boolean ((counting & 2)>>1)); digitalWrite (led[0], boolean (counting & 1)); } Ok now i will add 7 inputs : bit0 bit1 bit2 bit3 bits 0-3 will represent a four binary bit input Load Count CLR if load is high digital read the values of bit0-3 and output them on output (which is the led[4] in the program) Now if we turn load to low , maintain the value last known. if count is high, count from the last value (example: last input 5(decimal) - 0101(binary) and then i turn count on it will continue binary counting from 5(0101)) if clear is high all output is low Oh and this is the circuit in which I am basing the program on. http://www.expertcore.org/viewtopic.php?style=4&f=27&t=783
|
|
|
|
« Last Edit: February 06, 2013, 05:56:38 am by iwanna4get »
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25566
Solder is electric glue
|
 |
« Reply #4 on: February 06, 2013, 01:03:32 pm » |
Oh and this is the circuit in which I am basing the program on. So it is not a real circuit but an FPGA. Now if we turn load to low , maintain the value last known. if count is high, count from the last value (example: last input 5(decimal) - 0101(binary) and then i turn count on it will continue binary counting from 5(0101)) if clear is high all output is low There is nothing in that code that does anything with the circuit. All it does is output your count in binary to the 4 outputs. I am still confused as to why you need an external 4 bit counter at all. Simply output the count to LEDs attached to the pin outputs. If you want to clock that into the counter then pulse the load line at the end of the LEDview() function.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #5 on: February 07, 2013, 09:16:07 am » |
This is what I got. I haven't tested it out cause it's late and I don't have enough switches at the moment int in1 = 2; int in2 = 3; int in3 = 4; int in4 = 5; int LD = 6; int CNT = 7; int CLR = 8; int out1 = 9; int out2 = 10; int out3 = 11; int out4 = 12; int result = 0; int counting = 0; int counting2 = 0;
void setup(){ pinMode (in1, INPUT); pinMode (in2, INPUT); pinMode (in3, INPUT); pinMode (in4, INPUT); pinMode (LD, INPUT); pinMode (CNT, INPUT); pinMode (CLR, INPUT); pinMode (out1, OUTPUT); pinMode (out2, OUTPUT); pinMode (out3, OUTPUT); pinMode (out4, OUTPUT); }
void loop(){ while(CLR = 0){ if(LD = 1){ int j = digitalRead(in1); int k = digitalRead(in2); int l = digitalRead(in3); int m = digitalRead(in4); digitalWrite (out1, j); digitalWrite (out2, k); digitalWrite (out3, l); digitalWrite (out4, m); counting2 = LOAD(j, k, l, m); }else if(CNT = 1){ COUNT(); }else{ digitalWrite (out1, LOW); digitalWrite (out2, HIGH); digitalWrite (out3, HIGH); digitalWrite (out4, LOW); } }
}
int LOAD (int a, int b, int c, int d){ int sum = 0; if(a = 1){ sum = sum + 1; }else{ sum = sum; } if(b = 1){ sum = sum + 2; }else{ sum = sum; } if(c = 1){ sum = sum + 4; }else{ sum = sum; } if(d = 1){ sum = sum + 8; }else{ sum = sum; } return sum; }
void COUNT(){ for ( counting = counting2; counting < 16; counting++) { LEDview(); delay (1000); // The One second delay } } void LEDview(){ digitalWrite (out4, boolean ((counting & 8)>>3)); digitalWrite (out3, boolean ((counting & 4)>>2)); digitalWrite (out2, boolean ((counting & 2)>>1)); digitalWrite (out1, boolean (counting & 1)); }
void CLEAR(){ digitalWrite(out4, LOW); digitalWrite(out3, LOW); digitalWrite(out2, LOW); digitalWrite(out1, LOW); }
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25566
Solder is electric glue
|
 |
« Reply #6 on: February 07, 2013, 09:47:56 am » |
Well I can't follow what you are trying to do but one thing to watch is all those if statements with only a single equals sign in then are wrong. You need a double equals if you are going to compare things. Rather than just presenting the code it also helps if you say what it is trying to accomplish.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #7 on: February 07, 2013, 06:55:15 pm » |
I was sure those single = signs were double (==) yesterday ._. must have been more tired than I thought /* set the pins */ int in1 = 2; int in2 = 3; int in3 = 4; int in4 = 5; int LD = 6; int CNT = 7; int CLR = 8; int out1 = 9; int out2 = 10; int out3 = 11; int out4 = 12; int result = 0; int counting = 0; int counting2 = 0;
void setup(){ pinMode (in1, INPUT); pinMode (in2, INPUT); pinMode (in3, INPUT); pinMode (in4, INPUT); pinMode (LD, INPUT); pinMode (CNT, INPUT); pinMode (CLR, INPUT); pinMode (out1, OUTPUT); pinMode (out2, OUTPUT); pinMode (out3, OUTPUT); pinMode (out4, OUTPUT); }
void loop(){ while(CLR == 0){ /* while CLR is LOW do this */ /* if LOAD is HIGH read the inputs from in1-4 and give it to out1-4*/ if(LD == 1){ int j = digitalRead(in1); int k = digitalRead(in2); int l = digitalRead(in3); int m = digitalRead(in4); digitalWrite (out1, j); digitalWrite (out2, k); digitalWrite (out3, l); digitalWrite (out4, m); /*Get the value for count*/ counting2 = LOAD(j, k, l, m); }else if(CNT == 1){ /* if count is HIGH do COUNT()*/ COUNT(); }else{ /*for error checking */ digitalWrite (out1, LOW); digitalWrite (out2, HIGH); digitalWrite (out3, HIGH); digitalWrite (out4, LOW); } } /*when CLR is HIGH*/ digitalWrite(out4, LOW); digitalWrite(out3, LOW); digitalWrite(out2, LOW); digitalWrite(out1, LOW);
}
/* this function will compute for the decimal value of the 4 bit input and give it to sum*/ int LOAD (int a, int b, int c, int d){ int sum = 0; if(a = 1){ sum = sum + 1; }else{ sum = sum; } if(b = 1){ sum = sum + 2; }else{ sum = sum; } if(c = 1){ sum = sum + 4; }else{ sum = sum; } if(d = 1){ sum = sum + 8; }else{ sum = sum; } return sum; }
void COUNT(){ /*this is the binary counter program. counting is set to be = to counting2 since we would like to start from the last know data*/ for ( counting = counting2; counting < 16; counting++) { LEDview(); delay (1000); // The One second delay } } void LEDview(){ digitalWrite (out4, boolean ((counting & 8)>>3)); digitalWrite (out3, boolean ((counting & 4)>>2)); digitalWrite (out2, boolean ((counting & 2)>>1)); digitalWrite (out1, boolean (counting & 1)); }
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25566
Solder is electric glue
|
 |
« Reply #8 on: February 08, 2013, 03:13:43 pm » |
You are still using single = in that last code you posted /* this function will compute for the decimal value of the 4 bit input and give it to sum*/ int LOAD (int a, int b, int c, int d){ int sum = 0; if(a = 1){ sum = sum + 1; }else{ sum = sum; } if(b = 1){ sum = sum + 2; }else{ sum = sum; } if(c = 1){ sum = sum + 4; }else{ sum = sum; } if(d = 1){ sum = sum + 8; }else{ sum = sum; } return sum; } However that code is rubbish anyway, try /* this function will compute for the decimal value of the 4 bit input and give it to sum*/ int LOAD (int a, int b, int c, int d){ int sum = d<<3 | c<<2 | b<<1 | a; return sum;
Or if you don't understand that try /* this function will compute for the decimal value of the 4 bit input and give it to sum*/ int LOAD (int a, int b, int c, int d){ int sum = d*8 + c*4 + b*2 + a; return sum;
Hint:- anything multiplied by zero is zero, no IFs are needed I am still waiting for an explanation about what you think the code is doing.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #9 on: February 08, 2013, 08:29:00 pm » |
I have this circuit and gonna turn it to code  /* this function will compute for the decimal value of the 4 bit input and give it to sum*/ int LOAD (int a, int b, int c, int d){ int sum = d*8 + c*4 + b*2 + a; return sum;
Hint:- anything multiplied by zero is zero, no IFs are needed And Thanks Grumpy Mike, you are so right about the LOAD function
|
|
|
|
« Last Edit: February 08, 2013, 08:32:20 pm by iwanna4get »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #10 on: February 09, 2013, 12:14:40 am » |
Finally got it to work and do what I wanted it to do. Here's the code. (It's too long I know there is probably a way to make this shorter) int LD = 6; int swtch = 7; int CLR = 8; const int led[4] = {9,10,11,12}; const int in[4] = {2,3,4,5}; int counting, counting2, pin, x, y, z, h, j, k, l ;
void setup(){ for (pin =0; pin < 4; pin++ ) { pinMode (led[pin], OUTPUT); } for (pin =0; pin < 4; pin++ ) { pinMode (in[pin], INPUT); } pinMode(swtch, INPUT); pinMode(CLR,INPUT); pinMode(LD, INPUT); } void loop(){ x = digitalRead(swtch); y = digitalRead(CLR); z = digitalRead(LD); h = digitalRead(in[0]); j = digitalRead(in[1]); k = digitalRead(in[2]); l = digitalRead(in[3]); while(x==1){ for ( counting = counting2; counting < 16; counting++) { x = digitalRead(swtch); if(x==1){ LEDview(); delay(1000); }else{ break; } if (counting2 == 15){ counting2 = 0; } else{ counting2++; } } } if(y == 1){ CLEAR(); counting2 = 0; }else if(z == 1){ digitalWrite(led[0], h); digitalWrite(led[1], j); digitalWrite(led[2], k); digitalWrite(led[3], l); counting2 = LOAD(h,j,k,l); } }
void LEDview() { digitalWrite (led[3], boolean ((counting & 8)>>3)); digitalWrite (led[2], boolean ((counting & 4)>>2)); digitalWrite (led[1], boolean ((counting & 2)>>1)); digitalWrite (led[0], boolean (counting & 1)); }
int LOAD (int a, int b, int c, int d){ int sum = a*1 + b*2 + c*4 + d*8; return sum; }
void CLEAR() { for (pin =0; pin < 4; pin++ ) { digitalWrite(led[pin], LOW); } }
|
|
|
|
« Last Edit: February 09, 2013, 12:28:47 am by iwanna4get »
|
Logged
|
|
|
|
|
Cumming, Ga
Offline
Edison Member
Karma: 12
Posts: 1389
Ultimate DIY: Arduino
|
 |
« Reply #11 on: February 12, 2013, 06:43:32 am » |
And... isn't that FPGA design damn close to a BCD preset 74HC192 counter?
|
|
|
|
|
Logged
|
|
|
|
|
Anaheim CA.
Offline
Edison Member
Karma: 31
Posts: 2311
Experienced old Whitebeard with a Full head of Hair...
|
 |
« Reply #12 on: February 12, 2013, 09:46:22 am » |
@pwillard.. You got a new crystal ball?
|
|
|
|
|
Logged
|
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
|
|
|
|
|