Confused with symbol declarations and use

GrahamGo

Senior Member
I am trying to get to grips with Picaxe Basic. I see that there are two (maybe more?) ways of using port pins. I do not understand the difference between the following. Both do the same thing right?. Is one more efficient code-wise than the other? Which one is the preferred method and why? I found both definitions in the manual, but it doesn't explain the difference. Sorry for posting such a trivial question.

symbol sdata = b.4

high sdata

;--------------------------------------

symbol sdata = pinB.4

sdata = 1

;--------------------------------------
 

westaust55

Moderator
The first symbol statement is used when controlling outputs.
The 2nd symbol statement is used when reading the state of a pin defined as an input.

Explanations have been given by Technical in response to similar questions in the past. I leave it to you to search the forum for the "why" - sorry but limited access via iPhone away from home this week.
 
Last edited:

GrahamGo

Senior Member
Thanks, unfortunately the search function just spins "waiting for server", its only on the Forum part, the rest of the website works fine. Thanks, I will look into it to get my head straight. But I took the examples looking at two source codes and both seemed to be outputting a high to the a pin.

I have time now, I will mess with both versions until it becomes clear!

The Internets wonderful. Your somewhere on the road in Aussie, I am sitting in shorts on my Patio waiting for what looks to become a fantastic orange sunset in MX - cheers!
 

SAborn

Senior Member
The addition of the word Pin before the pin number is used for input pins (PinB.4) where as with a output pin just the pin number is used (B.4)

So to turn a led on connected to pin B.4 we would say in code ... High B.4

To read a switch connected to pin B.4 we would use ... If pinB.4 = 1 then.......... this is now checking if pin B.4 is taken to a high state by the switch.
 

GrahamGo

Senior Member
I sort of get it and accept it. Reminds me of a pointer and what it points to. But its still not intuitive yet. I just need to not question and to accept it. But to me High PinB.4 makes more sense! But this is my third day, so its not so bad. I know some Forth and C, but have never used Basic. Now I have one last question but will start another post. - sorry guy's..
 

BeanieBots

Moderator
A line like "High PinB.4" would lead to a very unexpected result!
"PinB.4" is actually a variable which holds the state of input pin B.4.
That variable can have one of two values, 0 or 1.
So "High PinB.4" would either set output 0 or output 1 high depending on what state input PinB.4 was in.
 

westaust55

Moderator
As an extension of the information by BeanieBots,
Pin identifiers B.0, B.1, C.0, C.1 are in fact pre-defined constants.
B.0 = 0,
B.1 = 1,
B.7 = 7,
C.0 = 8,
C.7 = 15, etc

The port.pin symbol names are the recommended usage for M2 and X2 parts so that all folks now and later can easily recognize the PICAXE pin involved in a command.
 

GrahamGo

Senior Member
I am trying to get to grips with Picaxe Basic. I see that there are two (maybe more?) ways of using port pins. I do not understand the difference between the following. Both do the same thing right?. Is one more efficient code-wise than the other? Which one is the preferred method and why? I found both definitions in the manual, but it doesn't explain the difference. Sorry for posting such a trivial question.

symbol sdata = b.4
high sdata
;--------------------------------------
symbol sdata = pinB.4
sdata = 1
;--------------------------------------
Here are two fragments of code. I have tried both and find that the expected output on the pins is correct. I personally find the fragment1 with = sign easier to read. but if I understand the advice in this thread, the fragment1 method is incorrect (or can lead to unexpected results) is this correct?


' edited fragment 1. from post#5 http://www.picaxeforum.co.uk/showthread.php?18500-Graphical-LCD-module-from-mobile&highlight=nokia
Symbol VDD_PIN = pinB.0
Symbol SCE_PIN = pinB.2
Symbol RST_PIN = pinB.3

InitialiseLcd:
VDD_PIN = 1 ; LCD power-on initialisation sequence
RST_PIN = 1
SCE_PIN = 1
RST_PIN = 0
RST_PIN = 1
HSpiSetup SPIMODE01, SPISLOW ; Setup SPI so we can send commands
SCE_PIN = 0 ; SPI initialised
. . . . . . . .

edited fragment2. from Picaxe manual
symbol CLK = B.5 ; clock (output pin)
symbol SDO = B.1

SpiSend:
shiftout_MSBFirst:
mask = var_out & MSBValue ‘ mask MSB
low SDO ‘ data high
if mask = 0 then skipMSB
high SDO ‘ data low
. . . . . . . .
 

BeanieBots

Moderator
As described earlier, your first code will give unexpected results. In breif, you have defined your symbols as inputs and then tried to use them as outputs. It can be a little confusing at first but will soon become second nature.
Code:
Symbol VDD_PIN = B.0 'define the VDD output pin
Symbol MyInputPin = pinB.7 'define an input pin
Symbol Active = 1 'define a state for "Active"

High VDD_PIN ' set the VDD_PIN high.
If MyInputPin = Active then...
 

hippy

Ex-Staff (retired)
Here are two fragments of code. I have tried both and find that the expected output on the pins is correct. I personally find the fragment1 with = sign easier to read. but if I understand the advice in this thread, the fragment1 method is incorrect (or can lead to unexpected results) is this correct?
I'm not completely sure what the advice of this thread is (!) but both fragments of code are correct and will give consistently predictable results but for the first fragment it is not obvious that the pins have been made outputs before assigning output pin values which is a prerequisite.

The real issue perhaps is there are two ways to control an output pin, using pin identifiers ...

High C.0
Low C.1

And using pin variables ...

pinC.0 = 1
pinC.1 = 0

Both will perform the same function, but the first, using pin identifier, will automatically make the pin an output, the second will not.

When it comes to reading a digital input pin the pin variable is used ...

If pinB.7 = 1 Then
b0 = pinB.7

And when using a command to read an input then the pin identifier is used ...

ReadTemp B.7, b0

In addition, because pin variables are traditionally perceived to be input pins, and the following can seem odd in that context ...

pinC.0 = 1
pinC.1 = 0

It is possible, and some recommend it, that 'outpin' is used to clarify ...

outpinC.0 = 1
outpinC.1 = 0

That's the same as above, using pinX.Y, and either can be used.
 

GrahamGo

Senior Member
But I didn't write the code, this was written by Hippy - Technical support, 19-06-2011 follow my link. I guess this is where my confusion has set in. Anyway point taken, shame though as I like the code style better.

Sorry Hippy, I posted this and then afterwards saw your post. Thanks its clearer now. The final question.... Is either one better than the other speedwise? I cant see the resulting code so don't have a clue.
 

hippy

Ex-Staff (retired)
I doubt there's much in it speed wise and I'd recommend using whatever you prefer or what's appropriate for the circumstance.

The one place it is useful to use "let pinX.Y=" is when you need to do something like -

If bit7 = 1 Then
High B.7
Else
Low B.7
End If

and you can shrink that down to "Let pinB.7 = bit7". That's particularly useful in bit banging serial / SPI / I2C style outputs and you want speed.
 

GrahamGo

Senior Member
Hi,
There is a 15% difference between the two methods, Low ABC (0.38ms) vs ABC_PIN = 0 (0.45ms).
The Let pinB.7 = bit7 is 500% quicker (1.1ms per clock) vs the for - next loop version (5.5ms per clock).

I was playing with this last night.

I am playing with a Nokia 3110 graphics chip, mainly to gain Basic coding experience. I started with your code example - thanks. I am now optimizing it, best I can. I have to admit that I have a few more questions to post with regard to Basic, so apologise ahead of time! My previous experience was with Forth and C, and I must admit that I miss inline assembler, is this something that could be introduced in the future?

Code:
setfreq m4

Symbol SDA_PIN = pinB.1
Symbol SDA = B.1
Symbol CS      = B.2
Symbol SCLK    = B.5

symbol mask = b3		
symbol var_out = b4


b0 = %01010101
var_out = b0

;          543210             ; Set all used pins of Port B as Output Low
  dirsB = %111111             ; 1=output 0=input
  dirsC = %010111


B0ToLcd:	;This method takes 8.8ms for 8 bits
  Low CS
  SDA_PIN = bit7 : PulsOut SCLK,1
  SDA_PIN = bit6 : PulsOut SCLK,1
  SDA_PIN = bit5 : PulsOut SCLK,1
  SDA_PIN = bit4 : PulsOut SCLK,1
  SDA_PIN = bit3 : PulsOut SCLK,1
  SDA_PIN = bit2 : PulsOut SCLK,1
  SDA_PIN = bit1 : PulsOut SCLK,1
  SDA_PIN = bit0 : PulsOut SCLK,1
  High CS
  
  
B0ToLCD2: ;This method takes 43.5ms for 8 bits
Low CS
	for b7 = 1 to 8 
	mask = var_out & $80 	; mask MSB
	low SDA 			‘ data high
	if mask = 0 then skipMSB
	high SDA 			‘ data low
	
skipMSB: pulsout SCLK,1 	' pulse clock for 10us
	var_out = var_out * 2 	' shift variable left for MSB
	next b7  
  High CS
  
  
  
  low SDA ;This method takes 3.12ms for 8 bits
  high SDA
  low SDA
  high SDA
  low SDA
  high SDA
  low SDA
  high SDA
  pause 2
  SDA_PIN = 0 ;This method takes 3.58ms for 8 bits
  SDA_PIN = 1
  SDA_PIN = 0
  SDA_PIN = 1
  SDA_PIN = 0
  SDA_PIN = 1
  SDA_PIN = 0
  SDA_PIN = 1
  end

speedcheck 4mhz.JPG
 
Last edited:

westaust55

Moderator
@Graham,
Have you tried running at a faster clock frequency?
For working with mobile phone displays even with the older X1 parts, I usually operate at 8 MHz and faster is a definite possibility.

Which PICAXE chip safe you using?
X1 and X2 parts have the SHIFTOUT function inbuilt within the firmware and this is easily 4 times faster that the routines using a For Next loop as given in the manuals. With the inbuilt functions further speed improvement was found by sending up to 5 bytes in one SHIFTOUT command (almost halves the time).
 

GrahamGo

Senior Member
@Graham,
Have you tried running at a faster clock frequency?
For working with mobile phone displays even with the older X1 parts, I usually operate at 8 MHz and faster is a definite possibility.

Which PICAXE chip safe you using?
X1 and X2 parts have the SHIFTOUT function inbuilt within the firmware and this is easily 4 times faster that the routines using a For Next loop as given in the manuals. With the inbuilt functions further speed improvement was found by sending up to 5 bytes in one SHIFTOUT command (almost halves the time).
I bought a few 08m2 and 14m2 parts and a couple of cheap LCD's. Yesterday I was simply curious about comparing the instruction timing and just left it at the default 4mhz. I will buy a few X2 parts. At the time, it never occurred to me to check the hardware differences in detail, the M2's just seemed better! As to the Nokia 3110 graphics, yes I will use 32mhz. I have just got it working using the 5x7 char's, but notice the characters touch each other, got to sort that out. I made a mistake the other day and chose setfreq 32, that stumped me for a while......
 

geoff07

Senior Member
I'm trying to convert a program developed for a 28X2 to work with 28X1 pin assignments, so I can simulate it in VSM, as I have to debug the analogue part of the hardware. This piece of code comprises the first two lines of the program:
Code:
#picaxe 28X1
      symbol HO_blue                = C.6
Surprisingly (to me), it results in the message:

Error: Unknown symbol - C.6

The intent is to connect an led to leg 17. What could I be doing wrong? I'm sure it is very simple but I have spent too long staring at it. It compiles if the directive is 28X2, but Port.pin notation should work with the 28X1, no?

(PE v5.4.0)
 

geoff07

Senior Member
Yes, I had tried that, and it does parse, but gives this in response to a high command:

symbol HO_blue = pinC.6
high HO_blue

Error: This command requires '6' not the variable 'pin6'!

so went back to what works with an X2. Wierd!
 

inglewoodpete

Senior Member
Yes, I had tried that, and it does parse, but gives this in response to a high command:

symbol HO_blue = pinC.6
high HO_blue

Error: This command requires '6' not the variable 'pin6'!

so went back to what works with an X2. Wierd!
No, not weird. Just the wrong syntax. You are trying to put code for a 28X2 into a 28X1.

When coding a 28X1, you must treat it like an 28X1 :):
Code:
#picaxe 28x1
Symbol HO_blue = 6 
High PortC HO_blue
 

geoff07

Senior Member
Thanks for that. I don't have any 28X1s, so I'm doing this for the first time simply to be able to use VSM. I did try to confirm the syntax in the manuals but didn't spot this. Odd though that the compiler writer chose two different syntaxes for the same task. Perhaps an opportunity for an upgrade, assuming the X1s are still selling.

And just spotted my typo. 'I before E except after C' as taught clearly does not apply. Another syntax problem!
 
Top