hello…please help me to solve this problem.i used adc0848 with 0851 microcontroller.this to control greenhouse using hsm 20g sensor.but the output can’t be display at terminal.what the problem with my coding.i’m using Keil to simulate.
#include<reg51.h> //include file for 8051 reg
#include <stdio.h>
sbit cs = P2^4;
sbit rw = P2^5;
sbit rd = P2^6;
sbit intr = P2^7;
sbit Motor = P3^0;
sbit LED_Spray = P3^1;
sbit FanIn = P3^2;
sbit FanOut = P3^3;
sbit Lamp =P3^4;
float real_temp,real_humi;
//void DelayMs(unsigned int count);
//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count)
{ // mSec Delay 11.0592 Mhz
unsigned int i; // Keil v7.5a
while(count) {
i = 115;
while(i>0) i–;
count–;
}
}
void ConvertAndDisplay (unsigned char value)
{
unsigned char x,d1,d2,d3;
x = value / 10;
d1 = value % 10;
d2= x % 10;
d3 = x /10;
P0 = d1;
DelayMs(250);
P0 = d2;
DelayMs(250);
P0 = d3;
DelayMs(250);
}
//---------------------------------------
// Initialize serial port
//---------------------------------------
void InitSerial(void)
{
SCON = 0x52; // setup serial port control
TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ)
TH1 = 0xFD; // TH1
TR1 = 1; // Timer 1 on
}
//--------------------------------------
//read ADC
//--------------------------------------
char ReadADC(void)
{
unsigned char value;
intr = 1 ;
cs = 1;
rw = 1;
rd = 1;
while(1)
{
//unsigned Read_Humidity;
P1 = 0X0A;
cs =0;
rw =0;
DelayMs(250);
rw=1;
cs =1;
P1 = 0XFF;
while(intr ==1);
cs =0;
rd = 0;
DelayMs(250);
value = P1;
cs = 1;
ConvertAndDisplay (value);
rd = 1;
}
//unsigned Read_Tempreture;
P2 = 0X09;
cs =0;
rw =0;
DelayMs(250);
rw=1;
cs =1;
P2 = 0XFF;
while(intr ==1)
cs =0;
rd = 0;
DelayMs(250);
value = P2;
cs = 1;
ConvertAndDisplay(value);
rd = 1;
return(value);
}
//-------------------------
//condition
//---------------------
void condition(void)
{
if (real_temp >=35)
{
Motor = 1;
FanIn =1;
FanOut=1;
}
if (real_humi >=90)
{
Lamp =1;
}
else if(real_humi <=30)
{
Lamp =0;
LED_Spray= 1;
}
else if(real_humi ==45)
{ Lamp =0;
LED_Spray= 0;
}
if(real_temp==27)
{ Motor = 0;
FanIn =0;
FanOut=0;
Lamp=0;
LED_Spray=0;
}
if(real_humi>=90)
{
FanOut=1;
Lamp=1;
}
else if(real_humi==45)
{
Motor = 0;
FanIn =0;
FanOut=0;
Lamp=0;
LED_Spray=0;
}
else if(real_humi<=30)
{
LED_Spray=1;
}
if(real_temp<=22)
{
if(real_humi<=30)
{
FanIn =1;
LED_Spray=1;
}
else if(real_humi==45)
{
FanOut=0;
Lamp=1;
}
else if(real_humi>=90);
{
FanOut=1;
Lamp=1;
}
}
}
float Read_Humidity()
{
unsigned char humi;
float real_humi;
humi = 0x0A;
humi=ReadADC(); //function call{read adc) sent to humi
real_humi=((humi-13.8)/1.76); //use eq.[y = 1.75x + 13.8]
return real_humi;
}
float Read_Temperature()
{
unsigned char temp;
float real_temp;
temp = 0x09;
temp=ReadADC(); //call function read adc
real_temp = ((temp+10.2)/5.1); //linear equ : y=5.1x - 10.2
return real_temp;
}
void main(void)
{ float temp,humidity;
InitSerial();
while(1)
{
temp=Read_Temperature();
humidity=Read_Humidity();
condition();
printf("Current Humidity :\n%.2f ",Read_Humidity()); //display current humidity
printf(“Current Temperature :\n%.2f”,Read_Temperature());//display current temperture
}
}