PDA

View Full Version : using same pin for multiple purposes



aahuja888
- 10th April 2014, 14:53
ADCON1 = 000111
TRISA = %111111
TRISB = %00000000
TRISC = %00000000
PORTC = %00000000
PORTB = %00000000
PORTA = %0000000
'***********************************************
#CONFIG
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF
#ENDCONFIG
'***********************************************
Symbol PBUP = PORTA.0
SYMBOL PBDN = PORTA.1
symbol MCB = PORTA.2
SYMBOL S1 = PORTA.3
'***************************
SEQ VAR BYTE
LED1 VAR PORTC.0
LED2 VAR PORTC.1
LED3 VAR PORTC.2
LED4 VAR PORTC.3
LED5 VAR PORTC.4
LED6 VAR PORTC.5
LED7 VAR PORTC.6
LED8 VAR PORTC.7
b1 var byte
'********************
SEQ = 0
'CMCON = 7
'ADCON1 = %0
'***************************************




' MCB=1
'LOOP4:
' WHILE(mcb!=0)
' PBUP=0
' PBDN=0
' WEND

'AGAIN:
'IF MCB=0 THEN PORTA=0 : PORTB=0: PORTC=0
'IF MCB=1 THEN PORTA=1 : PORTB=1 : PORTC=1


'GOTO MAINLOOP
'GOTO LOOP4

FOR MCB=0 TO 1 STEP 1
PORTB=0
PORTC=0
MCB=MCB-1
NEXT

FOR MCB= 1 TO 0 STEP -1
PORTB=1
PORTC=1
MCB=MCB+1
NEXT





MAINLOOP:

IF PBUP = 0 THEN GOSUB UPP
IF PBDN = 0 THEN GOSUB DNN
PAUSE 200
GOSUB ACTIONS
GOTO MAINLOOP

'**************************
UPP:
IF SEQ = 9 THEN RRT
SEQ = SEQ + 1
PAUSE 10
GOTO RRT
DNN:
IF SEQ = 0 THEN RRT
SEQ = SEQ -1
PAUSE 20
RRT : RETURN

'****************************************
ACTIONS:
IF seq =0 THEN
LED1=0:LED2=0:LED3=0:LED4=0:LED5=0:LED6=0:LED7=0
PORTB=%01111110
ENDIF


IF SEQ = 1 THEN
LED1 = 1 : LED2 = 0 : LED3 = 0 : LED4 = 0 : LED5 = 0 : LED6 = 0 :
LED7 = 0
PORTB=%00001100
ENDIF


IF SEQ = 2 THEN
LED1 =0 : LED2 = 1 : LED3 = 0 : LED4 = 0 : LED5 = 0 : LED6 = 0 :
LED7 = 0
PORTB=%10110110
ENDIF

IF SEQ = 3 THEN
LED1 = 0 : LED2 = 0 : LED3 = 1 : LED4 = 0 : LED5 = 0 : LED6 = 0 :
LED7 = 0
PORTB=%10011111
ENDIF


IF SEQ = 4 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 1 :LED5 = 0 :LED6 = 0 :
LED7 = 0
PORTB=%11001100
ENDIF


IF SEQ = 5 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 1 :LED6 = 0 :
LED7 = 0
PORTB=%11011010
ENDIF

IF SEQ = 6 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 1 :
LED7 = 0
PORTB=%11111010
ENDIF

IF SEQ = 7 THEN
LED1 = 0 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 0 :
LED7 = 1
PORTB=%00001110
ENDIF

IF SEQ = 8 THEN
LED1 = 1 :LED2 = 0 :LED3 = 0 :LED4 = 0 :LED5 = 1 :LED6 = 0 :
LED7 = 1
PORTB=%11111111
ENDIF

IF SEQ = 9 THEN
LED1 = 0:LED2 = 1 :LED3 = 0 :LED4 = 0 :LED5 = 0 :LED6 = 1 :
LED7 = 0
PORTB=%11011110

ENDIF
PAUSE 100
return

HOW TO USE MCB AS ENABLE AS WELL AS DISABLE?

tasmod
- 10th April 2014, 15:36
From the manual.

INPUT Make pin an input.
OUTPUT Make pin an output

Also PBP convention:-

MCB VAR PORTA.2

Symbol is Stamp compatibility, not that it really matters if you so wish. :)

You also have missing parameters in first lines for ADCON, TRIS etc either % or 0 or 1's

Demon
- 11th April 2014, 13:12
FOR MCB= 1 TO 0 STEP -1
PORTB=1
PORTC=1
MCB=MCB+1
NEXT


At first glance, this will run forever.

But, MCB is a bit; it can be 0 or 1. What happens first time through the loop when 1 is added to MCB when it is 1?

Robert

Amoque
- 11th April 2014, 14:32
In a loop (for/ next in this case) MCB will increment/ decrement by the step value automatically - no need to add or subtract the value. As:

For LP = 0 to -1 step -1
NOP
Next LP

LP will be set to 0 the first time through, "loop" and set the value of LP to -1, repeat the enclosed code, and then exit because the "to" condition (LP= -1) is met. Manipulating LP manually is not necessary and only bungs up the sequence. In the case where you subtract 1 from MCB: The initial loop starts with LP=0, in the code you reduce MCB to -1 (by subtracting 1), so the exit condition is met and the "loop" fails to repeat. NONE of the looping code is ever executed with LP equal to -1.