Thread: pLease check my programming~~

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    pLease check my programming~~

    hi..i'm a student..
    and i am really new with c programming..
    i had a project to build a security system...this is the 1st draft of my coding..but it seems to not working after i program it into my microcontroller. can u guys tell me if something wrong with this coding...pleasee...

    //---------------------------------------------
    Code:
    #include <16F877A.h>
    #device ADC=8
    #fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP
    #use delay (clock=4000000)
    #include <LCD.C>
    #include <keyy_padd.c>
    #include <string.h>
    
    temp();
    smoke();
    magnetic();
    
    
    
    char condition;
    unsigned int16 Smoke1, Magnetic1, Temp1;
    char lcd_buffer[5];
    char k;
    float adcValue, currTemp, voltage;
    
      
    
    void do_states(void)
    {
    
       if(condition == 1)                       //condition 1..it will assigned the 					program to go to temp() function 
       {
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Temperature");
       
       temp();
       }
    
       else if(condition == 2)		 //condition 2..it will assigned the 							program to go to smoke() function 
       {
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Smoke");
       smoke();
       }
       
       else if(condition == 3)		 //condition 3..it will assigned the 							program to go to magnetic() function 
       {
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Magnetic");
       magnetic();
       }
       else if(condition == 4)
       {
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Security Running");       //condition 4..it will displayed							       security running.
       }
    
    }
    void main()
    {
    
    setup_adc_ports(ALL_ANALOG);
      setup_adc(ADC_CLOCK_INTERNAL);
     set_tris_c(0b00000000);
    
    
    while(1)                         //will this while continue to loop and                                 checking..the circuit...? regardless of the 				keypad action and  lcd?
    {
    
                set_adc_channel(0);             // set the adc channel to port A0
    	    delay_us(50);
    	    adcValue=read_adc();
    
    	    delay_us(50);
    
    	    voltage=5.000*adcValue/255.000; 	//the calculation for 									temperature circuit
                currTemp = voltage/0.01;
    
                if(adcValue >= 20)
                    output_high(pin_A5);
                   
    
                 else
    
                     output_low(pin_A5);     
                     
                     
                    
                delay_us(250);
                
                set_adc_channel(1);      // change the adc channel to port A1 							(smoke detector)circuit..can i 	really 							change it like this?
                delay_us(50);
    	    adcValue=read_adc();
    
                      if (input(PIN_A1))
    
    			      	output_high(pin_A5);
                      
    
                       else
    
                       output_low(pin_A5);
                      
                    
                    
                    
                
                delay_us(250);
                
                set_adc_channel(2);         // change the adc channel to port A1 							(magnetic detector)circuit..can i 						really change it like this?
                delay_us(50);
    	   adcValue=read_adc();
                
                	if (input(PIN_A2))
    	
    		       	output_high(pin_A5);
                  
    
                 else
    
                     output_low(pin_A5);
                     
    
    }
    
    {
    
    lcd_init();				//start the LCD...
    
    lcd_gotoxy(1,1);
    lcd_putc('\f');
    lcd_putc("Choose 1.Temp 2.Smoke 3.Magnetic");
    
    
       k=get_key();                         //this suppose to make...when we press 					keypad 1...it will go to condition 1
       
       if(k=='1')
       {
          while(1)
          {
           condition = 1;
     
          }
       }
       
        if(k=='2')
       {
          while(1)
          {
           condition = 2;			//this suppose to make...when we press 							keypad 2...it will go to condition 2
       
          }
       }
         if(k=='3')
       {
          while(1)
          {
           condition = 3;			//this suppose to make...when we press 							keypad 3...it will go to condition 3
    
          }
      
       }
         else
             while(1)			//when other key is pressed then it will 					go to condition 4
          {
           condition = 4;
    
          }
    
       
    
    lcd_gotoxy(1,1);                        //indicating that the keypad had been 					press and waited to be executed
    lcd_putc('\f');
    lcd_putc("Security Starting");
    
    
    delay_ms(3000);
    
    if(k=='1')
    {
       do_states();                           //execute the function
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Next Menu");		//displayed after the function had been 					executed...what will the program do after 				this...how am i suppose to make it ask me to 
    				(choose 1.Temp 2.Smoke 3.Magnetic) again?
       
       delay_ms(1000);
       
    }
    if(k=='2')
    {
       do_states();
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Next Menu"); 
       
       delay_ms(1000);
       
    }
    if(k=='3')
    {
       do_states();
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Next Menu");
       
       delay_ms(1000);
       
    }
    
       lcd_gotoxy(1,1);
       lcd_putc('\f');
       lcd_putc("Done");
    }
    }
    
    
    temp()                                      //it will executed the function for 						every condition
    {
    
    if (output_low(pin_A5))
    {
                lcd_gotoxy(1,1);
                lcd_putc('\f');
    	    lcd_putc("curr temp: ");
    	    printf(lcd_buffer,"%3i",currTemp); //this will display the current 						temperature...will it?
    	    lcd_gotoxy(1,1);
    	    lcd_putc(lcd_buffer);
    }
          else if (output_high(pin_A5)) 
         {
                lcd_gotoxy(1,1);
                lcd_putc('\f');
    	   lcd_putc("Warning...FIRE!");
         }
    
          delay_ms(500);
       
    }
    
    
    smoke()
    {
     if (output_low(pin_A5))
     {
                  lcd_gotoxy(1,1);
                   lcd_putc('\f');
              lcd_putc("No FIre");
     }
    			else if (output_high(pin_A5)) 
             {
                      lcd_gotoxy(1,1);
                   lcd_putc('\f');
    				lcd_putc("Warning...FIRE!");
             }      
                
          delay_ms(500);
    }
    
    magnetic()
    {
     if (output_low(pin_A5))
     {
                  lcd_gotoxy(1,1);
                   lcd_putc('\f');
              lcd_putc("Door Lock");
     }
    			else if (output_high(pin_A5)) 
             {
                      lcd_gotoxy(1,1);
                   lcd_putc('\f');
    				lcd_putc("Warning...BREAK IN!");
             }     
                
             delay_ms(500);
    }
    //-----------------------------------------------


    thats all...can u guys please help me...please...please...


    i wanted to make...after the program asked me to (choose 1.temp 2.smoke and 3.magnetic)

    for example i choose keypad 1..
    and after the condition 1 had been executed...i would want the program to asked me again to choose

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    What errors are you getting?

    A short list for now :-

    * you have several inifinite loops :
    Code:
       if(k=='1')
       {
          while(1)
          {
           condition = 1;
     
          }
       }
    The parameter used for your while() loop is "1" which will always be true - so basically the program will execute everything within the while()'s curly braces ({ }) over and over again.

    ie.

    condition = 1
    condition = 1
    condition = 1
    condition = 1
    condition = 1
    .............
    (forever)

    Think about why you are writing the things you are writing. You are trying to abstract a result... Identify the result and translate it into code.

    If you really do intend for your program to sit there looping forever, you probably need to create a single, central loop and have your functionality fall back to it once it's finished doing whatever it needs to do.

    * your functions either need to return a value (eg. int, char etc.) or nothing (void).

    * you have a lot of global values which don't really need to be global - better to declare them within routines you need them and have your functions return their values etc.
    ie.
    Code:
    char condition;
    unsigned int16 Smoke1, Magnetic1, Temp1;
    char lcd_buffer[5];
    char k;
    float adcValue, currTemp, voltage;
    Do these really all need to be global to all aspects of functionality your program implements? For example, do_states uses your condition variable, but does that really need to be global? It doesn't change it... (although even if it did, it wouldn't be necessary, though forget that for now ), so why not just pass it as a parameter do_states(char condition).. ? etc. for all your functions and globals.

    On the whole, I'd probably take a step back and think about how you ought to separate the different parts of your program into the various things they do. Write your code function by function, it will help a lot. 'Function' isn't some kind of weird abstract computing word!! it just means 'function' literally... a function of your code! in truth most computing words are the same... a 'bus' carries data, for example, in the same way our buses carry people
    we are one

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Remove all tabs before you post code, and make sure the code is indented properly by using the "preview post" button.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM