Init on C??

Discussion in 'Programmer's Corner' started by Rk17, Apr 28, 2011.

  1. Rk17

    Thread Starter Member

    Apr 23, 2011
    43
    0
    ive got this code right here. Im just starting wioth c programing but i dont find
    Code ( (Unknown Language)):
    1. init
    command in the command list of C. Im using dev C++, and i cant quite understand the meaning of init in this program. What's the function of it. and also
    Code ( (Unknown Language)):
    1. TRISB = 0b00000000
    what does this stands for?

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(FOSC_XT & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF);
    3. void init(void)
    4. {
    5. // port directions: 1=input, 0=output
    6. TRISB = 0b00000000;
    7. }
    8. char counter;
    9. void main(void)
    10. {
    11. counter = 0;
    12. init();
    13. while (1){
    14. PORTB = counter;
    15. _delay(10000);
    16. counter++;
    17. }
    18. }
    i slightly understand the other commands aside from those two. tnx for any reply. More power allaboutcircuits!.
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    As used here init (actually init() ) is a function call. Have a look at 9 lines above the init() in your code and you'll see
    Code ( (Unknown Language)):
    1. void init(void)
    which is the start of the definition of the function.

    In this instance the call to init() does this:

    Code ( (Unknown Language)):
    1. TRISB=0b00000000;
    All this does is set all the Port B pins to be outputs - as the comment above the line describes.

    More info on this can be found in the datasheet for the chip you are using.
     
    Rk17 likes this.
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    That program you refer to is written for HI-Tech C for PIC MCUs. And will only work for the latter compiler. But the C++ reference does really confuse me. Are we still talking PIC MCUs, and HI-Tech C here :confused:
     
    Rk17 likes this.
  4. Rk17

    Thread Starter Member

    Apr 23, 2011
    43
    0
    @Dumbofixer>> tnx for the reply dumbofixer.. so what is the difference between

    Code ( (Unknown Language)):
    1. void init (void) and void main (void)
    do they have the same function w/c is as you said earlier to start the definition of the function?

    and regarding
    Code ( (Unknown Language)):
    1. TRISB=0b00000000;
    so the 0 means outputs? how bout the
    Code ( (Unknown Language)):
    1. small letter "b"
    what does it mean? do the zeros in the code indicate the pins on the ic? im still really confused.

    @to6afre im using the devc++ software but im still learning the basics of c. I just want to understand the simple program that i posted and relate it to what im learning right now thats why im asking questions about it :)
     
  5. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
  6. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    To answer the first bit, main is the part of the code executed when the program first starts. From there program execution and flow is up to you.

    The '0' indicates that that particular bit in the pport is designated an output, a '1' would designate it an input so you code have a mixture of inputs and outputs on the same port which are pins on the chip itself.

    The "b" signifies that the following number is a binary number, an"x" would signify hex and "o" would be octal. This is used to simplify setting up the bits as a number like "00110011" is easier to see which bits are inputs etc than using 51.
     
    Rk17 likes this.
  7. Rk17

    Thread Starter Member

    Apr 23, 2011
    43
    0
    so if zeros mean outputs, and there are 10 zeros in trisb? that does mean that their are 10 pins being used? tnx dumbofixer..
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You have to learn to crawl before you can walk, and walk before you can run. From your postings I have a feeling that you are still on the crawl level then it comes to PICs. So my advice to you is allow yourself to have a learning curve. Start simple and then you are confident take a step up. Trying to learn all at once will only confuse you. Then it comes to microcontrollers it is also important that you at least have some basic idea about the inner workings. Also knowledge about the binary and hexadecimal number systems are important. All this are topics you must learn by your self. We may give you push in the correct direction. But we will not be able tutor every detail. Hope you understand [​IMG]
     
    Rk17 likes this.
  9. Rk17

    Thread Starter Member

    Apr 23, 2011
    43
    0
    :) yeah i understand to6afre,, maybe im just getting too excited bout this stuff, ive already learned basic c programming in school but that was 2 years ago. I was not that interested at that time not knowing that it was this important in my higher years. Anyway thanks for the help ill continue learning the basics first and ask some questions about things i dont understand much...
     
Loading...