CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

AD7746 I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jim Lai



Joined: 20 Jul 2010
Posts: 4

View user's profile Send private message

AD7746 I2C
PostPosted: Tue Jul 20, 2010 10:09 am     Reply with quote

Hallo: I am new here, just wrote code for AD7746. By CCS compiling it shows NO ERROR, also program can run, but I found it run never down since it halted in the line:
Code:

#use i2c(master, sda=Device_SDA, scl=Device_SLC)

Can somebody help me to find errors? Thanks
Code:

#include <18f8722.h>
#device ICD=TRUE
//#use delay(internal=1Mhz)
#fuses HS, NOLVP, NOWDT
#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Device_SDA PIN_B1
#define Device_SLC PIN_B0
#use i2c(master, sda=Device_SDA, scl=Device_SLC)

#define I2C_ADDR_W   0x90
#define I2C_ADDR_R   0x91

#define REG_STATUS      0x00
#define REG_CAP_DATA   0x01
#define REG_VT_DATA      0x04
#define REG_CAP_SETUP   0x07
#define REG_VT_SETUP   0x08
#define REG_EXC_SETUP   0x09
#define REG_CONFIG      0x0A
#define REG_CAP_DAC_A   0x0B
#define REG_CAP_OFFSET   0x0D
#define REG_CAP_GAIN   0x0F
#define REG_CAP_V_GAIN   0x11

#define RESET_ADDR      0xBF
#define CAP_ZERO      0x800000L

void setup()
{
   
   //RESET
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(RESET_ADDR);
   i2c_stop();
   
   //wait for a boot
   delay_ms(1000);
   
   //Set up and configuration   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_CAP_SETUP);
   i2c_write(0x80);         //CAP setup, CAPEN single, continue or calib
   i2c_write(0x81);         //VT setup, VTEN, single conversion, continue, calib
   i2c_write(0x0B);         //Exe setup,EXON only on cap, EXCA, EXC Low=0, High=VDD     
   i2c_write(0x21);         //Configuration continue conversion
   i2c_write(0x00);         //CAP DAC A setup, disable
   i2c_write(0x00);         //CAP DAC B setup, disable
   i2c_stop();
   delay_ms(1000);
}


byte checkStatus()
{
   BYTE status;
   
   i2c_start();
   i2c_write(I2C_ADDR_R);
   i2c_write(REG_STATUS);     
   i2c_start();
   delay_us(10);
   i2c_write(I2C_ADDR_R);
   status = i2c_read(1);
   i2c_stop();
   
   return(status);     
               
}

byte *read3Byte(byte REG_ADDR3)
{
   byte *Array, readyStatus;
   
   while (readyStatus && 0x5) readyStatus = checkStatus();
   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_ADDR3);     
   i2c_start();
   delay_us(10);
   i2c_write(I2C_ADDR_R);
   *Array = i2c_read(1);
   *(Array+1) = i2c_read(1);
   *(Array+2) = i2c_read(0);
   i2c_stop();
   
   return(Array);
}

byte *read2Byte(byte REG_ADDR2)
{
   byte *Array, readyStatus;
   
   while (readyStatus && 0x5) readyStatus = checkStatus();
   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_ADDR2);     
   i2c_start();
   delay_us(10);
   i2c_write(I2C_ADDR_R);
   *Array = i2c_read(1);
   *(Array+1) = i2c_read(0);
   i2c_stop();
   
   return(Array);
}

byte *read6Byte()
{
   byte *Array, readyStatus;
   
   while (readyStatus && 0x5) readyStatus = checkStatus();
   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_CAP_DATA);     
   i2c_start();
   delay_us(10);
   i2c_write(I2C_ADDR_R);
   *Array = i2c_read(1);
   *(Array+1) = i2c_read(1);
   *(Array+2) = i2c_read(1);
   *(Array+3) = i2c_read(1);
   *(Array+4) = i2c_read(1);
   *(Array+5) = i2c_read(0);
   i2c_stop();
   
   return(Array);
}

byte *readREG()
{
   byte *Array, readyStatus;
   
   while (readyStatus && 0x5) readyStatus = checkStatus();
   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_CAP_SETUP);     
   i2c_start();
   delay_us(10);
   i2c_write(I2C_ADDR_R);
   *Array = i2c_read(1);
   *(Array+1) = i2c_read(1);
   *(Array+2) = i2c_read(1);
   *(Array+3) = i2c_read(0);
   i2c_stop();
   
   return(Array);
}

void offsetCalib()
{
   //Set up for offset calib   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_CONFIG);   
   i2c_write(0xFD);         //Configuration offset calib
   i2c_stop();
   delay_ms(5000);
}

void gainCalib()
{
   //Set up for offset calib   
   i2c_start();
   i2c_write(I2C_ADDR_W);
   i2c_write(REG_CONFIG);   
   i2c_write(0xFE);         //Configuration gain calib
   i2c_stop();
   delay_ms(5000);
}

#int_rda
void serial_interrupt()
{
   char c;
   c = getc();
   
   switch(c) {
      case 'o':
         printf("Please waiting for Offset Calibration");
         offsetCalib();
         break;
         
      case 'g':
         printf("Please waiting for Offset Calibration");
         gainCalib();
         break;
         
   }
}

void main()
{
   byte dataArray[6], REGarray[4];
   
   enable_interrupts(int_rda);
   enable_interrupts(global);
   
   setup();
   REGarray = readREG();
   dataArray = read6Byte();
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 20, 2010 11:25 am     Reply with quote

Quote:
byte checkStatus()
{
BYTE status;

i2c_start();
i2c_write(I2C_ADDR_R);
i2c_write(REG_STATUS);
i2c_start();
delay_us(10);
i2c_write(I2C_ADDR_R);
status = i2c_read(1);
i2c_stop();

return(status);

}

You need to do a NACK (parameter 0) on the last i2c_read() in any
transaction.

Also, make sure you have 4.7K pull-up resistors on SDA and SCL.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group