May 8, 2013 #1 H hemnath Advanced Member level 3 Joined Jun 24, 2012 Messages 702 Helped 61 Reputation 120 Reaction score 57 Trophy points 1,308 Location Chennai Visit site Activity points 6,589 I have 4 buttons connected to pic 18F2520. which are configured as inputs. I'm using CCS c compiler below is the routine i'm using now, HTML: if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 0)) // 0000 { variable = 0; } if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 1)) // 0001 { variable = 1; } like this ... goes upto if((input(PIN_A1) == 1) && (input(PIN_A2) == 1) && (input(PIN_A3) == 1) && (input(PIN_A4) == 1)) // 1111 { variable = 15; } The problem is, i have to type so many lines to check the input status and the program becomes very large. Is there any simple solution to read the input? Please help. Thanks in advance
I have 4 buttons connected to pic 18F2520. which are configured as inputs. I'm using CCS c compiler below is the routine i'm using now, HTML: if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 0)) // 0000 { variable = 0; } if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 1)) // 0001 { variable = 1; } like this ... goes upto if((input(PIN_A1) == 1) && (input(PIN_A2) == 1) && (input(PIN_A3) == 1) && (input(PIN_A4) == 1)) // 1111 { variable = 15; } The problem is, i have to type so many lines to check the input status and the program becomes very large. Is there any simple solution to read the input? Please help. Thanks in advance
May 8, 2013 #2 jayanth.devarayanadurga Banned Joined Dec 4, 2012 Messages 4,280 Helped 822 Reputation 1,654 Reaction score 791 Trophy points 1,393 Location Bangalore, India Visit site Activity points 0 #define to replace each button press? if PORTA = xxxx0000 if PORTA = xxxx0001 ... #define alloff {input(PIN_A1) && input(PIN_A2) && input(PIN_A3) && input(PIN_A4)};
#define to replace each button press? if PORTA = xxxx0000 if PORTA = xxxx0001 ... #define alloff {input(PIN_A1) && input(PIN_A2) && input(PIN_A3) && input(PIN_A4)};
May 8, 2013 #3 H hemnath Advanced Member level 3 Joined Jun 24, 2012 Messages 702 Helped 61 Reputation 120 Reaction score 57 Trophy points 1,308 Location Chennai Visit site Activity points 6,589 i've found a better solution, HTML: unsigned char variable[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; while(1) { a = (input_a() & 0b1111); value = variable[a]; output_b(value); } Looks so simple now Well any one know any other solution, please post. So that i can learn and it may help me.
i've found a better solution, HTML: unsigned char variable[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; while(1) { a = (input_a() & 0b1111); value = variable[a]; output_b(value); } Looks so simple now Well any one know any other solution, please post. So that i can learn and it may help me.
May 8, 2013 #4 jayanth.devarayanadurga Banned Joined Dec 4, 2012 Messages 4,280 Helped 822 Reputation 1,654 Reaction score 791 Trophy points 1,393 Location Bangalore, India Visit site Activity points 0 See my last post.
May 8, 2013 #5 betwixt Super Moderator Staff member Joined Jul 4, 2009 Messages 16,519 Helped 5,157 Reputation 10,347 Reaction score 5,211 Trophy points 1,393 Location Aberdyfi, West Wales, UK www.atv-projects.com Activity points 139,678 If the pins are all on the same port (I'm not sure of CCS syntax): if(port & 0x02) result |= 8; if(port & 0x04) result |= 4; if(port & 0x08) result |= 2; if(port & 0x10) result |= 1; Note that the result in the original code has it's bits reversed ! If they are in MSB -> LSB order the code is simpler: result = (port & 0x1E) >> 1; Brian.
If the pins are all on the same port (I'm not sure of CCS syntax): if(port & 0x02) result |= 8; if(port & 0x04) result |= 4; if(port & 0x08) result |= 2; if(port & 0x10) result |= 1; Note that the result in the original code has it's bits reversed ! If they are in MSB -> LSB order the code is simpler: result = (port & 0x1E) >> 1; Brian.