New to PIC, so I'm having problems

Thread Starter

platinum95

Joined Feb 4, 2012
3
Right, I got a PICkit 2 yesterday (16F690), and I was messing around with the sample programs, but I decided that I'm going to use C to program it because I couldnt get my head around whatever is in the samples (I think it's assembly?)

Anyway, I have a few question for my "Hello World" program.
1)What should I have in the header?
2)How do I declare an output or input pin?
3)How do I set a pin to either on or off?

This is my Hello World program so far

Rich (BB code):
#include "pic16F690.h"

main()

{
    TRISC=0;
    
again:
        PORTC,1;
 goto again;      

}
Of course, I'm sure most of it is a load of... incorrect programming...
It makes all the LEDs light up, which is a start! :D

I'm sorry if this has been answered before, but I did look and couldn't find anything.

Oh, and I have looked at numerous tutorials, but a lot of them are outdated or for a different chip.

Thanks very much!
 

spinnaker

Joined Oct 29, 2009
7,830
Well you have already got one thing right. You have the right header included.

If you right click on the header file next to your #inculde at the top of your source you can select Open file "pic16F690.h" and you will be able to see all of the various structures.

DO NOT use gotos in C. Instead use while

to correct your code

Rich (BB code):
LATC = x0FF;
while(1);
You should set latches and read ports.


but I believe you are going to have to set the bank to gain access to those registers. Since I have never worked with the 16F I can't help you there.


If you want to work with C then you should look at the 18f family. It is optimized for C and you don't need to mess with bank switching.

Here is a thread on the differences between 16F and 18F
http://forum.allaboutcircuits.com/printthread.php?t=48143


But before you go anywhere I suggest you study C. Your brief code snippet suggests your C skills need work.
 

Thread Starter

platinum95

Joined Feb 4, 2012
3
Excuse the gotos, I tried For loops but wasn't able to declare any variables, so I must be missing something there....
Really, all I want to know for now is how to turn a specific pin on or off.

Thanks for the reply!

**EDIT**
Nevermind, I worked it out. Thanks anyway! :D
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
It is quite fine to use goto's in C. The code works. As a style issue it is undesirable, as code using (one or many) goto instructions can be very hard to follow.

So for now you may want to use goto but try to get away from it.

When using C there is never a need to set the bank registers. That is one of the things a compiler will do for you.

Microchip gives away the HiTech C compiler for this series of chips.


If you want to work with C then you should look at the 18f family. It is optimized for C and you don't need to mess with bank switching.
Don't believe the hype. There ain't nothin' special in the hardware for C. Plus PIC18's have plenty of banks to be set correctly to get the correct register.
 

spinnaker

Joined Oct 29, 2009
7,830
It is quite fine to use goto's in C. The code works. As a style issue it is undesirable, as code using (one or many) goto instructions can be very hard to follow.

So for now you may want to use goto but try to get away from it.


.
It's better not to start bad habits. Yes there is a use for goto in C but it is rare. In 20 years of programming in C I may have used goto once or twice.
 

MrChips

Joined Oct 2, 2009
30,711
I am a professional programmer and have never found a use for GOTO. The GOTO statement was abandoned after the 1960s following Hoare and Dijkstra's paper.
 

Thread Starter

platinum95

Joined Feb 4, 2012
3
Don't get me wrong, I don't use gotos at all in C, I only used it there because I wasn't able to get any loops going.

Thanks for all the info!
 

MrChips

Joined Oct 2, 2009
30,711
Here are two constructs for endless loops:

Rich (BB code):
for (;;)
{
  // your code
}
Rich (BB code):
do
{
  // your code
}
while(1);
 

spinnaker

Joined Oct 29, 2009
7,830
I am a professional programmer and have never found a use for GOTO. The GOTO statement was abandoned after the 1960s following Hoare and Dijkstra's paper.
As I said there are rare instances when a goto will make things a bit more simple. But 99.9 % of the time, the use will only make your code harder to follow.

I can see where a programmer, writing well written code can easily go his whole career and never use one.
 
Top