Accessing portC on 14M??

Odessa

Senior Member
Supposing the DirsC command is used to set all portC pins to inputs on the 14M. Is there a quick way to then read the port for digital 1's & 0's?

In the User Manuals, I could only find statements of the type:

if portC pin0 = 1 then jump

to read the portC pins one at a time (and at huge program memory cost (about 7 bytes per statement of this type!))

Any suggestions are welcome, Thanks, Odessa.
 

westaust55

Moderator
Please read PICAXE manual 2 page 14 as well.

When used on the right of an assignment PINS applies to the input port
e.g. LET b1 = PINS
will load b1 with the current state of the entire input port.

The variable PINS is broken down into individual bit variables for reading from
individual inputs with an if...then command.
Only valid input pins are implemented.
14M pins = x : x : x : pin4 : pin3 : pin2 : pin1 : pin0
PortC pin = . . . . . . . . . . . . . . . C5 . . . C4 . . C3

So be aware that you can use this to read portC - C3, C4 and C5 which are normal inputs 0, 1 and 2 respectively.

But there is not easy way other than the IF...THEN test that you have already mentioned for portC - C0, C1 and C2
which by default are outputs 3,4 and 5 respectively.

Not tried by myself, but another way to try is:
Peek 07, b1, ; read the PortC register into byte variable b1.​
You can then mask which ever bits you wish to check:

eg b1 = b1 AND %00000011 to get the status of C0 and C1.
 
Last edited:

westaust55

Moderator
And for Technical, why not a "b1 = pinsc"?

variable = pinsc is not valid on any PICAXE.

but pinsc = value/variable is . . .


try this in the simulator.

Code:
#PICAXE 14M
main:
pause 1000
dirsc = 24
let pinsc = 8
b0 = 1
b2 = 3
High 3
high portc 4
pause 1000
goto main
For those with a 14M they can report back whether it works in reality
 

hippy

Ex-Staff (retired)
This works for reading the 14M input pins ...

#Picaxe14M
dirsc = %000000 ' All Port C inputs
Do
Peek $07,b0
SerTxd(#bit5,#bit4,#bit3,#bit2,#bit1,#bit0,CR,LF)
Pause 1000
Loop

As to why there is "LET PINSC=<var>" but not "LET <var>=PINSC", this is because PINSC is not a true variable on some PICAXE's. The first is effectively a command like "SETPINSC <var>" but in a more familiar format ( note you cannot use LET PINSC=b0+1 ), the second would need to be something like "READPINSC <var>", but that would require a change of firmware and enough code space to add the extra code. Trying to disguise that in the form of "LET <var>=PINSC" would also complicate matters when people expected PINSC to behave as a variable which could be used anywhere a variable can.The same goes for DIRSC.
 

Odessa

Senior Member
A couple of items:

1) Hippy, thanks, I think you answered my question via: Peek $07,b0

I will try that out. But, then another question arises: Where could I have found out that register $07 contains the portC input pin data for the 14M? I did a search on "$07" in both manual #1 & manual #2 and came up empty.

Next, if I peek at $07 without having assigned any portC pins as inputs, will this just return zero? I could simulate to find out, but if you know the answer without having to do that, could you please indicate so here?

2) Westaust55, I simulated your program:

#PICAXE 14M
main:
pause 1000
dirsc = 24
let pinsc = 8
b0 = 1
b2 = 3
High 3
high portc 4
pause 1000
goto main

Although it is not about reading portC, it is interesting insofar as it does demonstrate default input & output pins reversed in those functions using the dirsc command. That operates as expected. The assignation of constants to registers b0 & b2 has no apparent significance (am I missing something?), and the statement 'High 3' has no effect, since Out3 has been converted into an input by the 'dirsc=24' command. What are those three statements in the program for?

Regarding pin assignation overrides (or lack thereof):

A) It is interesting that the 'dirsc=24' command is not overriden for the Out3 pin by the later-occurring High 3 'command';

B) if I comment out 'dirsc=24' and simulate, then:
1) 'pinsc=8' becomes ineffectual;
2) however, 'high portC 4' does take effect by converting In1 to an output port and taking it high;
3) 'High 3' then operates on Out3 as expected.

If I simulate the following program for the 08M:

Test:
Dirs=0
High 1
High 2
b0=Pins
Goto Test

Pins 1 & 2 are initially set as inputs, as expected,then High 1 & High 2 commands override the Dirs=0 command for pins 1 & 2.

It appears in some cases (at least for the 14M), when portC commands operate on pins that are not always referred to in the Basic code as portC pins, the subsequent pin assignation protocol priority during code execution is uncertain (for us users). It must be specified for the compiler, though, so, in general, how to know, for the 14M, for example, when pin assignation commands as inputs or outputs via port specificity will or will not be overridden by other pin assignation commands?

Thanks, Odessa
 

Odessa

Senior Member
I tried to simulate the 'Peek $07,b0' command within a 14M program and received the following error in a window:

Peek at this address not recommended and will not correctly simulate.

I also tried to simulate on your program which includes this statement, Hippy, and obtained the same results.

Anybody know any way around this? I would like to simulate it if possible.
 

womai

Senior Member
To find out such registers, you need to study the datasheet of the underlying Microchip PIC; for the 14M that's the PIC16F684. There is a section close to the beginning called "SFR" (special function registers) which gives a list of all these memory locations. More detailed description is in the rest of the data sheet.

In your case you'd be looking for a register called "PORTC".

Those SFR listings are also great to find out other Picaxe hack, e.g. slowing down PWM with the prescaler, or changing the internal oscillator frequency for parts like the 08M.

Wolfgang
 
Last edited:

hippy

Ex-Staff (retired)
Operating with SFR's is not officially supported by Rev-Ed nor do we generally encourage using such methods because of the problems thay can cause and the support issues they raise. Although access to SFR's is available it's not usually needed by most PICAXE programmers but is there for those who need or want to use them at their own risk and have a good understanding of how the PICmicro the PICAXE builds upon works. Peek's of SFR are generally safe and should have not have any adverse effects on PICAXE operations but Poke's to SFR are more dangerous territory. Note that different PICAXE's may support different SFR's and they may even be in different places. What works with one PICAXE may not work with a different sized variant.

As you note, PICAXE Basic commands do some quite complicated things, like making a pin an output when setting its level, and these interact with the SFR's of the chip. Incorrect use of SFR's can therefore affect how the chip operates - sometimes beneficially for the programmer, sometimes not - and sometimes what you try to do gets over-written by subsequent PICAXE operations.

All of this goes towards why utilising Peek and Poke outside of the officially supported ranges ( see "Variables - Storage" in PICAXE Manual 2 ) is not implemented by the simulator. That simulates a PICAXE, not a PICmicro.

To answer the question about what happens with Peek $07 when you haven't assigned any inputs on Port C ... in the 16F684 datasheet you will see how the Port C is wired within the chip. Note how the outputs are gated and where the input is taken from. When input, input comes from the pin, when output, the input still comes from the pin so you will read back what is being output ( usually, loading on the output can sometimes affect that ).
 

Odessa

Senior Member
"The simulator doesn't allow for PEEKing and POKEing SFR registers--gotta use a real chip."

Ok, Ibenson, thanks for that notification. To whomever might be the appropriate party to make a recommendation, I make it regarding this feature for future versions of the simulator: to be able to Peek (at least, if not Poke too) the SFRs. Would that be possible, say for advanced users?

Odessa.
 

Odessa

Senior Member
"...utilising Peek and Poke outside of the officially supported ranges ... is not implemented by the simulator. That simulates a PICAXE, not a PICmicro."

Ok, got it. So, probably including that functionality in future simulation versions is unlikely. I am not a mainstream Picaxe user, and while there may not be too many users like me compared to the majority, I am often trying to stretch applications as far as they will go before having to resort to dropping a Picaxe for a given application, and buying direct from Microchip, and programming in C or a variant (due to monetary constraints, that might be more likely than upgrading to a larger Picaxe device). So, suggestions like the Peek command in this thread, while officially unsupported, are a good idea and certainly welcome. I hope they continue...

"To answer the question about what happens with Peek $07 when you haven't assigned any inputs on Port C ...I will check that datasheet, thanks!

Odessa
 

Odessa

Senior Member
Just as a contribution to this post, using the Peek command in my application has whittled down the code requirement for the function of interest from 47 bytes to 5 bytes....a GRAND savings in program memory for the 14M!!

Odessa
 

westaust55

Moderator
Accessing portC on 14M

I had suggested the Peek to location 7 in post 2 (albeit I did not test) after I looked at my memory map which contains a listing of accessible SRF registers for most PICAXE chips.

Peeks to SFR will always give that warning when you use the simulator in the PE.

See my meory map at: http://www.picaxeforum.co.uk/showthread.php?t=9525&p=79188

But YES, if you want to get into this type of programming you will need to download the relevant PIC datasheet from the Microchip website.
 
Top