codevision fat32
hello can any one help me out regarding my project ...? I am doing a project on AD5933 development .As the chip doesnt work self it need to be interfaced to a microcontroller .The micro controller i am using is AVR5a ATmega 16.I need help regarding the coding part .It needs to be converted from PIC c to AVR c.So pls help me out as soon as posible...i will so graceful .Thanks a lot .I have attached the coding part that needs to be done.
The program, given below in C form and created in MPLAB for PICs, needs to be adapted to run on an AVR microprocessor using the GCC compiler. Several of the commands used in the listing, such as i2c_start(); are not part of standard C and will need converting – you will have to develop or find I2C code for that purpose. The program can be extended as far as you wish, and may also involve Visual programming on a PC to make a graphical display, or link to Excel or MATLAB.
//This program is for the miniature impedance analyser using the AD5933 chip
//The PIC provides control and, through the PWM output on RC5, a clock.
#include "16F684.h"
//----------------------------------------------------------------------------------------------
//this section for PIC16F684 runnning on 10MHz Crystal
#fuses HS, NOPROTECT, NOBROWNOUT, NOMCLR, NOWDT, PUT, NOIESO
#use delay(clock=10000000)
#use rs232(baud=9600,xmit=PIN_A0,rcv=PIN_A2,PARITY=N,BITS=8,ERRORS)
#use I2C(master, sda=PIN_C0, scl=PIN_C1, slow) //comms with AD5933
#define sw1 pin_c2 //switches
#define sw2 pin_c3
#define add_wr 0x1A //I2C write address
#define add_rd 0x1B //I2C read address
//----------------------------------------------------------------------------------------------
//Send a byte (command or data) to a particular register
char byte_write(char reg, char value)
{
i2c_start(); // Start condition 1
i2c_write(add_wr); // Write address 2
i2c_write(reg); // Register Address 3
i2c_write(value); // Register Value 6
return(i2c_stop()); // Stop condition 8, returns ACK (zero if OK)
}
//----------------------------------------------------------------------------------------------
//Read a byte from a location previously set by set_ptr BUT NOTE (data sheet not helpful here)
//You have to set pointer EVERY TIME - so I have made this routine do that.
//it looks like you have to send a lot of data for every measurement.
char read_byte(char location)
{
char value;
byte_write(0xB0, location); // Set pointer
i2c_start(); // Start condition1
i2c_write(add_rd); // Read address 2
value=i2c_read(); // Read register 4
i2c_stop(); // Stop condition 6
return(value);
}
//----------------------------------------------------------------------------------------------
//Checking for specified bit in status register to become true
//Bit 0 = valid temperature, Bit 1 = valid Real/Imaginary Data, Bit 2 = sweep complete
char notready(char b)
{
char status;
status = read_byte(0x8F); //test status register
// printf("%d\r",status&0x07); //display
if(bit_test(status,b)) return(0); //If ready, returns 0
else return(1); //if still not ready, returns 1
}
//**********************************************************************************************
// Send sweep parameters.
// Addition code = (F/(MCLK/4)) x 2^27 so F = code x MCLK/2^29
// and code = F * 2^29 / 500000
void setup_sweep(float f_start, float f_end, long steps)
{
int32 code, temp; //code variables
//Calculate Start code
code = (int32) (f_start * 536870912 / 500000); //initial frequency
byte_write(0x82, make8(code,2));
byte_write(0x83, make8(code,1));
byte_write(0x84, make8(code,0));
//Write number of Increments //max 511 steps
byte_write(0x88, make8(steps,1));
byte_write(0x89, make8(steps,0));
//Calculate Increment code
temp = (int32) (f_end * 536870912 / 500000); //final frequency
temp -= code; //difference
temp /= steps; //divided by no. of increments
byte_write(0x85, make8(temp,2));
byte_write(0x86, make8(temp,1));
byte_write(0x87, make8(temp,0));
//Control: Clock = external
byte_write(0x81, 0x08); //set control (low byte) to ext clock
//Enter standby mode
byte_write(0x80, 0xB1); //standby, PGAx1 (input & output -> GND)
byte_write(0x80, 0x11); //Init Start Frequency, 2.0V p-p, PGA gain = 1
}
//**********************************************************************************************
//Measure Chip Temperature - issue command, wait till valid, read result, scale
void get_t(void)
{
long t;
byte_write(0x80, 0x90); //Command 'measure t' leaving range = 2V
while(notready(0)) ; //wait for valid data
t = read_byte(0x92) * 256; //read
t += read_byte(0x93); //read
printf("Chip Temperature is %ld deg C\r\n",t/32); //Report
}
//**********************************************************************************************
//Get and send Re and Im data registers issue command, wait till valid, read result, scale
void get_Z(long settle)
{
signed long Re, Im; //16-bit results
byte_write(0x8A, make8(settle,1)); //set settling time every step
byte_write(0x8B, make8(settle,0)); //max 511 cycles (can be x2 or x4 in chip)
byte_write(0x80, 0x31); //Command: Go to Next increment
while(notready(1)) ; //wait for valid data
Re = read_byte(0x94) * 256; //Re register high byte
Re += read_byte(0x95); //Re register low byte
Im = read_byte(0x96) * 256; //Im register high byte
Im += read_byte(0x97); //Im register low byte
printf("Re = %4ld, Im = %4ld \r\n",Re, Im); //Report
}
//----------------------------------------------------------------------------------------------
void flash(void)
{
output_high(PIN_C4);
delay_ms(10);
output_low(PIN_C4);
delay_ms(190);
}
//**********************************************************************************************
//Initialise chip functions
//Master clock for AD5933 is derived from PIC pin RC5 (CCP1 out) for low frequency master clock.
//In this application, use example from data sheet Range 1 (100R to 1k0)
//Will set Vout = 2V, PGA gain = 1, supply = 5V, I-V scale resistor = 100R
//Gain does vary with temperature so must compensate (in production anyway)
#define f_st 300 //Start frequency
#define f_nd 400 //End frequency
#define inc 50 //Number of increments
#define settle 100 //Settle for 100 cycles at each frequency
void main(void)
{
long ctr;
setup_ccp1(CCP_PWM); //set up PWM on RC5. PWM driven by Timer 2
setup_timer_2(T2_DIV_BY_1, 4, 1); //500kHz clock = (10MHz/4) / 5
set_pwm1_duty(2); //40% duty cycle
lp:
flash();
if(!input(sw1)) //'GO' button
{
putc('\f'); //form feed
setup_sweep(f_st,f_nd,inc); //Initialise chip, set up sweep, start signal
get_t(); //read and print temperature
byte_write(0x80, 0x21); //Control: Start Sweep
for(ctr=0;ctr<inc;ctr++) //Increment / Read loop
{
printf("%03ld Hz\t",f_st+(f_nd-f_st)/inc*ctr);
get_Z(settle);
if(!input(sw2)) //'STOP' button
{
byte_write(0x81, 0x18); //RESET (interrupts sweep but keep ext clock
break; //exit for loop
}
}
}
goto lp;
}
//----------------------------------------------------------------------------------------------