Turning On a Pin on a Microcontroller and creating CLASS FUNCTIONS

Hi,

I am wondering if anyone could give me a hint on how to TURN ON at last a pin on a 8-bit Motorola Micro Controller. I am currently using C programming language, and not exactly sure how to start it.

Also, I know that there are differences between C# and C++. However, I am not fully sure if C# can have can have a CLASS FUNCTION like C++ does. If C# can have CLASS FUNCTIONS, can you please give me a sample that can guide me in creating my own. I have been trying my knowledge in C++, but it does not seemed to be working.

I'll appreciate your very kind reply.
Thank you.
katz
I can't answer your C# question, however for the pin on a micro...
Usually (but not always) the pins are controlled by a bit in a register. The register usually has a #define set up for its address and the bits quite often have #defines set up for them as well. So all you need to do is some bitwise manipulation. For example if the register is #define REG1 and the bit that switches the pin on and off is #define PIN1 then...

1
2
3
4
5
6
/* switch pin on */
REG1 |= PIN1; /* bitwise OR REG1 with PIN1 */

/* switch pin off */

REG1 &= ~PIN1; /* bitwise AND REG1 with not PIN1 */

This is a fairly simplistic example, the switch pin off example will probably need to be more complex to preserve the existing bits in the register. The same principles apply though.

Hope this helps
Last edited on
Should that switching of pin ON and OFF be done inside the main function or somewhere else?
There is no hard rule for that decision. It depends on what you are trying to do.
The only real rule with micros is keeping the interrupt routines as small as possible, signalling the main thread to do the main interrupt servicing but that is true for most platforms, not just micros.
I am trying to turn on a pin to see if the code if properly being read. And then from then on, I will need to connect the transducers and valves to control an gas flow.

So what do you mean by interrupt routines?
For some reason, I found one of the thing that causes the error...

Under the ====> REG1 |= PIN1 <====
My compiler does not seem to like the character " | ".. It's the character before the equal (=) sign in between the REG1 and PIN1 variables.
http://en.wikipedia.org/wiki/Interrupt

tl;dr: Interrupts are signals sent from the hardware to your CPU, some can be masked, others (quite a few) cannot. They force the execution of a specific interrupt routine (to be written by you), the execution time of which, when it comes to asynchronous interrupts, can vary greatly, which can cause jitter, e.g. in stepper motor control, where you might want to hold a certain position (like for instance when the controller is 'too precise').

Timers use interrupts, for another example.
Last edited on
On the error you have, it is standard C/C++ syntax. You could try the equivalent 'long' code which is
REG1 = REG1 | PIN1;
Last edited on
but what data type should I use for the REG1 and PIN1? I meant, how should I declare and/or initialize each of the variable?
These are just examples. You should have documentation (or go and download it) for the micro you are working on. Usually, if a compiler supports your micro, all of the registers and bits are pre-defined.
Topic archived. No new replies allowed.