Forum: µC & Digital Electronics problem with reading external ADC values.

von vema (Guest)

Attached files:

Rate this post
0 useful
not useful
I have started newly working with AVR microcontroller.I am using 
ATmega32-A microcontroller. I have connected one external ADC(AD7798). I 
want read external ADC values using SPI communication.I have tried so 
much but i am not able to get ADC values. My project is I have to 
generate waveform using micro controller with programmable waveform 
generator. I am successfully doing this and giving this signal to 
sensor. I want read sensor output given to ADC and trying to read adc 
value. I have attached simple circuit diagram below. I wrote code like 
    // I have initialized PORTB like this

    // SPI initialisation
    // SPI clock rate fck/16
    // SPI master
    // SPI MSB first
    // SPI CPOL = 1, CPHA = 1

    PORTB.3 = 1; 
    void main (void){
     printf("adc value :%x\n",ReadAd());

I wrote all function in another file

    #define ADC_CS PORTB.3
    #define WG_CS PORTB.4
    #define MOSI PORTB.5
    #define MISO PINB.6
    #define SCK PORTB.7

    #define ADC_CS_PIN PINB.3
    #define WG_CS_PIN PINB.4

    char spi(char data)
    //Start transmision
    SPDR = data;
    //Wait for transmision complete
    while(!(SPSR & 0x80));
    return SPDR;

    //Sets the waveform generator output to given phase
    void SetWGPhase(unsigned int phase)
    SPCR = 0x5A;
    WG_CS = 0;
    spi((char)((phase>>8)|0xC0));     //Load into phase register 0
    spi((char)(phase & 0x00FF));
    WG_CS = 1;

    unsigned int ReadAd(void)
    unsigned int data;
    //Read data
    CheckStatus();         //Wait for data ready in 
                             adc register
    spi(0x58);              //Place readinstruction 
                            in communication register
    data = (spi(0xFF)<<8);    //Read 8 most significant 
                             bits from data register
    data |= spi(0xFF);      //Read 8 leastsignificant 
                             bits from data register  
    return data;

    void CheckStatus(void) 
    char adcStatus; 

    (void) spi(0x40); 
    adcStatus = spi(0xFF); 
    } while((adcStatus & 0x80) > 0); 
    void ChipSelectAd(char s)
    if(s == 1){
        PORTB.3 = 0;    //Switch on AD
        while(PINB.3);  //Wait for chip select pin
            PORTB.3 = 1;    //Switch of AD
I have read data sheet hundreds of times but still I am able to read ADC 
value. I am confusing what mistakes I am doing. I have checked each and 
every pin and I have checked ADC input pin using Oscilloscope, i am 
getting 0.6V input signal. Please help me to solve this problem. 
Tommarow is the deadline for this project. Please any one help me to 
read ADC values.

Thanks in advance.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.