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

Can't get I2C in 18F452 working!

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



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

Can't get I2C in 18F452 working!
PostPosted: Mon Aug 06, 2007 12:53 am     Reply with quote

I can't recieve anything from I2C bus. I have 24LC64 and DS1307 on the bus. Pull-up resistors are 4.7K (tried 1k to 10k). I Can't read or write to EEPROM and can't read DS1307. I've tryed software and hardware I2C, slow and fast...
Here is my code:
Code:
////////////////////////
#include <18F452.h>
#device adc=16
#fuses HS,NOWDT,NOPROTECT,NOLVP

#use delay(clock=20000000)
#use i2c(Master, sda=PIN_C4,scl=PIN_C3,force_sw)
char k;
   
    int16 raw_lighting;
    int tempb, l_duration, cur_min1, cur_min2, start_hour, start_min;
    int lmode;   //0 - авто(ночью горит),
                 //1 - заданная продолжителность дня отн. рассвета
                 //2 - ручной режим (время вкл-выкл)
               
boolean setted, heatstate;
   
float lighting, templighting, temperature, tmin, tmax, tmode, delta;
  BYTE sec;
  BYTE min, min_on, min_off;
  BYTE hrs, hrs_on, hrs_off;
  BYTE day;
  BYTE month;
  BYTE yr;
  BYTE dow;

#define DQ1 PIN_C0 
#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3

#define ADR_FIRST_RUN 0

#define ADR_TMODE 3
#define ADR_LMODE 5
#define ADR_LDURATION 6 // продолжительность дня отн. рассвета
#define ADR_LON 7    //время включения света
#define ADR_LOFF 9   //время выключения света

#define ADR_TMAX_MONTH 10
#define ADR_TMAX_DAY 11
#define ADR_TMAX_HRS 12
#define ADR_TMAX_MIN 13
#define ADR_TMIN_MONTH 14
#define ADR_TMIN_DAY 15
#define ADR_TMIN_HRS 16
#define ADR_TMIN_MIN 17
#define ADR_TMAX 20
#define ADR_TMIN 24

#include <math.h>
#include <stdlib.h>
#include <24AA64.c>
#include <mylcd.c>
#include <mykbd.c>
#include <ds1822single.c>
#include <ds1307.c>
#include <menus.c>
#include <stdlib.h>
////-------- functions cutted--------

#int_RB
void RB_isr(void) {
   k=kbd_getc();
beep();}

void main() {

int tempdelay;
//============ init =================
   lcd_init();
   lcd_putc("\fLCD init OK\n");

   kbd_init();
   init_ext_eeprom();
   ds1307_init();
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);

tmax=20;
tmin=0;
tmode=25;
lmode=0;
heatstate = FALSE;
delta=0.5;
hrs_on=20;
hrs_off=22;
min_on=0;
min_off=20;
onewire_reset();
onewire_write(0xCC);
onewire_write(0x44);
lcd_putc("1820 init OK\n");
ENABLE_INTERRUPTS(INT_rb);
enable_interrupts ( GLOBAL );


 printf(lcd_putc,"\fread rom 1      \n");
if ((read_rom(0)==0)||(read_rom(0)==0xFF)) // <==  program hangs here when reading eeprom
  menu();
  else
  {printf(lcd_putc,"read rom 2      ");
tmode=read_rom(ADR_TMODE);
lmode=read_rom(ADR_LMODE);
heatstate = FALSE;
delta=0.5;
hrs_on=read_rom(ADR_LON);
hrs_off=read_rom(ADR_LOFF);
min_on=read_rom(ADR_LON+1);
min_off=read_rom(ADR_LOFF+1);
l_duration=read_rom(ADR_LDURATION);
  }
  k=0;

ds1820_read();

while (TRUE) {
....
 }
}

and hese is the 24lc64 driver:
Code:


#define EEPROM_ADDRESS long int
#define EEPROM_SIZE   8192

BOOLEAN ext_eeprom_ready() {
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(0xa0);  // then the device is ready.
   i2c_stop();
   delay_ms(5);
   return !ack;
}

void init_ext_eeprom()
{
   output_FLOAT(EEPROM_SCL);
   output_FLOAT(EEPROM_SDA);
}

void write_rom(int16 address, int8 data) {
short int status;
 output_high(pin_e2);
while(!ext_eeprom_ready());
   i2c_start();

   i2c_write(0xa0);
   i2c_write(address >> 8);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   delay_ms(5) ;
   status=i2c_write(0xa0);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0xa0);
      i2c_stop();
      delay_ms(5);
   }

output_low(pin_e2);
}


BYTE read_rom(long int address) {
BYTE data;

 output_high(pin_e2);
   while(!ext_eeprom_ready());
   i2c_start();
   i2c_write(0xa0);
   i2c_write(address >> 8);
   i2c_write(address);
   i2c_start();
   i2c_write(0xa1);
   data=i2c_read(0);
   i2c_stop();
    output_low(pin_e2);

   return(data);
}

void WRITE_FLOAT(long int n, float data) {

int f;

for (f = 0; f < 4; f++)

write_rom(f + n, *(&data + f) ) ;

}
float READ_FLOAT(long int n) {

int f;

float data;

for (f = 0; f < 4; f++)

*(&data + f) = read_rom(f + n);

return(data);

}

I've tried to delete eeprom or clock ic's from sheme and from program - no effect. Can't read i2c.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 06, 2007 1:15 am     Reply with quote

1. Make a simple test program, similar to the one in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=30346&start=10

2. Don't use your eeprom routines. Use the CCS driver for the 24LC64.
Here is the driver file location:
Quote:
c:\Program Files\picc\Drivers\2464.c


3. Make sure that you have a pull-up resistor (4.7K) on SDA and
also on SCL.

4. Make sure the address pins on the eeprom (A0, A1, A2) are
all connected to ground. Also connect the WP pin to ground.

5. Post your compiler version. You can find it at the top of the .LST
file, which is in your project directory. The version number is a 4-digit
number in this format: x.xxx


Last edited by PCM programmer on Mon Aug 06, 2007 1:26 am; edited 1 time in total
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Mon Aug 06, 2007 1:24 am     Reply with quote

I have pull-up resistors on SDA and SCL...
I will try 2464.c...
And what about RTC?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 06, 2007 1:27 am     Reply with quote

For this test, disconnect all other i2c chips from the i2c bus, except for
the eeprom.
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