Serial Comm using AT89C51 and ADC0832

Discussion in 'Embedded Systems and Microcontrollers' started by @m@, Aug 21, 2011.

  1. @m@

    Thread Starter Member

    Mar 9, 2009
    15
    0
    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:confused:..?
     
Loading...