If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Registered Member #2042
Joined: Sat Mar 21 2009, 03:44PM
Location:
Posts: 9
Hi guys. I am working my way through learning how to use the PIC family of microcontrollers and I have a question.
I understand the concept of initializing pins as inputs or outputs using something like the following
TRISC = 0b00000000; or TRISC = 0b11111111;
For this particular application I would like to be able initialize an individual pin something like
Pin.1 = input; Pin.2 = output; ....
Now this initialization will all happen only once at the beginning of each program, so I realize I could use some methods of storing the values and putting them in one TRIS statement, and I'd love to get input on that, but I was just wondering if there was a method to directly access a single pin.
Registered Member #1623
Joined: Tue Aug 05 2008, 03:31PM
Location: The Netherlands
Posts: 39
If I understand correctly you want to set a pin to input or output on runtime. Depending on the language your using you could either use:
ASM: bsf TRISC, RC(0 to 7)
C: TRISC |= 0b(and then all zero's till you've found the one you wish to set) example 0b00001000 or TRISC &= ~0b(same as above, but this code is to clear a bit)
Registered Member #2042
Joined: Sat Mar 21 2009, 03:44PM
Location:
Posts: 9
Thank you for the response, perhaps if I explain the project it will help. Basically I am trying to make a PIC an Arduino (I know, deadly sin that I will be flamed for). This project has no other purpose than to learn more about the PIC and I felt it would be a good way to learn. So basically I am trying to replicate all the functions found in the Arduino software to be used on a PIC. Things such as digitalRead(), digitalWrite(), analogRead(), and pinMode().
pinMode() is the specific method I have a question for. Normally, the user would have something like the following.
But the problem is how to keep up with what has been set. As you can see there are 3 separate calls to the pinMode() function. Ideally my pinMode() would do something like the following:
pinMode(int pin_Name, int pin_State) { TRISC.pin_Name = pin_State; }
This way I would be able to set each pin individually without haveing to keep refreshing the entire TRISC = 0b00000000 If there isn't a way to update a single pin, I realize I could do something with arrays[] storing all of the pin assignments and setting everything once. Example psuedocode
This is just thoughts as the syntax wouldn't work, but hopefully my point is getting across. Hopefully I haven't rambled and made things even more unclear, but anymore input on either of the two methods described above would be appreciated.
Registered Member #56
Joined: Thu Feb 09 2006, 05:02AM
Location: Southern Califorina, USA
Posts: 2445
For this particular application, you could probably get away with the way that arduino does it,
void pinMode(uint8_t pin, uint8_t mode)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg;
if (port == NOT_A_PIN) return;
// JWS: can I let the optimizer do this?
reg = portModeRegister(port);
if (mode == INPUT) {
uint8_t oldSREG = SREG;
cli();
*reg &= ~bit;
SREG = oldSREG;
} else {
uint8_t oldSREG = SREG;
cli();
*reg |= bit;
SREG = oldSREG;
}
}
The method you are using seems like it has roots in Java or other object oriented languages, things are done differently in C. Bitwise operations will be you friend for this job.
Registered Member #1623
Joined: Tue Aug 05 2008, 03:31PM
Location: The Netherlands
Posts: 39
In what language are you programming? The codes I posted will do what you want to do, but if you would like to have more info about them please tell me in which language you program.
Registered Member #2042
Joined: Sat Mar 21 2009, 03:44PM
Location:
Posts: 9
@... Haha I see my "TRISC.pin_Name = pin_State;" has given away my object oriented background. This is my first real jump into the wonderful world that is C, so I have much to learn. Doing a quick search, it looks like a lot of that code uses other functions written by the Arduino team. Am I correct in assuming this? If that is the case, I am a little unclear as to how the code snippet works and would greatly appreciate a quick walk through on how it is working.
@Lethal Shot I'm programming in C, specifically the MCC18 C compiler in MPLAB I understand the use of TRISC as you have explained it, but doing it like that would require me to keep track of what pins were already set. For example if I had a a switch statement within the pinMode() such as
pinMode(int pin, int state) { switch(pin) case 0: if(state==1) { TRISC = 0b00000001; } else { TRISC = 0b00000000; } case n: //same thing just different port }
That would allow me to pass a pin number and pin state, but the problem comes in when I do this pinMode(1, 1); pinMode(4, 1);
as this would have the final setting of TRISC = 0b00010000; Only setting pin 4, instead of the desired pin 1 and pin 4.
So is there a built in way to just update a port with a new value or would I need to create one? Example TRISC = 0b0001000; update to TRISC = 0b00010100;
But this has a side effect. |= is a read-modify-write operation, a TRIS register is not guaranteed to return the same value as was last written to it (explained in the datasheet that has a diagram of the circuit). So a read-modify-write operation has the possibility of changing more bits than the one you wanted to change.
If it is documented it might be ok, but for a general library the sensible thing would be to use a static variable of some sort to keep a copy of the TRIS value.
Registered Member #2042
Joined: Sat Mar 21 2009, 03:44PM
Location:
Posts: 9
Thanks guys. I'm having some success using your suggestions. Apologies to Lethal Shot, you quickly gave me the answer but I overlooked it. As it is now, I have implemented a delay(), pinMode(), and digitalWrite(). Next will be digitalRead(), analogRead(), and analogWrite(). Right now, I am writing all of these methods in a header file (Arduino.h) and #include Arduino.h is in another file called program.c
Is this the accepted way to pull something like this off? Specifically putting all of the functions in a header file should give me the ability to include it in any future projects correct?
Registered Member #1623
Joined: Tue Aug 05 2008, 03:31PM
Location: The Netherlands
Posts: 39
Many code is written like this. It is not uncommon that functions or blocks of code that are used often are gathered in a header file. This speeds up programming and improves readability of code. However this has 1 drawback. Usually code like this has some extra checks in to make it multi-functional (a delay that is variable for instance). So for very time critical software or software that is just able to run on the processor, the macros will kill you code effiency. But as long as you create programs that are well within the limits of the PIC your using, this way of coding is just fine.
1 question though, I understand the functions 'digitalRead()' and 'analogRead()', but how are you going to pull 'analogWrite()' of without any external circuitry? The only way I can think of that a PIC can do that is with a R2R ladder, a filter with PWM output or an external DAC.
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.