📄 msp430iic_zjy.c
字号:
/****************************************************************/
/* eeprom.PRJ, VER0.0 */
/*SUB_24C256.C, VER0.0 */
/****************************************************************/
#include "msp430x44x.h"
#define ADDREE (unsigned int)0x00
#define SDA_LOW1 P6DIR |=BIT3;P6OUT &= ~BIT3 //时钟信号 数据信号高低控制定义-------通过电阻上拉,当口线设置
#define SDA_HIGH1 P6DIR&=~BIT3 // 为输入时,相当与输出高电平,设置为输出时,相当于输出低电平
#define SCL_LOW1 P6DIR |= BIT4;P6OUT &= ~BIT4
#define SCL_HIGH1 P6DIR&=~BIT4
#define DeviceCode 0xa0 //定义器件码
/******* 延时 *********/
void I2cWait1(void)
{
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
}
/******* I2C通信启动 *********/
void I2cStart(void)
{
SDA_HIGH1; //数据线输出高电平
SCL_HIGH1; //时钟线输出高电平
I2cWait1(); //延时
SDA_LOW1; //数据线拉低
I2cWait1(); //延时
SCL_LOW1; //时钟线拉低
}
/******* I2C通信停止 *********/
void I2cStop(void) //I2c停止信号
{
SDA_LOW1; //数据线拉低
I2cWait1(); //延时
I2cWait1();
SCL_HIGH1; //时钟线拉高
I2cWait1(); //延时
SDA_HIGH1; //数据线变高
}
/******* I2C通信初始化 *********/
void I2cInital(void)
{
SDA_HIGH1; //数据线变高
SCL_HIGH1; //时钟线变高
}
/******* 发送一个字节数据 *********/
unsigned char I2cSentByte(unsigned char bytedata)
{
unsigned char i,ack=2;
for(i=0;i<8;i++) //循环 8 次
{
if(bytedata & 0x80) { SDA_HIGH1; } //将高位输出
else { SDA_LOW1; }
bytedata <<= 1; //数据左移
I2cWait1(); //延时
SCL_HIGH1; //时钟信号变高,模拟正脉冲
I2cWait1(); //延时
SCL_LOW1; //时钟信号变低,为下一次输入做准备
I2cWait1(); //延时
}
SDA_HIGH1; //数据线变高,为接受响应信号作准备
I2cWait1(); //延时
SCL_HIGH1; //时钟信号变高
I2cWait1(); //延时
//ack= !(P6IN&BIT3) ? 0:1 ; //取得确认信号
while(P6IN&BIT3);
SCL_LOW1; //时钟信号变低
I2cWait1(); //延时
return ack; //返回确认信号
}
/******* 接受一个字节数据 *********/
unsigned char I2cReceiveByte(void)
{
unsigned char i,bytedata=0;
SDA_HIGH1;
for(i=0;i<8;i++) //循环 8 次
{
SCL_HIGH1; //时钟信号变高
I2cWait1(); //延时
bytedata <<= 1; //数据左移
if( P6IN&BIT3 ) bytedata|=0x01; //取得输入数据
SCL_LOW1; //时钟信号变低
I2cWait1(); //延时
}
return bytedata; //返回接收数据
}
/****** 发送确认信号 ********/
void SendAck1 (unsigned char ack)
{
if ( ack==0 ) { SDA_LOW1; } //发送 0
else { SDA_HIGH1; } //发送 1
SCL_HIGH1; //时钟信号变高
I2cWait1(); //延时
SCL_LOW1; //时钟信号变低
I2cWait1();
SDA_HIGH1;
//数据线变高,为下次传送作准备
}
/****** 写入单个字节数据 ********/
void I2cOneByteWrite(unsigned int address, unsigned char data)
{
unsigned int i;
unsigned char address2; // address1,
//address1=(unsigned char ) ((address&0xff00)>>8); //地址高字节
address2=(unsigned char) (address&0x00ff); //地址低字节
I2cStart(); //启动总线
I2cSentByte(DeviceCode); //发送器件码
//I2cSentByte(address1); //发送地址高字节
I2cSentByte(address2); //发送地址低字节
I2cSentByte(data); //发送写入的数据
I2cStop(); //总线停止
for( i=0;i<16000;i++); //延时,等待写入
}
/****** 写入一页数据 ********/
void I2cOnePageWrite(unsigned int address, unsigned char data)
{
unsigned int i;
unsigned char address2; // address1,
address2=(unsigned char) (address&0x00ff); //地址低字节
I2cStart(); //启动总线
I2cSentByte(DeviceCode); //发送器件码
I2cSentByte(address2); //发送地址低字节
for(i=0;i<16;i++)
{
I2cSentByte(data); //发送写入的数据
}
I2cStop(); //总线停止
for( i=0;i<16000;i++); //延时,等待写入
}/**/
/****** 读出 单个字节数据 ********/
unsigned char I2cReadOneByte(unsigned int address)
{
unsigned char data;
unsigned char address2; //address1,
// address1=(unsigned char ) ((address&0xff00)>>8); //地址高字节
address2=(unsigned char) (address&0x00ff); //地址低字节
I2cStart(); //启动总线
I2cSentByte(DeviceCode); //发送器件码
// I2cSentByte(address1); //发送地址高字节
I2cSentByte(address2); //发送地址低字节
I2cStart(); //启动总线
I2cSentByte(DeviceCode|0x01);
//发送器件码---读
data=I2cReceiveByte(); //读取 第 N 个数据
SendAck1(1); //发送 确认信号 1
I2cStop(); //总线停止
return data;
}
void main(void)
{
unsigned char value,i;
unsigned char a[16];
WDTCTL = WDTPW+WDTHOLD; //SHUT WDT
P5DIR |=BIT0+BIT1;
P5OUT =0;
P6DIR |= BIT5+BIT6+BIT7; //设置P6口为输出方式
P6OUT &= ~(BIT5+BIT6+BIT7); //使A2,A1,A0为高
I2cInital();
value=0x59;
I2cOneByteWrite(ADDREE,value );
I2cOnePageWrite(ADDREE,value );
value=0x00;
for(i=0;i<16;i++)
{
a[i]=I2cReadOneByte(ADDREE+i);
if(a[i]==0x59)
P5OUT |= BIT1;
else
P5OUT |= BIT0;
}
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -