# Pic programming hi-tech c

#### BoVice

Joined Apr 5, 2017
3
Good morning everyone,

I'm using my old PICkit 2 and 44 pin demo board from my school days to get "re-aquainted". I'm using the hi-tech c compiler.
I made a simple program to turn on the the 8 lights connected to PORTD when the button connected to RB0 is pressed, and then go out when not pressed. I notice that sometimes when the lights come on, one or two of them are not lit. No specific ones In particular.

I've also noticed a few anomalies when I make changes in my program statements.

For example, I used an "if/else" statement;

ANSEL = 0x00
ANSELH = 0x00

Void main (void)
{
If (RB0 = 0x00)
TRISD = 0XFF;
ELSE
TRISD = 0xFF;
}

Now this program does turn the lights on and off. But when I change the argument of RB0 = 0x00 to RB0 = 0x01 I would expect this to work opposite of how it did before, yet it doesn't.

I'm not sure if my thinking / program is incorrect or if the PIC is internally compromised somehow. A "shove" in the right direction would be appreciated

#### BoVice

Joined Apr 5, 2017
3
TRISD = 0x00 after the "else".

My apologies

#### MrChips

Joined Oct 2, 2009
22,531
You have made a common mistake in syntax that is not flagged as an error by the compiler.

RB0 = 0x00 is an assignment statement
RB == 0x00 is the proper statement for equality test

You could have used

if (RB)

#### spinnaker

Joined Oct 29, 2009
7,835
And it is else not ELSE.

and void not Void

and if not If.

and

ANSEL = 0x00;
ANSELH = 0x00;

not

ANSEL = 0x00
ANSELH = 0x00

And the above code is outside your main statement.

And your if statement is superfluous anyway. Both branches do the same thing. plus they don't do anything except set PORTD as an output.

And you don't have a while loop. Your code is going to end before you even think of pressing the switch.

With all of that wrong I do not see how the code could possible compile let alone work.

Please don't tell us you manually retyped the code in instead of pasting thereby introducing all of those syntax and coding errors?

Plus hitech is dead and buried. Long past time to move on to xc.

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,835
Assuming you do your switches like I do this should work.

Code:
void main (void)
{

ANSEL = 0x00;
ANSELH = 0x00;
TRISD = 0X00;

while(1)
{
if (RB0 == 0x00)
LATD = 0;
else
LATD = 0xFF;
}

}

Last edited:

#### AlbertHall

Joined Jun 4, 2014
10,778
If (RB0 = 0x00)
TRISD = 0XFF;
You can control the outputs by changing the pins from inputs to outputs, but it is not the normal way of doing it and it will cause problems when you want to use bit operations on the port because of the 'read-modify-write' method.

TRISD = 0XFF
TRISD = 0x00; // Set Port D to all outputs

#### spinnaker

Joined Oct 29, 2009
7,835
TRISD = 0x00; // Set Port D to all outputs
oops my dyslexia strikes again.

#### spinnaker

Joined Oct 29, 2009
7,835
You can control the outputs by changing the pins from inputs to outputs, but it is not the normal way of doing it and it will cause problems when you want to use bit operations on the port because of the 'read-modify-write' method.
And you can only do it if the circuit is wired appropriately.

#### BoVice

Joined Apr 5, 2017
3
And it is else not ELSE.

and void not Void

and if not If.

and

ANSEL = 0x00;
ANSELH = 0x00;

not

ANSEL = 0x00
ANSELH = 0x00

And the above code is outside your main statement.

And your if statement is superfluous anyway. Both branches do the same thing. plus they don't do anything except set PORTD as an output.

And you don't have a while loop. Your code is going to end before you even think of pressing the switch.

With all of that wrong I do not see how the code could possible compile let alone work.

Please don't tell us you manually retyped the code in instead of pasting thereby introducing all of those syntax and coding errors?

Plus hitech is dead and buried. Long past time to move on to xc.
Actually i DID retype the code. I was at work and had some downtime and figured i would look for an answer. Code was on my home computer.
sorry if i offended you

#### spinnaker

Joined Oct 29, 2009
7,835
Actually i DID retype the code. I was at work and had some downtime and figured i would look for an answer. Code was on my home computer.
sorry if i offended you

It didn't "offend" me. It just made the post awfully confusing and it caused people to chase down problems that did not exist. Next time wait till you get home in front of your code to post.