您的位置 首页 ADAS

MSP430口线模仿I2C总线

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

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

* 功用: 完成MSP430口线模仿I2C总线协议与24C04通讯.*
* 描绘: 主体系作业时钟为12MHz,I2C作业时钟频率为1MHz.给某地址写入一个数据*
* 再读出来,如写入前和读出后的数据共同,则P1.0输出高电平,不然输出低电*
* 平.*
*/|\ /|\ *
* MSP430x22x410k 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 for(j=0;j<125;j++);
}
void Setting_System_Clock_For_On_Chip_RC( char Frequency )
{
switch (Frequency)
{
case 1 :DCOCTL = BCSCTL1 = CALBC1_1MHZ;break;
case 8 :DCOCTL = BCSCTL1 = CALBC1_8MHZ;break;
case 12:DCOCTL = BCSCTL1 = CALBC1_12MHZ;break;
case 16:DCOCTL = 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);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/272594.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部