veerubiji
Full Member level 2
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 microcontroller with programmable waveform generator. I am successfully doing this and giving this signal to sensor. I want read sensor output from ADC. I have attached simple circuit diagram below. I wrote code like this
from using above code I am not able to read ADC values,nothing is printing.I have checked the signal before ADC using Oscilloscope i am getting signal. So i have tried to read values form AVdd and AIN1 of ADC.For that i have wrote code like this
even still i am not able to get these values. Anyone can suggest me what mistake i am doing and how to modify it. I have attached simple circuit connections please have a look at. I am little bit confusing about MISO and MOSI but they are looking correct.
Thanks in advance.
Code:
// I have initialized PORTB like this
PORTB=0x00;
DDRB=0xBF;
// SPI initialisation
// SPI clock rate fck/16
// SPI master
// SPI MSB first
// SPI CPOL = 1, CPHA = 1
SPCR=0x5D;
PORTB.3 = 1;
void UniCom(void){
switch(Command){
case(INF):
printf("\r\n\r\n");
printf("sweden\r\n");
printf("university\r\n");
printf("masters\r\n");
Command = 0;
break;
case(WGF):
if(Param < 500)
SetWGFreq(Param);
Command = 0;
break;
case(MEM):
printf("adc Value");
ReadAd()
Command = 0;
break;
case(CLEARM):
MeasureAll();
Command = 0;
break;
default:
Command = 0;
break;
}
}
void main(void){
status = 0x0010;
init(); //Initialize controller
SetWGFreq(125); //Initialize waveformgenerator 125 kHz
Command = INF;
debug = 0; //Controls output during motor running
while (1){
if(rx_counter0) getCom();
if(Command) runCom();
-------------------
------------------
#asm("WDR"); //Reset WD timer
} // EOF "while(1)"
} // EOF "main(void)"
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 kHz
void SetWGFreq(unsigned int freq)
{
unsigned long freg;
-----------
-----------
}
unsigned int ReadAd(void)
{
unsigned int data;
ChipSelectAd(1);
//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;
adcStatus = 0xFF;
//Read status
while(!(adcStatus & 0x80))
{
spi(0x40);
adcStatus = spi(0xFF);
}
}
void ChipSelectAd(char s)
{
if(s == 1){
PORTB.3 = 0; //Switch on AD
while(PINB.3); //Wait for chip select pin
}
else
PORTB.3 = 1; //Switch of AD
}
Code:
void Avdd(void)
{
//Set configuration register
spi(0x10);
spi(0x00); //Bipolar, gain 1
spi(0x07); //AVDD monitor selected
}
void PressCh(void)
{
//Set configuration register
spi(0x10);
spi(0x27); //Burnout current, bipolar, gain 128
spi(0x10); //BUF, AIN1 selected
}
void SensorPower(char p)
{
if(p == 1)
p = 0x10;
else
p = 0x00;
//Set mode register
spi(0x08);
spi(p); //PSW on/of
spi(0x09); //50 Hz rejection mode 80 dB
}
unsigned int AnalogVoltCalc(unsigned int raw)
{
return (unsigned int)(raw*0.21423-6988);
}
unsigned int PressCalc(unsigned int raw)
{
//return (unsigned int)(raw*0.91701-30049); //This gives pressure in psia
return (unsigned int)(raw*1.0261-34117); //This gives pressure in psia
}
void MeasureAll(void)
{
ChipSelectAd(1);
SensorPower(1);
Avdd(); //Set adc to measure regulated voltage
for (i = 0; i < 30000; i++); //Give circuit time to settle
//Wait(delay);
for (i = 0; i < 3; i++)
analogVoltRaw = ReadAd();
PressCh(); //Set adc to measure pressure
for (i = 0; i < 30000; i++); //Give circuit time to settle
//Wait(delay);
for (i = 0; i < 3; i++)
pressRaw = ReadAd();
SensorPower(0);
ChipSelectAd(0);
analogVolt = AnalogVoltCalc(analogVoltRaw);
press = PressCalc(pressRaw);
}
Thanks in advance.