Discussion in "Project Doubts" started by    bagusdj    Jan 23, 2013.
Wed Jan 23 2013, 05:13 pm
#1
hey guys, i want to make if function, the value is the adc reading of a sensor. in other reference i see a function like readadc. in this program that i used, i dont see any function like that.

so the example will be :

if(xxx<something)
{
}

xxx=adc value/reading of a sensor

here is the code

// Program to interface LDR using ADC 0808. The output of LDR is displayed on LCD. Controller interrupt is used to generate the clock for driving ADC 0808.
#include<reg52.h>

sbit ale=P1^0;  //address latch enable
sbit oe=P1^3;  //output enable
sbit sc=P1^1;  //start conversion
sbit eoc=P1^2;  //end of conversion
sbit clk=P1^7;  // clock
sbit ADD_A=P1^4;  // Address pins for selecting input channels.
sbit ADD_B=P1^5;
sbit ADD_C=P1^6;
sfr lcd_data_pin=0xA0;  //P2 port
sbit rs=P3^7;
sbit rw=P3^1;
sbit en=P3^6;
sbit triac=P3^0;
sfr input_port=0x80;  //P0 port
unsigned int bitvalue,decimal_value,key,left_value,value,number,ascii1,ascii2,ascii3,flag,key1;

void timer0() interrupt 1  // Function to generate clock of frequency 500KHZ using Timer 0 interrupt.
{
clk=~clk;
}

void delay(unsigned int count)  // Function to provide time delay in msec.
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}

void lcd_command(unsigned char comm)  //Function to send command to LCD.
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(10);
en=0;
}

void lcd_data(unsigned char disp)  //Function to send data to LCD.
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(10);
en=0;
}

lcd_dataa(unsigned char *disp)  //Function to send string data to LCD.
{
int x;
for(x=0;disp[x]!=0;x++)
{
  lcd_data(disp[x]);
}
}

void lcd_ini()  //Function to inisialize the LCD
{
lcd_command(0x38); 
delay(5);
lcd_command(0x0F); 
delay(5);
lcd_command(0x80);  //Force cursor to blink at line 1 positon 0
delay(5);
}

   void BCD()  // Binary to decimal conversion to send the data to LCD
{
  key1++;
  key=0;
  flag=0;
    number=input_port;
    number= number*39;
    number= number/100;
    value=number%10;
number=number/10;
ascii1=value+48;
if(number!=0)
{
  value=number%10;
  number=number/10;
  ascii2=value+48;
    flag=1;
}
else
{
   ascii2=48;
   flag=1;
}
if(number!=0)
{
 value=number%10;
    number=number/10;
    ascii3=value+48;
    key=2;
}
else
{
  ascii3=48;
  key=2;
}
if(key==2)
lcd_data(ascii3);
if(flag==1)
lcd_data(ascii2);
lcd_data(ascii1);
if(key1==3)
{
key1=0;
ascii3=0;
ascii2=0;
ascii1=0;
delay(10);
} 
}

void adc()  //Function to drive ADC
{
while(1)
{
  ADD_C=0;  // Selecting input channel 2 using address lines
  ADD_B=0;
  ADD_A=1;
  delay(2);
  ale=1;
  delay(2);
  sc=1;
  delay(1);
  ale=0;
  delay(1);
  sc=0;
  while(eoc==1);
  while(eoc==0);
  oe=1;
  BCD();
  lcd_command(0x88);
  delay(2);
  oe=0;
}
}

void main()
{
eoc=1;
ale=0;
oe=0;
sc=0;
key1=0;
TMOD=0x02;  //timer0 setting for generating clock of 500KHz using interrupt enable mode.
TH0=0xFD;
IE=0x82;
TR0=1;
lcd_ini();
lcd_dataa("value : ");
lcd_command(0x88);
adc();
}
Thu Jan 24 2013, 02:25 am
#2

adc() is your readadc().. but adc() prints the result instead of returning it.

adc() sets up the signals to get the reading from the ADC 0808
The ADC 0808 sends the data to port 0 also called "input_port".
BCD() then reads the value from "input_port" and prints it.


I see the the word ="comm" is printed in bold.
That means that "comm" has a special meaning to the compiler so should not be used as a variable name.

Thu Jan 24 2013, 10:18 pm
#3



adc() is your readadc().. but adc() prints the result instead of returning it.

adc() sets up the signals to get the reading from the ADC 0808
The ADC 0808 sends the data to port 0 also called "input_port".
BCD() then reads the value from "input_port" and prints it.


I see the the word ="comm" is printed in bold.
That means that "comm" has a special meaning to the compiler so should not be used as a variable name.


ExperimenterUK



what should i do? i try adding this to main() its not working too

if(BCD>20)
{triac=0;}
else
{triac=1;}
Mon Jan 28 2013, 12:49 am
#4
@ bagusdj
do it as
if(number>
20)
{triac=0;}
else
{triac=1;} 
 bagusdj like this.
Mon Jan 28 2013, 09:11 am
#5


@ bagusdj
do it as

if(number>
20)
{triac=0;}
else
{triac=1;} 

majoka



hi there majoka, thanks for replying. i try with this code, compiled successfully but not working in hardware.


// Program to interface TGS2600 using ADC 0809. The output of TGS2600 is displayed on LCD. Controller interrupt is used to generate the clock for driving ADC 0808.
#include<reg52.h>

sbit ale=P1^0;  //address latch enable
sbit oe=P1^3;  //output enable
sbit sc=P1^1;  //start conversion
sbit eoc=P1^2;  //end of conversion
sbit clk=P1^7;  // clock
sbit ADD_A=P1^4;  // Address pins for selecting input channels.
sbit ADD_B=P1^5;
sbit ADD_C=P1^6;
sfr lcd_data_pin=0xA0;  //P2 port
sbit rs=P3^7;
sbit rw=P3^1;
sbit en=P3^6;
sbit triac=P3^0;
sfr input_port=0x80;  //P0 port
unsigned int bitvalue,decimal_value,key,left_value,value,number,ascii1,ascii2,ascii3,flag,key1;
void cct_init(void);
void initint0(void);
void firing(void);

void timer0() interrupt 1  // Function to generate clock of frequency 500KHZ using Timer 0 interrupt.
{
clk=~clk;
}

void delay(unsigned int count)
{
unsigned int i;                         
    while(count) {
        i = 115;
                while(i>
0) i--;
        count--;
}}

void cct_init(void)
 {
 P3=0x05;					 //triac is on when active low
 }

void initINT0(void)
 {
 IT0=1;
 EX0=1;
 EA=1;
 }

void external0_isr(void) interrupt 0
 {
firing(); 
 }

void firing(void)								   //conducted if interrupt occurs
{
if(number>
10 & number<=20)	//1st condition
{triac=1;
delay(5);
triac=0;
delay(1);
triac=1;
EA=0;
EA=1;}
 else if(number>
20 & number<=30)	//2nd condition
{triac=1;
delay(4);
triac=0;
delay(1);
triac=1;
EA=0;
EA=1;}
 else if(number>
30 & number<=40)	//3rd condition
{triac=1;
delay(3);
triac=0;
delay(1);
triac=1;
EA=0;
EA=1;}
 else if(number>
40 & number<=50)	//4th condition
{triac=1;
delay(2);
triac=0;
delay(1);
triac=1;
EA=0;
EA=1;}
 else if(number>
50)		 //5th condition; full power
{triac=0;
EA=0;
EA=1;}
 else
{triac=1;			   //6th condition; triac not firing at all
EA=0;
EA=1;}
}

void lcd_command(unsigned char comm)  //Function to send command to LCD.
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(10);
en=0;
}

void lcd_data(unsigned char disp)  //Function to send data to LCD.
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(10);
en=0;
}

lcd_dataa(unsigned char *disp)  //Function to send string data to LCD.
{
int x;
for(x=0;disp[x]!=0;x++)
{
  lcd_data(disp[x]);
}
}

void lcd_ini()  //Function to inisialize the LCD
{
lcd_command(0x38); 
delay(5);
lcd_command(0x0E); 
delay(5);
lcd_command(0x80);  //Force cursor to blink at line 1 positon 0
delay(5);
}

   void BCD()  // Binary to decimal conversion to send the data to LCD
{
  key1++;
  key=0;
  flag=0;
    number=input_port;
    number= number*39;
    number= number/100;
    value=number%10;
number=number/10;
ascii1=value+48;
if(number!=0)
{
  value=number%10;
  number=number/10;
  ascii2=value+48;
    flag=1;
}
else
{
   ascii2=48;
   flag=1;
}
if(number!=0)
{
 value=number%10;
    number=number/10;
    ascii3=value+48;
    key=2;
}
else
{
  ascii3=48;
  key=2;
}
if(key==2)
lcd_data(ascii3);
if(flag==1)
lcd_data(ascii2);
lcd_data(ascii1);
if(key1==3)
{
key1=0;
ascii3=0;
ascii2=0;
ascii1=0;
delay(10);
} 
}

void adc()  //Function to drive ADC
{
while(1)
{
  ADD_C=0;  // Selecting input channel 2 using address lines
  ADD_B=0;
  ADD_A=1;
  delay(2);
  ale=1;
  delay(2);
  sc=1;
  delay(1);
  ale=0;
  delay(1);
  sc=0;
  while(eoc==1);
  while(eoc==0);
  oe=1;
  BCD();
  lcd_command(0x88);
  delay(2);
  oe=0;
}
}


void main()
{
cct_init();
initINT0();
eoc=1;
ale=0;
oe=0;
sc=0;
key1=0;
TMOD=0x02;  //timer0 setting for generating clock of 500KHz using interrupt enable mode.
TH0=0xFD;
IE=0x82;
TR0=1;
lcd_ini();

lcd_dataa("PPM : ");
lcd_command(0x88);

adc();

}


in this forum, comm is bold but in my keil compiler it is not.


[ Edited Mon Jan 28 2013, 10:11 am ]
Mon Jan 28 2013, 02:42 pm
#6
@ bagusdj
try to understand the things instead of hit and trial
firstly simple trigger the triace if it is in your hardware
it may be a possibility that ADC output is always less than 20 so that condition not meet
debug code step by step at once it would be difficult to debug
Mon Jan 28 2013, 02:47 pm
#7


@ bagusdj
try to understand the things instead of hit and trial
firstly simple trigger the triace if it is in your hardware
it may be a possibility that ADC output is always less than 20 so that condition not meet
debug code step by step at once it would be difficult to debug

majoka


Could you tell me,how to debug sir? I'm using keil c51 as compiler.
Mon Jan 28 2013, 03:38 pm
#8
@ bagusdj
firstly you describe what is your project that you want to do?
Mon Jan 28 2013, 04:15 pm
#9


@ bagusdj
firstly you describe what is your project that you want to do?

majoka



i need my device to detect sensor reading everytime interupt is conducted. at certain value, the triac fired after delayed for certain period, depends on the sensor reading range desired.

ex: if interrupt is conducted and the sensor reading is 34, so triac will be fired after being delayed 3mS. interrupt always occured every 10ms because the interrupt source is zero cross detector. by the next interrupt, it will check the sensor reading again.
Tue Jan 29 2013, 07:37 am
#10
I see a couple of errors.
"number" get changed in bdc() so can't be tested later.
Make a copy, and test the copy.

lastReading=number=input_port;

In firing(void)
you test with
if(number>10 & number<=20)
Here & is the wrong function, use &&

Your test code has a lot of repeated lines. When you see repeated lines look for a neater way of doing things.
Try this
 firing(void)
{
triac=1;
if((lastReading>
10) && (lastReading<=20))      //1st condition
{
delay(5);
}
else if((lastReading>
20) && ( lastReading<=30))        //2nd condition
{
delay(4);
}
else if((lastReading>
30) && (lastReading<=40))        //3rd condition
{
delay(3);
}
else if((lastReading>
40) && (lastReading<=50))        //4th condition
{
delay(2);
}
else if(lastReading>
50)    //5th condition; full power
{
triac=0;
}

// do stuff common to all readings
delay(1);
triac=1;      //default condition; triac not firing at all
EA=0;
EA=1;
}


EDIT
Luckily Bagusdj realised that I missed out the triac=0; needed for most cases.
See his later code.


[ Edited Wed Jan 30 2013, 11:18 pm ]

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

PedroDug
Sun May 19 2024, 05:39 pm
JewelAmuck
Sun May 19 2024, 03:06 pm
Minniemus
Sun May 19 2024, 07:39 am
Andyhet
Sat May 18 2024, 07:28 am
BrettTibre
Fri May 17 2024, 06:14 pm
Gordonfax
Fri May 17 2024, 10:28 am
Davidspils
Fri May 17 2024, 10:19 am
Patricknoind
Fri May 17 2024, 09:53 am