kbbhushan
Junior Member level 1
Hi,
I am doing a project using ade7753 and atmega32.
I am not able to establish communication between two.
Can someone please help me.
This is spi code.
int main(void)
{
// initialize our libraries
// initialize the UART (serial port)
uartInit();
// set the baud rate of the UART for our debug/reporting output
uartSetBaudRate(9600);
// initialize the timer system
timerInit();
// initialize rprintf system
// - use uartSendByte as the output for all rprintf statements
// this will cause all rprintf library functions to direct their
// output to the uart
// - rprintf can be made to output to any device which takes characters.
// You must write a function which takes an unsigned char as an argument
// and then pass this to rprintfInit like this: rprintfInit(YOUR_FUNCTION);
rprintfInit(uartSendByte);
// initialize vt100 library
vt100Init();
// clear the terminal screen
vt100ClearScreen();
sbi(DDRD,1);
cbi(DDRD,0);
char result;
spi_init();
ade7753_write_to_reg(0x13,0x02);
result = ade7753_read_from_reg(0x13);
rprintf("Result is %d\r\n",(uint8_t)result);
// spi_tx();
// spi_rx();
//}
}
void ade7753_write_to_reg( char addr, char value ){
rprintf("In ade7753_write_to_reg\r\n");
spi_start();
//write address to communication register first
addr = (addr+0x80);
rprintf("This address is being written %d\r\n",addr);
spi_tx(addr);//address of the reigster to write
_delay_us(0.05);
spi_tx(value);//value to be written to register
spi_end();
}
unsigned char ade7753_read_from_reg( char addr)
{
unsigned char regval;
rprintf("In ade7753_read_from_reg\r\n");
spi_start();
//write the address of reading register to communication register first
addr = (addr+0x80);
rprintf("This is the reading register address %d",addr);
_delay_us(4) ; //t9=4uswait till after address is loaded
regval= spi_rx();
return regval;
}
void spi_init(void)
{
/*set MOSI out ; SCK out ; SS out ;*/
DDR_SPI = DDR_SPI | (1 << MOSI) | (1 << SCK) | (1 << SS);
/*Set MISO in */
DDR_SPI = DDR_SPI & ~(1 << MISO);
/*Enable SPI; set Master mode; SPI Mode = 3; f' = f/128 */
SPCR = (1 << SPE) | (0 << DORD) | (1 << MSTR) | ( 0 << CPOL) | ( 1 << CPHA) | (0 << SPR1) | (1 << SPR0);
return;
}
void spi_tx(unsigned char data)
{
/* Start transmission */
SPDR = data;
rprintf("This is in spi_tx data is %d",data);
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
return;
}
unsigned char spi_rx(void)
{
/*Send a dummy character */
SPDR = DUMMY;
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return data register */
rprintf("This is in spi_tx data is %d",SPDR);
return SPDR;
}
void spi_start(void)
{
/*End, if any, previous session */
spi_end();
/*Set SS low - start a new session */
PORT_SPI = PORT_SPI & ~(1 << SS);
return;
}
void spi_end(void)
{
/*Set SS high */
PORT_SPI = PORT_SPI | (1 << SS);
return;
}
I am doing a project using ade7753 and atmega32.
I am not able to establish communication between two.
Can someone please help me.
This is spi code.
int main(void)
{
// initialize our libraries
// initialize the UART (serial port)
uartInit();
// set the baud rate of the UART for our debug/reporting output
uartSetBaudRate(9600);
// initialize the timer system
timerInit();
// initialize rprintf system
// - use uartSendByte as the output for all rprintf statements
// this will cause all rprintf library functions to direct their
// output to the uart
// - rprintf can be made to output to any device which takes characters.
// You must write a function which takes an unsigned char as an argument
// and then pass this to rprintfInit like this: rprintfInit(YOUR_FUNCTION);
rprintfInit(uartSendByte);
// initialize vt100 library
vt100Init();
// clear the terminal screen
vt100ClearScreen();
sbi(DDRD,1);
cbi(DDRD,0);
char result;
spi_init();
ade7753_write_to_reg(0x13,0x02);
result = ade7753_read_from_reg(0x13);
rprintf("Result is %d\r\n",(uint8_t)result);
// spi_tx();
// spi_rx();
//}
}
void ade7753_write_to_reg( char addr, char value ){
rprintf("In ade7753_write_to_reg\r\n");
spi_start();
//write address to communication register first
addr = (addr+0x80);
rprintf("This address is being written %d\r\n",addr);
spi_tx(addr);//address of the reigster to write
_delay_us(0.05);
spi_tx(value);//value to be written to register
spi_end();
}
unsigned char ade7753_read_from_reg( char addr)
{
unsigned char regval;
rprintf("In ade7753_read_from_reg\r\n");
spi_start();
//write the address of reading register to communication register first
addr = (addr+0x80);
rprintf("This is the reading register address %d",addr);
_delay_us(4) ; //t9=4uswait till after address is loaded
regval= spi_rx();
return regval;
}
void spi_init(void)
{
/*set MOSI out ; SCK out ; SS out ;*/
DDR_SPI = DDR_SPI | (1 << MOSI) | (1 << SCK) | (1 << SS);
/*Set MISO in */
DDR_SPI = DDR_SPI & ~(1 << MISO);
/*Enable SPI; set Master mode; SPI Mode = 3; f' = f/128 */
SPCR = (1 << SPE) | (0 << DORD) | (1 << MSTR) | ( 0 << CPOL) | ( 1 << CPHA) | (0 << SPR1) | (1 << SPR0);
return;
}
void spi_tx(unsigned char data)
{
/* Start transmission */
SPDR = data;
rprintf("This is in spi_tx data is %d",data);
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
return;
}
unsigned char spi_rx(void)
{
/*Send a dummy character */
SPDR = DUMMY;
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return data register */
rprintf("This is in spi_tx data is %d",SPDR);
return SPDR;
}
void spi_start(void)
{
/*End, if any, previous session */
spi_end();
/*Set SS low - start a new session */
PORT_SPI = PORT_SPI & ~(1 << SS);
return;
}
void spi_end(void)
{
/*Set SS high */
PORT_SPI = PORT_SPI | (1 << SS);
return;
}