Hi all!!!!!!
I write this code to read/write data to 24LC256, but it not work. Anyone can help me???
#include "string.h"
#include "msp430x22x2.h"
#include <intrinsics.h>
union reg {
struct bit {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}_BIT;
unsigned char _BYTE;
};
union reg* _P2_DIRECT = (union reg*)0x2A ;
union reg* _P2_OUT = (union reg*)0x29 ;
union reg* _P2_IN = (union reg*)0x28 ;
union reg* _P2_SEL = (union reg*)0x2E ;
union reg* _P3_DIRECT = (union reg*)0x1A ;
union reg* _P3_OUT = (union reg*)0x19 ;
union reg* _P3_IN = (union reg*)0x18 ;
#define RS _P2_OUT->_BIT.b6
#define EN _P2_OUT->_BIT.b7
#define DATA_4 _P2_OUT->_BIT.b0
#define DATA_5 _P2_OUT->_BIT.b1
#define DATA_6 _P2_OUT->_BIT.b2
#define DATA_7 _P2_OUT->_BIT.b3
#define SCLDIR _P3_DIRECT->_BIT.b2
#define SDADIR _P3_DIRECT->_BIT.b1
#define SCLOUT _P3_OUT->_BIT.b2
#define SDAIN _P3_IN->_BIT.b1
#define SDAOUT _P3_OUT->_BIT.b1
#define EEPROMS_ID 0xA0
void LCD_out(unsigned char data)
{
unsigned char convert=0;
convert=data&0x10;
if(convert==0x10)DATA_4=1;
else DATA_4=0;
convert=data&0x20;
if(convert==0x20)DATA_5=1;
else DATA_5=0;
convert=data&0x40;
if(convert==0x40)DATA_6=1;
else DATA_6=0;
convert=data&0x80;
if(convert==0x80)DATA_7=1;
else DATA_7=0;
}
//******************************************************
void lcd_delay(int time)
{
while(--time);
}
void lcd_wait_busy(void)
{
lcd_delay(1000);
}
//********************LCD Funtion***********************
//write 4 bit to LCD
void lcd_write_4bits(unsigned char dat)
{
EN=1;
LCD_out(dat);
lcd_delay(10);
EN=0;
lcd_delay(10);
}
void lcd_write_cmd(unsigned char cmd)
{
EN=1;
lcd_wait_busy();
RS=0;
lcd_write_4bits(cmd);
lcd_write_4bits(cmd << 4);
}
void lcd_write_data(unsigned char data)
{
lcd_wait_busy();
RS=1;
lcd_write_4bits(data);
lcd_write_4bits(data << 4);
}
void init_lcd()
{
lcd_delay(15000);
RS=0;
lcd_write_4bits(0x03 << 4);
lcd_delay(4100);
lcd_write_4bits(0x03 << 4);
lcd_delay(100);
lcd_write_4bits(0x03 << 4);
lcd_write_4bits(0x02 << 4);
lcd_write_cmd(0x28);
lcd_write_cmd(0x0C);
lcd_write_cmd(0x06);
lcd_write_cmd(0x01);
}
//***************************************************************
void LCDputs(char *s,unsigned char row)
{
unsigned char len;
if(row==1) lcd_write_cmd(0x80);
else lcd_write_cmd(0xC0);
len=strlen(s);
while(len!=0)
lcd_write_data(*s);
s++;
len--;
}
}
//******************************************************************************
//******************************************************************************
void delay_1(void)
{
int i=0x0f;
while(i--);
}
void I2C_start(void)
{
SCLDIR =1;
SDADIR =1;
SDAOUT = 1; SCLOUT = 1;
delay_1();
SDAOUT = 0; delay_1();
SCLOUT = 0; delay_1();
}
void I2C_stop(void)
{
SCLDIR =1;
SDADIR =1;
SCLOUT = 1; SCLOUT = 0;
SDAOUT = 0; delay_1();
SCLOUT = 1; delay_1();
SDAOUT = 1;
}
unsigned char I2C_write(unsigned char dat)
{
SCLDIR =1;
SDADIR =1;
unsigned char i;
for (i=0;i<8;i++)
{
SDAOUT = (dat & 0x80) ? 1:0;
SCLOUT=1;SCLOUT=0;
dat<<=1;
}
SCLOUT = 1; delay_1();
SCLOUT = 0;
return dat;
}
unsigned char I2C_read(void)
{
SCLDIR =1;
SDADIR =0;
char rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
delay_1();
SCLOUT = 1; delay_1(); /* Set SCL */
rd_bit = SDAIN; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCLOUT = 0; /* Clear SCL */
}
return dat;
}
void I2C_noack()
{
SCLDIR =1;
SDADIR =1;
SDAOUT = 1; /* Set SDA */
delay_1();
SCLOUT = 1; delay_1();
SCLOUT = 0; /* Call for send data to i2c bus */
SCLOUT = 1; /* Set SCL */
}
//******************************************************************************
//******************************************************************************
unsigned char EEPROM_byteread(unsigned int addr)
{
unsigned char dat;
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_start(); /* Start i2c bus */
I2C_write(EEPROMS_ID+1); /* Connect to EEPROM for Read */
dat = I2C_read(); /* Receive data */
I2C_noack();
I2C_stop(); /* Stop i2c bus */
return dat;
}
void EEPROM_bytewrite(unsigned int addr, unsigned char val)
{
I2C_start();
I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr>>8); /* Request RAM address (Hight byte) */
I2C_write(addr&0x00FF); /* Request RAM address (Low byte) */
I2C_write(val); /* Write sec on RAM specified address */
I2C_stop(); /* Stop i2c bus */
}
//##############################################################################
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
_P2_DIRECT->_BYTE=0xff;
_P2_SEL->_BIT.b6=0;
_P2_SEL->_BIT.b7=0;
unsigned char test;
init_lcd();
EEPROM_bytewrite(0x0002, 0x0f);
//delay_1();
test=EEPROM_byteread(0x0002);
if(test==0x0f){
LCDputs("wellcome", 1);
LCDputs("EEPROM interface", 2);
}
}