MSP430的IO口模拟I2C总线对AT24C25进行读写程序

功能: 实现MSP430口线模拟I2C总线协议与24C04通信.                                           *
* 描述: 主系统工作时钟为12MHz,I2C工作时钟频率为1MHz.给某地址写入一个数据     *
*         再读出来,如写入前和读出后的数据一致,则P1.0输出高电平,否则输出低电        *
*         平.                                                                                                                   *
*                                                  /|\    /|\                                                                     *
*               MSP430x22x4          10k   10k  ATMEL 24c04                                           *     
*                   master                     |      |       slave                                                       *
*             ---------------------------    |      |    ----------                                                      *
*           -|XIN     P3.1/UCB0SDA|<-|----+>|SDA      |                                                     *
*  32kHz |                                  |   |         |             |                                                     *
*           -|XOUT                         |    |        |             |                                                     *
*            |          P3.2/UCB0SCL|<-+-----> |SCL     |                                                      *
*            |                                  |             |             |                                                      *
*             --------------------------                ----------                                                       *
*                                                                                                                                 *
* 作者: Singel                                                                                                             *
* 时间: 2008年10月17日22时35分                                                                             *
*       此例在 IAR Embedded Workbench IDE for MSP430 v3.42a 调试通过 .            *
********************************************************************************************/

功能: 实现MSP430口线模拟I2C总线协议与24C04通信.                                           *
* 描述: 主系统工作时钟为12MHz,I2C工作时钟频率为1MHz.给某地址写入一个数据     *
*         再读出来,如写入前和读出后的数据一致,则P1.0输出高电平,否则输出低电        *
*         平.                                                                                                                   *
*                                                  /|\    /|\                                                                     *
*               MSP430x22x4          10k   10k  ATMEL 24c04                                           *
*                   master                     |      |       slave                                                       *
*             ---------------------------    |      |    ----------                                                      *
*           -|XIN     P3./UCB0SDA|<-|----+>|SDA      |                                                     *
*  32kHz |                                  |   |         |             |                                                     *
*           -|XOUT                         |    |        |             |                                                     *
*            |          P3./UCB0SCL|<-+-----> |SCL     |                                                      *
*            |                                  |             |             |                                                      *
*             --------------------------                ----------                                                       *
*                                                                                                                                 *
* 作者: Singel                                                                                                             *
* 时间: 2008年10月17日22时35分                                                                             *
*       此例在 IAR Embedded Workbench IDE for MSP430 v3.42a 调试通过 .            *
********************************************************************************************/
#include <MSP430x22x4.h>
#define  SlaveWriteAddress  0xa0
#define  SlaveReadAddress   0xa1
#define  OwnAddress         0xee
#define  I2CSDA             BIT1
#define  I2CSCL             BIT2
#define  I2CSDA_SET_1       P3OUT |=  I2CSDA
#define  I2CSDA_SET_0       P3OUT &=~ I2CSDA
#define  I2CSCL_SET_1       P3OUT |=  I2CSCL
#define  I2CSCL_SET_0       P3OUT &=~ I2CSCL
#define  I2CSDA_INPUT_IN    P3IN&I2CSDA
unsigned char READI2CBUF;

void delay5us( void )
{
    unsigned ;
    while(count--);
}     

void Delay_MS( unsigned int m )
{
    unsigned int i,j;
    ;i<m;i++)
        ;j<;j++);
} 

void Setting_System_Clock_For_On_Chip_RC( char Frequency )
{
    switch (Frequency)
    {
         :DCOCTL = BCSCTL1 = CALBC1_1MHZ;break;
         :DCOCTL = BCSCTL1 = CALBC1_8MHZ;break;
        :DCOCTL = BCSCTL1 = CALBC1_12MHZ;break;
        :DCOCTL = BCSCTL1 = CALBC1_16MHZ;break;
    }
} 

void I2C_Pins_DIR_Setting ( unsigned char SDADIR )
{
  P3DIR  |= I2CSDA + I2CSCL;
  )
    {
      P3DIR &=~ I2CSDA;
      P3OUT &=~ I2CSDA;
    }
}

void Engender_I2C_start_signal(void)
{
  I2CSCL_SET_1;  delay5us();
  I2CSDA_SET_1;  delay5us();
  I2CSDA_SET_0;  delay5us();
}       

void Engender_I2C_stop_signal(void)
{
  I2CSDA_SET_0;  delay5us();
  I2CSCL_SET_1;  delay5us();
  I2CSDA_SET_1;  delay5us();
}       

void Engender_I2C_ack_signal(void)
{
  I2CSCL_SET_0;  delay5us();
  I2CSCL_SET_1;  delay5us();
  I2CSCL_SET_0;  delay5us();
}          

void Engender_I2C_noack_signal(void)
{
    I2CSDA_SET_1;  delay5us();
    I2CSCL_SET_0;  delay5us();
    I2CSCL_SET_1;  delay5us();
    I2CSCL_SET_0;  delay5us();
}  

void WRITE_BYTE_TO_24C04( unsigned char BytEDAta )
{
    unsigned char count;
    ;count<;count++)
    {
        I2CSCL_SET_0;
        delay5us();
        if(BytEDAta&0x80)I2CSDA_SET_1;
        else             I2CSDA_SET_0;
        delay5us();
        I2CSCL_SET_1;
        delay5us();
        BytEDAta<<=;
    }
}          

unsigned char READ_BYTE_TO_24C04( void )
{
    unsigned ;
    I2CSCL_SET_0;
    ;count<;count++)
    {
        readbyte=readbyte<<;
        I2CSCL_SET_1;
        delay5us();
        if(I2CSDA_INPUT_IN)readbyte=readbyte|0x01;
        else               readbyte=readbyte&0xfe;
        delay5us();
        I2CSCL_SET_0;
        delay5us();
    }
    I2CSCL_SET_0;
    return readbyte;
}
void WRITE_DATA_TO_24C04( unsigned char Address,unsigned char Date )
{
    Setting_System_Clock_For_On_Chip_RC();
    I2C_Pins_DIR_Setting();
    Engender_I2C_start_signal();
    WRITE_BYTE_TO_24C04(SlaveWriteAddress);
    Engender_I2C_ack_signal();
    WRITE_BYTE_TO_24C04(Address);
    Engender_I2C_ack_signal();
    WRITE_BYTE_TO_24C04(Date);
    Engender_I2C_ack_signal();
    Engender_I2C_stop_signal();
    Delay_MS();
    I2CSCL_SET_0;
    I2CSDA_SET_0;
    Setting_System_Clock_For_On_Chip_RC();
}
unsigned char READ_DATA_FROM_24C04( unsigned char Address )
{
    unsigned char readdate;
    Setting_System_Clock_For_On_Chip_RC();
    I2C_Pins_DIR_Setting();
    Engender_I2C_start_signal();
    WRITE_BYTE_TO_24C04(SlaveWriteAddress);
    Engender_I2C_ack_signal();
    WRITE_BYTE_TO_24C04(Address);
    Engender_I2C_ack_signal();
    Delay_MS();
    Engender_I2C_start_signal();
    WRITE_BYTE_TO_24C04(SlaveReadAddress);
    I2C_Pins_DIR_Setting();
    Engender_I2C_ack_signal();
    readdate=READ_BYTE_TO_24C04();
    I2C_Pins_DIR_Setting();
    Engender_I2C_noack_signal();
    Engender_I2C_stop_signal();
    I2CSCL_SET_0;
    I2CSDA_SET_0;
    Setting_System_Clock_For_On_Chip_RC();
    return (readdate);
}

void main( void )
{
    unsigned char Data=0x59;
    WDTCTL  =  WDTPW+WDTHOLD;
    Setting_System_Clock_For_On_Chip_RC();
    P1DIR |= BIT0;
    )
    {
        WRITE_DATA_TO_24C04(0X00,Data);
        READI2CBUF=READ_DATA_FROM_24C04(0x00);
        if(Data == READI2CBUF)
        {
            P1OUT |=  BIT0;
        }
        else
        {
            P1OUT &=~ BIT0;
        }
        Delay_MS();
    }
}

通过微处理器I/O口模拟I2C总线对AT24C进行读写之前应注意一下两个问题:
一、微处理器的两个模拟I/O口在和SDA,SCL连接时必须使用上拉电阻。
一、I2C总线空闲的时候,两条信号线应该维持高电平。否则,上拉电阻上会有耗电。特别是在上电过程中,I/O线上电平也应保持在高电平状态。也就是说:当Master的I2C使用的是I/O软件模拟时,一定要保证该两个I/O上电默认均为输入(或高阻)或者输出高电平,切不可默认为输出低电平。I/O默认为输入时,可以通过外部上拉电阻将I2C信号线拉至高电平。
该程序通过调试,可以直接应用,程序如下:

/***********************************************************************************************
* 文 件 名 : AT24C256.S43.C
* 功能描述 : I/O模拟I2C时序读写AT24CXX(支持字节写、页写、字节读、顺序读)
* 作 者 : 梦回大唐
* 创建日期 : 2011-4-18
* 版 本 : Version1.0
*************************************************************************************************/
#include <msp430x16x.h>
#define SDA_IN            P5DIR &=~BIT0    // P5.0 IN
#define SDA_OUT           P5DIR |=BIT0     // P5.0 OUT
#define SDA_LOW           P5OUT &=~BIT0   // sda=0
#define SDA_HIGH          P5OUT |=BIT0   // sda=1
#define SCL_IN            P5DIR &=~BIT1    // P5.1 IN
#define SCL_OUT           P5DIR |=BIT1    // P5.1 OUT
#define SCL_LOW           P5OUT &=~BIT1
#define SCL_HIGH          P5OUT |=BIT1
#define W_EEPROM_LENGH    14

#define   TURE                1
#define   FALSE               0
#define   AckError            0x55
#define   OutOfRang           0xaa
#define   OutOfAddr           0xbb
unsigned ,,,,,,,,,,,,,};
unsigned char    x[W_EEPROM_LENGH];
void i2c_delay(unsigned char us);
void i2c_delay_ms(unsigned char ms);
void i2c_start();
void i2c_stop(void);
void i2c_SendAck(void);
void i2c_SendNoAck(void);
unsigned char i2c_check_ACK(void);
void i2c_SendByte(unsigned char data);
unsigned char i2c_RevByte(void);
unsigned char EEPROM_ByteWrite(unsigned int addr,unsigned char data);
unsigned char EEPROM_RandomRead(unsigned int addr);
unsigned char EEPROM_SequentialRead(unsigned int addr,unsigned int n,unsigned char* p);
unsigned int EEPROM_PageWrite(unsigned int page,unsigned char* p,unsigned char n);

void i2c_delay(unsigned char us)
{
    unsigned char tmp;
    while(us--)
    {
        ;tmp<;tmp++)
        {
            _NOP();
        }
    }
}

void i2c_delay_ms(unsigned char ms)
{
    unsigned int tmp;
    while(ms--)
    {
    ;tmp<;tmp++)
    {
        _NOP();
    }
    }
}

void i2c_start(void)
{
    SDA_OUT;
    i2c_delay();
    SDA_HIGH;
    i2c_delay();
    SCL_HIGH;
    i2c_delay();
    SDA_LOW;
    i2c_delay();
    SCL_LOW;
    i2c_delay();
}

void i2c_stop(void)
{
    SDA_OUT;
    SDA_LOW;
    i2c_delay();
    SCL_HIGH;
    i2c_delay();
    SDA_LOW;
    i2c_delay();
    SDA_HIGH;
}

void i2c_SendAck(void)
{
    SDA_OUT;
    SDA_LOW;
    i2c_delay();
    SCL_LOW;
    i2c_delay();
    SCL_HIGH;
    i2c_delay();
    SCL_LOW;
    SDA_HIGH;
}

void i2c_SendNoAck(void)
{
    SDA_OUT;
    SDA_HIGH;
    i2c_delay();
    SCL_LOW;
    i2c_delay();
    SCL_HIGH;
    i2c_delay();
    SCL_LOW;
}

unsigned char i2c_check_ACK(void)
{
    unsigned char AckStatus;
    SDA_IN;
    SCL_HIGH;
    i2c_delay();
    if(P5IN & 0x01)
    {
        AckStatus = FALSE;
    }
    else
    {
        AckStatus = TURE;
    }
    SCL_LOW;
    i2c_delay();
    SDA_OUT;
    return AckStatus;
}

void i2c_SendByte(unsigned char data)
{
    unsigned char tmp;
    SDA_OUT;
    ;tmp<;tmp++)
    {
        if(data & 0x80)
        {
            SDA_HIGH;
        }
        else
        {
            SDA_LOW;
        }
        i2c_delay();
        SCL_HIGH;
        i2c_delay();
        SCL_LOW;
        i2c_delay();
        data <<= ;
    }
    i2c_delay();
}

unsigned char i2c_RevByte(void)
{
    unsigned char tmp;
    unsigned ;
    SDA_IN;
    SCL_LOW;
    i2c_delay();
    ;tmp<;tmp++)
    {
        SCL_HIGH;
        i2c_delay();
        DATA <<= ;
        if(P5IN & 0x01)
        {
            DATA |= 0x01;
        }
        else
        {
            DATA &= 0xfe;
        }
        SCL_LOW;
    }
    SDA_OUT;
    return DATA;
}

unsigned char EEPROM_ByteWrite(unsigned int addr,unsigned char data)
{
    unsigned char Dev_addr; //设备地址
    unsigned char AddrLow;
    unsigned char AddrHigh;
    AddrLow = (unsigned char)addr;
    AddrHigh = (unsigned );
    Dev_addr = );
    i2c_start();
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(AddrHigh);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(AddrLow);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(data);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_stop();
    i2c_delay_ms();
    ;
}

unsigned char EEPROM_RandomRead(unsigned int addr)
{
    unsigned char Dev_addr; //设备地址
    unsigned char AddrLow;
    unsigned char AddrHigh;
    unsigned char tmp;
    AddrLow = (unsigned char)addr;
    AddrHigh = (unsigned );
    Dev_addr = );
    i2c_start();
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    i2c_SendByte(AddrHigh);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(AddrLow);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    i2c_start();
    Dev_addr = );
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    tmp = i2c_RevByte();
    i2c_SendNoAck();
    i2c_stop();
    i2c_delay();
    return tmp;
}

unsigned int EEPROM_PageWrite(unsigned int page,unsigned char* p,unsigned char n)
{
    unsigned char Dev_addr;
    unsigned char AddrLow;
    unsigned char AddrHigh;
    unsigned int tmp;
    )|(page > )) //根据读写的设备而变更为适合的页数和每页字节数
    {
        return OutOfRang;
    }
    tmp = ((unsigned ; //得出页首地址
    AddrLow = (unsigned char)tmp;
    AddrHigh = (unsigned );
    Dev_addr = );
    i2c_start();
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    i2c_SendByte(AddrHigh);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(AddrLow);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    while(n--)
    {
        i2c_SendByte(*p++);
        if(i2c_check_ACK() == FALSE)
        {
            return AckError;
        }
    }
    i2c_stop();
    i2c_delay_ms();
    ;
}

unsigned char EEPROM_SequentialRead(unsigned int addr,unsigned int n,unsigned char* p)
{
    unsigned char Dev_addr; //设备地址
    unsigned char AddrLow;
    unsigned char AddrHigh;
     - addr)) //检查预写入地址是否有效
    {
    return OutOfAddr;
    }
    AddrLow = (unsigned char)addr;
    AddrHigh = (unsigned );
    Dev_addr = );
    i2c_start();
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    i2c_SendByte(AddrHigh);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_SendByte(AddrLow);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    i2c_delay_ms();
    i2c_start();
    Dev_addr = );
    i2c_SendByte(Dev_addr);
    if(i2c_check_ACK() == FALSE)
    {
        return AckError;
    }
    while(n--)
    {
        *p = i2c_RevByte();
        p++;
        if(n)
        i2c_SendAck();
        else
        i2c_SendNoAck();
    }
    i2c_stop();
    ;
}

main()
{
    //unsigned char tt,tt1;
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
    DCOCTL = 0X73;
    BCSCTL1= 0X87; // 设置时钟频率4.00MHz
    P5DIR |=0X03;
    P5OUT |=0X03;
    //EEPROM_ByteWrite(0x0,12);
    //tt=EEPROM_RandomRead(0x0);
    //tt1=tt;
    EEPROM_PageWrite(,dat,);
    EEPROM_SequentialRead(,,x);
}
上一篇:2017微软秋招A题


下一篇:Linux 常用命令小结