News:

;) This forum is the property of Proton software developers

Main Menu

Re assigning symbols

Started by Parmin, Apr 08, 2021, 05:18 AM

Previous topic - Next topic

Parmin

Not sure what to call this
 Can you reassign symbols?

Say if you want to direct an output pulse to a different pin based on an input
 
 Symbol Dog = PortA.0
 Symbol Cat = PortA.1


 If Door1 = 1 then
  Symbol foodOut = Dog
 endif

 if Door1 = 0 then
  Symbol foodOut = Cat
 endif

Stephen Moss

Les will be the ultimate authority on this but I don't think you can as I believe the compiler process the symbols first, if so then I would think that foodOut would be fixed to the last Symbol definition (in this case Cat).

However, I am assuming from your code that foodOut your output pulse in which case you do not need the Symbol command in your If-Then statements as Cat and Dog are just a meaning full terms for the respective port pins.
Symbol Dog = PortA.0
Symbol Cat = PortA.1

 If Door1 = 1 then
  Dog = foodOut   'foodOut sent to PortA.0
 endif

 if Door1 = 0 then
  Cat = foodOut   'foodOut sent to PortA.1
 endif
Although it is not clear from your post what you mean by a pulse and what its source, without knowing that it may not be possible to do what you want this way.

That said for a beginner keeping everything separated may make things easier to keep track of as it breaks everything down into individual chunks but you could simplify you could the above code with an Else statement, the effect is the same it just looks a little neater, i.e.
Symbol Dog = PortA.0
Symbol Cat = PortA.1

 If Door1 = 1 then
  Dog = foodOut   'foodOut sent to PortA.0
 else             'otherwise (Door = 0)
  Cat = foodOut   'foodOut sent to PortA.1
 endif

top204

#2
The Symbol directive only operates at compile-time, and creates a constant value or alias, that cannot be changed.

For example, if the line "Symbol MyPin = PORTA.0" is used, the compiler's parser will see "PORTA.0" wherever the text "MyPin" is present in the code, the same with a constant value: "Symbol MyValue = 100", the compiler's parser will see the value "100" wherever the text "MyValue" is present in the code. This is what is meant by "compile-time". The same when the Dim directive is used to create a constant or alias.

Stephen is correct, in that a condition can be used, or you can use the Input, Output, High, Low, PinSet, PinClear commands and use a variable to hold the pin names; Pin_A0, Pin_A1, Pin_B3 etc... See the manual's Input, Output, High, Low, PinSet, PinClear sections, but it will still need a variable to hold the Pin in question because they operate at "run-time".

Gabi

try:

$define Dog Bsf LATA,0,0
$define Cat Bsf LATA,1,0
$define ClearOutput Clrf LATA,0     ' or change to selective clear
$define pulse 100                   ' pulse period (ms)

 Dim Door1 As PORTB.0               ' input test pin
 
 Proc doPulse(in As Byte)
      If in = 0 Then
         Dog
      Else
         Cat
      EndIf
      DelayMS(pulse)
      ClearOutput
 EndProc

main:
    Do
      doPulse(Door1)
    Loop
GL & 73
YO4WM

Gamboa

Hi,

When I see this "Bsf LATA, 0,0" my vision gets cloudy.

Regards,
Gamboa
Long live for you

Parmin

Thank you for the answers.
I am not very eloquent in expressing my question, but I think this is solved.
I will try Gabi's solution.

top204

#6
Gabi is correct in the method, but do not use assembler mnemonics.

Use PinSet or PinClear or the other Pin altering commands. These will also manipulate any RAM bank switching that needs to take place, whereas assembler mnemonics will not, because not all devices have the PORT or LAT or TRIS SFRs in bankless RAM, and if the program was in another RAM bank, and the Bcf LATA,0 mnemonic is used, it will not alter the LATA SFR, it will alter the same address, but in a different RAM bank, where another variable might be sitting.

The Pin manipulating commands will alter the LATx SFRs, if the device has them, even if the PORT is used as the parameter, and they will produce the same tight assembler code.

Assembler mnemonics are only for use when the code around them is known absolutely, and it is known that no RAM bank switching, or page switching on the 14-bit core devices, is required, or the banks are manipulated around the assembler code. If these are not manipulated, very subtle anomalies will occur in a piece of code's operation because assembler mnemonics are handed directly to the assembler program and the compiler does not manipulate around them.

Bsf (Bit Set in File) is the mnemonic used by PinSet and Bcf (Bit Clear in File) is the mnemonic used by PinClear.

Parmin

Thanks Les.
Got it working now  :)