Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ad5933+atmega32

Status
Not open for further replies.
This is an international forum and for many people English is not their first language. I am guessing this is the case here so it is very difficult to know what you do or do not know.

You have not given us a lot to go on. We have no idea of your programming skills. We do no know what programming language you are using of your experienced with it.

It is possible that this project may be too difficult for you current level of experience. If so you need to do some simpler microcontroller based projects first. If I2C is your problem maybe learn to program and read back an I2C EEPROM. Build you skills one step at a time.

Do not be suprised if you do not get an answer. It is possible that someone here has experience with the ad5933. Maybe not. If you want code help you have to post the code! Make sure it is readable by using the # icon for inserting code into your post. If it is long include it as a zip. If people can look at the code maybe they can help. It it is too long the changes of getting help is small. Asking for help with a specific problem is better.
 
here is the code:

Code:
Chip type           : ATmega32
Program type        : Application
Clock frequency     : 16.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 512
*****************************************************/

#include <mega32.h>  
#include <math.h>

// I2C Bus functions
#asm
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=0
   .equ __scl_bit=1
#endasm
#include <i2c.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR=tx_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
// Place your code here

}

// Declare your global variables here
    
#define LCDwidth 16	//LCD characters


#define startfreq 200	//Start Frequency


#define incfreq 100	//Incremental Frequency


#define SUCCESS 0xff	//Flag    

//Command
#define Start 0xa4
#define Stop 0x94
#define Trans 0x84
#define ACK 0xc4 

//I2C bus Status
#define START 0x08
#define ReSTART 0x10
#define SLA_W 0x1a
#define SLA_R 0x1b
#define MT_SLA_ACK 0x18
#define MT_SLA_NACK 0x20
#define MR_SLA_ACK 0x40
#define MR_SLA_NACK 0x48
#define MT_DATA_ACK 0x28
#define MT_DATA_NACK 0x30
#define MR_DATA_ACK 0x50
#define MR_DATA_NACK 0x58
#define TWINT 0x80

//AD5933 control codes
#define Init 0x10	//Initialize with start Freq
#define Sweep 0x20	//Start Frequency Sweep
#define IncFreq 0x30	//Increment Frequency
#define RepFreq 0x40	//Repeat Frequency
#define MeaTemp 0x90	//Measure Temperature
#define PowerDown 0xa0	//Power down mode
#define Standby 0xb0	//Standby mode
#define Range2V 0x00	//Output Voltage range 2V
#define Range1V 0x06	//Output Voltage range 1V
#define Range400mV 0x04 //Output Voltage range 400mV
#define Range200mV 0x02 //Output Voltage range 200mV
#define gainx5 0x00	//PGA gain x5
#define gainx1 0x01	//PGA gain x1

//AD5933 Register addresses
#define Control_high 0x80
#define Control_low 0x81
#define Freq_high 0x82
#define Freq_mid 0x83
#define Freq_low 0x84
#define FreqInc_high 0x85
#define FreqInc_mid 0x86
#define FreqInc_low 0x87
#define NumInc_high 0x88
#define NumInc_low 0x89
#define NumSettle_high 0x8a
#define NumSettle_low 0x8b
#define Status 0x8f
#define Temp_high 0x92
#define Temp_low 0x93
#define Real_high 0x94
#define Real_low 0x95
#define Imag_high 0x96
#define Imag_low 0x97   

	
  /****************************************************************************
	Function : char Init_TWI(void)
	Setup the TWI module
	Baudrate : 250kHz @ 16MHz system clock
	Own address : OWN_ADR (Defined in TWI_driver.h)
****************************************************************************/
unsigned char Init_TWI(void)
{
	//TWAR = OWN_ADR;	//Set own slave address
	TWBR = 0x18;            //Set baud-rate to 250 KHz at 16 MHz xtal
	TWCR = 0x04;		//Enable TWI-interface
	return 1;
}
	
	
/****************************************************************************
	Function : void Wait_TWI_int(void)
	Loop until TWI interrupt flag is set
****************************************************************************/
void Wait_TWI_int(void)
{
	while(!(TWCR & TWINT));
}

/****************************************************************************
	Function :unsigned char	Send_start(void)
	Send a START condition to the bus and wait for the TWINT to be set and   
	see the result. If it failed, return the TWSR value, if succeeded, return 
	SUCCESS.
****************************************************************************/
unsigned char	Send_start(void)
{
	TWCR=Start;		//Send START
	
	Wait_TWI_int();		//Wait for TWI interrupt flag to be set

    	if((TWSR & 0xF8)!=START || (TWSR & 0xF8)!=ReSTART)
		return TWSR;	//If it failed, return the TWSR value 			
    	return SUCCESS;		//If succeeded, return SUCCESS
}

/****************************************************************************
	Function : void Send_stop(void)
	Send a STOP condition to the bus
****************************************************************************/
void Send_stop(void)
{
	TWCR = Stop;		//Send a STOP condition
}  

/****************************************************************************
	Function : unsigned char Send_adr(unsigned char adr)							
	Send a SLA+W/R to the bus
****************************************************************************/
unsigned char Send_adr(unsigned char adr)
{
	Wait_TWI_int();		//Wait for TWI interrupt flag set

	TWDR = adr;
	TWCR = Trans;   	//Clear int flag to send byte 

	Wait_TWI_int();		//Wait for TWI interrupt flag set

	if((TWSR & 0xF8)!= MT_SLA_ACK || (TWSR & 0xF8)!= MR_SLA_ACK)	
		return TWSR;	//If NACK received return TWSR					
		
	return SUCCESS;		//Else return SUCCESS
}

/****************************************************************************
	Function : unsigned char Send_byte(unsigned char data)
	Send one byte to the bus.
****************************************************************************/
unsigned char Send_byte(unsigned char data)
{
	Wait_TWI_int();		//Wait for TWI interrupt flag set

	TWDR = data;
 	TWCR = Trans;   	//Clear int flag to send byte 

	Wait_TWI_int();		//Wait for TWI interrupt flag set

	if((TWSR & 0xF8)!= MT_DATA_ACK)					
		return TWSR;	//If NACK received return TWSR															
	return SUCCESS;		//Else return SUCCESS
}

/****************************************************************************
	Function : unsigned char Set_pointer(unsigned char reg_loc)
	Set the pointer to a register location.
****************************************************************************/
unsigned char Set_pointer(unsigned char reg_loc)
{
	Send_start();
 	Send_adr(SLA_W);
    	Send_byte(0xb0);	//Pointer command code '1011 0000'
    	Send_byte(reg_loc);	//a register location at which the pointer points
    	return 1;
}

/****************************************************************************
	Function : unsigned char Byte_write(unsigned char reg_addr, unsigned char data)
	Write a byte to AD5933.
****************************************************************************/
unsigned char Byte_write(unsigned char reg_addr, unsigned char data)
{
	Send_start();
 	Send_adr(SLA_W);
    	Send_byte(reg_addr);
   	Send_byte(data);
   	Send_stop();
	return 1;
}



/****************************************************************************
	Function : unsigned char Byte_read(unsigned char reg_loc)
	Read a byte from AD5933.
****************************************************************************/
unsigned char Byte_read(unsigned char reg_loc)
{
	Set_pointer(reg_loc);	//set the pointer location
	
	//Receive a byte
	Send_start();
 	Send_adr(SLA_R);	
	TWCR = Trans;
	Wait_TWI_int(); 	//Wait for TWI interrupt flag set
	return TWDR;
}


     /****************************************************************************
	Function : unsigned long int Data_proc(unsigned char data_high, unsigned char data_low)
	Calculate the code value from AD5933.
****************************************************************************/
unsigned long int Data_proc(unsigned char data_high, unsigned char data_low)
{
	unsigned long int data;
	data=(unsigned long int)data_high*256+data_low;	
	if(data > 0x7fff)
	{
		data=0x10000-data;
	}	
	return data;
}


/****************************************************************************
	Function : void Display(unsigned long int frequency, unsigned long int impedance)
	A function used to display the measured results.
****************************************************************************/
void Display(unsigned long int frequency, unsigned long int impedance)
{
	char lcd_buffer[16];    	// LCD display buffer
	lcd_init(LCDwidth);      	//initialize the display
    	lcd_clear();       		//clear the display
    	lcd_gotoxy(0,0); 	    	//position to upper left on display
    	lcd_putsf("Freq=");    		//constant string from flash
    	lcd_gotoxy(0,1);  		//position to bottom left on display 
    	lcd_putsf("Imped="); 		//constant string from flash
    
    	//display present freq
    	sprintf(lcd_buffer,"%-li",frequency);
    	lcd_gotoxy(5,0);
    	lcd_puts(lcd_buffer);
    
    	//display measured impedance at present freq 
    	sprintf(lcd_buffer,"%-li",impedance);
    	lcd_gotoxy(6,1);
    	lcd_puts(lcd_buffer);
} 


void main(void)
{      
// Declare your local variables here
 unsigned char i,a,b;
	unsigned long int R,I;		//Real and Imag numbers
	
	unsigned long int impedance;
	//Refence Impedance values used in the system
	unsigned long int ReImpedance[]={200000};
	//Analog switches control code
		   float code,Calcode; 


// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0xB0;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x48;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 250.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xD2;
SPSR=0x00;

// Clear the SPI interrupt flag
#asm
    in   r30,spsr
    in   r30,spdr
#endasm

// I2C Bus initialization
i2c_init();

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
    Init_TWI();   
while (1)
      {
      // Place your code here
		//set start frequency
      	      		 Byte_write(Freq_low, 0x00);
      	    	 Byte_write(Freq_mid ,0x19);
      	    	 Byte_write(Freq_high, 0x00);
            
           	//set Incremental frequency
               	Byte_write(FreqInc_low,0x00);
            	Byte_write(FreqInc_mid,0xC0);
            	Byte_write(FreqInc_high,0x80);
 			
            	//Set Num of Inc
            	Byte_write(NumInc_high, 0x00);
            	Byte_write(NumInc_low, 0x63);
            
            	//Set Num of Settling Time Cycles
            	Byte_write(NumSettle_high, 0x02);
            	Byte_write(NumSettle_low,0x01);
            
            	//Working mode
           	Byte_write(Control_low, 0x00);
            	Byte_write(Control_high, 0xb0);
          

	              Byte_write(Control_high, 0x10);  
            	Byte_write(Control_high, 0x20);
            	while(!(Byte_read(Status) & 0x02));
            
            	a=Byte_read(Real_high);
            	b=Byte_read(Real_low);
            	R=Data_proc(a, b);

            	a=Byte_read(Imag_high);
            	b=Byte_read(Imag_low);
            	I=Data_proc(a, b);
            
            	Calcode=sqrt(R*R+I*I);

		 Byte_write(Control_high, 0x94);  
		  Byte_write(Control_high, 0xA0);
            	impedance=(unsigned long int)(ReImpedance[i]*code/Calcode);
           	Display(startfreq,impedance);    
      };
}
 
Hello,

I have the same problem... which is............ the stop bit which is too short! By adding a simple _ms_delay(1) after sending the stop bit resolves my problems

void Send_stop(void)
{
TWCR = Stop; //Send a STOP condition
_delay_ms(1);
}


Good luck
 
Hi, Any one can you clear me my doubt in this above program.......

Code:
impedance=(unsigned long int)(ReImpedance[i]*[B]code[/B]/Calcode);

In this line what is the meaning of code variable....In that code variable what values will be there??? when i am using this the impedance will be 0. i found in that code variable does not have any values.... what should i do for that?? Pls clear my doubt.. Thanks.....
 
here is the code:

Code:
Chip type           : ATmega32
Program type        : Application
Clock frequency     : 16.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 512
*****************************************************/

#include <mega32.h> 
#include <math.h>

// I2C Bus functions
#asm
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=0
   .equ __scl_bit=1
#endasm
#include <i2c.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR=tx_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
// Place your code here

}

// Declare your global variables here
   
#define LCDwidth 16    //LCD characters


#define startfreq 200    //Start Frequency


#define incfreq 100    //Incremental Frequency


#define SUCCESS 0xff    //Flag   

//Command
#define Start 0xa4
#define Stop 0x94
#define Trans 0x84
#define ACK 0xc4

//I2C bus Status
#define START 0x08
#define ReSTART 0x10
#define SLA_W 0x1a
#define SLA_R 0x1b
#define MT_SLA_ACK 0x18
#define MT_SLA_NACK 0x20
#define MR_SLA_ACK 0x40
#define MR_SLA_NACK 0x48
#define MT_DATA_ACK 0x28
#define MT_DATA_NACK 0x30
#define MR_DATA_ACK 0x50
#define MR_DATA_NACK 0x58
#define TWINT 0x80

//AD5933 control codes
#define Init 0x10    //Initialize with start Freq
#define Sweep 0x20    //Start Frequency Sweep
#define IncFreq 0x30    //Increment Frequency
#define RepFreq 0x40    //Repeat Frequency
#define MeaTemp 0x90    //Measure Temperature
#define PowerDown 0xa0    //Power down mode
#define Standby 0xb0    //Standby mode
#define Range2V 0x00    //Output Voltage range 2V
#define Range1V 0x06    //Output Voltage range 1V
#define Range400mV 0x04 //Output Voltage range 400mV
#define Range200mV 0x02 //Output Voltage range 200mV
#define gainx5 0x00    //PGA gain x5
#define gainx1 0x01    //PGA gain x1

//AD5933 Register addresses
#define Control_high 0x80
#define Control_low 0x81
#define Freq_high 0x82
#define Freq_mid 0x83
#define Freq_low 0x84
#define FreqInc_high 0x85
#define FreqInc_mid 0x86
#define FreqInc_low 0x87
#define NumInc_high 0x88
#define NumInc_low 0x89
#define NumSettle_high 0x8a
#define NumSettle_low 0x8b
#define Status 0x8f
#define Temp_high 0x92
#define Temp_low 0x93
#define Real_high 0x94
#define Real_low 0x95
#define Imag_high 0x96
#define Imag_low 0x97  

   
  /****************************************************************************
    Function : char Init_TWI(void)
    Setup the TWI module
    Baudrate : 250kHz @ 16MHz system clock
    Own address : OWN_ADR (Defined in TWI_driver.h)
****************************************************************************/
unsigned char Init_TWI(void)
{
    //TWAR = OWN_ADR;    //Set own slave address
    TWBR = 0x18;            //Set baud-rate to 250 KHz at 16 MHz xtal
    TWCR = 0x04;        //Enable TWI-interface
    return 1;
}
   
   
/****************************************************************************
    Function : void Wait_TWI_int(void)
    Loop until TWI interrupt flag is set
****************************************************************************/
void Wait_TWI_int(void)
{
    while(!(TWCR & TWINT));
}

/****************************************************************************
    Function :unsigned char    Send_start(void)
    Send a START condition to the bus and wait for the TWINT to be set and  
    see the result. If it failed, return the TWSR value, if succeeded, return
    SUCCESS.
****************************************************************************/
unsigned char    Send_start(void)
{
    TWCR=Start;        //Send START
   
    Wait_TWI_int();        //Wait for TWI interrupt flag to be set

        if((TWSR & 0xF8)!=START || (TWSR & 0xF8)!=ReSTART)
        return TWSR;    //If it failed, return the TWSR value            
        return SUCCESS;        //If succeeded, return SUCCESS
}

/****************************************************************************
    Function : void Send_stop(void)
    Send a STOP condition to the bus
****************************************************************************/
void Send_stop(void)
{
    TWCR = Stop;        //Send a STOP condition
} 

/****************************************************************************
    Function : unsigned char Send_adr(unsigned char adr)                           
    Send a SLA+W/R to the bus
****************************************************************************/
unsigned char Send_adr(unsigned char adr)
{
    Wait_TWI_int();        //Wait for TWI interrupt flag set

    TWDR = adr;
    TWCR = Trans;       //Clear int flag to send byte

    Wait_TWI_int();        //Wait for TWI interrupt flag set

    if((TWSR & 0xF8)!= MT_SLA_ACK || (TWSR & 0xF8)!= MR_SLA_ACK)   
        return TWSR;    //If NACK received return TWSR                   
       
    return SUCCESS;        //Else return SUCCESS
}

/****************************************************************************
    Function : unsigned char Send_byte(unsigned char data)
    Send one byte to the bus.
****************************************************************************/
unsigned char Send_byte(unsigned char data)
{
    Wait_TWI_int();        //Wait for TWI interrupt flag set

    TWDR = data;
     TWCR = Trans;       //Clear int flag to send byte

    Wait_TWI_int();        //Wait for TWI interrupt flag set

    if((TWSR & 0xF8)!= MT_DATA_ACK)                   
        return TWSR;    //If NACK received return TWSR                                                           
    return SUCCESS;        //Else return SUCCESS
}

/****************************************************************************
    Function : unsigned char Set_pointer(unsigned char reg_loc)
    Set the pointer to a register location.
****************************************************************************/
unsigned char Set_pointer(unsigned char reg_loc)
{
    Send_start();
     Send_adr(SLA_W);
        Send_byte(0xb0);    //Pointer command code '1011 0000'
        Send_byte(reg_loc);    //a register location at which the pointer points
        return 1;
}

/****************************************************************************
    Function : unsigned char Byte_write(unsigned char reg_addr, unsigned char data)
    Write a byte to AD5933.
****************************************************************************/
unsigned char Byte_write(unsigned char reg_addr, unsigned char data)
{
    Send_start();
     Send_adr(SLA_W);
        Send_byte(reg_addr);
       Send_byte(data);
       Send_stop();
    return 1;
}



/****************************************************************************
    Function : unsigned char Byte_read(unsigned char reg_loc)
    Read a byte from AD5933.
****************************************************************************/
unsigned char Byte_read(unsigned char reg_loc)
{
    Set_pointer(reg_loc);    //set the pointer location
   
    //Receive a byte
    Send_start();
     Send_adr(SLA_R);   
    TWCR = Trans;
    Wait_TWI_int();     //Wait for TWI interrupt flag set
    return TWDR;
}


     /****************************************************************************
    Function : unsigned long int Data_proc(unsigned char data_high, unsigned char data_low)
    Calculate the code value from AD5933.
****************************************************************************/
unsigned long int Data_proc(unsigned char data_high, unsigned char data_low)
{
    unsigned long int data;
    data=(unsigned long int)data_high*256+data_low;   
    if(data > 0x7fff)
    {
        data=0x10000-data;
    }   
    return data;
}


/****************************************************************************
    Function : void Display(unsigned long int frequency, unsigned long int impedance)
    A function used to display the measured results.
****************************************************************************/
void Display(unsigned long int frequency, unsigned long int impedance)
{
    char lcd_buffer[16];        // LCD display buffer
    lcd_init(LCDwidth);          //initialize the display
        lcd_clear();               //clear the display
        lcd_gotoxy(0,0);             //position to upper left on display
        lcd_putsf("Freq=");            //constant string from flash
        lcd_gotoxy(0,1);          //position to bottom left on display
        lcd_putsf("Imped=");         //constant string from flash
   
        //display present freq
        sprintf(lcd_buffer,"%-li",frequency);
        lcd_gotoxy(5,0);
        lcd_puts(lcd_buffer);
   
        //display measured impedance at present freq
        sprintf(lcd_buffer,"%-li",impedance);
        lcd_gotoxy(6,1);
        lcd_puts(lcd_buffer);
}


void main(void)
{     
// Declare your local variables here
 unsigned char i,a,b;
    unsigned long int R,I;        //Real and Imag numbers
   
    unsigned long int impedance;
    //Refence Impedance values used in the system
    unsigned long int ReImpedance[]={200000};
    //Analog switches control code
           float code,Calcode;


// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xB0;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x48;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 250.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xD2;
SPSR=0x00;

// Clear the SPI interrupt flag
#asm
    in   r30,spsr
    in   r30,spdr
#endasm

// I2C Bus initialization
i2c_init();

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
    Init_TWI();  
while (1)
      {
      // Place your code here
        //set start frequency
                         Byte_write(Freq_low, 0x00);
                   Byte_write(Freq_mid ,0x19);
                   Byte_write(Freq_high, 0x00);
           
               //set Incremental frequency
                   Byte_write(FreqInc_low,0x00);
                Byte_write(FreqInc_mid,0xC0);
                Byte_write(FreqInc_high,0x80);
            
                //Set Num of Inc
                Byte_write(NumInc_high, 0x00);
                Byte_write(NumInc_low, 0x63);
           
                //Set Num of Settling Time Cycles
                Byte_write(NumSettle_high, 0x02);
                Byte_write(NumSettle_low,0x01);
           
                //Working mode
               Byte_write(Control_low, 0x00);
                Byte_write(Control_high, 0xb0);
         

                  Byte_write(Control_high, 0x10); 
                Byte_write(Control_high, 0x20);
                while(!(Byte_read(Status) & 0x02));
           
                a=Byte_read(Real_high);
                b=Byte_read(Real_low);
                R=Data_proc(a, b);

                a=Byte_read(Imag_high);
                b=Byte_read(Imag_low);
                I=Data_proc(a, b);
           
                Calcode=sqrt(R*R+I*I);

         Byte_write(Control_high, 0x94); 
          Byte_write(Control_high, 0xA0);
                impedance=(unsigned long int)(ReImpedance[i]*code/Calcode);
               Display(startfreq,impedance);   
      };
}
 
maxx has not been seen here on ETO for nearly seven years, he is unlikely to see your request.

JimB
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top