New to PIC, so I'm having problems

Discussion in 'Embedded Systems and Microcontrollers' started by platinum95, Feb 4, 2012.

  1. platinum95

    Thread Starter New Member

    Feb 4, 2012
    3
    1
    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

    Code ( (Unknown Language)):
    1.  
    2. #include "pic16F690.h"
    3.  
    4. main()
    5.  
    6. {
    7.     TRISC=0;
    8.    
    9. again:
    10.         PORTC,1;
    11.  goto again;      
    12.  
    13. }
    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!
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    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

    Code ( (Unknown Language)):
    1.  
    2. LATC = x0FF;
    3. while(1);
    4.  
    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.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
  4. platinum95

    Thread Starter New Member

    Feb 4, 2012
    3
    1
    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: Feb 4, 2012
    richiechen likes this.
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.


    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.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    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.
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,367
    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.
     
  8. platinum95

    Thread Starter New Member

    Feb 4, 2012
    3
    1
    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!
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,367
    Here are two constructs for endless loops:

    Code ( (Unknown Language)):
    1.  
    2. for (;;)
    3. {
    4.   // your code
    5. }
    Code ( (Unknown Language)):
    1.  
    2. do
    3. {
    4.   // your code
    5. }
    6. while(1);
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    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.
     
Loading...