View previous topic :: View next topic |
Author |
Message |
robert campbell2 Guest
|
Freeze when trying to do I2C on 16F688 |
Posted: Mon May 31, 2004 12:01 pm |
|
|
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
|
|
Posted: Mon May 31, 2004 3:30 pm |
|
|
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 |
|
|
robert campbell2 Guest
|
16f688 i2c problem |
Posted: Mon May 31, 2004 5:05 pm |
|
|
I do in fact have the pullup resistors. Any other thoughts?
Thanx |
|
|
robert campbell
Joined: 31 May 2004 Posts: 3
|
robert campbell2 |
Posted: Mon May 31, 2004 5:17 pm |
|
|
Also, MPLAB sim hangs on the first I2c write command. |
|
|
robert campbell
Joined: 31 May 2004 Posts: 3
|
robert campbell2 |
Posted: Mon May 31, 2004 5:18 pm |
|
|
Also, MPLAB sim hangs on the first I2c write command. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 31, 2004 7:29 pm |
|
|
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
} |
|
|
|
|