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.

Problem in reading/Writing adc CS5460 Register with Atmega88

Status
Not open for further replies.

bhavesh_emd

Newbie level 1
Joined
Jun 27, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,324
Hello friends,

I am new to ADC cs5460 & AVR atmega88.

I am using CS5460 as adc with atmega88, I have problem in reading/writing adc Registers, (after this solution i can go for adc calibration & conversion.)

Please send me solution AND C Code.
Tools: AVR studio(with GCC c compiler), AVR dragon, atmega88

My C Code as below:

#include<avr/io.h>
#define F_CPU 8000000UL
#include<util/delay.h>

#include<avr/interrupt.h>

/CS5460A commands- 8 bit
#define CONVERT_CONTINOUS 0xE8
#define CONVERT_ONCE 0xE0
..........
.........
//CS5460A register addresses- 8 bit

#define CS5460A_REG_CONFIG 0x00
.........
........
//configure data- 24 bit
#define CLOCK_DIVIDER 0x00000004
............
// CS5460 pin assignment with atmega88
#define ADC_SDO PD7 //output from ADC
#define ADC_SDI PB1 //input to ADC
#define ADC_CS PB2 //cs of ADC
#define ADC_CLK PB5 //clk to ADC

......fn proto types here.................
void CS5460AResetConfigure();
void CS5460AReset();
void CS5460AWriteCommand(unsigned int command); // write 8 bit command/data
void CS5460AWrite24Bits(unsigned long RegisterData,unsigned int RegisterAddress); // (24 bit reg valu, 8 bit address)
unsigned long CS5460ARead24Bits(unsigned int RegisterAddress); //

//***********************
void main()
{

char *ptr;




DDRB= 0x1 |((1<< ADC_CLK) | (1<<ADC_CS) | (1<<ADC_SDI));
// ADC cs, clk as output and Osillator out put to LCD
DDRD=0x77;
// PD2 as CS to LCD , PD3 , PD4 RD and WR of LCD, PD5 as LCD Data, PD6 CD to EEPROM as output, PD3 as RD to LCD, PD7 as SDO to ADC as input
CLKPR = (1<<CLKPCE);
CLKPR = 0x05;
//5- 32 factor 8000 Khz/ 1 = 250 khz, set value

CS5460AResetConfigure();
_delay_ms(500);

do
{
ptr = dis_buffer; // lcd pointer

CS5460AWrite24Bits(CLOCK_DIVIDER, CS5460A_REG_CONFIG);
// to read config regis
_delay_us(100);
sprintf(dis_buffer,"%06X",(CS5460ARead24Bits(CS5460A_REG_CONFIG)));
// regis read


..........LCD code her for display......


} while(1);
} // end main
//*************************
void CS5460AResetConfigure()
{
CS5460AReset();

}

//**************************
void CS5460AReset()
{
unsigned int i;
for (i=0;i<3;i++)
{
CS5460AWriteCommand(CS5460A_SYNC1);
//temp++;
}

CS5460AWriteCommand(CS5460A_SYNC0);
//CS5460AWrite24Bits(SOFT_RESET,CS5460A_REG_CONFIG);//issue soft reset
return;
}
//***************************
void CS5460AWriteCommand(unsigned int command)
{

unsigned int WriteByte;
WriteByte=command;


PORTB = PORTB & ~(1<<ADC_CS); // low adc cs
_delay_us(100);

for( i=7;i>=0;i--) // write 8 bit msb to lsb
{
PORTB = PORTB | (1<< ADC_CLK); // CLK high
_delay_us(100);

if(!(WriteByte & (1<<i)))
{
PORTB = PORTB & ~(1<<ADC_SDI); // data bit lo
_delay_us(100);

}
else
{
PORTB = PORTB | (1<<ADC_SDI); // data bit hi
_delay_us(100);

}
PORTB = PORTB & ~(1<<ADC_CLK); // CLK low
_delay_us(100);
WriteByte=WriteByte<<1;

if(i==0)
break;
}

}

//**************************************
unsigned long CS5460AWrite24Bits(unsigned long RegisterData,unsigned int RegisterAddress)
{
unsigned int Bitcounter;
unsigned int command;

PORTB = PORTB & ~(1<<ADC_CS); // low adc cs
_delay_us(100);
command= (CS5460A_WRITE | RegisterAddress); // write command with Reg Add
CS5460AWriteCommand(command); // call fn to write 8 bit

for(Bitcounter=23;Bitcounter>=0;Bitcounter--)
{
PORTB = PORTB | (1<<ADC_CLK); // CLK high
_delay_us(100);

if(RegisterData & (1<<Bitcounter))
PORTB = PORTB | (1<<ADC_SDI); // data bit high
else
PORTB = PORTB & ~(1<<ADC_SDI); // data bit low

PORTB = PORTB & ~(1<<ADC_CLK); // CLK low
_delay_us(100);
if(Bitcounter==0)
break;
}
PORTB = PORTB | (1<<ADC_CS); // high adc cs


}
//*************************************
unsigned long CS5460ARead24Bits(unsigned int RegisterAddress)
{

unsigned long databits = 0x000000;
int i=0,j=0;

PORTB = PORTB & ~(1<<ADC_CS); // low adc cs
//send read command with register address
unsigned int WriteByte=CS5460A_READ & RegisterAddress;

CS5460AWriteCommand(WriteByte); // write reg addr with read command


for(j=2; j>=0;j--) // write msb to lsb
{
for(i=7;i>=0;i--)
{

PORTB = PORTB | (1<<ADC_CLK); // CLK high
_delay_us(100);

if(!(0xFE & (1<<i))) // write SYNC0 cmd on SDI while reading
{
PORTB = PORTB & ~(1<<ADC_SDI);
_delay_us(100);
}// data low, if clear
else
{PORTB = PORTB | (1<<ADC_SDI); // data high, if set
_delay_us(100);
}


// read SDO
if(PIND & (1<<ADC_SDO)) // read SDO is high from cs5460
{
data[j]= data[j] | (1<<i);} // set corresponding bit high

PORTB = PORTB & ~(1<<ADC_CLK); // CLK low
_delay_us(100);
f(i==0)
break;
}

if(j==0)
break;

}
PORTB = PORTB | (1<<ADC_CS); // high adc cs
databits = (databits) | data[2];
databits = (databits<< 8) | data[1];
databits = (databits<< 8) | data[0];



return(databits);
}
 

hey what is your problem???
you can not write and read the register???
u should give proper clock for the reading purpose....
here is my for reading the register and it was working...

1st you send the command for reading the register with its address...

dout=0x10; //command fo reading the register
serial_out=dout;
P11=0; //make clk low
++nop;
++nop;
adc_cmd(); //send the command to iC

dout1=0xfefefe; //send the sync signal for completing the reading 24bits
serial_out1=dout1;
P11=0;
++nop;
++nop;
adc_read();

adc_cmd()
{
counter = 0x08;
while(counter)
{
if(serial_out & 0x80) // input command to sdi pin
{
din=1;
++nop;
++nop;
}
else
{
Din=0;
++nop;
++nop;
}
P11=1;//sclk high
++nop;
++nop;
++nop;
P11=0;//sclk=0;
++nop;
++nop;
serial_out= serial_out << 1;
++nop;
++nop;
--counter;
}
}

adc_read()
{
counter = 0x18;
while(counter)
{
if(serial_out1 & 0x00800000)
{
din=1;
++nop;
++nop;
}
else
{
Din=0;
++nop;
++nop;
}
P11=1;
++nop;
++nop;
++nop;
P11=0;//sclk=0;
++nop;
++nop;
--counter;
serial_out1 = serial_out1 << 1;
++nop;
++nop;
serial_out1 = serial_out1 << 1;
++nop;
++nop;
a=P12;//dout pin 6 from adc
if(a)
serial_in = serial_in | 0x00000001;
--counter;


still if u need any help u r most welcom
 

hey what is your problem???
you can not write and read the register???
u should give proper clock for the reading purpose....
here is my for reading the register and it was working...

1st you send the command for reading the register with its address...

dout=0x10; //command fo reading the register
serial_out=dout;
P11=0; //make clk low
++nop;
++nop;
adc_cmd(); //send the command to iC

dout1=0xfefefe; //send the sync signal for completing the reading 24bits
serial_out1=dout1;
P11=0;
++nop;
++nop;
adc_read();

adc_cmd()
{
counter = 0x08;
while(counter)
{
if(serial_out & 0x80) // input command to sdi pin
{
din=1;
++nop;
++nop;
}
else
{
Din=0;
++nop;
++nop;
}
P11=1;//sclk high
++nop;
++nop;
++nop;
P11=0;//sclk=0;
++nop;
++nop;
serial_out= serial_out << 1;
++nop;
++nop;
--counter;
}
}

adc_read()
{
counter = 0x18;
while(counter)
{
if(serial_out1 & 0x00800000)
{
din=1;
++nop;
++nop;
}
else
{
Din=0;
++nop;
++nop;
}
P11=1;
++nop;
++nop;
++nop;
P11=0;//sclk=0;
++nop;
++nop;
--counter;
serial_out1 = serial_out1 << 1;
++nop;
++nop;
serial_out1 = serial_out1 << 1;
++nop;
++nop;
a=P12;//dout pin 6 from adc
if(a)
serial_in = serial_in | 0x00000001;
--counter;


still if u need any help u r most welcom
Bhoomi shah ma'm I need the code. Please mail it tome.
My id is abdulmajeed.cet@gmail.com
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top