Like Tree1Likes
  • 1 Post By Matticus

Code ignores IF Statement

This is a discussion on Code ignores IF Statement within the C Programming forums, part of the General Programming Boards category; Here is my code: Code: char adc(unsigned char); unsigned char temp, temp1, temp2,temp3,temp4,temp5,send; unsigned int temp_res; void Forward (void){ RC3_bit=1;//right ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    Code ignores IF Statement

    Here is my code:

    Code:
    char adc(unsigned char);
    unsigned char temp, temp1, temp2,temp3,temp4,temp5,send;
    
    
    unsigned int temp_res;
    void Forward (void){
            RC3_bit=1;//right forward
            RC4_bit=0;//right reverse
    
            RC6_bit=1;//Left forward
            RC7_bit=0;//Left Reverse
    
    
            RC0_bit=1;//right enable
            RC1_bit=1; //Left enable
    
    }
    void Reverse (void){
            RC3_bit=0;//right forward
            RC4_bit=1;//right reverse
    
            RC6_bit=0;//Left forward
            RC7_bit=1;//Left Reverse
    
    
            RC0_bit=1;//right enable
            RC1_bit=1; //Left enable
    }
    
    void Right (void){
            RC3_bit=1;//right forward
            RC4_bit=0;//right reverse
    
            RC6_bit=0;//Left forward
            RC7_bit=0;//Left Reverse
    
    
            RC0_bit=1;//right enable
            RC1_bit=1; //Left enable
    }
    
    void Left (void){
            RC3_bit=0;//right forward
            RC4_bit=0;//right reverse
    
            RC6_bit=1;//Left forward
            RC7_bit=0;//Left Reverse
    
    
            RC0_bit=1;//right enable
            RC1_bit=1; //Left enable
    }
    
    
    
    void STOP (void){
            RC3_bit=0;//right forward
            RC4_bit=0;//right reverse
    
            RC6_bit=0;//Left forward
            RC7_bit=0;//Left Reverse
    
    
            RC0_bit=1;//right enable
            RC1_bit=1; //Left enable
    }
    
    void main(void) {
    
            ANSELH = 0x00;
            ANSEL = 0x00; // Set AN2 pin for A/D
            TRISC = 0x00; // Set data Direction on Port C (output)
            PORTC = 0x00; // Start with zero on Port C
            TRISB = 0xF0; // Set Port B as an input port
            PORTB = 0x00;
    
            
            C1ON_bit = 0;               // Disable comparators
            C2ON_bit = 0;
            PWM1_Init(8000);
            PWM1_Start();
    
    
        while(1){
    
              temp_res = ADC_Read(2);   // Get 10-bit results of AD conversion
              temp1 = temp_res >>2;
    
              PWM1_Set_Duty(temp1);
    
              delay_ms(100);
             
             if (RB4_bit == 1)
                {
    
                Forward();
    
                }
              
               }
    }
    The code is for a PIC16F690 which is connected to an H-Bridge. The intent of the code is to drive the robot forward whenever RB4 is high.

    Whenever I run the code it ignores the IF statement and the Forward() function is called and the robot immediately goes forward.

    Any ideas?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    What is connected to the port pin that is being read by RB4? Once the robot starts forward, how do you stop it?

    Jim

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Whether I connect RB4 to ground or power it still continues to go forward.

    I initialized PORTB as all inputs.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    I guess to better answer the question. I have a push button connected between RB4 and power.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,548
    FYI: The name of the signals on pin RB4 are "RB4/AN10/SDI/SDA".
    The name AN10 suggests analog input; on PIC chips they normally default to analog.
    You likely need to disable analog input.

    You might also check to make sure SDI and SDA are not enabled.

    From Chip Manual
    The ANSEL and ANSELH registers are used to disable
    the input buffers of I/O pins, which allow analog voltages
    to be applied to those pins without causing excessive
    current. Setting the ANSx bit of a corresponding pin will
    cause all digital reads of that pin to return 0 and also
    permit analog functions of that pin to operate correctly.
    Tim S.
    Last edited by stahta01; 07-02-2012 at 06:40 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Does this port perhaps have internal pullups? Looking at the data sheet for this processor it appears that it does. So show how you have connected your switch to this pin. You will probably need to connect this switch to ground and reverse your logic.

    Jim

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    I set ANSEL = ANSELH = 0x00;

    This should disable the analog input right?

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Jim,

    Whether I connect it to power or ground it continues to go forward.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,279
    Did you initialize your TRISB register?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,548
    Quote Originally Posted by Neil Naik View Post
    I set ANSEL = ANSELH = 0x00;

    This should disable the analog input right?
    I think that should do it.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    When you talk about connecting it to ground, are you still using a switch? Show you connection schematic. You will probably need to connect the switch to ground and change your logic. This pin is driven high by internal inputs. You should also think about adding logic to stop the robot when your switch is not pressed.

    Jim

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Yes matticus I defined it as TRISB = 0xF0;

    I planned on using RB4, RB5, RB6, and RB7 later on after I get the Forward function working.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,279
    Have you tried:

    Code:
    if(RB4_bit == 1)
        Forward();
    else
        STOP();
    Because when RB4 is high, it sets certain outputs.
    I don't see these outputs changing anywhere else in your "while" loop, so when RB4 goes low, the outputs are still going to remain as they were (because they haven't been told to change).
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. do-while loop ignores break
    By edsoneicc in forum C Programming
    Replies: 8
    Last Post: 02-28-2012, 10:19 AM
  2. getchar ignores EOF
    By inliner in forum C Programming
    Replies: 9
    Last Post: 10-21-2010, 11:40 AM
  3. do - while loop ignores getchar()
    By TheIggle in forum C Programming
    Replies: 3
    Last Post: 07-15-2010, 07:51 AM
  4. compiler ignores what i wrote
    By ashinms in forum C Programming
    Replies: 10
    Last Post: 05-07-2009, 02:30 PM
  5. converting c code to c++, the foreach statement
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-12-2006, 11:03 AM

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