CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Need Help in C question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Lion
Guest







Need Help in C question
PostPosted: Mon May 25, 2009 3:24 am     Reply with quote

Hello everyone,

I am using PIC 16F877 and CCS 3.222 version, MPLAB V8.1 .

My question is that I am using UV tron flame detector.

When the sensor detect the fire, it will output_high.

I am using UV tron and Temperature sensor at the same time.
Code:

if (Temp>30 && input(PIN_D5) )  // when Temperatrue>30 and output
                                                // is high to excute some function
{
//***********function code ***************//
}
else
{

}

Is it correct?

Thank you so much
Ttemah
Guest







PostPosted: Mon May 25, 2009 4:16 am     Reply with quote

Probably.
Only though, if your 'temp' value, is directly reading in degrees. Most sensors, won't...

Best Wishes
Lion
Guest







Any help will be appreciated
PostPosted: Mon May 25, 2009 11:28 am     Reply with quote

Hello everyone,

Maybe I need to describe problem clearly.

I know that I can use " while(! input(PIN_D5)) " to wait for the PIN_D5 go high or use "while(input(PIN_D5))" to wait for the PIN_D5 go low.

My question is that may I use "if" to do the same thing? I mean that I have two conditions for " if " loop. One is " Temp>30 " and the other one is UV sensor output is high.

May I use " if (Temp> 30 && input(PIN_D5))" ? So far, I tried many ways but it can not get in the " if loop " Sometimes when Temp> 30 but PIN_D5 does not high. The code get in the loop.

Do I do something wrong? Could you give me some suggestion?




I really really appreaciate it.
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Mon May 25, 2009 12:42 pm     Reply with quote

I think there is nothing wrong in your code, it's supposed to work. However, I would suggest that you force both conditions to be true inside an infinite loop, as below:

Code:

while (TRUE)
{
   if (Temp>30 && input(PIN_D5) )
   {
      Turn_on_a_led_or_toggle_an_IO_pin;
      delay_1_second;
   }
   else
   {
      Toggle_another_IO_pin;
      delay_1_second;
   }
}


Force temperature to be greater than 30 degrees and check with an oscilloscope that pin D5 is high. The led should be turning on and off each second, or an IO pin should be toggling.

Hope that helps. Regards,
Adriano.
Lion
Guest







I am confused by the C
PostPosted: Mon May 25, 2009 2:57 pm     Reply with quote

Hello everyone,

apcaye!! Thank you for your reply.

Following is my code. I think that I can not add a "while(1)" in there in my situation.

I am confused. I use many "goto" command in my code. Does it damage the code structure?

In Problem 1, I try to check two conditions but it can not get in the "Problme 1" code in the beginning but I tries many method. Now it always get in the "Problem One" code but the flame detector do not detect the fire (if detect the fire, flame detector will output high). I do not understand why the code do not consider the condition?

My second question:
May I use the condition like (input(PIN_A2)!=0) or (input(PIN_A2)>1)
or (input(PIN_A2)==0)

The syntax is correct or not? If the PIN_A2 is used to check the flame detector output is high or low.

I really need someone help me to figure out.
Any help will be really really apprecitated.
Thank you so much
Code:


#include <16F877.h>
#device ADC=10

#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include<stdio.h>
#include <math.h>  // for temperature sensor

void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);

enable_interrupts(global);

//while(1)
//{
       delay_ms(3000);
       output_high(PIN_D0); //go forward ;  this pin do not decide yet.
       output_high(PIN_D1); // go forward ;  this pin do not decide yet.
       output_high(PIN_C0); //Start Fire Gun
       output_high(PIN_C1); //Start barbette

       delay_ms(5000);  // initial ans settle down everything

start1:

//*** Temperature Sensor Code in this line ***///


        printf("Temperature now is in start1 %d\n\r",Temp);
        delay_ms(700);
        Ultrasonic();
        delay_ms(700);

if (Temp>29 && input(PIN_A2))  ==============> Problem 1
{
output_low(PIN_D0);  //stop Mother Vehicle
output_low(PIN_D1);  //stop Mother Vehicle
delay_ms(1000);      //This delay is due to inertia of DC motor
output_low(PIN_C0);  //
delay_ms(2000);     

output_low(PIN_C1); 

//printf("Find the Fire\n\r");
}
}
else if(Temp>29) 
{
start2:
   if(distance6<27&&distance6>16&&distance5>12&&distance5<19)   
        {                                                                   
printf("Distance 6 and 5 are good .Temperature is %d  \n\r ", Temp);
          delay_ms(3000);
          output_low(PIN_D0);   // Stop Forward ;
          output_low(PIN_D1);   // Stop Forward ;
          output_low(PIN_C0);   // Stop Fire Gun;
          output_low(PIN_C1);   // Stop the barbette;
                                             
          delay_ms(1000);

          output_high(PIN_E0);  //Open back door.
          output_high(PIN_C2);  //Open back door.
          delay_ms(500);
          output_low(PIN_C2);
 
//**now DV1 and DV2 go out **//

          delay_ms(1500);   

connect_again:
         
          output_low(PIN_C5);
          delay_ms(500);
          output_high(PIN_C5);
          delay_ms(1500);
//*** wireless connect code.  I do not show in this line***//         
          delay_ms(3000);                 

          if(kbhit())
          {
             TempDV1=getc();       
           
             delay_ms(2500);
             output_low(PIN_C5); // For some function purpose
             delay_ms(1000);
             output_high(PIN_C5);// For some function purpose
             delay_ms(1500);
             
             //*** wireless connect code.  I do not show in this line***// 
             delay_ms(3000);
           }

           if(kbhit())
          {
          TempDV2=getc();     //In here, maybe I need put     TempDV2ACK=getc(); to get ACK return
          delay_ms(2000);
          output_low(PIN_C5);
          }

          if (TempDV1>28 && TempDV1<120 || TempDV2>28 &&
                                                                            TempDV2<120)     
          {
              if (TempDV2>TempDV1+3)  //DV2 is higher than DV1
              {
                  output_high(PIN_D3);   
                  output_high(PIN_D4);   //turn_right
                  delay_ms(7000);       
                  output_low(PIN_D3);    //stop turn_right ;
                  output_low(PIN_D4);    //Stop turn_right ;
 
                  delay_ms(1500);        //for relay, it needs time to release.
                  output_high(PIN_D0);   //  go_straight();
                  output_high(PIN_D1);   //  go_straight();
                  output_high(PIN_C0);
                  output_high(PIN_C1);
 
DV1_Detect:       
       
                  if ( Temp>29&& input(PIN_A2)) 
                      {
                      output_low(PIN_D0);   //stop();
                      output_low(PIN_D1);
                      delay_ms(1000);      //This delay is due to inertia of DC
                                                     //motor
                      output_low(PIN_C0);  //Stop Fire Gun
                      delay_ms(2000);      //this delay is due to inertia of DC
                                                    //motor
                      output_low(PIN_C1);  //Stop the barbette
                      }
                  else
                      {
                      goto DV1_Detect;
                      }
               }
               else if(TempDV1>TempDV2+3)
               {
                  output_high(PIN_B3);   //turn_left();
                  output_high(PIN_C3);   //turn_left();
                  delay_ms(7000);
                  output_low(PIN_B3);    //stop turn_left;
                  output_low(PIN_C3);    //Stop turn_left;
         
                  delay_ms(1500);       
                  output_high(PIN_D0);   //  go_straight();
                  output_high(PIN_D1);   //  go_straight();
                  output_high(PIN_C0);
                  output_high(PIN_C1);

DV2_Detect:

                  if( Temp>29 && input(PIN_A2)) //Detect Temp>29 and check
                                                                //flame detector is
                                                                //output_high
                  {
                      output_low(PIN_D0);   //stop();
                      output_low(PIN_D1);   //stop();
                      delay_ms(1000);      //This delay is due to inertia of DC
                                                    //motor
                      output_low(PIN_C0);  //
                      delay_ms(2000);      //this delay is due to inertia of DC
                                                     //motor
                      output_low(PIN_C1);  //Stop the barbette
                      printf("Find the FIRE ")
                  }
                  else
                  {
                      goto DV2_Detect;
                  }
               }
               else
               {
                 goto connect_again;
               }
          }
          else
          {
          goto connect_again;
          } 
       }
       else
       {
       goto start2;
       }
}
else
{

delay_ms(1000);
goto start1;
}
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Wed May 27, 2009 6:21 am     Reply with quote

You're welcome, Lion. If you can't add a while(1) loop in your code, create another test program with only this simple loop. One way to try to solve a problem is to isolate it, as in this case.

Have you simulated your code, step-by-step? It could help a lot. In MPLAB IDE's Stimulus window, you can simulate an I/O pin going high or low, which is fine to test your "if" statements.

Yes, using "goto" is not a good programming practice, you should avoid them. Try to rewrite your code, replace them with function calls, for example.

"input(PIN_A2)>1" is not a valid test, because the "input" function return a boolean value. The other tests you mentioned are Ok.

Hope that helps! Regards,
Adriano.
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Wed May 27, 2009 6:28 am     Reply with quote

And one more thing: you should initialize your I/O pins, telling the compiler what pins are input and what pins are output. By default, all pins are input. I use to write directly to TRISx registers to do that, but there should be a function for that if you prefer.

Regards,
Adriano.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group