Making an output pin name variable

Getting the hardware SPI module on the 14M2 chip working and getting the code to control which LED string using AllyCat's suggested approach working are issues that can be investigated separately.

To get you started here is some code that I have tested on my 14M2 chip to get you started using the hardware SPI module.
The first program POST_PE_APA102_Example_6.bas is the PE sample program APA102_Example_6 modified to use the hardware SPI module.
Replace the code in the Main loop with your own code to write patterns to the string using the macros head(), tail() and send().

The second program POST_Example_Flashing_Random_Colours.bas demonstrates using storage variables to store pre-calculated patterns that can be quickly written to the string using bptr. This was so fast that I've put a "PAUSE 8000" after each string update so that you can clearly see each of the 4 different sets of random colours.
 

Attachments

Hi,
I have tried to use the ports B0,B1 and B2 as the switchable "string select" clock lines. The internal SPI clock is on B7. Since B2 doesn't have weak pullup, it got a 39k resistor to +5. Each of B0, B1 and B2 have a diode (1N4148) pointing to B7. The software to select a channel looks like this:
Code:
SYMBOL pupmask= %00011000        ; Pullup mask to pull up B.0 & B.1
There appears to be a number of errors, or at least imprecision, in the code and description: B0, B1 and B2 are not "Ports"; B.0 , B.1 and B.2 (with dots) are port PINS. I'm surprised if pin B.2 does not have a Weak Pullup, but using an external resistor would not be a substitute because the "gate" Output could not be Enabled/Disabled by the program.

The mask to enable weak pullups on pins B.0 and B.1 should be %00000011 , but IMHO it is better NOT to use symbols here because the compiler cannot check the value used (unless you are trying to trick the compiler to set an unconventional pin such as SERIN).

Should Input SCK come before Pullup? Would just one instance of Pullup be sufficient or does PINSB=0 cancel the Pullup when a pin is made an input?
PULLUP is a "Global" command (i.e. it will update ALL the pins on Port B and/or Port C), so the programmer must keep their own record of all the pins' status if they need to be controlled independently. AFAIK these pullups are connected directly to the INPUT circuitry of the chip so they can be connected only when the pin is configured as an Input. Also, when a pin is configured for an "alternative" function (such as with the SPI Module) then the "Normal" I/O functions become unavailable; It may be necessary for the POKESFR command to be used last, to prevent the PICaxe Operating System "Overwriting" the desired value.

Cheers, Alan.
 
Hi,

I'm surprised if pin B.2 does not have a Weak Pullup, but using an external resistor would not be a substitute because the "gate" Output could not be Enabled/Disabled by the program.
I don't understand that. Surely switching the pin between output low and input (floating) will keep it either low or the same as the pin that's at the other end of the diode?
 
Hi,

The mask to enable weak pullups on pins B.0 and B.1 should be %00000011 , but IMHO it is better NOT to use symbols here because the compiler cannot check the value used (unless you are trying to trick the compiler to set an unconventional pin such as SERIN).
I worked out the mask value from the online manual entry which gives:
20X2bit0-bit7 = C.0, C.6, C.7, B.0, B.1 B.5, B.6, B.7
I'm not clear about avoiding a symbol. If it's a constant, does the constant not replace the symbol when the compiler gets t work?
 
Hi,

Yes, sorry, you're probably correct on both points: "Wired-OR" gates (e.g. Chip Enables) are usually Active Low (Idle High), but I don't think that's relevant in this application anyway. Similarly, I was thinking of the 20M2 and most other M2s which have a full complement of Weak Pullups (Ports B and C) with a simple direct bit to pin mapping. So there must be another reason why you've had no success with the SPI hardware module.

Yes the specified variable/constants will be substituted at run-time, but the complier is not able to "predict" what value any variable (or probably even program-constant) may contain at run-time, so it generally cannot give any "warnings" if any mistakes are made in enabling inappropriate or unavailable Pullup Resistors, etc..

Cheers, Alan.
 
Surely switching the pin between output low and input (floating) will keep it either low or the same as the pin that's at the other end of the diode?
I believe that AllyCat's circuit operates as an AND gate, which is exactly what we want. i.e to AND the level at the B.0 (or B.1 or B.2) pin with the level at the SCK pin.

Setting the B.0 (or B.1 or B.2) pin output low will disable that pin's "AND" with the SCK pin.
The signal that goes to that pin's LED string will be low when the SCK pin is high and low when SCK is low.

When you set the B.0 (or B.1 or B.2) pin to input (floating) the pullup resistor (internal or external) connected to that pin will pull the voltage of that pin up to 5V which will enable that pin's "AND" with the SCK pin.
The signal that goes to that pin's LED string will be low when the SCK pin is low and high when SCK is high.bg
 
AllyCat, you beat me to an update. Posting yours while I was composing mine.
Surely switching the pin between output low and input (floating) will keep it either low or the same as the pin that's at the other end of the diode?
I believe that AllyCat's circuit operates as an AND gate, which is exactly what we want.
i.e. it AND's the B.0 (or B.1 or B.2) pin's output with the SCK pin's output.

Setting the B.0 (or B.1 or B.2) pin output low will disable that pin's "AND" with the SCK pin.
The signal that goes to that pin's LED string will be low when the SCK pin is high and low when SCK is low.

When you set the B.0 (or B.1 or B.2) pin to input (floating) the pullup resistor (internal or external) connected to that pin will pull the voltage of that pin up to 5V which will enable that pin's "AND" with the SCK pin.
The signal that goes to that pin's LED string will be low when the SCK pin is low and high when SCK is high.
 
I can now report some progress. Thanks to Flenser's program in post 41, I found my errors in activating the 14M2 SPI module. Mainly, I was using the wrong mode and speed. There were also a couple of plain cock-up errors.

I have also succeeded in getting Allan's output switching to work. I don't understand why but the line PINSC=0 was not setting the port C pins as outputs. I also tried OUTPINSC=0 but that didn't work either. However, setting individual pins, LOW C.0, LOW C.1 etc. has got things working.

Thanks all for assistance. Now to make some interesting patterns ...
 
I don't understand why but the line PINSC=0 was not setting the port C pins as outputs.

I haven't looked at your code, but this is from the manual for [LET] PINSC = ...
If the bit is set to 0 the output will be off (low), if the bit is set to 1
the output will be on (high).
Note that this command will only function on pins configured as outputs

When you use LOW C.0, it automatically makes C.0 an output and then sets it. If you don't make C.0 (or any other pin) an output, then LET PINS ... doesn't do anything.
 
Hi,
... I don't understand why but the line PINSC=0 was not setting the port C pins as outputs. I also tried OUTPINSC=0 but that didn't work either. However, setting individual pins, LOW C.0, LOW C.1 etc. has got things working.
It's probably that neither of those "byte" instructions automatically switch the individual pins from their default Input mode. You need to use either a {LET} DIRS = %00000011 assignment to set them as Outputs, or LOW/HIGH/TOGGLE instructions which automatically set the Output mode for the related pin(s).

Ah, just beaten by Aries.

Cheers, Alan.
 
Well that explains it. A bit of a surprise - I'm accustomed to using commands that set the pin/port to output as required. Clser attention to the manual is required!
 
I'm accustomed to using commands that set the pin/port to output as required. Clser attention to the manual is required!
I think the point is that "OUTPINS =" sets output pins (as it says on the tin). It does not set direction (hence the DIRS command for that purpose) but it does allow you to set a whole lot of pins (a) simultaneously and (b) with a single command.
 
Back
Top