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

Help with my code

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



Joined: 19 Nov 2007
Posts: 9

View user's profile Send private message

Help with my code
PostPosted: Mon Dec 03, 2007 8:40 pm     Reply with quote

I'm just looking for someone to take a look at the code I wrote, because I'm not sure if it's correct or not. I think I coded everything correct, but when I look at the addresses to see the values that I read from certain registers, they aren't the correct values, so I don't know if it's my code or something else.

Here's my header file I wrote, I have a couple of other header files, but they are only a list of definitions. It's pretty long because their are a lot of addresses to configure, but it's all the same thing:

Code:

#use i2c(Master, sda=PIN_C4, scl=PIN_C3)

void ConfigAD7147(void)
{
//initializing some simple variables so I can read back
//and store them in specific values
int data1, data2, LOW_LIMIT_DATA1, LOW_LIMIT_DATA2;
int HIGH_LIMIT_DATA1, HIGH_LIMIT_DATA2, COMPLETE_LIMIT_DATA1, COMPLETE_LIMIT_DATA2;

//Bank 2 Registers 0x80-0xDF
i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(STAGE0_CONNECTION);
i2c_write(0xFF);
i2c_write(0xFE);   //STAGE0_CONNECTION [6:0]
i2c_write(0x1F);
i2c_write(0xFF);   //STAGE0_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE0_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE0_SENSITIVITY
i2c_write(0x02);
i2c_write(0x58);   //STAGE0_OFFSET_LOW
i2c_write(0x01);
i2c_write(0xF4);   //STAGE0_OFFSET_HIGH
i2c_write(0x02);
i2c_write(0x58);   //STAGE0_OFFSET_HIGH_CLAMP
i2c_write(0x02);
i2c_write(0x58);   //STAGE0_ OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFB);   //STAGE1_CONNECTION [6:0]
i2c_write(0x1F);
i2c_write(0xFF);   //STAGE1_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE1_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE1_SENSITIVITY
i2c_write(0x03);
i2c_write(0xB6);   //STAGE1_OFFSET_LOW
i2c_write(0x03);
i2c_write(0x20);   //STAGE1_OFFSET_HIGH
i2c_write(0x03);
i2c_write(0xB6);   //STAGE1_OFFSET_HIGH_CLAMP
i2c_write(0x03);
i2c_write(0xB6);   //STAGE1_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xEF);   //STAGE2_CONNECTION [6:0]
i2c_write(0x1F);
i2c_write(0xFF);   //STAGE2_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE2_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE2_SENSITIVITY
i2c_write(0x05);
i2c_write(0x78);   //STAGE2_OFFSET_LOW
i2c_write(0x04);
i2c_write(0xB0);   //STAGE2_OFFSET_HIGH
i2c_write(0x05);
i2c_write(0x78);   //STAGE2_OFFSET_HIGH_CLAMP
i2c_write(0x05);
i2c_write(0x78);   //STAGE2_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xBF);   //STAGE3_CONNECTION [6:0]
i2c_write(0x1F);
i2c_write(0xFF);   //STAGE3_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE3_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE3_SENSITIVITY
i2c_write(0x07);
i2c_write(0xD0);   //STAGE3_OFFSET_LOW
i2c_write(0x07);
i2c_write(0x08);   //STAGE3_OFFSET_HIGH
i2c_write(0x07);
i2c_write(0xD0);   //STAGE3_OFFSET_HIGH_CLAMP
i2c_write(0x07);
i2c_write(0xD0);   //STAGE3_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE4_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE4_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE4_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE4_SENSITIVITY
i2c_write(0x0A);
i2c_write(0x8C);   //STAGE4_OFFSET_LOW
i2c_write(0x09);
i2c_write(0xC4);   //STAGE4_OFFSET_HIGH
i2c_write(0x0A);
i2c_write(0x8C);   //STAGE4_OFFSET_HIGH_CLAMP
i2c_write(0x0A);
i2c_write(0x8C);   //STAGE4_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE5_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE5_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE5_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE5_SENSITIVITY
i2c_write(0x0D);
i2c_write(0x16);   //STAGE5_OFFSET_LOW
i2c_write(0x0B);
i2c_write(0xB8);   //STAGE5_OFFSET_HIGH
i2c_write(0x0D);
i2c_write(0x16);   //STAGE5_OFFSET_HIGH_CLAMP
i2c_write(0x0D);
i2c_write(0x16);   //STAGE5_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE6_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE6_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE6_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE6_SENSITIVITY
i2c_write(0x02);
i2c_write(0x8A);   //STAGE6_OFFSET_LOW
i2c_write(0x01);
i2c_write(0xF4);   //STAGE6_OFFSET_HIGH
i2c_write(0x02);
i2c_write(0x8A);   //STAGE6_OFFSET_HIGH_CLAMP
i2c_write(0x02);
i2c_write(0x8A);   //STAGE6_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE7_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE7_CONNECTION[12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE7_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE7_SENSITIVITY
i2c_write(0x04);
i2c_write(0x7E);   //STAGE7_OFFSET_LOW
i2c_write(0x03);
i2c_write(0xE8);   //STAGE7_OFFSET_HIGH
i2c_write(0x04);
i2c_write(0x7E);   //STAGE7_OFFSET_HIGH_CLAMP
i2c_write(0x04);
i2c_write(0x7E);   //STAGE7_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE8_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE8_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE8_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE8_SENSITIVITY
i2c_write(0x07);
i2c_write(0x08);   //STAGE8_OFFSET_LOW
i2c_write(0x06);
i2c_write(0x40);   //STAGE8_OFFSET_LOW
i2c_write(0x07);
i2c_write(0x08);   //STAGE8_OFFSET_HIGH_CLAMP
i2c_write(0x07);
i2c_write(0x08);   //STAGE8_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE9_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE9_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE9_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE9_SENSITIVITY
i2c_write(0x09);
i2c_write(0x60);   //STAGE9_OFFSET_LOW
i2c_write(0x08);
i2c_write(0x98);   //STAGE9_OFFSET_LOW
i2c_write(0x09);
i2c_write(0x60);   //STAGE9_OFFSET_HIGH_CLAMP
i2c_write(0x09);
i2c_write(0x60);   //STAGE9_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE10_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE10_CONNECTION [12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE10_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE10_SENSITIVITY
i2c_write(0x0D);
i2c_write(0x48);   //STAGE10_SENSITIVITY
i2c_write(0x0C);
i2c_write(0x80);   //STAGE10_OFFSET_HIGH
i2c_write(0x0D);
i2c_write(0x48);   //STAGE10_OFFSET_HIGH_CLAMP
i2c_write(0x0D);
i2c_write(0x48);   //STAGE10_OFFSET_LOW_CLAMP
i2c_write(0xFF);
i2c_write(0xFF);   //STAGE11_CONNECTION [6:0]
i2c_write(0x3F);
i2c_write(0xFF);   //STAGE11_CONNECTION[12:7]
i2c_write(0x00);
i2c_write(0x00);   //STAGE11_AFE_OFFSET
i2c_write(0x26);
i2c_write(0x26);   //STAGE11_SENSITIVITY
i2c_write(0x11);
i2c_write(0x30);   //STAGE11_OFFSET_LOW
i2c_write(0x10);
i2c_write(0x68);   //STAGE11_OFFSET_HIGH
i2c_write(0x11);
i2c_write(0x30);   //STAGE11_OFFSET_HIGH_CLAMP
i2c_write(0x11);
i2c_write(0x30);   //STAGE11_OFFSET_LOW_CLAMP
i2c_stop();

//Bank 1 Registers 0x00-0x07
i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(PWR_CONTROL);
i2c_write(0x00);
i2c_write(0xB0);  //PWR_CONTROL
i2c_write(0x00);
i2c_write(0x00);  //STAGE_CAL_EN
i2c_write(0x32);
i2c_write(0x30);  //AMB_COMP_CTRL0
i2c_write(0x04);
i2c_write(0x19);  //AMB_COMP_CTRL1
i2c_write(0x08);
i2c_write(0x32);  //AMB_COMP_CTRL2
i2c_write(0x00);
i2c_write(0x00);  //STAGE_LOW_INT_EN
i2c_write(0x00);
i2c_write(0x00);  //STAGE_HIGH_INT_EN
i2c_write(0x00);
i2c_write(0x00);  //STAGE_COMPLETE_INT_EN
i2c_stop();

//Bank 1 registers, reinitalize 0x01
i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(STAGE_CAL_EN);
i2c_write(0xFF);
i2c_write(0xFF);  //STAGE_CAL_EN, using all 12 stages
i2c_stop();

//Bank 1 registers, 0x08-0x0A
i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(STAGE_LOW_LIMIT_INT);  //STAGE_LOW_LIMIT_INT
i2c_stop();
i2c_start();
i2c_write(0x59);
LOW_LIMIT_DATA1 = i2c_read();
LOW_LIMIT_DATA2 = i2c_read();
HIGH_LIMIT_DATA1 = i2c_read();
HIGH_LIMIT_DATA2 = i2c_read();
COMPLETE_LIMIT_DATA1 = i2c_read();
COMPLETE_LIMIT_DATA2 = i2c_read();
i2c_stop();

//Just practicing reading from registers
//in this case it is register 0x02
/*i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(0x02);  //AMB_COMP_CTRL0
i2c_stop();
i2c_start();
i2c_write(0x59);
data1 = i2c_read();
data2 = i2c_read();
i2c_stop();*/

//i2c_start();
//i2c_write(0x58);
//i2c_write(STAGE0_CONNECTION+1);
//i2c_write(0x1FFF);
//i2c_stop();

//i2c_start();
//i2c_write(0x58);
//i2c_write(STAGE0_CONNECTION+2);
//i2c_write(0x0000);
//i2c_stop();

}


And here is my source code. It doesn't really do anything, I'm just trying to read from a certain address to make sure the code is actually working, but I don't know if I did anything wrong or not:

Code:


#include <18F4520.h>
#include <AD7147RegMap.h>
#include <My_own_code.h>
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)

void main()
{
int data3, data4;

//Just practicing reading from registers
//in this case it is register 0x02
i2c_start();
i2c_write(0x58);
i2c_write(0x00);
i2c_write(0x02);  //AMB_COMP_CTRL0
i2c_stop();
i2c_start();
i2c_write(0x59);
data3 = i2c_read();
data4 = i2c_read();
i2c_stop();
}


If anyone has time to take a look at this, it would be greatly appreciated. Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 03, 2007 8:58 pm     Reply with quote

Quote:
i2c_start();
i2c_write(0x59);
data3 = i2c_read();
data4 = i2c_read(0);
i2c_stop();
}

The last read operation before the i2c stop must do a "NAK". In CCS
this is done by giving it a parameter of 0, as shown above.

There are three places in your posted code where this must be done.
achotto



Joined: 19 Nov 2007
Posts: 9

View user's profile Send private message

PostPosted: Mon Dec 03, 2007 9:36 pm     Reply with quote

I made those changes, but when I look at the values of the read commands, they are still incorrect, is there anything else wrong with my code? Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 03, 2007 10:24 pm     Reply with quote

Quote:

When I look at the values of the read commands, they are still incorrect.

How are they incorrect ? What values do you read, and what values
do you expect to read ?

There are two types of AD7147. One of them has an SPI interface,
and the other has an i2c interface. Are you sure that you have
the one with the i2c interface ? From the data sheet:
Quote:

SERIAL INTERFACE
The AD7147 is available with an SPI-compatible interface. The
AD7147-1 is available with an I 2 C-compatible interface. Both
parts are the same,with the exception of the serial interface.


Do you have pull-up resistors on the SDA and SCL lines ?

Do you have the i2c address select pins, AD0 and AD1, connected to
ground ?
achotto



Joined: 19 Nov 2007
Posts: 9

View user's profile Send private message

PostPosted: Tue Dec 04, 2007 6:58 am     Reply with quote

Well in my header file, I am writing a 0x32 and 0x30 to the AMB_COMP_CTRL0, but when I read back from this address I get 0x06 and 0x00, and I don't know why this is.

I'm positive I have the I2C device, the package I got in the mail says AD7147ACPZ-1500RL7, and checked on the data sheet and it says it's an I2C device. And yes, I have pull up resistors on the SDA and SCL lines and ADD0 and ADD1 are also connected to ground. So I'm pretty sure I have everything connected correctly.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Dec 04, 2007 9:55 am     Reply with quote

One thing that might be a 'gotcha' is when you finally get this working, what will happen when there is some sort of 'glitch' in the communications? Your, current, code assumes that all will be well between your devices. You don't have any way to handle a missed bit, or byte, and the I2C bus could hang. Just something to think about.

Ronald
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Dec 04, 2007 10:24 am     Reply with quote

You could try a force_hardware to see if hardware I2C will work better...??
It defaults to bit bang unless you specify force_hardware.

Code:
#use I2C(MASTER,SDA=FRAM_SDA,SCL=FRAM_SCL,FORCE_HW,FAST,RESTART_WDT)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 04, 2007 11:27 am     Reply with quote

Make a small test program, in which you just write and read from only
one register, such as AMB_COMP_CTRL0. Test the program to see if
it works. If it doesn't work, post it. Also post the values that it read.

When you post the program, it should be compilable, with all variable
and constant declarations shown. Also show all #include, #fuses, and
#use statements.
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