Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 20:30 18 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : ChangePin CSub

Author Message
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3167
Posted: 04:13pm 25 May 2016
Copy link to clipboard 
Print this post

A few people have asked for a special feature in MMBasic to force an I/O pin into a special state. For example, to add a pullup to a frequency input (piclover) or force an SPI output to open collector/drain (Rob).

Rather than add more options to the interpreter you can use this CSub instead. Note that invalid inputs will crash the PIC32.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Usage:
' ChangePin pin, function
'
' Where pin is the I/O pin's number
' function is a number and can be any one of these:
' -7 ANSELCLR Disable analogue input
' -6 ANSELSET Force analogue input
' -3 TRISCLR Digital output
' -2 TRISSET Digital input
' -1 TRISINV Invert the digital input/output setting
' 5 LATCLR Digital output low
' 6 LATSET Digital output high
' 7 LATINV Invert digital output
' 9 ODCCLR Normal output (not open drain)
' 10 ODCSET Open drain output
' 11 ODCINV Invert open drain setting
' 13 CNPUCLR Disable pull up resistor
' 14 CNPUSET Enable pull up resistor (do not use on an output)
' 15 CNPUINV Invert pull up setting
' 17 CNPDCLR Disable pull down resistor
' 18 CNPDSET Enable pull down resistor (do not use on an output)
' 19 CNPDINV Invert pull down setting
' The mnemonic (LATCLR, etc) refers to the name of the relevant PIC32 register
'
' For example:
' SETPIN 15, FIN ' set pin 15 as a frequency input
' CHANGEPIN 15, 14 ' add a pullup resistor to that pin
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

CSub ChangePin
00000000 27BDFFE0 AFB10014 3C119D00 AFB00010 8CA50000 00808021 8E220024
8C840000 AFBF001C 0040F809 AFB20018 00409021 8E220028 0040F809 8E040000
24030001 8FBF001C 00431004 AE420000 00001821 00001021 8FB20018 8FB10014
8FB00010 03E00008 27BD0020
End CSub


Geoff Graham - http://geoffg.net
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1421
Posted: 05:19am 26 May 2016
Copy link to clipboard 
Print this post

I made a CFunction that did what I needed, but this one seems much more flexible. ChangePin probably won't be used by most MMBasic users, but it comes in useful when you have to do something special.



Micromites and Maximites! - Beginning Maximite
 
piclover
Senior Member

Joined: 14/06/2015
Location: France
Posts: 134
Posted: 07:38am 27 May 2016
Copy link to clipboard 
Print this post

Interesting, but I prefer patching the firmware with the trick I already mentioned, which consists in not resetting the pull up/down bits of the pin in the SETPIN function when the new configuration is CIN/FIN/PIN. This way, by using, for example:
SETPIN 16,DIN,PULLUP
SETPIN 16,FIN

You get pin 16 configured as a frequency counting pin, with a weak pullup resistor: no risk of crashing the PIC with this method and it stays compatible with existing programs. You don't need either to expand the syntax for SETPIN (which would indeed consume a lot more memory).

The trivial change to do is at line 916 of External.c, enclosing it with a if () clause, like so:
if (cfg != EXT_FREQ_IN && cfg != EXT_PER_IN && cfg != EXT_CNT_IN) {
// make sure any pullups/pulldowns are removed in case we are changing from
// a digital input to an analog one or changing the pull up/down resistors.
PinSetBit(pin, CNPUCLR); PinSetBit(pin, CNPDCLR);
}


Once compiled it adds only 4 bytes to the MMBASIC firmware.Edited by piclover 2016-05-28
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1421
Posted: 11:01am 27 May 2016
Copy link to clipboard 
Print this post

That works if you have the source and can recompile.
Micromites and Maximites! - Beginning Maximite
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 11:18am 27 May 2016
Copy link to clipboard 
Print this post

I'm obviously not understanding it's usage.

Thought I could reverse the activation of my relays with using this.
1=On, 0=Off instead of 0=On, 1=Off.

[Code]
Dim Integer RlFanHi=2 'Fan Relay control pin

Setpin RlFanHi,Dout,OC : Pin(RlFanHi)=1 'Set to Open Drain & 1 so no fan on bootup

'ChangePin RlFanHi,11
ChangePin 2, 11
[/code]

Is that not how Function 11 is interpreted?

Tried Function -1 as well, but assumed it was the wrong one an I need a OC output.

Also unsure if the function accepts variable names, or just pin numbers; tried both.

Thanks

Phil



Edited by Phil23 2016-05-28
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3167
Posted: 01:25pm 27 May 2016
Copy link to clipboard 
Print this post

  piclover said   Interesting, but I prefer patching the firmware with the trick I already mentioned, which consists in not resetting the pull up/down bits of the pin in the SETPIN function when the new configuration is CIN/FIN/PIN.

That might sound logical to you but it adds a hidden consequence to the order in which pins are configured. This is the sort of thing that would trip up the unwary and deliver more condemnation on my head.

  Phil23 said   I'm obviously not understanding it's usage. Thought I could reverse the activation of my relays with using this.
1=On, 0=Off instead of 0=On, 1=Off.

No, it does not work that way. "Invert open drain setting" means that if the open drain was on (ie, the output was in open drain mode) this will set it off (ie, not in open drain) and vice versa. It does not invert the way that the PIN(xx)= command is interpreted.

In your case you could do the following in BASIC:
Const RelayON = 0
Const RelayOFF = 1
...
PIN(xx) = RelayON


I doubt that many people would want to use the ChangePin sub. I wrote it to accommodate the few specialised circumstances where some input or output feature of the PIC32 is not supported within MMBasic.

Geoff
Geoff Graham - http://geoffg.net
 
piclover
Senior Member

Joined: 14/06/2015
Location: France
Posts: 134
Posted: 02:02pm 27 May 2016
Copy link to clipboard 
Print this post

  Geoffg said  
  piclover said   Interesting, but I prefer patching the firmware with the trick I already mentioned, which consists in not resetting the pull up/down bits of the pin in the SETPIN function when the new configuration is CIN/FIN/PIN.

That might sound logical to you but it adds a hidden consequence to the order in which pins are configured. This is the sort of thing that would trip up the unwary and deliver more condemnation on my head.

Come on... It's just a matter of adding a note in the documentation.

People not needing the weak pull up/down resistor for CIN/FIN/PIN won't notice a difference and won't "trip up" on anything, and the others (like me), will be happy to have that feature.

Currently, it's totally silly to have pull up/down resistors for DIN/INT and not for the other types of digital inputs (CIN/FIN/PIN)... especially since when linked to the same circuit, it is perfectly legit to use the various functions for a single pin (e.g. for a pin linked to an open drain RTC 1Hz/32768Hz output pin: it can be used first, by configuring the RTC to output 32768Hz, for trimming the MM clock with a SETPIN FIN (and here we need a pullup), and then a SETPIN INTH, target, PULLUP with the RTC configured to output at 1Hz, to trigger an interrupt every second).
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024