# help for this code

**URL:** <https://community.sparkfun.com/t/help-for-this-code/20716>\
**Category:** MicroMod\
**Tags:** avr-atmel\
**Created:** [May 20, 2011, 3:18am UTC](https://community.sparkfun.com/t/help-for-this-code/20716 "2011-05-20T03:18:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sya](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sya](https://community.sparkfun.com/u/sya)\
**Post date:** [May 20, 2011, 3:18am UTC](https://community.sparkfun.com/t/help-for-this-code/20716/1 "2011-05-20T03:18:48Z")

</div>

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

}

}

---

<div class="post-metadata">

**Author:** ![MichaelN](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelN](https://community.sparkfun.com/u/MichaelN)\
**Post date:** [May 20, 2011, 10:25am UTC](https://community.sparkfun.com/t/help-for-this-code/20716/2 "2011-05-20T10:25:17Z")

</div>

Try giving more information, such as your schematic, and what results you’re actually seeing. Links to datasheets is always a good idea too. And if you want anyone to read code, please make sure it is properly indented, and use the “Code” option when posting to make it easier to read…

---

<div class="post-metadata">

**Author:** ![stevech](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@stevech](https://community.sparkfun.com/u/stevech)\
**Post date:** [May 21, 2011, 7:28am UTC](https://community.sparkfun.com/t/help-for-this-code/20716/3 "2011-05-21T07:28:50Z")

</div>

The OP’s posting here is in an atmel AVR forum. But his code is apparently for an 8051 microprocessor.

That, and the lack of big-picture leaves us unable to help.
