spi embedded c code for atmega 16

Status
Not open for further replies.

nimmyj

Newbie level 6
Joined
Nov 2, 2012
Messages
12
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Visit site
Activity points
1,359
can anyone help me giving spi embedded c code for interfacing between ad7796 and atmega 16. ad7796 is a Low Power, 16-/24-Bit Sigma-Delta ADC for Bridge Sensors. the foil type strain gauge output is connected to the atmega 16 through ad7796. the interfacing by spi protocol. help needed.
 

Have you utilized the SPI interface of the ATmega16 in past projects?

The source code you received from the Analog Devices forum should give you the step to initialize the device and utilize it.

AD7792.C
Code:
#include<stdio.h>
#include<aduc841.h>
/******************************************************************
DEFINE CONTROL PINS OF ADUC841 FOR THE PURPOSE OF AD7792 CONTROL.
Customers should define the pins according to their design.

******************************************************************/
sbit CS=0x0A0;
sbit SCLOCK=0x0A6;
sbit DIN=0x0A2;
sbit DOUT=0x0A3;

unsigned char DataRead[3];


void WriteToReg(unsigned char ByteData);
void ReadFromReg(unsigned char nByte);
void Delay(unsigned int Time)
{

	while(Time)
	{
		Time--;
	}
}


void main()
{

	int ResetTime;
	/* Set up UART */
	T3CON = 0x086;
	T3FD = 0x08;
	SCON   = 0x052;
	
	printf("Hello\r\n");

	/* PRECONFIGURE...*/
	ResetTime=32;
	SCLOCK=1;

 	CS=0;		  //to keep DIN=1 for 32 sclock to reset the part
 	DIN=1;
 	while(ResetTime--)
	{
		Delay(10);
		SCLOCK=0;
		Delay(10);
 		SCLOCK=1;
	}
 	CS=1;	
	
    printf("Reset\r\n");
	
	
	WriteToReg(0x10); //write to Communication register.The next step is writing to Configuration register.
	WriteToReg(0x00); //set the Configuration bipolar mode.Gain=1.
	WriteToReg(0x80); //Configuration internal reference selected.

	while(1)
	{
	
		WriteToReg(0x08);//write to Communication register.The next step is writing to Mode register.
		WriteToReg(0x20);//set the mode register as single conversion mode.
		WriteToReg(0x00);//inter 64 kHZ clock.internal clock is not available at the clk pin.
		
	   
		
		
		WriteToReg(0x40);//write to Communication register.The next step is to read from Status register.
		ReadFromReg(1);	
		while((DataRead[0]&0x80)==0x80)//wait for the end of convertion by polling the status register RDY bit
	 
		{
		  
			WriteToReg(0x40); 
			ReadFromReg(1);	
		}
		 
		 
		
		WriteToReg(0x58);//write to Communication register.The next step is to read from Data register.
		ReadFromReg(2);	
		printf("Data:%02BX %02BX\r\n",DataRead[0],DataRead[1]); 

        
       
	}

}    


void WriteToReg(unsigned char ByteData) // write ByteData to the register
{
	unsigned char temp;
	int i;	
	CS=0;
	temp=0x80;
	for(i=0;i<8;i++)
	{
 		if((temp & ByteData)==0)
		{		
      		DIN=0;
		}	
 		else
		{
			 DIN=1;
     	}
		SCLOCK=0;
		Delay(10);
	   	SCLOCK=1;
		Delay(10);
 		temp=temp>>1;
	}
	CS=1;
}


void ReadFromReg(unsigned char nByte) // nByte is the number of bytes which need to be read
{
	int i,j;
   	unsigned char temp;
   	DIN=1;
 	CS=0;
    temp=0;
	DOUT=1;

	for(i=0; i<nByte; i++)
	{
		for(j=0; j<8; j++)
	    {
	     	SCLOCK=0;
	     	if(DOUT==0)
	     	{
				temp=temp<<1;
		 	}else
		 	{
				temp=temp<<1;
		 		temp=temp+0x01;
			}
			Delay(10);
	        SCLOCK=1;
			Delay(10);
		  }
		  DataRead[i]=temp;
		  temp=0;
	}
    CS=1;
}


Rather than use a softSPI interface and routine provided in the above source code, you can use the hardware SPI peripheral module available on the ATmega16.

The hardware SPI peripheral module should simplify the task.

BigDog
 

Is there hardware spi peripheral module in Atmega 16? i am a beginner of atmega 16 programming. but i need spi interfacing of Atmega 16 and ad7796
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…