EmbeddedRelated.com
Forums

how can i correct this code??

Started by sheikh adnan July 13, 2005
hi..
i m trying to write READ/ WRITE code for 24C64 with I2C standard, using 89c52, i m implementing my own I2C but having trouble with the code below that it does not read any charachter. here is the code for this written in KEIL micro vision..
#include <REG52.H>       // Header file for the 89C52 MCU
#include <STDIO.H>       // Standard I/O header file
//------
// Value Definitions
//------
#define  TRUE   0x01   // Value representing TRUE
#define  FALSE   0x00   // Value representing FALSE
#define  ON    0x01   // Value representing ON
#define  OFF    0x00   // Value representing OFF
#define  HIGH   1       // Value representing ON
#define  LOW    0       // Value representing OFF
#define   DELAY_BLINK  1500   // Value for delay time - blink
//------
// I/O Port Defines
//------
sbit   SDATA =  P1^0;     // Serial data
sbit   SCLK  =  P1^1;     // Serial clock
//------
// I2C Peripheral Function Prototypes
//------//------
// I2C Functions - Bit Banged
//------
void i2c_start (void);      // Sends I2C Start Trasfer
void i2c_stop (void);      // Sends I2C Stop Trasfer
void i2c_write (unsigned char input_data); // Writes data over the I2C bus
unsigned long i2c_read (void);    // Reads data from the I2C bus
//------
// Support Function Prototypes
//------
void initialize_system (void);
void delay_time (unsigned int time_end);    // To pause execution for pre-determined time
//------
// MAIN FUNCTION
//------
void main (void)
{
long  dum;
initialize_system ();
i2c_start();
//i2c_write('A');
//dum=  i2c_read();
i2c_read();
i2c_stop();

printf("%s",i2c_read()); 
}

//------
// I2C Functions - Bit Banged
//------
//------
//  Routine: i2c_start
// Inputs:  none
// Outputs: none
// Purpose: Sends I2C Start Trasfer
//------
void i2c_start (void){
 SDATA = HIGH;       // Set data line high
 SCLK = HIGH;       // Set clock line high
 SDATA = LOW;       // Set data line low (START SIGNAL)
 SCLK = LOW;        // Set clock line low
}
//------
//  Routine: i2c_stop
// Inputs:  none
// Outputs: none
// Purpose: Sends I2C Stop Trasfer
//------
void i2c_stop (void){
 unsigned char input_var;
 SCLK = LOW;        // Set clock line low
 SDATA = LOW;       // Set data line low
 SCLK = HIGH;       // Set clock line high
 SDATA = HIGH;       // Set data line high (STOP SIGNAL)
 input_var = SDATA;      // Put port pin into HiZ
 }
//------
//  Routine: i2c_write
// Inputs:  output byte
// Outputs: none
// Purpose: Writes data over the I2C bus
//------

/*void i2c_write(unsigned char output_data){
 unsigned char index;
 for(index = 0; index < 8; index++){   // Send 8 bits to the I2C Bus
                                   // Output the data bit to the I2C Bus
  SDATA = ((output_data & 0x80) ? 1 : 0);
       output_data  <<= 1;              // Shift the byte by one bit
  SCLK = HIGH;               // Clock the data into the I2C Bus
  SCLK = LOW;     
 }
 index = SDATA;       // Put data pin into read mode
 SCLK = HIGH;                // Clock the ACK from the I2C Bus
 SCLK = LOW;     
}
*/    
//------
//  Routine: i2c_read
// Inputs:  none
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//------
unsigned long i2c_read (void)
{
 unsigned char index, input_data;
   index = SDATA;       // Put data pin into read mode
  
 input_data = 0x80;
 for(index = 0; index < 7; index++)   // Send 8 bits to the I2C Bus
 {
  input_data >>= 1;     // Shift the byte by one bit
  SCLK = HIGH;               // Clock the data into the I2C Bus
        input_data |= SDATA;       // Input the data from the I2C Bus
  SCLK = LOW;     
 }
 return 'A';
}
 
//------
// SUPPORT FUNCTIONS
// If wana initilize any thing then use this function
//------
void initialize_system (void)
{
   
    SCON  = 0x50;             // SCON: mode 1, 8-bit UART, enable rcvr
    TMOD |= 0x20;                  // TMOD: timer 1, mode 2, 8-bit reload  
    TH1   = -3;                    // TH1:  reload value for 9200 baud @ 11.0592MHz
    TR1   = 1;                     // TR1:  timer 1 run                         
    TI    = 0;                     // TI:   set TI to send first char of UART
 TI=1;
}

////////////////////////////////////////////////////////////////////////////////
//  Routine: delay_time
// Inputs:  counter value to stop delaying
// Outputs: none
// Purpose: To pause execution for pre-determined time
////////////////////////////////////////////////////////////////////////////////
/*
void delay_time (unsigned int time_end){
 unsigned int index;
 for (index = 0; index < time_end; index++);
}
*/
 


Start your day with Yahoo! - make it your home page

I don't know how the 89c52 manage the I/O pin.
I suggest you to change the behavior of SDA pin (PORT DATA DIRECTION
REGISTER)...

--- In AT91SAM7@AT91..., sheikh adnan <adnansheikh2002@y...>
wrote:
> hi..
> i m trying to write READ/ WRITE code for 24C64 with I2C standard,
using 89c52, i m implementing my own I2C but having trouble with the
code below that it does not read any charachter. here is the code for
this written in KEIL micro vision..
> #include <REG52.H> // Header file for the 89C52 MCU
> #include <STDIO.H> // Standard I/O header file
>
//------
> // Value Definitions
>
//------
> #define TRUE 0x01 // Value representing TRUE
> #define FALSE 0x00 // Value representing FALSE
> #define ON 0x01 // Value representing ON
> #define OFF 0x00 // Value representing OFF
> #define HIGH 1 // Value representing ON
> #define LOW 0 // Value representing OFF
> #define DELAY_BLINK 1500 // Value for delay time - blink
>
//------
> // I/O Port Defines
>
//------
> sbit SDATA = P1^0; // Serial data
> sbit SCLK = P1^1; // Serial clock
>
//------
> // I2C Peripheral Function Prototypes
>
//------//------
> // I2C Functions - Bit Banged
>
//------
> void i2c_start (void); // Sends I2C Start Trasfer
> void i2c_stop (void); // Sends I2C Stop Trasfer
> void i2c_write (unsigned char input_data); // Writes data over the
I2C bus
> unsigned long i2c_read (void); // Reads data from the I2C bus
>
//------
> // Support Function Prototypes
>
//------
> void initialize_system (void);
> void delay_time (unsigned int time_end); // To pause execution
for pre-determined time
>
//------
> // MAIN FUNCTION
>
//------
> void main (void)
> {
> long dum;
> initialize_system ();
> i2c_start();
> //i2c_write('A');
> //dum= i2c_read();
> i2c_read();
> i2c_stop();
>
> printf("%s",i2c_read());
> }
//------
> // I2C Functions - Bit Banged
>
//------
>
//------
> // Routine: i2c_start
> // Inputs: none
> // Outputs: none
> // Purpose: Sends I2C Start Trasfer
>
//------
> void i2c_start (void){
> SDATA = HIGH; // Set data line high
> SCLK = HIGH; // Set clock line high
> SDATA = LOW; // Set data line low (START SIGNAL)
> SCLK = LOW; // Set clock line low
> }
>
//------
> // Routine: i2c_stop
> // Inputs: none
> // Outputs: none
> // Purpose: Sends I2C Stop Trasfer
>
//------
> void i2c_stop (void){
> unsigned char input_var;
> SCLK = LOW; // Set clock line low
> SDATA = LOW; // Set data line low
> SCLK = HIGH; // Set clock line high
> SDATA = HIGH; // Set data line high (STOP SIGNAL)
> input_var = SDATA; // Put port pin into HiZ
> }
>
//------
> // Routine: i2c_write
> // Inputs: output byte
> // Outputs: none
> // Purpose: Writes data over the I2C bus
>
//------
>
> /*void i2c_write(unsigned char output_data){
> unsigned char index;
> for(index = 0; index < 8; index++){ // Send 8 bits to the I2C Bus
> // Output the data bit to the I2C Bus
> SDATA = ((output_data & 0x80) ? 1 : 0);
> output_data <<= 1; // Shift the byte by one bit
> SCLK = HIGH; // Clock the data into the I2C Bus
> SCLK = LOW;
> }
> index = SDATA; // Put data pin into read mode
> SCLK = HIGH; // Clock the ACK from the I2C Bus
> SCLK = LOW;
> }
> */
>
//------
> // Routine: i2c_read
> // Inputs: none
> // Outputs: input byte
> // Purpose: Reads data from the I2C bus
>
//------
> unsigned long i2c_read (void)
> {
> unsigned char index, input_data;
> index = SDATA; // Put data pin into read mode
>
> input_data = 0x80;
> for(index = 0; index < 7; index++) // Send 8 bits to the I2C Bus
> {
> input_data >>= 1; // Shift the byte by one bit
> SCLK = HIGH; // Clock the data into the I2C Bus
> input_data |= SDATA; // Input the data from the I2C Bus
> SCLK = LOW;
> }
> return 'A';
> }
//------
> // SUPPORT FUNCTIONS
> // If wana initilize any thing then use this function
>
//------
> void initialize_system (void)
> {
>
> SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr
> TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit
reload
> TH1 = -3; // TH1: reload value for 9200
baud @ 11.0592MHz
> TR1 = 1; // TR1: timer 1 run

> TI = 0; // TI: set TI to send first
char of UART
> TI=1;
> }
////////////////////////////////////////////////////////////////////////////////
> // Routine: delay_time
> // Inputs: counter value to stop delaying
> // Outputs: none
> // Purpose: To pause execution for pre-determined time
>
////////////////////////////////////////////////////////////////////////////////
> /*
> void delay_time (unsigned int time_end){
> unsigned int index;
> for (index = 0; index < time_end; index++);
> }
> */ >
> ---------------------------------
> Start your day with Yahoo! - make it your home page


Message
Hi,
 
When using 24c64 parts you normally have to  send two strings.
 
Set the bus idle.
Then (send the hardware address) then write the address in the part you wish to access.
Set the bus idle
Then (send the hardware address) then read from the part.  It will deliver successive
read data bytes.
 
Depending on the compiler, the code usually looks something like..
 
{I2C Stop(); default}
I2C Start();
I2C write("write" hardware address);
I2C write(desired memory byte address);
I2C Start();
I2C write("read" hardware address);
read_data=I2C read();
I2C Stop;
 
Good Luck.
 
Regards,
Steve
-----Original Message-----
From: A...@yahoogroups.com [mailto:A...@yahoogroups.com] On Behalf Of sheikh adnan
Sent: Wednesday, July 13, 2005 5:07 AM
To: A...@yahoogroups.com
Subject: [AT91SAM7] how can i correct this code??

hi..
i m trying to write READ/ WRITE code for 24C64 with I2C standard, using 89c52, i m implementing my own I2C but having trouble with the code below that it does not read any charachter. here is the code for this written in KEIL micro vision..
#include <REG52.H>       // Header file for the 89C52 MCU
#include <STDIO.H>       // Standard I/O header file
//------
// Value Definitions
//------
#define  TRUE   0x01   // Value representing TRUE
#define  FALSE   0x00   // Value representing FALSE
#define  ON    0x01   // Value representing ON
#define  OFF    0x00   // Value representing OFF
#define  HIGH   1       // Value representing ON
#define  LOW    0       // Value representing OFF
#define   DELAY_BLINK  1500   // Value for delay time - blink
//------
// I/O Port Defines
//------
sbit   SDATA =  P1^0;     // Serial data
sbit   SCLK  =  P1^1;     // Serial clock
//------
// I2C Peripheral Function Prototypes
//------//------
// I2C Functions - Bit Banged
//------
void i2c_start (void);      // Sends I2C Start Trasfer
void i2c_stop (void);      // Sends I2C Stop Trasfer
void i2c_write (unsigned char input_data); // Writes data over the I2C bus
unsigned long i2c_read (void);    // Reads data from the I2C bus
//------
// Support Function Prototypes
//------
void initialize_system (void);
void delay_time (unsigned int time_end);    // To pause execution for pre-determined time
//------
// MAIN FUNCTION
//------
void main (void)
{
long  dum;
initialize_system ();
i2c_start();
//i2c_write('A');
//dum=  i2c_read();
i2c_read();
i2c_stop();

printf("%s",i2c_read()); 
}

//------
// I2C Functions - Bit Banged
//------
//------
//  Routine: i2c_start
// Inputs:  none
// Outputs: none
// Purpose: Sends I2C Start Trasfer
//------
void i2c_start (void){
 SDATA = HIGH;       // Set data line high
 SCLK = HIGH;       // Set clock line high
 SDATA = LOW;       // Set data line low (START SIGNAL)
 SCLK = LOW;        // Set clock line low
}
//------
//  Routine: i2c_stop
// Inputs:  none
// Outputs: none
// Purpose: Sends I2C Stop Trasfer
//------
void i2c_stop (void){
 unsigned char input_var;
 SCLK = LOW;        // Set clock line low
 SDATA = LOW;       // Set data line low
 SCLK = HIGH;       // Set clock line high
 SDATA = HIGH;       // Set data line high (STOP SIGNAL)
 input_var = SDATA;      // Put port pin into HiZ
 }
//------
//  Routine: i2c_write
// Inputs:  output byte
// Outputs: none
// Purpose: Writes data over the I2C bus
//------

/*void i2c_write(unsigned char output_data){
 unsigned char index;
 for(index = 0; index < 8; index++){   // Send 8 bits to the I2C Bus
                                   // Output the data bit to the I2C Bus
  SDATA = ((output_data & 0x80) ? 1 : 0);
       output_data  <<= 1;              // Shift the byte by one bit
  SCLK = HIGH;               // Clock the data into the I2C Bus
  SCLK = LOW;     
 }
 index = SDATA;       // Put data pin into read mode
 SCLK = HIGH;                // Clock the ACK from the I2C Bus
 SCLK = LOW;     
}
*/    
//------
//  Routine: i2c_read
// Inputs:  none
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//------
unsigned long i2c_read (void)
{
 unsigned char index, input_data;
   index = SDATA;       // Put data pin into read mode
  
 input_data = 0x80;
 for(index = 0; index < 7; index++)   // Send 8 bits to the I2C Bus
 {
  input_data >>= 1;     // Shift the byte by one bit
  SCLK = HIGH;               // Clock the data into the I2C Bus
        input_data |= SDATA;       // Input the data from the I2C Bus
  SCLK = LOW;     
 }
 return 'A';
}
 
//------
// SUPPORT FUNCTIONS
// If wana initilize any thing then use this function
//------
void initialize_system (void)
{
   
    SCON  = 0x50;             // SCON: mode 1, 8-bit UART, enable rcvr
    TMOD |= 0x20;                  // TMOD: timer 1, mode 2, 8-bit reload  
    TH1   = -3;                    // TH1:  reload value for 9200 baud @ 11.0592MHz
    TR1   = 1;                     // TR1:  timer 1 run                         
    TI    = 0;                     // TI:   set TI to send first char of UART
 TI=1;
}

////////////////////////////////////////////////////////////////////////////////
//  Routine: delay_time
// Inputs:  counter value to stop delaying
// Outputs: none
// Purpose: To pause execution for pre-determined time
////////////////////////////////////////////////////////////////////////////////
/*
void delay_time (unsigned int time_end){
 unsigned int index;
 for (index = 0; index < time_end; index++);
}
*/
 


Start your day with Yahoo! - make it your home page