Hi people!
I just bought an ad7787 and I cant get it running.I have read the code you attached but Im having some troubles understanding it, probably because my knowledge about C is not that good. Anyway, I have connected the device to a atmega1281 that I have used a lot, by the SPI. I can see in my oscilloscope that when I send my commands to comm register the sclk and cs lines are correct but the data is not.
Then I try to read the data register but the value I get is 0x80 no matter what value is on the AIN2.
I attached my scheme and my code.
Cheers,
Dan.
/*
* Prueba del ADC con el mote
* Created: 30/07/2012 10:05:44
* Author: Daniel
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <util/delay.h>
#define F_CPU 4000000UL //4 MHz
#include <avr/sfr_defs.h>
//Variables globales
unsigned char data=0x3D,aux2,aux4;
int adc=0x00;
//Mis funciones
void SPI_MasterInit(void);
void _delay_ms (double __ms);
writetoreg(unsigned char data);
int readfromreg();
int main(void)
{
//Configurar puertos e inicializar
SPI_MasterInit();
sei();
while(1)
{
data=0x3D; //continuous mode - ain2(single ended)
writetoreg(data); //write to comm register
adc = readfromreg();//read data
}
}
void SPI_MasterInit(void)
{
/* Set MOSI,SS and SCK output, all others input */
DDRB = (1<<DDB1)|(1<<DDB2)|(1<<DDB0)|(1<<DDB4)|(1<<DDB5);
PORTB = (1<<PB0)|(1<<PB4)|(1<<PB2);//fsync disp1
/* Enable SPI, Master, set clock rate fck/16 , MSB first*/
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(0<<DORD)|(1<<CPOL)|(0<<CPHA);
}
writetoreg(unsigned char data)
{
PORTB = (0<<PB0)|(1<<PB4)|(1<<PB5);//cs low
// Start transmission
SPDR = data;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
aux2 = SPSR;
PORTB = (1<<PB0)|(1<<PB4)|(1<<PB5);// cs high
}
int readfromreg()
{
PORTB = (0<<PB0)|(1<<PB4)|(1<<PB5);//cs low
// Start transmission
while(!(SPSR & (1<<SPIF)));
adc = SPSR;
PORTB = (1<<PB0)|(1<<PB4)|(1<<PB5);// cs high
return adc;
}