Searching \ for '[PIC]In C, how do you set a pin to high voltage?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/power.htm?key=voltage
Search entire site for: 'In C, how do you set a pin to high voltage?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]In C, how do you set a pin to high voltage?'
2010\05\28@220924 by yamanoor sairam

picon face
Hi,

I suggest that you must read this thread:

http://www.electro-tech-online.com/micro-controllers/95452-how-change-single-bit-mikroc.html

This should work in any compiler:

PORTD |= 1<<0;

Regards,
Sairam




________________________________
From: Xiao Xu <spam_OUTxiaoxu193TakeThisOuTspamgmail.com>
To: Microcontroller discussion list - Public. <.....piclistKILLspamspam@spam@mit.edu>
Sent: Sat, May 29, 2010 5:44:51 AM
Subject: Re: In C, how do you set a pin to high voltage?

btw, here is my code if it helps:

#include <pic.h>
void main()
{

RA0=1;
PORTAbits.RA0=1;
NOP();
}

the 0-bit of PORTA(0x05 for 16F684) refuse to turn into 1!

- Xiao Xu



On Fri, May 28, 2010 at 8:13 PM, Xiao Xu <xiaoxu193spamKILLspamgmail.com> wrote:
{Quote hidden}

2010\05\29@022946 by Wouter van Ooijen

face picon face
> PORTD |= 1<<0;

But *do google for "read modify write problem"*. The above line might
*fail* on a PIC (with all compilers).

--

Wouter van Ooijen

-- -------------------------------------------
Van Ooijen Technische Informatica: http://www.voti.nl
consultancy, development, PICmicro products
docent Hogeschool van Utrecht: http://www.voti.nl/hvu

2010\05\29@093243 by Olin Lathrop

face picon face
Wouter van Ooijen wrote:
>> PORTD |= 1<<0;
>
> But *do google for "read modify write problem"*. The above line might
> *fail* on a PIC (with all compilers).

The above line will turn on the intended bit.  The failure is that it might
alter other bits.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2010\05\29@114144 by Xiao Xu

picon face
Alright, here's my entire program, I added TRISA in:
I'm currently running a 16F684 on a PICKit 1, and I'm trying to get 1 of the
lED to light up. But unfortunately it refuses to.

#include <pic.h>

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \

& UNPROTECT & BORDIS);


> void init()

{

PORTA = 0;

  CMCON0 = 7;

  ANSEL = 0;

  TRISA4 = 0;

  TRISA5 = 0;

}

main()

{

  init();

  while(1)

  {

      for (int i = 0; i < 255; i++)

          for (int j = 0; j < 129; j++);


>        RA4 = 1;


>        for (int i = 0; i < 255; i++)

          for (int j = 0; j < 129; j++);


>        RA4 = 0;

  }

}


- Xiao Xu



On Sat, May 29, 2010 at 9:32 AM, Olin Lathrop <olin_piclistspamspam_OUTembedinc.com>
wrote:
> Wouter van Ooijen wrote:
>>> PORTD |= 1<<0;
>>
>> But *do google for "read modify write problem"*. The above line might
>> *fail* on a PIC (with all compilers).
>
> The above line will turn on the intended bit.  The failure is that it
might
> alter other bits.
>
>
> ********************************************************************
> Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
> (978) 742-9014.  Gold level PIC consultants since 2000.
> -

2010\05\29@114244 by Xiao Xu

picon face
and by the way, the program works perfectly fine on the MPLAb simulator, but
when I put it into the 16F684 and run it on the PICKit, no LEDs are lit up.
- Xiao Xu


On Sat, May 29, 2010 at 11:41 AM, Xiao Xu <@spam@xiaoxu193KILLspamspamgmail.com> wrote:

{Quote hidden}

2010\05\29@121849 by RussellMc

face picon face
You say it works in the simulator.
Can you explain what you  mean by "works". May be less obvious than it appears.

LED on correct pin ? (has happened :-)).
LED between pin and where?
Resistor in series with pin?
LED which ?polarity.

If pin to ground then LED Anode towards pin, cathode towards ground.

If pin to Vdd, LED Cathode towards pin, LED anode towards VDD.

Series resistor wise. Say 270 ohm to 1k range to start.

_______

The following "proves" your hardware.

Desocket PIC if socketed.
Power up circuit.

1. IF there is a resistor in series with the LED (as there should be).

Connect PIC pin location connected to LED to ground with a wire - observe.
Connect PIC pin location connected to LED to Vdd  with a wire - observe.

LED should light during one of those states and extinguish for other.
If not, reverse LED polarity and retry.

2.  If there is NO series LED resistor.
Repeat as in 1. above BUT use a 270 ohm to 1000 ohm range resistor
instead of a wire  place of the wire.

If no LED lights then hardware is wrong.
Fix this, then fix software if needed.

3. If PIC is NOT socketed then 1. above will still work and PROBABLY
won't damah\ge PIC.

4. 2. above may work but R may need to be say 100 ohms - keep it short
time wise :-).

5. If LD was inserted backwards if\t MAY have died.

6. LED po\polarity check.

Connect from Vdd via eg 1000 ohms via LED to ground.
LED Anode must be towards Vdd to work.



         Russell










On 30 May 2010 03:41, Xiao Xu <RemoveMExiaoxu193TakeThisOuTspamgmail.com> wrote:
{Quote hidden}

>> -

2010\05\29@122710 by Oli Glaser

flavicon
face

----- Original Message -----
From: "Xiao Xu" <TakeThisOuTxiaoxu193EraseMEspamspam_OUTgmail.com>
To: "Microcontroller discussion list - Public." <RemoveMEpiclistspamTakeThisOuTmit.edu>
Sent: Saturday, May 29, 2010 4:41 PM
Subject: Re: [PIC]In C, how do you set a pin to high voltage?


> Alright, here's my entire program, I added TRISA in:
> I'm currently running a 16F684 on a PICKit 1, and I'm trying to get 1 of
> the
> lED to light up. But unfortunately it refuses to.

Have you tested the actual pin to make sure it's not the LED or faulty
connection? (or LED wrong way round?)
I tried your code on a 16F690, and it flashes an LED okay (I used RC0 but as
long as you are clearing ANSEL, it should be the same)


2010\05\29@123633 by Adam Field

flavicon
face
> #include <pic.h>

Maybe you also need your specific pic header file, something like
p16f684.h. This file will change depending on your compiler suite.

>
> __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
>
>  & UNPROTECT & BORDIS);

You have unprotect twice, but no oscillator selection. Double check
these settings. I don't recognize some of the features here, but I
haven't used a 16F684 (or your compiler) either.


>   TRISA4 = 0;
>
>   TRISA5 = 0;

You should instead set everything to ouput as TRISA=0; TRISC=0;. You
may have floating inputs (if you haven't tied the pins to a rail with
a resistor) which can cause unexpected behavior.

2010\05\29@124743 by Bob Blick
face
flavicon
face

On Sat, 29 May 2010 11:41:28 -0400, "Xiao Xu" said:

> >        RA4 = 1;

You must have really bad luck. I am not sure on the 16F684, but pretty
much traditionally RA4 is an open-drain pin.

It is the one pin that is a problem for beginners.

For now, try  to use RA3, or RA2, or RA1, or RA0.

Not RA4 or RA5.

After you get it all working, check out the 16F684 datasheet for PORT A
and you will see that not all the pins on that port are identical, and
using them as outputs is special.

Cheerful regards,

Bob

--
http://www.fastmail.fm - Does exactly what it says on the tin

2010\05\29@125603 by Oli Glaser

flavicon
face
>Maybe you also need your specific pic header file, something like
>p16f684.h. This file will change depending on your compiler suite.

>
> __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
>
> & UNPROTECT & BORDIS);

>You have unprotect twice, but no oscillator selection. Double check
>these settings. I don't recognize some of the features here, but I
>haven't used a 16F684 (or your compiler) either.

It does work with these settings as they are (at least for me), the
oscillator selection is INTIO (internal RC, no clock out) but the two
UNPROTECTs are uneccesary. HTC config fuses are a bit different from
Microchips (or other) compilers.
Something like this should work just as well:
_CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & BORDIS);



2010\05\29@130756 by Oli Glaser

flavicon
face
> You must have really bad luck. I am not sure on the 16F684, but pretty
> much traditionally RA4 is an open-drain pin.
>
> It is the one pin that is a problem for beginners.
>
> For now, try  to use RA3, or RA2, or RA1, or RA0.

Forgot to add, this is still probably good advice whether it's open drain or
not, as it seems the pin (RA4) is also used as the Osc clock out/in.
Sticking to RA0 or 1 would be easier.


2010\05\29@131935 by Oli Glaser

flavicon
face
Sent again, this message should have been before my last one, they keep
getting lost..

> You must have really bad luck. I am not sure on the 16F684, but pretty
> much traditionally RA4 is an open-drain pin.
>
> It is the one pin that is a problem for beginners.
>
> For now, try  to use RA3, or RA2, or RA1, or RA0.

I just had a look at the 16F684 datasheet, and I can't see any mention of
RA4 being open drain ( I may be wrong/blind though ) I think RA3 is the only
problem pin, being input only.

>From the datasheet:

Figure 4-5 shows the diagram for this pin. The RA4 pin
is configurable to function as one of the following:
. a general purpose I/O
. an analog input for the ADC
. a Timer1 gate (count enable)
. a crystal/resonator connection
. a clock output


2010\05\30@105421 by Barry Walsh

flavicon
face
You have had very good advice to check the hardware, I agree.

What level of optimisation are you using in the compiler ?

>       for (int i = 0; i < 255; i++)
>
>           for (int j = 0; j < 129; j++);
>

'i' and 'j' are not declared volatile so may be getting optimised to
nothing.

This would give a flickering output on the pin too fast to be very
visible as a flicker. Your choice of LED, resistor etc could affect if
it lights at all.
I assume you don't have access to an oscilloscope for confirming this.

Try a very simple build of software that just turns the LED on and
another build to turn it off just in case the polarity is not what you
expect.

Barry.




{Original Message removed}

2010\05\30@170054 by Mark Rages

face picon face
On Sat, May 29, 2010 at 11:47 AM, Bob Blick <bobblickEraseMEspam.....ftml.net> wrote:
{Quote hidden}

Xiao Xu is using the PICkit 1, which has LEDs charlieplexed across the
PORTA outputs. (RA1,2,4,5)

See page 36 of http://ww1.microchip.com/downloads/en/DeviceDoc/40051D.pdf

Regards,
Mark
markrages@gmail
--
Mark Rages, Engineer
Midwest Telecine LLC
EraseMEmarkragesspammidwesttelecine.com

2010\05\30@181206 by Olin Lathrop

face picon face
Xiao Xu wrote:
{Quote hidden}

No comments, messed up indentation.  No thanks.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2010\05\30@181525 by Olin Lathrop

face picon face
Adam Field wrote:
> You should instead set everything to ouput as TRISA=0; TRISC=0;. You
> may have floating inputs (if you haven't tied the pins to a rail with
> a resistor) which can cause unexpected behavior.

And what about MCLR and PGM (if your PIC has PGM)?  MCLR will always be a
input, so always needs to be externally driven.

You need to show your complete schematic.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2010\05\30@181950 by Olin Lathrop

face picon face
Oli Glaser wrote:
> I just had a look at the 16F684 datasheet, and I can't see any mention
> of RA4 being open drain ( I may be wrong/blind though ) I think RA3 is
> the only problem pin, being input only.

RA4 is not open drain on the 16F684.  However, it is on many PICs, so it was
a reasonable thing to suggest looking at.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2010\05\30@190252 by Oli Glaser

flavicon
face

----- Original Message -----
From: "Olin Lathrop" <RemoveMEolin_piclistEraseMEspamEraseMEembedinc.com>
To: "Microcontroller discussion list - Public." <RemoveMEpiclistspam_OUTspamKILLspammit.edu>
Sent: Sunday, May 30, 2010 11:19 PM
Subject: Re: [PIC]In C, how do you set a pin to high voltage?


> Oli Glaser wrote:
>> I just had a look at the 16F684 datasheet, and I can't see any mention
>> of RA4 being open drain ( I may be wrong/blind though ) I think RA3 is
>> the only problem pin, being input only.
>
> RA4 is not open drain on the 16F684.  However, it is on many PICs, so it
> was
> a reasonable thing to suggest looking at.

Of course it was a reasonable thing to look at, which is why I looked :-) I
wasn't suggesting otherwise, I was just relaying the info I found in the
datasheet to help clarify matters.


2010\05\31@170607 by Xiao Xu

picon face
I got one LED to flash on the PICkit. Thanks for the help everyone!

I have another question though. Below is a link of schematic for the LEDs on
the PICkit (page 2 of link below), does anyone know what combination of bits
are required for each LED?

For example, if I set PORTA = 0b00010000, D0 LED lights up. If I set PORTA =
0b00100000, D1 LED lights up.
How can I figure out what combination of high/low/no voltages are required
to light up each of the other LEDs?
thanks.

Thanks!

Link to schematic(page 2)
docs.google.com/fileview?id=0Bw19P-iQm5zpYTJhZDIzYzYtMTZiOS00ODEwLTliYzItOGM1ODUyOGI4NjNi&hl=en
- Xiao Xu


On Mon, May 31, 2010 at 3:33 PM, Xiao Xu <RemoveMExiaoxu193TakeThisOuTspamspamgmail.com> wrote:

{Quote hidden}

>> --

2010\05\31@191040 by Oli Glaser

flavicon
face

----- Original Message -----
From: "Xiao Xu" <spamBeGonexiaoxu193STOPspamspamEraseMEgmail.com>
To: "Microcontroller discussion list - Public." <KILLspampiclistspamBeGonespammit.edu>
Sent: Monday, May 31, 2010 10:05 PM
Subject: Re: [PIC]In C, how do you set a pin to high voltage?


{Quote hidden}

Glad you got it sorted.
Better schematic for Pickit 1 here:
ww1.microchip.com/downloads/en/DeviceDoc/40051D.pdf
With the settings for lighting each of the LEDs, couple of examples to get
you going (don't want to spoil the fun of working it out totally :-).
Basically you have to make sure that there is a channel for current through
ONLY the LED you want to light. You can do this by setting all the pins that
are not used to inputs, making then high resistance and unable to sink
(hardly) any current. I think the examples below *should* work..

So if you wanted to light D2, you would set:
TRISA4 = 0;    // Output for high
TRISA5 = 1;    // Make RA5 high resistance so no current can flow into it,
stopping D0 from lighting too
TRISA2 = 0;    // 0V (Current sink for D2)
TRISA1 = 1;    // Make RA1 high resistance
PORTA = 0b00010000;    // Set RA4 to high, lighting D2 - set all bits at
once to avoid read modify write problems

To light D0, you would set:
TRISA4 = 0;    // Output for high
TRISA5 = 0;    // 0V (Current sink for D0)
TRISA2 = 1;    // Make RA2 high resistance
TRISA1 = 1;    // Make RA1 high resistance
PORTA = 0b00010000;    // Set RA4 high, lighting D0 this time, as current
now has channel to ground through RA5

Better way, less instructions (does same as above):

for D2:
TRISA = 0b11101011;    // Set RA4 and 2 outputs, rest high resistance inputs
PORTA = 0b00010000;    // Set RA4 to high, lighting D2 - set all bits at
once to avoid read modify write problems

for D0:
TRISA = 0b11001111;    // Set RA4 and 5 as outputs, rest as high resistance
inputs
PORTA = 0b00010000;    // Set RA4 high, lighting D0 this time, as current
now has channel to ground through RA5

So the idea is to set the LEDs anode and cathode pins as outputs, and all
the rest (not involved) as inputs, using the TRISA register. Then set *only*
the LED anode high using PORTA. Hope I've got it right :-)



'[PIC]In C, how do you set a pin to high voltage?'
2010\06\01@145314 by Xiao Xu
picon face
Alright thanks Olin! That cleared it up so much

I should probably take a refresher on the electronics
- Xiao Xu


On Mon, May 31, 2010 at 7:10 PM, Oli Glaser <EraseMEoli.glaserspamEraseMEtalktalk.net> wrote:

>
> {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2010 , 2011 only
- Today
- New search...