• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Stuck in programming

Status
Not open for further replies.

vxr

New Member
i have 3 switches SW1, SW2,SW3

starting always from SW1 in need the pic to know which is pressed because dependanct on the switch pressed i need to send eg pulses for example
Starting from SW1
if Sw1 is pressed nothing happens
if SW2 is pressed i need to send 1 pulse
if sw3 is pressed i need to send 2 pulses
 

kinjalgp

Active Member
Since you are doing nothing with SW1, you don't need to scan it.
Assuming you have connected SW1, SW2 & SW3 to Ports pin D0, D1 and D2 respectively with active low logic (with entire port pulled high and common switch terminals connected to GND), check for following data on Port.

Code:
If Port Value = 0xFD then ; For SW2
       Send one pulse
Elseif Port Value = 0xFB then ; For SW3
       Send two pulses
End
 

vxr

New Member
all switches are used so you have 3 options it can be Either SW1 or SW2 or SW3..

The Problem i am facing each switch turns on a Video source on a screen the screen starts by default at Video Source 1.

to change the Video Source i used a relay to switch on the source switch, but the screen changes the video sources consecutivly. Video source 1 then video source 2 then video source 3.

lets say we start from the beginning when the video source is 1, if SW3 is pressed we have to let the relay close, open, close then open to get to Video Source 3. if now SW1 is presed the relay has to close then open only.


i hope you understand what i mean..... another thing i am using assembly language if you can please help....

thanks
 

kinjalgp

Active Member
I don't understand why do you want to check for SW1 when you don't want to do anything with it. You have not mentioned which microcontroller you are using. I have shown a simple program of what you want in generic 8051 assembly language. It is simple to port it to any other assembly language.

Code:
; ASSUMPTIONS:
; D0 OF PORT1 = SW1
; D1 OF PORT1 = SW2
; D2 OF PORT1 = SW3
; KEY IS A VARIABLE IN WHICH YOU STORE PORT DATA

.EQU KEY, 0x20                             ; DEFINITION OF 'KEY' VARIABLE

MAIN:                                      ; PROGRAM ENTRY POINT
     LCALL SCAN_KEY                        ; CHEK FOR KEY PRESS
     LCALL EXECUTE_KEY                     ; IF KEY IS PRESSED CHECK WHICH ONE IS THAT
     SJMP MAIN                             ; LOOP BACK TO MAIN

SCAN_KEY:
     MOV A, P1                             ; PORT1 DATA TO ACC.
     CJNE A, #0xFF, SCAN_KEY               ; LOOP HERE TILL A KEY IS PRESSED
     MOV KEY, A                            ; THEN MOVE ACC. DATA TO KEY VAR.
     RET                                   ; RETURN TO CALLER

EXECUTE_KEY:
     MOV A, KEY                            ; MOVE KEY VARIABLE TO ACC.
     CJNE A, #0xFE, CHECK_SW2              ; CHECK IF IT IS SW1 ELSE CHECK FOR SW2
     ;DO NOTHING
     SJMP END_OF_EXECUTE_KEY               ; GOTO END OF SUBROUTINE
CHECK_SW2:
     CJNE A, #0xFD, CHECK_SW3              ; CHECK IF IT IS SW2 ELSE CHECK FOR SW3
     LCALL SEND_ONE_PULSE                  ; SEND ONE PULSE
     SJMP END_OF_EXECUTE_KEY               ; GOTO END OF SUBROUTINE
CHECK_SW3:
     CJNE A, #0xFB, END_OF_EXECUTE_KEY     ; IF NOT SW3 THEN RETURN TO CALLER
     LCALL SEND_TWO_PULSES                 ; SEND TWO PULSES
END_OF_EXECUTE_KEY:
     RET                                   ; RETURN TO CALLER
 

vxr

New Member
- The microcontroller is PIC16F84

- The 3 switches SW1, SW2 and SW3 choose between Video Source1, Video Source2 and Video Source3 respectivly. So if Video Source1 is required SW1 has to be pressed

- The actual problem i am facing is:
all this would be connected to a LCD screen, the screen has a source button which when pressed once will change Video source input from 1 to 2 pressed again would change from 2 to 3 and so on. i want to build a circuit having the PIC16F84 were PORT A is Input and PORT B is Output.

RA0 is connected to SW1
RA1 is connected to SW2
RA3 is Connected to SW3

RB0 to RB5 are connected to LEDs
RB6 is connected to a relay which has the output connected to LCD screen source switch
RB7 is connected to a relay which switches between 2 Audio Source inputs

*Video Source1 and Video Source3 Use Audio Source1
*Video Source2 use Audio Source2

* as soon as the LCD is Powered it displays Video Source 1 and Audio Source 1 (may be this is were you think there is no need to scan input from SW1)

lets say we powered the LCD now Video Source1 and Audio Source1 are active. now i want to watch Video Source 3 so i would need to press SW3
the PIC16F84 has to latch the relay at RB6 twice which is like pushing the LCD source Switch 2 times also latch the relay at RB7 once to change the Sound Source, but what if i press SW3 again the PIC should not do anything, or what if i press SW1 now the PIC must Latch the realy at RB6 once and the relay at RB7 once.
also when the same switch is pressed the PIC should do nothing because the Video Source input has to go 3 latch cycles to reach the same source
eg. current video source is 2 if SW2 is pressed nothing should happen because them the PIC has to change the video source from 2 to 3 then from 3 to 1 then from 1 to 2, also the same for the audio but there the latch cycle is 2 because there are only 2 inputs either input 1 or input 2

so what i really need is a way for the PIC to know which switch was last pressed and what is the new switch pressed and what actions are required for that specific switch.

i hope you understand what i want i am using asembly language for this
learnt how to read if the switches are SWitched ie. high or low but from there i am lost i tried to figure a way but could not suceed.


Thanks for your help
 

kinjalgp

Active Member
Now here you are doing some unnecessary things.
1) Since three switches are used for 3 relays, you don't need to bother about which switch was pressed last time. When SW2 is pressed, RELAY 2 will turn on. Even if it is turned on and you make logic one to its port pin, nothing will happend either to realy or to your video singal i.e no interruptions. The pin was at logic 1 and will remain 1. Thats it!.
2) The cycling which you are talking about is generally done when you have only one switch to switch between multiple sources. Here you have 3 switches so no cycling is required. So just forget about the cycling part.

And regarding, PIC assembly, I am not that familiar with PICs. But you can definitely convert above 8051 program to PIC assembly.

And..... inspite of this, if you want to check what key was pressed last time, declare another variable called "OLD_KEY" and in the beginning of KEY_SCAN routine, transfer the contents of KEY variable to OLD_KEY variable. Thats it. The OLD_KEY will now contain the value of last key. Now you can do whatever processing on OLD_KEY as required.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top