MSP430口线模拟I2C总线

2008年11月04日 21:11    MSP430
关键词: MSP , 模拟 , 总线

MSP430口线模拟I2C总线

/**********************************************************************************************功能: 实现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 调试通过 .            *
********************************************************************************************/


#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 char count=8;
      while(count--);
    }     

void Delay_MS( unsigned int m )
    {   
      unsigned int i,j;
      for(i=0;i<m;i++)
          for(j=0;j<125;j++);
    }
void Setting_System_Clock_For_On_Chip_RC( char Frequency )
    {
      switch (Frequency)
        {
          case 1 COCTL = BCSCTL1 = CALBC1_1MHZ;break;
          case 8 COCTL = BCSCTL1 = CALBC1_8MHZ;break;
          case 12COCTL = BCSCTL1 = CALBC1_12MHZ;break;
          case 16COCTL = BCSCTL1 = CALBC1_16MHZ;break;
        }   
    }

void I2C_Pins_DIR_Setting ( unsigned char SDADIR )
    {
      P3DIR  |= I2CSDA + I2CSCL;
      if(SDADIR==1)
        {
          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;
      for(count=0;count<8;count++)
        {
          I2CSCL_SET_0;
          delay5us();
          if(BytEDAta&0x80)I2CSDA_SET_1;
          else             I2CSDA_SET_0;
          delay5us();
          I2CSCL_SET_1;
          delay5us();
          BytEDAta<<=1;
        }
    }         
unsigned char READ_BYTE_TO_24C04( void )
    {
      unsigned char count,readbyte=0;
      I2CSCL_SET_0;
      for(count=0;count<8;count++)
        {
          readbyte=readbyte<<1;
          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(1);
      I2C_Pins_DIR_Setting(0);
      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(15);
      I2CSCL_SET_0;
      I2CSDA_SET_0;
      Setting_System_Clock_For_On_Chip_RC(12);
     }          
unsigned char READ_DATA_FROM_24C04( unsigned char Address )
    {
      unsigned char readdate;
      Setting_System_Clock_For_On_Chip_RC(1);
      I2C_Pins_DIR_Setting(0);
      Engender_I2C_start_signal();
      WRITE_BYTE_TO_24C04(SlaveWriteAddress);
      Engender_I2C_ack_signal();
      WRITE_BYTE_TO_24C04(Address);
      Engender_I2C_ack_signal();
      Delay_MS(15);        
      Engender_I2C_start_signal();
      WRITE_BYTE_TO_24C04(SlaveReadAddress);
      I2C_Pins_DIR_Setting(1);
      Engender_I2C_ack_signal();
      readdate=READ_BYTE_TO_24C04();
      I2C_Pins_DIR_Setting(0);    
      Engender_I2C_noack_signal();
      Engender_I2C_stop_signal();
      I2CSCL_SET_0;
      I2CSDA_SET_0;   
      Setting_System_Clock_For_On_Chip_RC(12);
      return (readdate);
    }

void main( void )                   
    {
      unsigned char Data=0x59;
      WDTCTL  =  WDTPW+WDTHOLD;  
      Setting_System_Clock_For_On_Chip_RC(12);           
      P1DIR |= BIT0;
      while(1)
        {
          WRITE_DATA_TO_24C04(0X00,Data);
          READI2CBUF=READ_DATA_FROM_24C04(0x00);
          if(Data == READI2CBUF)
            {
              P1OUT |=  BIT0;
            }
          else
            {
              P1OUT &=~ BIT0;
            }
          Delay_MS(15);
        }
    }
欢迎分享本文,转载请保留出处:http://www.eechina.com/thread-2880-1-1.html     【打印本页】
maanzhenhow 发表于 2011-4-13 08:53:33
谢谢了
maanzhenhow 发表于 2011-4-13 08:53:52
太谢谢了
您需要登录后才可以发表评论 登录 | 立即注册

相关在线工具

相关文章

相关视频演示

厂商推荐


关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备11013910号 | 京公网安备11010502021702
回顶部