Jump to content

Controlling PIC PINs


levon
 Share

Recommended Posts

No sure if this should be here, or in the ASM section.... anyways.

in trying to get this xy pad working, i need to switch the assignment of some pins.

to measure the X axis i need to assign the pins as such.

Pin 1: 5v+

Pin 2: d/c

Pin 3: Analog in

Pin 4: GND

and the Y axis

Pin 1: Analog in

Pin 2: GND

Pin 3: 5v+

Pin 4: d/c

im assuming this has to be done in assembler, but i cant for the life of me figure out how to do this. if anyone could point me in the right direction, would be great. I have plenty of free pins of the J5A/B port on the Midibox core to use. so basically i just need to use A0-A3 of the J5 port, and assign

A0  5v+ and Analog in

A1 unassigned and GND

A2 Analog in and 5v+

A3 GND and unassigned.

1. can this be done in C, or does it have to be done in assembler?

2, any hint at what functions can assign a pin to be analog in... GND, and 5v+

cheers, this has been doing my head in for a few days.

Link to comment
Share on other sites

Yup,

I can give the ASM answer, but you'll have some lookup work to do..

and this might not work.. I'll try to point out the difficult spots as we go.

Download the microchip datasheet for the chip you are using..PIC18F452 or whichever.

Download the MIOS function reference..

Find the MIOS code to turn OFF analog multiplexing.. add that to your program. MIOS_AIN_UNMUXED??

It would be great to turn off MIOS analog stuff completely.. not sure how, but someone will chime in.

In the datasheet, read up on configuring the analog inputs as analog or digital. THis may be a problem..

You can select how many are analog and how many are digital, but it's not a free form assignment.

Usually, if you want just one analog input, it MUST be A0.. there is no option for making A0 digital and a2 analog, for instance. the exception is that one of those analog inputs may be re-assigned as a reference, but that doesn't help us.

In the end, the ADCON0 and ADCON1 registers will control which pins are analog and which are digital.

Then you can set bits in the TRISA register to split the digital pins into inputs (1) and outputs(0)

Then you can set LATA bits to determine which outputs are HIGH (1) or Low(0).

note: for the top three analog inputs, that will be TRISE and LATE, as they are in port E instead of A

All of these can be set/reset/played with "live" while the program is running, if need be. I do NOT know what it will take to get MIOS to leave these alone. Obviously if it is scanning any analog inputs at all, then you may have to deal with that..

Note that using I/O pins to supply +5 and Gnd will not work if current requirements get near 20ma.. although you can get a FEW more ma than that, it will drop the voltage as you get near the limits.

I think once all the details are worked out, you'll find it easier to wire up each side individually and not try swapping pin meanings from X to Y, if that is possible at all.

That way you could avoid all this mucking around and get on to the fun stuff.

Have Fun, (or.. Good Luck??)

LyleHaze

Link to comment
Share on other sites

This won't work properly with the AIN handler of MIOS8, because pin drivers have to be reconfigured, and some delay has to be inserted *before* the next conversion is started to get stable results. MIOS8 doesn't provide a hook for this, and because of various reasons (e.g. execution time requirements due to high update rate) I don't see a way to provide it for the 8bit version.

MIOS32 already got this optional hook, and an example which scans a XY touchpanel is located here:

http://svnmios.midibox.org/listing.php?repname=svn.mios32&path=%2Ftrunk%2Fapps%2Ftutorials%2F022_glcd_touchpanel%2F

AIN_ServicePrepare() switches the pin configuration, and APP_AIN_NotifyChange() gets the conversion results.

Only solution I see for PIC: you have to program an analog conversion handler by yourself. The performance won't be so good like with MIOS32, but it should be sufficient so long not so many other tasks are handled by the PIC as well.

You could handle the conversion from the Tick() hook.

This (assembly based) routine shows, how to control the ADC.

The interesting part is located in AIN_Init and AIN_Tick, AIN_Handler is very application specific (you can ignore it)

http://svnmios.midibox.org/filedetails.php?repname=svn.mios&path=%2Ftrunk%2Fapps%2Fsynthesizers%2Fmidibox_sid_v2%2Fsrc%2Fain.inc

Best Regards, Thorsten.

Link to comment
Share on other sites

  • 4 weeks later...

Thanks for both of your replies.

ive been reading the datasheet alot, and i dont think it is possible with a 18f452 to do what i want.

assigning the pins to analog and digital can only be done in a specific order, and does not allow me to switch between the two different order of pins i need.

sigh....

i have however found these to tutorials on getting them working with an arduino

http://kalshagar.wikispaces.com/Arduino+and+a+Nintendo+DS+touch+screen

and AVR not running Arduino.

http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=139

lylehaze i think the Transistor way might be the easiest... though i understand how transistors work... im not sure how i would design a circuit to do the job. im not sure if im on the right track here.... but i think i need to do the following.

use one transistor connected to a Dout pin, one to ground and the other to the AIN pin... then when the Dout pin is high, it will connect the AIN pin to GND, when Dout is low, it will be 5V+ right?

then basically have 2 of these, and in code i can set Dout0 to high, Dout1 to low... read the X value... store it, switch the Dout pins....read the Y value.

or is there an easier way of doing it?

thanks for all your info though, ive spend weeks reading up on ASM, its been... er... fun. :P

Link to comment
Share on other sites

I regret that I don't have time to get deeper into this.. I'll be going offline in about an hour, and will probably not see the net again for most of a week. SO if it seems like I'm ignoring you, I am. :)

Pins 1 and 3 need to be analog inputs OR +5 volt supplies.

To make this work in MIOS8, you HAVE to use analog inputs for these. You can also attach PNP transistors that will turn these into +5 volt suppliers when either transistor is turned on.. The transistors will have the emitter connected to +5 volts, the collector connected to the AIN pin in question, and the Base connected, through a resistor to a spare DOUT... when the DOUT is HIGH, the input should act like a analog input, when the DOUT is LOW, the analog input will read 5 volts, and it can supply power to your XY pad.

For pins 2 and 4, which must be ground or (no connection), you can use NPN transistors,in the same way. The emitter will be ground, the collector will be your Pin1 or Pin4, and the base will connect to a spare DOUT through a resistor. When the DOUT is HIGH, pin 2 or 4 will be GROUNDED. when the dout is LOW, there will be no connection to pin 2 or 4.

Yes, yes, this is all very messy. Working with individual transistors, scary stuff, all that.

Radio Shack sells bags of 10 transistors for about 2 bucks each. You'll need a bag of PNP and a bag of NPN.. the resistors I mentioned can be.. Oh hell, I don't know. 1K will work, 10 K probably will too.

Build it, test it, have fun, that's what counts. What's nice about this method is that you do NOT have to re-configure MIOS. You're doing the switching in hardware outside the core. You only need control of a few DOUTS to switch the transistors, and of course expect to see 5 volts on the analog inputs when they are being used as power supplies (as expected)

I'll be offline for a few days, but there are others here that can help with this if you ask nicely. :-)

Radio Shack: PNP transistors, bag of 15 276-1604 $2.59

NPN, bag of 15 276-1617 $2.59

Have Fun,

LyleHaze

Link to comment
Share on other sites

There is no need for using transistors, because digital pins will be in tristate (-> floating) once they are switched to input mode via the TRISx register.

assigning the pins to analog and digital can only be done in a specific order, and does not allow me to switch between the two different order of pins i need.

You are right! As a workaround you could connect a spare digital and an analog pin together.

To measure the resistance, switch the digital pin into tristate mode.

To drive the analog input, disable tristate mode and set the logic level (0/1) via LATx register

i have however found these to tutorials on getting them working with an arduino

This tutorial basically explains the same method I used for my STM32 based example.

My example looks a bit more complicated, as it is prepared for multitasking. You want to allow multitasking for your application as well (I guess), e.g. to handle incoming MIDI data. If this isn't required, you could program it as a single thread in the Tick() hook, you could use MIOS_Delay() to insert delays between measurements, etc...

Best Regards, Thorsten.

Link to comment
Share on other sites

And what do you guys think of this ?

Q0 is a basic NOT door. Input low :

- pin 1 left floating, X value is pin 1 measurement .

- pin 2 set to ground .

- pin 3 set to +5V, pin Y discontinued, should keep the previous value (measurement) of pin 3.

- pin 4 left floating.

Input high : .....

So, continuous X and Y values ? Or total mess ?

(Not sure about the behaviour of Qx and Qy emitters when the base is low. In the worst case, I'd leave them apart and on AIN_NotifyChange only consider those two pins half of the time; or putting some RC filter to reject jitter ... But what is the main part of this circuit worth ?)

xy switcher.JPG

xy switcher.JPG

Link to comment
Share on other sites

Dubstructor,

There are a few things in that drawing that trouble me a bit..

BUT my experience with discrete transistor circuits is not great, so instead of exposing my ignorance on the subject, I'll just say that I'd want to test it thoroughly before deciding to keep it.

I think TKs "no parts" solution is looking better all the time.

If you want to discuss what I _think_ might be a problem in this circuit, PM me and we can

exchange artwork by E-Mail.

Have Fun,

LyleHaze

Link to comment
Share on other sites

  • 2 months later...

ok, some progress on this, but still need a bit of guidance.

my friend helped me put together this code, calling the pins directly.

ADCON1bits.PCFG = 0xF;


//OR

//Set which ports are analog and digital

//currently an0 = analog rest are digital

ADCON1bits.PCFG0 = 1;

ADCON1bits.PCFG1 = 1;

ADCON1bits.PCFG2 = 1;

ADCON1bits.PCFG3 = 1;


//Set ANO as the analog channel to do the read from

ADCON0bits.CHS0 = 0;

ADCON0bits.CHS0 = 0;

ADCON0bits.CHS0 = 0;



//VRef+/an2 = VDD  = 5v

//vref-/an3 = vss  = gnd

//an0 = analog in

//an1,4,5,6,7 can be digitial IO, as they are digital

//they are referred to as RA0/RA1 etc



//Turn on adc

ADCON0bits.ADON = 1;


//wait for adc to do conversion

while (AD1CON1bits.DONE == 0)

{

    continue;

}


int result = ADRESL | (ADRESH << 8);


//turn off adc

I've put this in the main loop, have AIN_unmuxed() and it all compiles fine.

When i upload it to the midibox it will turn on, and then crash and restart about 5 seconds later.

Have i missed anything in turning off the MOIS AIN functions?

cheers

Link to comment
Share on other sites


//Turn on adc
ADCON0bits.ADON = 1;

//wait for adc to do conversion
while (AD1CON1bits.DONE == 0)
{
continue;
}[/code] IIRC it's /DONE, which means that in your source it's the wrong way around and should be
[code]
ADCON0bits.ADON = 1;

AD1CON1bits.DONE = 1;

//wait for adc to do conversion
while (AD1CON1bits.DONE)
continue;

Otherwise the app will wait until it's "not done" which will result in an endless loop.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...