Like Tree4Likes

PIC programming noob needs critique of code and help with syntax/commands

This is a discussion on PIC programming noob needs critique of code and help with syntax/commands within the C Programming forums, part of the General Programming Boards category; I am somewhat new to programming, and very new to C. I have been teaching myself C++, and have learned ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    PIC programming noob needs critique of code and help with syntax/commands

    I am somewhat new to programming, and very new to C. I have been teaching myself C++, and have learned that it's inapplicable to my PIC project . I need help with a project that is pretty in nature and I think I am pretty close to getting most of the code, at least intellectually.
    Ok, so to give you an idea of what I am working on, I am trying to build a circuit that will turn a fan on for a specified amount of time. The circuit will incorporate a rotary switch, a general on/off switch, and, of course, the fan. The following code is what I have so far, any and all comments and insight would be appreciated.
    Code:
    #include <iostream>
    void main ()
    TRISA=0        //   not really sure which pin this turns to output,
                         //   but it needs to be the one the fan is connected to
                         //   and I also need to know how to turn the ports
                         //    the rotary switch is connected to to input
    PORTA=1      //    this feels wrong, but I need to have the Vcc pin turned to input 
                        //    so I can regulate it with a switch in the circuit
    if PORTA=1
    {
            if PORT1=1        //   this is the port that the first pin of the rotary switch is connected to
               {
                 delay_ms(6*10e3)    //   one minute of fan time, and I know the delay cmd is wrong, 
                                                  //    but is there something similar that will turn the port 
               }                               //    on for x amount of milliseconds?
             else if PORT2=1
               {
                  delay_ms(12*10e3)
               }                                //     here the 'on' time would be for two minutes
              else if PORT3=1         //     this syntax may be wrong to designate the RA1-7 ports, 
                                     //      but it's the best I can do for the moment
               {
                  delay_ms(30*10e3)    //five minutes this time
               }
                   else if PORT4=1        // you get the idea....
    }
    return(0)

    I know there is a bunch I am leaving out, but it feels close to right, but am not sure how to phrase it properly in C. Also, it would be nice to use the switch/case cmd to clean it up a bit, but the if/else if works for now. I have read the datasheets for the PIC I am using, and a reviewed a bunch of websites that have example code in C, but have yet to find a definitive source for specific command use for programming PICs. By the by, I am using a p16f88 uC.


    Thanks for any help you could offer.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    C Problems:

    If statements require "(" and ")" around condition.
    "=" is assignment "==" is equals
    #include <iostream> is a C++ header; not valid in C

    PIC Note:
    TRISA=0 /* set all 8 bits to output */
    PORTA=1 /* sets bit zero to 1 and the other seven bits to 0.)

    PORT1 and etc. are not standard naming for PICs.

    Tim S.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Ok, thanks for the corrections. My edited, though not complete code is as follows.
    Code:
    void main ()
    TRISA=1        //  bits all set now to input from rotary switch                    
    PORTA=1                       //    still wrong. any documentation on how to get particular 
                                       //  ports set up in the way i want them to?                     
                                       //    so I can regulate it with a switch in the circuit
    if (TRISA=1)                  // this is supposed to say to the chip, if the switch is on (the main                  power switch) go ahead. 
    {       
              if (PORTA0=1)        //  perhaps this should read 'if bit0' is (whatever the command is to set       a port so that the current will flow from a designated port) set,                                                //   turn on for x amount of seconds.            
              {             
                delay_ms(6*10e3)        //   one minute of fan time, and I know the delay cmd is wrong,                                                      
                                                   but is there something similar that will turn the port              
               }                              //    on for x amount of milliseconds?        
              else if (PORTA1=1)            
               {              
                 delay_ms(12*10e3)             
               }                                //     here the 'on' time would be for two minutes          
               else if (PORTA2=1)         //     this syntax may be wrong to designate the RA1-7 ports,                                    
                                                        but it's the best I can do for the moment             
                {              
                  delay_ms(30*10e3)    //five minutes this time            
                }               
               else if(PORTA3=1)        // you get the idea....
    }
    return(0)
    Last edited by sirkit; 12-08-2011 at 02:36 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    You are joking about fixing you code, right?

    If not read this link If Statements in C - Cprogramming.com Tutorial

    Tim S.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    updated code as per the tut you reco'd. any more thoughts on what commands i need to assign the ports properly? i don't need you to write the code for me per se, i just need to know what the commands are and what they do. thanks again for your help.
    Last edited by sirkit; 12-08-2011 at 02:38 PM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    You still did not fix the code...
    Code:
    if (TRISA=1)
    should be
    Code:
    if (TRISA == 1)
    .

    You also need
    Code:
    {}
    around the body of main.

    There are just way too many errors in your code.

    Don't worry about the PIC for now, just follow a tutorial or 2 to learn C on PC. You need to learn basic syntax first.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Your main declaration should always be "int main()". Which makes it more confusing is you declare it as void then at the end you returned an int.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    Technically in embedded applications running on bare metal, main returning void is fine. It's the only instance where "void main" is fine because there is no run-time/OS to return to. Storing the return value in the return register takes up code space for nothing.

    However, the OP has much bigger issues to worry about at the moment...
    stahta01 likes this.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Ok, thanks for the help, but I still need to know the commands to turn on and off the ports. I realize my code has errors, that was my whole point in posting it. I'll go back and fix it when I know what to fix. I will change the void main to int main, I had intended to do it that way from the beginning, but one of the examples I looked at used void main when programming a PIC so I just did the same. I also realize that the == sign needs to be used and will fix that. I am feeling my way in the dark here, and it's not fun, so if any of you could recommend a particular site to help with PIC programming, that would be really useful. I am teaching myself C along the way, and am now learning about function calls. It's not easy to teach yourself something so complex and I am doing my best. Thanks for the insights you have offered so far.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    Your main declaration should always be "int main()". Which makes it more confusing is you declare it as void then at the end you returned an int.
    Actually in an imbedded application like a microcontroller, void main() is accepable, since there's no operating system to return a value to.

    (And, yes, they nailed on that one too...)

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    The command is something like
    Code:
    LATA |= (1 << 5);
    to output high and
    Code:
    LATD &= ~(1 << 5);
    to output low. Those are bitwise operators, and to understand them, you need to understand boolean logic and binary representation of integers first.

    If you are learning C, don't learn it on a microcontroller. That's like learning to drive on a bus. It makes your life much more difficult, and teaches you all kinds of bad habits (like keep on hacking and changing random things until code works, which is what you are doing right now).

    Learn it properly on a PC first. Once you have a pretty good grasp, move on to microcontroller.

    You won't find a site teaching you C and PIC at the same time, because they all assume people learning to program a PIC already have good understanding of C, since no one should be learning C on a PIC.

    Learning C is hard. Learning embedded programming is also hard, even for people with good programming background. And you are trying to learn both at the same time.
    stahta01 likes this.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    updated code

    Did some more research and came up with this:
    Code:
    #include <pic.h>
    
    void main()
    {
    TRIS A==0       //sets output to fan
    PORTB==0       //CLEARS PORT B
    
    IF (TRISB0==1)
    {
    TRISA==1
    DELAY_MS(6*10E3)     //ONE MINUTE ON
    }
    ELSE IF (TRISB1==1)
    {
    TRISA==1
    DELAY_MS(12*10E3)      //2 MINUTES ON
    }
    ELSE IF (TRISB2==1)
    {
    TRISA==1
    DELAY_MS(30*10E3)       //5 MINUTES ON
    }
    ELSE IF (TRISB3==1)
    {
    TRISA==1
    DELAY_MS(60*10E3)         //10 MINUTES ON
    }
    ELSE IF (TRISB4==1)
    {
    TRISA==1
    DELAY_MS(120*10E3)       //20 MINUTES ON
    }
    ELSE IF (TRISB5==1)
    {
    TRISA==1
    DELAY_MS(180*10E3)       //30 MINUTES
    }
    ELSE IF (TRISB6==1)
    {
    TRISA==1
    DELAY_MS(360*10E3)      //ONE HOUR ON
    }
    ELSE IF (TRISB7==1)
    {
    TRISA==1
    DELAY_MS(720*10E3)     //2 HOURS ON
    }
    }
    RETURN ()
    Is this right?

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    You are missing a lot of ";" semi-colons.

    It is therefore obvious that you have never even tried to compile this C source code.

    NOTE: C is a case sensitive language where "if" does NOT mean the same as "IF".

    Tim S.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    point taken on the compilation. will do it tomorrow and see what's what. thanks too for the tip on cap sensitivity. good night

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    From a 10 seconds glance, I saw at least 5 different errors. Now you are really just wasting time trying random things and hoping it will accidentally work.

    That won't improve your programming.
    stahta01 and Salem like this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can some one help me with this..... C programming noob here.
    By kkmoslehpour in forum C Programming
    Replies: 11
    Last Post: 04-27-2011, 12:08 PM
  2. Linked List question from a newb(Need code Critique)
    By Gatt9 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2006, 02:11 AM
  3. Critique my code?
    By bigbadbowlindud in forum C++ Programming
    Replies: 20
    Last Post: 06-24-2004, 12:54 PM
  4. Running external commands from within c code
    By Nova_Collision in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2004, 12:23 PM
  5. could ya'll critique this code
    By linuxdude in forum C Programming
    Replies: 0
    Last Post: 02-19-2004, 08:55 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21