Plzzz给我正确的水位指示器代码? [英] Plzzz give me correct code for water level indicator?

查看:127
本文介绍了Plzzz给我正确的水位指示器代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为微控制器创建了一个代码自动水位指示器。我必须通过交换阀门来编写代码自动盐水和纯水收集....我怎样才能创建使用嵌入式c ....... 。

给出示例程序

 #include< reg51.h> 
sbit rs = P1 ^ 0;
sbit rw = P1 ^ 1;
sbit e = P1 ^ 2;
sbit quat = P3 ^ 0; // 四水位
sbit half = P3 ^ 1; // 坦克的一半
sbit quat_3 = P3 ^ 2; // 三级坦克
sbit full = P3 ^ 3; // 满级坦克
sbit motor = P3 ^ 4; // 引脚连接到电机

void 延迟( int k) // 延迟功能
{
int i,j;
for (i = 0 ; i< k; i ++)
for (j = 0 ; j< 1275; j ++);
}

void write( int j)
{
rs = 1 ; // 选择rs引脚转换为数据模式
rw = 0 ; // 选择rw引脚写入模式
P2 = j; // 将值放在引脚上
e = 1 ; // 高脉冲
延迟( 1 < /跨度>);
e = 0 ; // 低脉冲
返回;
}

void cmd( int j) // 命令功能
{
P2 = j; // 将数据放在引脚上
rs = 0 ; // 选择rw pin为命令模式
rw = 0 ; // 选择编写
e = 1 < /跨度>;
延迟( 1 );
e = 0 ;
return ;
}

void puts( char * a) // 在LCD上显示字符串的功能'
{
unsigned int p = 0 ;
for (; a [p]!= 0 ; p ++)
write (一个[p]);
}

void lcd_init( void // 初始化LCD的功能
{
cmd(0x38);
延迟( 1 );
cmd(0x0c); // LCD启用cmd
延迟( 1 );
cmd(0x01); // clear lcd cmd
cmd(0x80); // LCD的起点
}

void main()
{
lcd_init(); // LCD初始化
quat = half = quat_3 = full = 1 ; // 配置为输入引脚
quat = half = quat_3 = full = 0 ; // 降低输入引脚
motor = 0 ;
while 1
{
if (quat == 0&& half == 0&& quat_3 == 0&& full == 0 // 当tank为空时
{
cmd(0x80); // 将光标移动到LCD的起点
puts( EMPTY); // 在lcd上显示为空
motor = 1 ; // start motor
}
else if (quat == 1&& half == 0&& quat_3 == 0&& full == 0 // 当坦克是quater时
{
cmd(0x80);
puts( QUATER); // dispalys on lcd
}
其他 如果(quat == 1&& half == 1&& quat_3 == 0&& full == 0 // 当tank为half $ b时$ b {
cmd(0x80);
puts( HALF); // 在lcd上显示一半
}
其他 如果(quat == 1&& half == 1&& quat_3 == 1&& full == 0 // 当坦克是四分之三时
{
cmd(0x80);
puts( 3/4 FULL); // 在lcd上显示3/4已满
}
else if (quat == 1& half = = 1&& quat_3 == 1&& full = = 1 // 当油箱满时
{
cmd(0x80);
puts( FULL); // 在lcd上显示完整
motor = 0 ; // 停止电机
}

}
}





我的尝试:



i试图使用if而不是while ....但我没有得到所需的输出>>>>>>>





plzzz给我正确的代码!!!!请帮助.......大学项目....我的标记在你手中!!!!!!

解决方案

Quote:

我试过使用if而不是while ....

如果,而有不同的用法。试图替换另一个是没有意义的。它只表明你不知道你做了什么。

引用:

但我没有得到所需的输出

这不是提供信息,我们甚至不知道什么是所需的输出。通过有关问题的有用信息改进问题。

提出问题是一个问题技能 [ ^ ]



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


你的I / O端口配置错误:

季铵盐=半= quat_3 =满= 1; //配置为输入引脚
quat = half = quat_3 = full = 0; //降低输入引脚



第一行根据需要设置输入端口。

但是第二行将它们设置为低电平输出。



正确的用法是:

 sbit mybit = P3 ^  0  

// 将端口设置为输入
mybit = 1 ;
// 读取输入
= mybit;

// 将端口设置为低电平输出
mybit = 0 ;
// 将端口设置为高级输出(和输入)
mybit = 1 ;


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

#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!!!!!!

解决方案

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.


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:

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;


这篇关于Plzzz给我正确的水位指示器代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆