您的位置 首页 5G

MSP430单片机硬件IIC

MSP430IIC接口是很方便的物件,但是还是有很多人弃之不用,当然有各种原因,但是如果你确实要用IIC接口,而且容许的话,用它还是很舒服的

//MSP430IIC接口是很便利的物件,可是仍是有很多人弃之不必,当然有各种原因,可是假如你的确要用IIC接口,并且容许的话,用它仍是很舒畅的,或许开端不太顺,可是一旦搞通了你会领会到他的优点。
我看了一些网上的程序,有的依然用模仿方法的思想处理某些环节,比方应对信号。非要追寻”ACK”不行,其实接口现已供给了“NACK”中止,何须非要反向处理呢?事实上,假如在正常传送中呈现“NACK”中止,你要考虑替换器材或许考虑体系的安稳性了。

#include <msp430x16x.h>
unsigned char RX_BUF[10];
unsigned char TX_BUF[16];
unsigned char Rece_data[16];

int RX_COUNTER;
int TX_COUNTER;

void Init_IIC(void);
void EEPROM_WriteN(unsigned char mAddr,unsigned char sAddr,unsigned char n);
unsigned char EEPROM_ByteRead(unsigned char mAddr,unsigned char sAddr);
void EEPROM_ReadN(unsigned char mAddr,unsigned char sAddr,unsigned char n);
//=======================================
//USART0中止
//=======================================
#pragma vector = USART0TX_VECTOR
__interrupt void I2C_ISR(void)
{
switch(I2CIV)
{
case 2: I2CTCTL |= I2CSTP; I2CIFG=0; break;//无应对
case 10: RX_BUF[RX_COUNTER] = I2CDRB;
RX_COUNTER++;
break;

case 12: I2CDRB = TX_BUF[TX_COUNTER];
TX_COUNTER++;
break;
default: break;
}
}

//====================================
//体系时钟初始化
//====================================
void INIT_SYSTIMER(void)
{
unsigned char i;
BCSCTL1 &= ~XT2OFF; //选 XT2
do
{
IFG1 &= ~OFIFG; //铲除震动器失效标志
for (i = 0xFF; i > 0; i–); //安稳时刻
}
while ((IFG1 & OFIFG)!=0); //假如震动器失效标志存在,等候
BCSCTL2 = SELM_2 + SELS + DIVS_3; //MCLK时钟源=XT2,SMCLK时钟源=XT2/8
}
//=================================
void delay(unsigned int i)
{
unsigned int j;
for(j=0;j}

//=================================================
void Init_IIC(void)
{
P3SEL |= 0x0a;
U0CTL = I2C + SYNC;
U0CTL &= ~I2CEN;
I2CTCTL |= I2CSSEL_2; //SMCLK
I2CPSC = 9; //I2C时钟 = SMCLK / 10: 100KH;
I2CSCLH = 0x03; //SCL 高电平为:5 *I2C 时钟
I2CSCLL = 0x03; //SCL 低电平为:5 *I2C 时钟
I2CSA = 0X50;
I2CNDAT = 0x02;
U0CTL |= I2CEN; //I2C 模块有用
I2CIE |= TXRDYIE + RXRDYIE; //Enable TX RX interrupt
}
//=================================================
//关于FM24L16的写操作 发送N个数据
//16>n>0
//0x50<=mAddr<=0x57 sAddr<16;
//=================================================
void EEPROM_WriteN(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
I2CSA = mAddr; //页地址
TX_BUF[0] = sAddr; //子地址
I2CNDAT = n; //计数器初值
TX_COUNTER = 0;
while (I2CDCTL & I2CBUSY); //查看模块忙
I2CIFG =0; //中止标志清零
I2CIE |= TXRDYIE;
U0CTL |= MST;
I2CTCTL |= I2CTRX + I2CSTT + I2CSTP;
while ((~I2CIFG) & ARDYIFG); //查看发送完毕
}
//===================================================
//读1个字节
//===================================================
unsigned char EEPROM_ByteRead(unsigned char mAddr,unsigned char sAddr)
{
I2CSA = mAddr; //页地址
TX_BUF[0] = sAddr; //子地址
TX_COUNTER = 0; //计数器初值
RX_COUNTER = 0;

while (I2CDCTL & I2CBUSY); //查看模块忙
I2CNDAT = 1; //1位数据=子地址
I2CIFG = 0; //中止标志清零
I2CIE |= TXRDYIE;
U0CTL |= MST;
I2CTCTL |= I2CTRX + I2CSTT; //置位开始位 发送子地址
while ((~I2CIFG) & ARDYIFG); //查看发送完毕

I2CIFG &= ~ARDYIFG;
I2CIE |= RXRDYIE;
I2CTCTL &= ~I2CTRX;
I2CTCTL |= I2CSTT + I2CSTP; //接纳1位数据后置位中止位
while ((~I2CIFG) & ARDYIFG); //查看接纳完毕

return RX_BUF[0]; //回来接纳的1字节数据
}
//======================================
//n=读N个字节,mAddr页地址;sAddr子地址
//======================================
void EEPROM_ReadN(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
unsigned char i;
unsigned char k;
unsigned char m=0;
k = sAddr + n;
for(i=sAddr;i {
Rece_data[m]=EEPROM_ByteRead(mAddr,i);
m++;
}
}



//==================================
void SAVE_DATA(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
TX_BUF[1] = 0X41;
TX_BUF[2] = 0X42;
TX_BUF[3] = 0X43;
TX_BUF[4] = 0X44;
TX_BUF[5] = 0X45;
TX_BUF[6] = 0X46;
TX_BUF[7] = 0X47;
TX_BUF[8] = 0X48;
TX_BUF[9] = 0X49;
TX_BUF[10] = 0X4A;
TX_BUF[11] = 0X4B;

EEPROM_WriteN(0x50,0x00,n+1);
}

//==================================
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
INIT_SYSTIMER();
Init_IIC();
_EINT(); //翻开中止

while(1)
{
//=================================

SAVE_DATA(0x50,0x00,11);
EEPROM_ReadN(0x50,0x00,11);

delay(10);
//=================================
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部