View previous topic :: View next topic |
Author |
Message |
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
How can i make here an subroutine to reduce code size? |
Posted: Mon Feb 27, 2006 7:35 am |
|
|
Code: | while(TRUE) {
k=kbd_getc();
switch(k) {
case '1' : if(toggle1)
output_low(RELAIS_K1);
else
output_high(RELAIS_K1);
toggle1 =! toggle1;
break;
case '2' : if(toggle2)
output_low(RELAIS_K2);
else
output_high(RELAIS_K2);
toggle2 =! toggle2;
break;
case '3' : if(toggle3)
output_low(RELAIS_K3);
else
output_high(RELAIS_K3);
toggle3 =! toggle3;
break;
case '4' : if(toggle4)
output_low(RELAIS_K4);
else
output_high(RELAIS_K4);
toggle4 =! toggle4;
break;
}
}
}
|
|
|
|
Alex N
Joined: 10 Feb 2006 Posts: 16
|
|
Posted: Mon Feb 27, 2006 7:48 am |
|
|
Try to use
Code: |
case 'x':
output_bit(RELAIS_Kx,!input(RELAIS_Kx));
break;
|
where 1, 2, 3 and 4 should be instead of x. |
|
|
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
|
Posted: Mon Feb 27, 2006 8:04 am |
|
|
This did not toggle the output pin
If i press the 1 key the output goes low, after i press the 1 key it will be low
It must go to high state |
|
|
Alex N
Joined: 10 Feb 2006 Posts: 16
|
|
Posted: Mon Feb 27, 2006 8:17 am |
|
|
[spam], if you press the 1 key and RELAIS_K1 is low, !input(RELAIS_K1) is high, which is placed to RELAIS_K1. Pay attention to !input. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Feb 27, 2006 8:21 am |
|
|
Check to see if your compiler supports the output_toggle() function.
output_toggle(pin) where pin = PORT_A3 pin names
If not, XOR (~), the #BYTE and #BIT directives are your friends.
Code: | #byte LATCHA = 0xF89 //18F4520, your mileage may vary
#bit RELAY_K3 = LATCHA.1
RELAY_K3 = 1; // on
RELAY_K3 = 0; // off
RELAY_K3 ~= 1; // toggle |
_________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
|
Posted: Mon Feb 27, 2006 8:22 am |
|
|
This works
output_toggle(RELAIS_K1);
Thx for help
Last edited by The Puma on Mon Feb 27, 2006 8:51 am; edited 1 time in total |
|
|
Alex N
Joined: 10 Feb 2006 Posts: 16
|
|
Posted: Mon Feb 27, 2006 8:32 am |
|
|
Rwyoung, thank you for the useful information. It turned out, that my compiler (as opposed to the help index) also supports output_toggle function. |
|
|
|