interfacing atmega32 and ade7758
hello
to all members ,please help me to interface properly the ade7758 and
atmega 32 .i am unable to communicate these two microcontrollers ics.
please help i am send the proteus design as well (proteus 7.4)
///--------------------MAIN file-----------------------------
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/delay.h>
#include <inttypes.h>
#include "lcd_lib.h"
#include "ade758.h"
#define F_CPU 11059200UL
char text[] = { 'Y','A','S','I','R',' ','A','T','E','E','Q','8','\n'};
//Strings stored in AVR Flash memory
//delay 1s
void delay_ms(unsigned int ms)
{
while(ms)
{
_delay_ms(1);
ms--;
}
}
//------------------------------------------------------
int main(void)
{
LCDinit();//init LCD bit, dual line, cursor right
LCDclr();//clears LCD
unsigned char result;
spi_init();
while(1) //loop demos
{
ade7753_write_to_reg(0x93,0x31); //the register is 0x13 in write mode;
delay_ms(50);
result = ade7753_read_from_reg(0x13);
LCDsendChar(result);
delay_ms(1000);
LCDsendChar('A'); //this is a 4-bit lcd write function
while(1); //--stop here do not do any thing
}
return 0;
}
//------------------ADE7758.c---------------------
#include<avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/delay.h>
#include <inttypes.h>
#include "ade758.h"
#define F_CPU 11059200UL
void ade7753_write_to_reg( char addr, char value )
{
spi_start();
//write address to communication register first
spi_tx(addr);//address of the reigster to write
//_delay_us(0.05); //---t7 in the datasheet
_delay_us(0.5); //---but i have given delay very greater than t7=50ns
spi_tx(value);//value to be written to register
spi_end();
}
unsigned char ade7753_read_from_reg( char addr)
{
unsigned char regval;
spi_start();
//write the address of reading register to communication register first
spi_tx(addr);
//_delay_us(4) ;i have given grater delay than 4us so that no doubt.
_delay_us(100);
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/16 */
SPCR = (1 << SPE) | (0 << DORD) | (1 << MSTR) | ( 0 << CPOL) | ( 1 << CPHA) | (0 << SPR1) | (1 << SPR0);
//DORD indicates that the MSB is transmitted first .
return;
}
void spi_tx(unsigned char data)
{
/* Start transmission */
SPDR = data;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
return;
}
unsigned char spi_rx(void)
{
delay_ms(100); //this delay is critical as when we send data from slave to master
/*Send a dummy character */
SPDR = 0x32;
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return data register */
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;
}