EmbDev.net

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


von vema (Guest)


Attached files:

Rate this post
useful
not useful
Hi,
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 
this
1
    // I have initialized PORTB like this
2
3
    PORTB=0x00;
4
    DDRB=0xBF;
5
   
6
    // SPI initialisation
7
    // SPI clock rate fck/16
8
    // SPI master
9
    // SPI MSB first
10
    // SPI CPOL = 1, CPHA = 1
11
12
    SPCR=0x5D;
13
    PORTB.3 = 1; 
14
    
15
    void main (void){
16
 
17
     printf("adc value :%x\n",ReadAd());
18
    
19
    }
20
21
I wrote all function in another file
22
23
    #define ADC_CS PORTB.3
24
    #define WG_CS PORTB.4
25
    #define MOSI PORTB.5
26
    #define MISO PINB.6
27
    #define SCK PORTB.7
28
29
    #define ADC_CS_PIN PINB.3
30
    #define WG_CS_PIN PINB.4
31
32
    char spi(char data)
33
     {
34
    //Start transmision
35
    SPDR = data;
36
    //Wait for transmision complete
37
    while(!(SPSR & 0x80));
38
    return SPDR;
39
     } 
40
41
    //Sets the waveform generator output to given phase
42
    void SetWGPhase(unsigned int phase)
43
     {
44
    SPCR = 0x5A;
45
    WG_CS = 0;
46
    while(WG_CS_PIN);
47
    spi(0x20);
48
    spi(0x00);
49
    spi((char)((phase>>8)|0xC0));     //Load into phase register 0
50
    spi((char)(phase & 0x00FF));
51
    WG_CS = 1;
52
    }
53
54
    unsigned int ReadAd(void)
55
    {
56
    unsigned int data;
57
     ChipSelectAd(1);
58
    //Read data
59
    CheckStatus();         //Wait for data ready in 
60
                             adc register
61
    spi(0x58);              //Place readinstruction 
62
                            in communication register
63
    data = (spi(0xFF)<<8);    //Read 8 most significant 
64
                             bits from data register
65
    data |= spi(0xFF);      //Read 8 leastsignificant 
66
                             bits from data register  
67
    return data;
68
    
69
    ChipSelectAd(0);
70
    }
71
72
    void CheckStatus(void) 
73
    { 
74
    char adcStatus; 
75
76
    do 
77
    { 
78
    (void) spi(0x40); 
79
    adcStatus = spi(0xFF); 
80
    } while((adcStatus & 0x80) > 0); 
81
    }    
82
                                      
83
    void ChipSelectAd(char s)
84
    {
85
    if(s == 1){
86
        PORTB.3 = 0;    //Switch on AD
87
        while(PINB.3);  //Wait for chip select pin
88
    }
89
        else
90
            PORTB.3 = 1;    //Switch of AD
91
     }
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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.