PDA

View Full Version : Variable to ports



MattCritt
- 4th April 2013, 16:29
I am sort of new using pic basic pro. I am learning it in school and i am working on a final project.

I am trying to take a variable and send it to several different parts simultaneously, i do not know if this is possible but my example is


would it work to:


'Initialize Variables


LED1 var byte
led1.0=porta.1
led1.1=portb.3
led1.2=portd.1



And then later in my code just write LED1=1 to turn on all 3 ports and LED1=0 to turn them off. would this work

thanks,

Demon
- 4th April 2013, 16:43
LED=%11111111 to turn all on
LED=%00000000 to turn all off (0 would work also)

Robert

HenrikOlsson
- 4th April 2013, 17:59
Hi,
No, you can't really do it like that.
You can't create an alias to individal pins "spanning" several ports like that - not that I know anyway.

And, another point:
When you say LED.1 = PortB.3 you assign the value of PortB.3 to LED.1 - that's definitely not what you want to do. To create aliases you use the VAR statement, just like you do when creating variables.

LED VAR PortB ' LED is now an alias of PortB

Pretty useless example but anyway....

/Henrik.

peterdeco1
- 4th April 2013, 18:46
Just a thought. Haven't tried it:

LED1 var byte
CLEAR
LET LED1 = (YOUR VARIABLE #)
GOTO READPORT

READPORT:
IF LED1.BIT0 = 1 THEN HIGH PORTA.1
IF LED1.BIT1 = 1 THEN HIGH PORTB.3
IF LED1.BIT2 = 1 THEN HIGH PORTD.1

Demon
- 4th April 2013, 20:27
Actually, there was a way posted a while ago. I just can't remember what to use as search string to find it.

Robert


EDIT: Here, read this:

http://www.picbasic.co.uk/forum/showthread.php?t=3753

HenrikOlsson
- 4th April 2013, 21:40
Hi,
Yes of course but then you are actually executing code which writes each bit to the individual port pins.
Each output is written sequentially, they are not all written simultaneously which the OP asked about.

But there's no way around that as far as I know.

/Henrik.

Archangel
- 11th April 2013, 21:39
here is an "exemplar" code you may glean some use of, it is for a pic16f690 demo board


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BOR_OFF & _FCMEN_OFF & _IESO_OFF
DEFINE OSC 4
I VAR BYTE
LED var byte
TEMP var WORD
OVERFLOW VAR WORD
Relays VAR BYTE : Relays = 0

'DEFINE ADC_BITS 10 ' Set A/D for 8-bit operation
'DEFINE ADC_CLOCK 3 ' Set A/D clock Fosc/8
'DEFINE ADC_SAMPLEUS 50 ' Set A/D sampling time @ 50 uS


temp = 0
OVERFLOW = 0
serout porta.0,2, [$FE,1,"Loops ",# temp," "]
ADCON1 = 0
ANSEL = 0
portc = %00000000
TRISC = %00000000
;LOW portB.7

;MyPort var byte
main:

'ADCIN 0, TEMP

myPort var bit[8]
Portc.0 = MyPort[0]
Portc.1 = myPort[1]
Portc.2 = myPort[2]
Portc.3 = myPort[3]
Porta.1 = myPort[4]
Porta.2 = myPort[5]
Porta.3 = myPort[6]
PortB.7 = myPort[7]
'random temp
'myPort = %00000000
TrisA = %00000000
TrisB = %00000000
Trisc = %00000000
'FOR led = 0 to 4 + 1


pattern var byte
index var byte
'Pattern = %11111111

'Pattern = %00001111
FOR index = 0 to 19
;MYpORT = MYpORT + 1
LOOKUP index,[%00000000,%00000001,%00000010,%00000100,%00001000, %00010000,%00100000,%01000000,%10000000,%00000101, %11111010,%00000101,%11111010,%00001001,%00000110, %11111111,%11111001,%11111111], pattern
'pattern = (pattern >>1)
portc = ~~Pattern 'turn portb.0 on or off based on whether the bit is 0 or 1
;Portc.0 = MyPort[0]
;Portc.1 = myPort[1]
;Portc.2 = myPort[2]
;Portc.3 = myPort[3]
;Porta.1 = myPort[4]
;Porta.2 = myPort[5]
;Porta.3 = myPort[6]
;PortB.7 = myPort[7]
;2 bitwise NOTS 1st not changes T/F to Pattern and second inverts value
;PORTC = Pattern is a T/F statement the ( ! ) changes it to ENGLISH :D
PAUSE 300
NEXT index

temp = temp + 1


serout porta.0,2, ["Loop ",# temp," "]
IF TEMP >= 65535 THEN
OVERFLOW = OVERFLOW + 1
IF OVERFLOW >=1 THEN SEROUT PORTA.0,2,["OVERFLOW ",#OVERFLOW," "]
PAUSE 5000
ENDIF
'TEMP = TEMP / 5
'pause temp MAX 2
'portc = portc << 1

'TRISC = I
'next led


'FOR led = 0 to 3 + 1
'pause temp
'portc = portc >> 1

'next led

goto main

end


The code listed above has lots of unimportant stuff in it as I use it to test snippits the important part is portc = ~~Pattern 'turn portb.0 on or off based on whether the bit is 0 or 1