Jump to content
Sign in to follow this  
vino

problem with I2C

Recommended Posts

hi,

i got a problem with i2c

 

what i have to do is i have a national LM75

digital temperature sensor i have to read temperature

from it and store in an EEPROM.

 

initially iam trying to write and read to an EEPROM

that is 24LC16B and using PIC16F873 compiler is C2C

compiler from picant.com and i2c routines in picant website in i2c examples

 

The output iam seeing on LCD is the hex value of the

address loction that is where iam storeing not the

data in it.

 

I have attached my code, kindly check it and mail me

if iam making any errors

 

quick response is appreciated

thank u

 

abt program :

 

first is declarations

then configuration of I2C

then I2C protocol routines

functions for LM75

functions for displaying on LCD in hex

then in main i wrote the write and read of EEPROM

 

iam not using the wait function iam getting problems

instead i have used delays.

 

 

thank u

vinod

 

 

 

 

// Configuration Registers for I2C

 

char SSPSTAT@0x94;  // In Bank 1

char SSPCON@0x14;   // In Bank 0

char SSPCON2@0x91;  // In Bank 1

char SSPBUF@0x13;   // I2C Buffer

char SSPADD@0x93;   // I2C Slave Address register

char PIR1@0x000C;

char trisc@0x0087;

char PIE1@0x008C;

//char intcon@0x000B;

//char PORTB@0x06;

 

 

#define RSMASK 0x10

#define EBIT 0x05

 

 

/********** function proto types **********/

 

void lcd_start (void);

void lcd_clear(void);

void lcd_print_ch(char ch);

void lcd_cursor(char x, char y);

void lcd_raw_send ( unsigned char in,unsigned char mask );  

void lcd_printdec(unsigned char dec_val);

 

/****************************************/

 

 

#define SSPIE  0x0003

#define PEIE   0x0006                                                                        

 

               

 

// Bits of SSPSTAT

#define SMP 7

#define CKE 6

#define D_A 5

#define P   4

#define S   3

#define R_W 2

#define R_W_MASK 0x04

#define UA  1

#define BF  0

 

 

// Bits of SSPCON2

#define GCEN    7

#define ACKSTAT 6

#define ACKDT   5

#define ACKEN   4

#define RCEN    3

#define PEN     2

#define RSEN    1

#define SEN     0

 

 

/* mask for register test

 

 

#define MASK_BF         0x01*/

#define MASK_D_A    0x20

#define MASK_SSPIF  0x08

#define MASK_SSPOV  0x40  

 

/*#define MASK_T0IF    0x04

#define MASK_RBIF    0x01

#define MASK_CKP     0x10

#define MASK_R_W     0x04*/

 

// Bits of PIR1

 

#define PSPIF  7

#define ADIF   6

#define RCIF   5

#define TXIF   4

#define SSPIF  3

#define SSPIF_MASK 0x08

#define CCP1IF 2

#define TMR2IF 1

#define TMR1IF 0

 

 

 

// Bits of SSPCON

#define WCOL  7

#define SSPOV 6

#define SSPEN 5

#define CKP   4

#define SSPM3 3

#define SSPM2 2

#define SSPM1 1

#define SSPM0 0

 

/* Function Prototypes  */

void ConfigureI2C(void);

void I2CSend( char );

char I2CReceive(void);

void I2CAck(void);

void I2CNak(void);

void I2CStart(void);

void I2CStop(void);

void I2CWait(void);

void lm75_start( char address );

void read_temp_lm75( char address );

 

//Variables

char msb_temp=0; char lsb_temp=0;

char mux;

 

 

/*************************  I2C Routines  ************************************/

// configure I2C  

 

void ConfigureI2C(void)

{

 set_bit( STATUS, RP0 );          //bank 1  

 SSPSTAT = 0x80;                  // Set I2C levels

 SSPCON2 = 0x00;                  

 set_tris_c( 0x18 );              //RC3,RC4 as inputs

       

 SSPADD = 0x09;                  // 100k at 4Mhz clock        

 clear_bit( STATUS, RP0 );

 

 SSPIF = 0;

 SSPCON = 0x28;

   

}

 

/***********************/

/* Send a char via I2C */

/***********************/

void I2CSend( char ch )

{

 SSPBUF = ch;               // Load DATA to send

// I2CWait();                 // Wait for completion

}

/**************************/

/* Receive a char via I2C */

/**************************/

char I2CReceive(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 

 while ( SSPSTAT & R_W_MASK  ); // Wait for Transmit to end

 

 set_bit( SSPCON2, RCEN );      // Enable I2C receiver

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

// I2CWait();                     // Wait for data to arrive.

 

 return SSPBUF;                 // Return the data

}

/**************************/

/* Send an I2C ACK        */

/**************************/

void I2CAck(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 clear_bit( SSPCON2, ACKDT );   // Setup for ACK

 set_bit( SSPCON2, ACKEN );     // Send ACK

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

 // I2CWait();                     // Wait for completion

}

/**************************/

/* Send an I2C NaK        */

/**************************/

void I2CNak(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 set_bit( SSPCON2, ACKDT );     // Setup for NAK

 set_bit( SSPCON2, ACKEN );     // Send NAK

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

// I2CWait();                     // Wait for completion

}

/**************************/

/* Generate an I2C START  */

/**************************/

void I2CStart(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 set_bit( SSPCON2, SEN );       // Initiate START condition

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

// I2CWait();                     // Wait for completion

}

/***************************/

/* Generate an I2C RESTART */

/***************************/

void I2CRestart(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 set_bit( SSPCON2, RSEN );      // Initiate START condition

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

// I2CWait();                     // Wait for completion

}

/**************************/

/* Generate an I2C STOP   */

/**************************/

void I2CStop(void)

{

 set_bit( STATUS, RP0 );        // *** Register page 1 ***

 set_bit( SSPCON2, PEN );       // Generate STOP condition

 clear_bit( STATUS, RP0 );      // *** Register page 0 ***

// I2CWait();                     // Wait for completion

}

 

 

/**************************************/

/* Waits for I2C process to complete  */

/**************************************/

void I2CWait(void)

{

 while ( !( PIR1 & SSPIF_MASK ) );     // Wait for Interrupt

 

 clear_bit( PIR1, SSPIF );             // Clear Interrupt flag

}

 

 

 

//functions for lm75 digital temperature sensor

 

void lm75_start( char address )

{

  //pointer on temperature reader

 I2CStart();

 I2CSend( 0x90 );

 I2CAck();

 I2CSend( 0 );

 I2CAck();

 I2CStop();

 delay_us(15);

}

 

 

 

void read_temp_lm75( char address )

{

 I2CStart();

 I2CSend( 0x90 );

 I2CAck();

 msb_temp=I2CReceive();

 I2CNak();

 lsb_temp=I2CReceive();

 I2CNak();

 I2CStop();

}

 

 

// LCD functions  

 

void lcd_start (void)

{

  set_bit ( STATUS, RP0 ) ;     // select bank 1

  TRISA = 0x17;                 // make bits 0,1,2 as input 3,4 as output

  TRISB = (TRISB & 0xc0) ;      // select bank 0

  OPTION_REG=0x07;              // set prescalar to 256

  clear_bit( STATUS, RP0 );

  delay_ms(4);

  lcd_raw_send (0x33,0) ;

  lcd_raw_send (0x32,0) ;       // lcd initialize

  lcd_raw_send (0x2c,0) ;

  lcd_raw_send (0x06,0) ;

  lcd_raw_send (0x0c,0) ;                

  lcd_clear () ;

  INTCON=0xa0;          // configure interrupt

  TMR0=0;

  PORTA=0;              // write PORTA equal to zero

 

}

 

void lcd_raw_send ( unsigned char in,

                  unsigned char mask )

{

  unsigned char pb ;

  asm swapf param00_lcd_raw_send,F

 

  pb = (in & 0x0f ) | mask ;

 

  pb = pb | (PORTB & 0xc0) ;

 

  PORTB = pb ;

  delay_ms(2);

  set_bit ( PORTB, EBIT ) ;

  delay_ms(2);

  clear_bit ( PORTB, EBIT ) ;

  asm swapf param00_lcd_raw_send,F

  pb = (in & 0x0f ) | mask ;

  pb = pb | (PORTB & 0xc0) ;

  PORTB = pb ;

  delay_ms(2);

  set_bit ( PORTB, EBIT ) ;

  delay_ms(2);

   clear_bit ( PORTB, EBIT ) ;

  delay_ms(2);

}    

 

 

/* Function prints a single character to LCD */

 

void lcd_print_ch(unsigned char in)

{ lcd_raw_send(in,RSMASK);  }

 

 

/* Function clears LCD display */

 

void lcd_clear(void)

{

  lcd_raw_send(0x01,0) ;

  delay_ms(2);

  lcd_raw_send(0x02,0) ;

  delay_ms(2);

 

}

 

 

/* Function positions cursor at value of x,y */

 

void lcd_cursor ( unsigned char x,

                                                                   unsigned char y )

{  

  if ( y==0 )

   { y=0x80; }

  else

  { y=0xc0; }

  lcd_raw_send(y+x,0) ;

 

}

 

 

/* Function to print two digit value to LCD */

 

void lcd_printdec(unsigned char dec_val)

{

  unsigned char count=0;

 

  while(dec_val > 9)

   { dec_val=(dec_val-10);

     count++; }

  lcd_print_ch(count+0x30);      //print 2 digit

  lcd_print_ch(dec_val+0x30);    //print 1 digit  

}

 

 

 

//Program starts here  

 

 

void main(void)

{

 ConfigureI2C();

 lcd_start();

 

while(1)

 

{

 

// write data to slave

     I2CStart();

     I2CSend( 0xa6 );  // slave address with write to page

               //delay_ms(3);

     //I2CAck();  

     I2CSend(0x40);    // word address

     //delay_ms(3);

     //I2CAck();      

     I2CSend(0x18);    // send data    

     delay_ms(3);

     I2CAck();

     I2CStop();                      

     delay_ms(3000);

                 

// Random read above written data                    

                 

                 

                  I2CStart();

                  I2CSend( 0xa6 );    // slave address with page selection and write bit

                  delay_ms(3);

     //I2CAck();

                  I2CSend(0x40);      // word address

     delay_ms(3);

     //I2CAck();

     I2CRestart();       // send start

                  I2CSend( 0xa7 );    // slave address with page selection and read bit

                  delay_ms(3);

                  mux=I2CReceive();   //receive data from slave

                  delay_ms(3);

                  I2CNak();

     I2CStop();

     delay_ms(3000);                  

                  lcd_printdec(mux);  //display on LCD

                 

                 

                 

                 

}  

 

 

}

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...