Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have created a code automatic waterlevel indicator for micro controller.i have to write a code automatic salt water and pure water collection by interchanging of valves....how can i create using embedded c........
example program is given
C#
#include<reg51.h>
sbit rs=P1^0; 	
sbit rw=P1^1; 	
sbit e=P1^2; 		
sbit quat=P3^0; // Quad water level 
sbit half=P3^1; // half level of tank
sbit quat_3=P3^2; // three -fourth level of tank
sbit full=P3^3; 	//full level of tank
sbit motor=P3^4;  //pin connected to motor

void delay(int k) //delay function
{
int i,j;
for(i=0;i<k;i++)
  for(j=0;j<1275;j++);
}

void write(int j) 
{
rs=1;  //selecting rs pin to data mode
rw=0;  //selecting rw pin to write mode
P2=j;  //putting value on the pins
e=1;  //high pulse 
delay(1);
e=0;	// low pulse
return;
}

void cmd(int j)  //command function
{
P2=j;  //put the data on pins
rs=0;  //selecting rw pin to command mode
rw=0;  //selecting to write
e=1;  
delay(1);
e=0;
return;
}

void puts(char *a) // function to display string on LCD'
{
unsigned int p=0;
for(;a[p]!=0;p++)
write(a[p]);
}

void lcd_init(void) // function to initialise the LCD
{
cmd(0x38); 
delay(1);
cmd(0x0c); //LCD turning on cmd
delay(1);     
cmd(0x01); //clear lcd cmd 
cmd(0x80); // starting point of LCD
}

void main()
{
lcd_init();  	//LCD intialization
quat=half=quat_3=full=1; //configuring as input pins
quat=half=quat_3=full=0; //lowering input pins
motor = 0;
while(1)
{
  if(quat==0&&half==0&&quat_3==0&&full==0)   //when tank is empty
  {
   cmd(0x80);   		// to move the cursor to starting point of LCD
   puts("EMPTY   ");      // dispalys empty on lcd 
		motor=1;							// start motor
  }
  else if(quat==1&&half==0&&quat_3==0&&full==0)	// when tank is quater
  {
   cmd(0x80); 
   puts("QUATER   ");     // dispalys Quarter on lcd
  }
  else if(quat==1&&half==1&&quat_3==0&&full==0)	// when tank is half
  {
    cmd(0x80); 
   puts("HALF    ");      // dispalys half on lcd
  }
  else if(quat==1&&half==1&&quat_3==1&&full==0)	// when tank is three-fourth
  {
   cmd(0x80); 
   puts("3/4 FULL   ");     // dispalys 3/4 full on lcd
  }
  else if(quat==1&&half==1&&quat_3==1&&full==1)	// when tank is full
  {
   cmd(0x80); 
   puts("FULL    ");    // dispalys full on lcd
   motor=0;            // to stop the motor 
  }
 
}
}


What I have tried:

i have tried to use if instead of while ....but i didnt got the desired output>>>>>>>


plzzz give me correct code!!!!help please .......college project....my marks in your hands!!!!!!
Posted
Updated 1-Dec-16 21:18pm
Comments
Dave Kreskowiak 1-Dec-16 22:57pm    
WRONG! Your marks are in YOUR hands, not ours.

We're not going to write your code for you. This is where YOU demonstrate what YOU have learned through the courses, not us.
Mohibur Rashid 2-Dec-16 0:22am    
If your marks are in our hand, you don't deserve it.

The purpose of you work is not getting the mark. Getting the mark identifies how much have you learned. I am guessing, you don't deserve the mark anyway. Sorry for saying that.

Quote:
i have tried to use if instead of while ....
if and while have different usages. There is no point to try to replace one by the other. It only show that you don't know what you do.
Quote:
but i didn't got the desired output
This not informative, we do not even know what is the desired output. Improve the question with useful information about the problem.
Asking questions is a skill[^]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
You I/O port configuration is wrong:
quat=half=quat_3=full=1; //configuring as input pins
quat=half=quat_3=full=0; //lowering input pins

The first line sets the ports to input as required.
But the second line sets them to outputs at low level.

The correct usage is:
C++
sbit mybit = P3^0

// Set port as input
mybit = 1;
// Read input
value = mybit;

// Set port as low level output
mybit = 0;
// Set port as high level output (and input)
mybit = 1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900