bhavesh_emd
Newbie level 1
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);
}
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);
}