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 support@ccsinfo.com

Freeze when trying to do I2C on 16F688

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







Freeze when trying to do I2C on 16F688
PostPosted: Mon May 31, 2004 12:01 pm     Reply with quote

Hello:

My program freezes when the first I2C_WRITE is encountered. The code is below, followed by the header file:

#include <16F688.h>
#include "C:\robspicc\archivalV1\archivalV1.h"

long int read_temp();
long int TP;

byte const DS_ADDR_WR = 0x90;
byte const DS_ADDR_RD = 0x91;
byte const DS_PTR_CFG = 0x01;
byte const DS_PTR_TEMP = 0x00;
byte const CFG_12BIT_ON = 0x60;
byte const CFG_10BIT_ON = 0x20;
byte const CFG_9BIT_ON = 0x00;
byte const CFG_10BIT_OFF = 0x21;
byte const CFG_9BIT_OFF = 0x01;

void main()
{
output_high(PWR); //Turns on temp sensor power
output_float(DS_CLK);
output_float(DS_DAT);
delay_ms(2000);

while(1){
output_high(LED); //LED For Debug
delay_ms(10);
output_low(LED);

TP = read_temp();
printf("%lu\r\n",TP);
delay_ms(2000);
}
}
long int read_temp()
{
int r1, r2;
// Start a single convert
I2C_START();
I2C_WRITE(DS_ADDR_WR); //Always Freezes here
/////////////////////////DEBUG
output_high(LED); //Used to indicate whether code reaches this point
delay_ms(500);
output_low(LED);
/////////////////////////DEBUG
I2C_WRITE(DS_PTR_CFG);
I2C_WRITE(CFG_9BIT_ON);
I2C_STOP();
delay_us(10);
I2C_START();
I2C_WRITE(DS_ADDR_WR);
I2C_WRITE(DS_PTR_CFG);
I2C_WRITE(CFG_9BIT_OFF);
I2C_STOP();
delay_us(10);
// Re-position pointer to temperature register..
I2C_START();
I2C_WRITE(DS_ADDR_WR);
I2C_WRITE(DS_PTR_TEMP);
I2C_STOP();
// Get the temperature
I2C_START();
I2C_WRITE(DS_ADDR_RD);
delay_us(10);
r1 = I2C_READ();
r2 = I2C_READ(0);
I2C_STOP();
return ((r1 << 8)+ r2);
}

And for the header file:

#use delay(clock=38400)
#fuses LP,NOWDT,NOBROWNOUT,NOMCLR,PUT,NOIESO,NOFCMEN
#use rs232(baud=300,BRGH1OK,parity=N,xmit=PIN_C2,rcv=PIN_A3,bits=8,INVERT)
#use i2c(Master, SDA=PIN_A1, SCL=PIN_A0, slow)

#define PWR PIN_C3
#define LED PIN_C4
#define DS_CLK PIN_A0
#define DS_DAT PIN_A1[/code]
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

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

PostPosted: Mon May 31, 2004 3:30 pm     Reply with quote

make sure you have pull up resistors on the SDA and SCL lines, or it will indeed lock ( own experience )
3.3K or something should be good Smile
robert campbell2
Guest







16f688 i2c problem
PostPosted: Mon May 31, 2004 5:05 pm     Reply with quote

I do in fact have the pullup resistors. Any other thoughts?

Thanx
robert campbell



Joined: 31 May 2004
Posts: 3

View user's profile Send private message

robert campbell2
PostPosted: Mon May 31, 2004 5:17 pm     Reply with quote

Also, MPLAB sim hangs on the first I2c write command.
robert campbell



Joined: 31 May 2004
Posts: 3

View user's profile Send private message

robert campbell2
PostPosted: Mon May 31, 2004 5:18 pm     Reply with quote

Also, MPLAB sim hangs on the first I2c write command.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 31, 2004 7:29 pm     Reply with quote

I wouldn't worry about MPLAB-SIM. There's no way that the
simulator knows that those pins are supposed to be used for
an i2c bus. How would it know ?

I think your problem is likely due to the comparator module
that is on Port A of the 16F688. It needs to be disabled
before Port A can be used for digital i/o. CCS has a
function that can do this. Example:

Code:
main()
{
setup_comparator(NC_NC_NC_NC);   // Disable comparators


// Put your other code here.


while(1);   // Prevent PIC from going to sleep
}
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