Discussion in "8051 Discussion Forum" started by    shaik    Aug 3, 2010.
Tue Aug 24 2010, 03:20 pm
#31
hello ajay,
Thanks for ur reply.

I change this in program, but still analyser not gives any o/p like some pins have value 1 and some pins have value 0.
I don´t know what is the problem?

Now, I try to get o/p of ADC first on Terminal and then try to see that on Analyser.
Can u tell me how can i see the o/p value of ADC on Terminal?

pls give ur sugestion for this problem.
Thanks in advance.
Wed Aug 25 2010, 12:11 am
#32
>>but still analyser not gives any o/p like some pins have value 1 and some pins have value 0.
I don´t know what is the problem?

well output depends on what value you set on ADC.

>>Can u tell me how can i see the o/p value of ADC on Terminal?
use serial library provided in serial communication tutorial. try it use it once.
 shaik like this.
Wed Aug 25 2010, 01:49 pm
#33
hello ajay,
Thanks for ur reply.

I m giving analog i/p from Pot.
Is there any another possibility to give analog i/p other than Pot?
Can i assign value of analog i/p in program like P1 = 01010101 or not?
But the problem is if i assign value like P1 = 01010101 then this is digital value not analog.


I just try to print ADC o/p value on terminal using UART [combining programs of UART and ADC into one], just check the following code.
#include <stdio.h>

#include "reg_c51.h"

#define channel P1_7    // ADC i/p
short int uart_data;	//UART data

void main (void)
{
	// For ADC
	unsigned int value_converted;  // ADC o/p

	ADCF = 0x80;   // configure channel P1.7 for ADC
        ADCON = 0x20;   // Enable the ADC
        ADCON  &= 0xF8;    // Clear the field SCH[2:0]
        ADCON |= channel;   // Select channel
        ADCON |= 0x08;  // Start conversion in standard mode

        while((ADCON & 0x01)!= 0x01)
        {}      // Wait flag 

        ADCON &= 0xEF; // Clear the End of conversion flag
        value_converted = (ADDH << 2 )+(ADDL); // read the value

        // printf("%d",value_converted);
       //  P2 = value_converted;
        // P0 = (value_converted >
>
 8);
       
 // For UART

 	uart_data = value_converted;

	SCON = 0x50; // uart in mode 1 (8 bit), REN=1 //
	TMOD = TMOD | 0x20 ; // Timer 1 in mode 2 //
	TH1 = 0xFD; // 9600 Bds at 11.059MHz //
	TL1 = 0xFD; // 9600 Bds at 11.059MHz //
	ES = 1; // Enable serial interrupt//
	EA = 1; // Enable global interrupt //
	TR1 = 1; // Timer 1 run //
	
	while(1); // endless //
}

// FUNCTION_PURPOSE: serial interrupt, echo received data.
// FUNCTION_INPUTS: P3.0(RXD) serial input
// FUNCTION_OUTPUTS: P3.1(TXD) serial output

void serial_IT(void) interrupt 4
{
	if (RI == 1)
	{ // if reception occur
		RI = 0; // clear reception flag for next reception
		//uart_data = SBUF; // Read receive data
		SBUF = uart_data; // Send back same data on uart
	}
	else 
		TI = 0; // if emission occur 
} // clear emission flag for next emission


But problem in this program is UART gives only 8 bits o/p, so i defined short int uart_data;, which occupied 2 Bytes = 16 Bits and unsigned int value_converted;, which occupied 4 Bytes = 32 Bits.

I cannot make Value_converted value to short int and cannot make uart_data to unsigned int.
Can u pls tell me wht can i do?

Please give ur sugetion for this problem
Thanks in advance
Wed Aug 25 2010, 11:37 pm
#34
void SerialInit(){
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xFD;
    TL1 = 0xFD;
    TR1 = 1;
}

void tx(unsigned char x){
    SBUF = x;
    while(!TI);
    TI = 0;
}

void SendADCData(unsigned short dat){
    const char ascii[] = "0123456789ABCDEF";
    unsigned char temp;
    temp = (dat>
>
12)&0xF;
    tx(ascii[temp]);
    temp = (dat>
>
8)&0xF;
    tx(ascii[temp]);
    temp = (dat>
>
4)&0xF;
    tx(ascii[temp]);
    temp = dat&0xF;
    tx(ascii[temp]);
}


call SerialInit once in your program in main function. then simply call SendADCData to send ADC data on hyper terminal.

SendADCData(value_converted);


[ Edited Wed Aug 25 2010, 11:38 pm ]
 shaik like this.
Thu Aug 26 2010, 06:03 pm
#35
hello ajay,
Thanks for ur reply.

I had one program in UART for user press any key from keyboard and that pressed key one can see on Terminal [e.g. If i press G then i see G on terminal], see the code.
#include <stdio.h>

#include "reg_c51.h"

short int uart_data;

void main (void)
{
	SCON = 0x50; // uart in mode 1 (8 bit), REN=1 //
	TMOD = TMOD | 0x20 ; // Timer 1 in mode 2 //
	TH1 = 0xFD; // 9600 Bds at 11.059MHz //
	TL1 = 0xFD; // 9600 Bds at 11.059MHz //
	ES = 1; // Enable serial interrupt//
	EA = 1; // Enable global interrupt //
	TR1 = 1; // Timer 1 run //
	
	while(1); // endless //
}

void serial_IT(void) interrupt 4
{
	if (RI == 1)
	{
	   RI = 0; // clear reception flag for next reception //
	   uart_data = SBUF; // Read receive data //
	   SBUF = uart_data;// Send back same data on uart//
	}
	else 
		TI = 0; 
}

This code runs perfect.
I change in my program which u send me now the code looks as follows.
#include <stdio.h>

#include "reg_c51.h"

#define channel P1_7    // ADC i/p

void SerialInit();
void tx(unsigned char x);
void SendADCData(unsigned short dat);

void main()
{
        unsigned short value_converted;  // ADC o/p	
       
        ADCF = 0x80;   // configure channel P1.7 for ADC
        ADCON = 0x20;   // Enable the ADC
        ADCON  &= 0xF8;    // Clear the field SCH[2:0]
        ADCON |= channel;   // Select channel
        ADCON |= 0x08;  // Start conversion in standard mode

        while((ADCON & 0x01)!= 0x01)
        {}      // Wait flag 

        ADCON &= 0xEF; // Clear the End of conversion flag
	value_converted = (ADDH << 2 )+(ADDL); // read the value        

	SerialInit();
	SendADCData(value_converted);

        //printf("%d",value_converted);

        //P2 = value_converted;
	//P0 = (value_converted >
>
 8);
       
        while (1)
        {}
}

void SerialInit()
{
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xFD;
    TL1 = 0xFD;
    TR1 = 1;
}
void tx(unsigned char x)
{
    SBUF = x;
    while(!TI);
    TI = 0;
}
void SendADCData(unsigned short dat)
{
    const char ascii[] = "0123456789ABCDEF";
    unsigned char temp;
    temp = (dat>
>
12)&0xF;
    tx(ascii[temp]);
    temp = (dat>
>
8)&0xF;
    tx(ascii[temp]);
    temp = (dat>
>
4)&0xF;
    tx(ascii[temp]);
    temp = dat&0xF;
    tx(ascii[temp]);
}

After changing in program, still i m not getting ADC o/p on terminal.
Please explain me SendADCData() function specially temp = (dat>>12)&0xF; statement.
As u told me that there was no problem in program, but [from last two weeks] up to today i m not able to catch the problem in program.

May be there was problem in ADC i/p.
As u know i m trying to give ADC i/p through P1_7[Pot-R12 on board], when i test this pin on Osciloscop then i can see the value is changes if i move pot form left to right n vice versa.
So may be there is no problem in ADC i/p.
Is there any another way to give analog signal as i/o to microcontroller?

Then possible problem is in UART, but first program is running so UART also worked on my microcontroller.
Then may be error in program, but program give no error n u also told me that there is no problem in program.
Is there any problem in COM port?

Is there any problem in Microcontroller.
I test the pins of microcontroller, it´s changes the state form 0 to 1 and also i test PWM program, this program also worked.
So may be not problem in microcontroller.

Then wht is the problem why my this ADC program not give any o/p?

From start to and only u help me n clear all douts.
Please help me for this problem.

If anyone told me that here is the problem then i can search n try to solve that problem.
But i not able to catch the problem in program or microcontroller or UART or ADC i/p or...

please give ur advice for this problem
Thanks in advance.



[ Edited Thu Aug 26 2010, 06:06 pm ]
Fri Aug 27 2010, 12:49 am
#36
First sorry from my side as i did not check your program properly, coz i am always in hurry when answering.

OK i will point your mistakes in ADC code.

#define channel P1_7
channel should be the number of ADC channel you are going to use not PIN. Pin configuration is already done by you in
ADCF = 0x80;
above statement.

so you should have defined as..
#define channel 7 // coz you are using channel 7 for analog input

waiting for end of conversion you used
while((ADCON & 0x01)!= 0x01)

it should be actually
while((ADCON & 0x10)!= 0x10)

coz 4th bit of ADCON is ADEOC (end of conversion) bit which is set by hardware to indicate conversion has finished.

There are few minor changes i made to uart code to display correct values. and it is working on my side so it should work on your board too. here is the final updated code.

#include <at89c51cc03.h>
 //change this header file as per your needs

#define channel 7    // ADC i/p

void SerialInit();
void tx(unsigned char x);
void SendADCData(unsigned short dat);

void main()
{
        unsigned short value_converted;  // ADC o/p    
       
        ADCF = 0x80;   // configure channel P1.7 for ADC
        ADCON = 0x20;   // Enable the ADC
        ADCON  &= 0xF8;    // Clear the field SCH[2:0]
        ADCON |= channel;   // Select channel
        ADCON |= 0x08;  // Start conversion in standard mode

        while((ADCON & 0x10)!= 0x10)
        {}      // Wait flag

        ADCON &= 0xEF; // Clear the End of conversion flag
        value_converted = (ADDH << 2 )+(ADDL); // read the value        

        SerialInit();
        SendADCData(value_converted);

        //printf("%d",value_converted);

        //P2 = value_converted;
        //P0 = (value_converted >
>
 8);
       
        while (1)
        {}
}

void SerialInit()
{
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xFD;
    TL1 = 0xFD;
    TR1 = 1;
}
void tx(unsigned char x)
{
    SBUF = x;
    while(!TI);
    TI = 0;
}
void SendADCData(unsigned short dat)
{
    const char ascii[] = "0123456789ABCDEF";
    unsigned char temp;
    temp = ((unsigned short)dat>
>
12)&0xF;
    tx(ascii[temp]);
    temp = ((unsigned short)dat>
>
8)&0xF;
    tx(ascii[temp]);
    temp = ((unsigned short)dat>
>
4)&0xF;
    tx(ascii[temp]);
    temp = dat&0xF;
    tx(ascii[temp]);
}


Test this code, i will explain you what i have done in uart code.
 shaik like this.
Fri Aug 27 2010, 02:00 pm
#37
hello ajay,
Thanks for ur reply.

wht value u got if u run this program?
if possible then send me snep of that o/p ?

bcoz still i m not getting any ADC o/p on Termiinal.

Is there problem in value_converted = (ADDH << 2 )+(ADDL); // read the value statement

bcoz first read ADDL and then ADDH.

I try with this staments
value_converted = ADDL; // read Low byte
value_converted += ADDH << 8; // read High byte


and also with this statments
value_converted = ADDL; // read Low byte
value_converted += ADDH << 2; // read High byte


but this changing with staments i was not getting ADC o/p

Is there any problem in this statments
ADCF = 0x80; // configure channel P1.7 for ADC
ADCON = 0x20; // Enable the ADC


means first i have to enable ADC n then configure channel.

so i also try like first enable n then configure channel.
ADCON = 0x20; // Enable the ADC
ADCF = 0x80; // configure channel P1.7 for ADC


but this changes also not gives o/p.

Pls give me ur sugetion for this problem.
thanks in advance.


[ Edited Fri Aug 27 2010, 06:54 pm ]
Sun Aug 29 2010, 01:16 am
#38
i used keil simulator to read ADC value. and its working fine.

I am sure there must be some problem with your board. or you are using a different crystal frequency.

The program i posted in my last post is working fine as i checked.

i suggest you try out a working serial code on your hardware to confirm the serial link. once you find that code working post that code here.
 shaik like this.
Tue Aug 31 2010, 05:26 pm
#39
hello ajay,
Thanks for ur reply.

I also check the program in Keil Simulator it´s working.
but when i test this program in microcontroller then it´s gives nothing on P2 and P0 ports.
Microtroller works perfect for another program like PWM, toggle pin, LED..

I also make one program like if my pot(P1_7) is low then all bits of P2 pin are low und vice versa.
This program works perfect i check this program on analyser,So means Pot worked as i/p value.

I have one dout that the value i gave in Pot (P1_7) is digital or analog?
Bcoz when i test ADC program (which u correct n send me) then it´s gives nothing on Analyser.
So may be Pot have digital value.
But when i move pot from left to right n right to left then it´s analog signal right?

please clear my this dout.

My seniour says it´s problem in program n i also think there is problem in program, but the same program gives o/p on simulator but not on Analyser.

One another dout is in ADC i must have to define all analog i/p channels n then only one channel have value..? In ADC program i clear all possible analog i/p channel n takes only one channel

wht can i do?

Give any another solution to test program.

please help me for this problem.
thanks in advance.


[ Edited Wed Sep 01 2010, 02:56 pm ]
Wed Sep 01 2010, 08:03 pm
#40
can you post a working serial port code which you got with your kit?
I rechecked the whole program with reference to datasheet. I do not see any problem in code.

I think you should first do a testing of serial communication on your board. once that is confirmed to be working it will be easy to debug your programs.
 shaik like this.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

MnoioCoove
Tue May 21 2024, 02:06 am
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