Help with switch button

Status
Not open for further replies.

peterzatko

New Member
I m begginer i need help.
I need to LED lights turn on and turn off after pressing the button. This means that a push button and LED is turned on first A push button and switches off LED1. This is necessary for all LED and buttons.


program LED_GENERATOR
' Declarations section

symbol LED1 = PORTA.3
symbol SWT1 = PORTA.1
symbol LED2 = PORTA.2
symbol SWT2 = PORTA.0
symbol LED3 = PORTA.7
symbol SWT3 = PORTA.4
symbol LED4 = PORTA.6
symbol SWT4 = PORTA.5
symbol LED5 = PORTB.0
symbol SWT5 = PORTB.5
symbol LED6 = PORTB.1
symbol SWT6 = PORTB.4
symbol LED7 = PORTB.2
symbol SWT7 = PORTB.6
symbol LED8 = PORTB.3
symbol SWT8 = PORTB.7

main:
TRISA = %00110011
PORTA = %00000000
TRISB = %11110000
PORTB = %00000000
' Main program

cmcon =%00000111 'Comparators Off
vrcon =%00000000
intcon =%00000000

loop:

if SWT1 = 0 And (LED1 = 1)then LED1 = 0

end if

if (SWT2 = 0 ) And (LED2 = 0)then LED2 = 1

end if

if (SWT3 = 0 ) And (LED3 = 0)then LED3 = 1

end if

if (SWT4 = 0 ) And (LED4 = 0)then LED4 = 1

end if

if (SWT5 = 0 ) And (LED5 = 0)then LED5 = 1

end if

if (SWT6 = 0 ) And (LED6 = 0)then LED6 = 1

end if

if (SWT7 = 0 ) And (LED7 = 0)then LED7 = 1

end if

if (SWT8 = 0 ) And (LED8 = 0)then LED8 = 1

end if

goto loop

end.
 

Attachments

  • LED.JPG
    168.9 KB · Views: 138
You would likely get good responses posting this in the uController section(s) of the forums. Try a PM to a mod and ask them about moving your thread.

Ron
 
Has the basic compiler a "toggle" keyword?

if so just use:

Code:
   if swt1 = 0 then toggle LED1
   if swt2 = 0 then toggle LED2
   '... etc

Make sure you have debounced you switches. I use about 250ms ( actually I make a beep )
 
You could use a switch state latch and parallel switch state logic to detect the "new press" state while ignoring the other switch states (no more waiting for a switch to be released). Then toggle flag bits (from on-to-off or from off-to-on) for each "new press" state.

The example below shows this toggle switch emulation using eight I/O pins for both switches and LEDs;

Regards...

Code:
; /*  Lighted Push Button Switches (emulated toggle switches)
;  *
;  *  swnew  ____---____-----___    sample active lo switches
;  *  swold  _____---____-----__    switch state latch
;  *  delta  ____-__-___-____-__    changes, press or release
;  *  newhi  ____-______-_______    filter out 'release' bits
;  *  flags  _____-------_____--    toggle flag bits for main
;  *  trisb  -----_______-----__    toggle tris bits for LEDs
;  */
;     while(1)
;     { delay_ms(25);            // 25 msec sample intervals
;       trisb = 0xFF;            // set PORTB to inputs
;       swnew = ~portb;          // sample active lo switches
;       swnew ^= swold;          // changes, press or release
;       swold ^= swnew;          // update switch state latch
;       swnew &= swold;          // filter out 'release' bits
;       flags ^= swnew;          // toggle flags bits for main
;       portb = 0;               // keep PORTB latch bits = '0'
;       trisb ^= flags;          // light only "active" LEDs
;     }
 

Attachments

  • eto example.png
    10.3 KB · Views: 147
Last edited:
Mike.. he's using basic... Bitwise may be a bit difficult.
 
^= , &= ,= ~ aren't available in basic.... Bitwise operators are pretty exclusive to C
 
In Oshonsoft Basic which I just had a look those operators Ian posted are used with the below method

Mathematical and logical operations
Mod, Sqr, Sqrt, Log, Exp, Sin, Cos, Not, And, Or, Xor, Nand, Nor, Nxor, High, Low, Toggle,

The +,-,*,/ etc are still used though so by using them and the above operators Mike's routine could be used once converted.

Regards Bryan
 
Granted..... But as I said the OP was working in basic... How are we to assume that " swnew ^= swold; " is known to someone who uses basic. It's C shorthand... We are here trying to help.

P.S. I use Oshonsoft myself.... Perhaps I didn't get the point across.
 
Last edited:
^= , &= ,= ~ aren't available in basic.... Bitwise operators are pretty exclusive to C

Are you sure about that Ian? When I was playing with Swordfish BASIC several years ago, it had all those operators (XOR, AND, OR, etc.)...

You're right. I apologize. It may be a big leap to go from swnew ^= swold in C to swnew = swnew XOR swold in BASIC.
 
Last edited:
I have never used swordfish basic... But I have never come across a basic that uses the C style shorthand syntax eg... ++, --, += etc...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…