Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC 0832 and AT89C51 code in Keil uV

Status
Not open for further replies.

illegal121

Member level 2
Member level 2
Joined
Jun 20, 2011
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Oman
Activity points
1,650
hello Everyone..
there is my code for interfacing ADC0832 and AT89C51..

#include<reg52.h>
#include<stdio.h>

sbit ADC_CS = P2^0;
sbit ADC_CLK = P2^1;
sbit ADC_DO = P2^2;
sbit ADC_DI = P2^3;


void InitSerial(void);
void write_adc_byte(char data_byte);
char ReadADC(unsigned char channel);
void DelayMs(unsigned int count);



//---------------------------------------
// Main program
//---------------------------------------
void main(void)
{
InitSerial(); // Initialize serial port
while(1)
{
putchar(0x0C); // clear Hyper terminal
printf("Ch 0 : %bu\n\r",ReadADC(0));
printf("Ch 1 : %bu\n\r",ReadADC(1));
DelayMs(100); // Delay about 100 mS
}
}

//---------------------------------------
// 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 analog from ADC
// Single end mode(2 channel)
//---------------------------------------
char ReadADC(unsigned char channel)
{
unsigned char i,k;
unsigned char AdcResult; // 8 bit

ADC_CS=0; // Active chip select
k++; // Delay about 1us
ADC_CLK=0; // make clock low first
k++;k++;
channel = channel? 0xE0 : 0xC0;
k++; // delay about 1us
//--- write command 3 bit ----------
for(i=0; i< 3;i++) {
ADC_DI = (channel & 0x80) != 0;
channel<<=1;
ADC_CLK=1;
k++;k++; // delay about 1us
ADC_CLK=0;
}

//--- read ADC result 8 bit --------
AdcResult=0;
for(i=0;i<8;i++) {
ADC_CLK=1;
k++; // delay about 1us
ADC_CLK=0;
k++; // delay about 1us
AdcResult<<=1;
AdcResult=AdcResult | (ADC_DO & 0x01);
ADC_CLK=1;
k++; // delay about 1us
ADC_CLK=0;
k++; // delay about 1us
}
ADC_CS=1;
return(AdcResult);
}

//---------------------------------------
// 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--;
}

}
this code shows output
ch 0 : 255
ch 1 : 255
Even when I am changing voltages at input of ch0 and ch1... why..?
 

still not properly working... both channel changing together with the same value with wrong digital conversion.. :s

---------- Post added at 02:24 ---------- Previous post was at 02:22 ----------

i think there is something problem with this section..

//--- read ADC result 8 bit --------
AdcResult=0;
for(i=0;i<8;i++) {
ADC_CLK=1;
k++; // delay about 1us
ADC_CLK=0;
k++; // delay about 1us
AdcResult<<=1;
AdcResult=AdcResult | (ADC_DO & 0x01);
ADC_CLK=1;
k++; // delay about 1us
ADC_CLK=0;
k++; // delay about 1us
}
ADC_CS=1;
return(AdcResult);
}
:roll:
 

copy code simulation,what matter with this??? 未命.jpg
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top