您的位置 首页 芯闻

msp430读写24c512程序

*文件名:msp430f169i2cc*整体描述:MSP430F169单片机硬件IIC软件,字节方式,主方式*IIC接口:P33=SCL,P31=SDA;(开漏输出)*相应寄存

*文件名:msp430f169i2c.c

*全体描绘:MSP430F169单片机硬件IIC软件,字节方法,主方法
* IIC接口:P3.3=SCL,P3.1=SDA;(开漏输出)
* 相应寄存器:地址寄存器 I2COA 用于寄存本身从地址(从方法时才有用)
* 地址寄存器 I2CSA 用于寄存外围的从机地址(主方法时才有用)
* 操控寄存器 U0CTL 硬件I2C的设置、使能、形式等。
发送操控寄存器 I2CTCTL
* 数据操控寄存器 I2CDCTL 指示I2C总线的状况
*
* U0CTL — RXDMAEN,TXDMAEN,I2C, XA, LISREN,SYNC,MST,I2CEN
* 0 0 1 0 0 1 1 1 (0x17)
* I2CTCTL –I2CWORD,I2CRM,I2CSSEL1,I2XSSEL0, I2CTRX,I2CSTB, I2CSTP, I2CSTT
* 0 1 1 0 * 0 * *
************************************************************/
#include
#include “fpgacode.h”

#define WR24C512
#define LED1_1 (0x20)
#define LED1_0 (0xdf)

#define SDA_1 P3OUT |= BIT1 //串行数据线,SDA = 1
#define SDA_0 P3OUT &=~ BIT1 //SDA = 0
#define SCL_1 P3OUT |= BIT3 //串行时钟线,SCL = 1
#define SCL_0 P3OUT &=~ BIT3 //SCL = 0

#define SDADIR_IN P3DIR &=~ BIT1 //SDA,I/O口为输入
#define SDADIR_OUT P3DIR |= BIT1 //I/0口为输出
#define SDA_IN ((P3IN >> 1) & 0x01) //Read SDA

#define SCLDIR_IN P3DIR &=~ BIT3 //SCL,I/O口为输入
#define SCLDIR_OUT P3DIR |= BIT3 //I/0口为输出
#define SCL_IN ((P3IN >> 3) & 0x01) //Read SCL

#ifdef WR24C512
static int numi = 0; //Data Pointer
#else
static int numj = 0; //Data Pointer
#endif

#define I2CSLA 0x50

void I2c_Idle_Check(void)
{
while (I2CBUSY & I2CDCTL); // I2C ready? 在闲暇状况:0,闲暇;1:忙
}

void DelayTime10us(unsigned char n)
{
unsigned char i;
while(n–) // 5 cycles
for(i=0;i<10;i++); // 8mhz 110:771 + 4 to while
}

void I2cBusSendByte(unsigned char c)
{
while((I2CIFG & TXRDYIFG) != TXRDYIFG); // 检测发送预备
I2CDRB = c; // 写发送寄存器
}

void I2C_Send(unsigned char ndatNum)
{
// 留意:通讯完毕,I2CMST 主动清零,再次通讯有必要从头置位
P3OUT = 0x00; // clear P3 output register
P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
U0CTL |= (I2C + SYNC); // select I2C mode;XA=0,7bit_addresing;
U0CTL &= ~I2CEN; // i2c功能使能无效
// I2CTCTL = I2CRM + I2CSSEL_2; // x(x>256)字节形式 ,new start测验运用

// 挑选方法 I2CRM=0,最终用户运用
I2CTCTL = I2CSSEL_2;
I2CNDAT = 2 + ndatNum; // 最终用户运用,2byte地址 + 128byte数据

I2CPSC = 2; // set scl
I2CIFG = 0;
I2CSA = 0x50; // Slave address of At24c512
U0CTL |= I2CEN; // enable I2C module, 7 bit addr, master mode 08-26
U0CTL |= MST;


while (I2CBUSY & I2CDCTL); // I2C ready? 在闲暇状况:0,闲暇;1:忙
I2CTCTL |= I2CTRX + I2CSTT + I2CSTP ; // I2CRM =0,发动总线,发送从器材地址
while((I2CIFG & NACKIFG) == 0x02); // ack 为低电平,等候地址应对位 ,判别 无应对NACKIFG = 1
}

unsigned char I2cSendSubAddr(unsigned int suba,unsigned char ndatNum)
{
unsigned char Hi_suba,Lo_suba;

I2C_Send(ndatNum); // 发动总线,等候地址应对位

Hi_suba = ( unsigned char )( suba >> 8 );
Lo_suba = ( unsigned char )( suba & 0x00ff );
I2cBusSendByte( Hi_suba ); //发送器材子地址
while((I2CIFG & 0x02) == NACKIFG ) // 等候数据的应对
{
I2CTCTL |= I2CSTP; // 无应对,完毕总线
return(0);
}
I2cBusSendByte( Lo_suba ); //发送器材子地址
while((I2CIFG & 0x02 )==0x02 ) // 等候数据的应对
{
I2CTCTL |= I2CSTP; // 无应对,完毕总线
return(0);
}
return(1);
}

unsigned char I2cSendStr(const unsigned char * ps,unsigned char num)
{
// unsigned char i;
unsigned char tempnum;
tempnum = num-1;
for(numi=0;numi {
I2cBusSendByte(*ps);
while((I2CIFG & 0x02) == NACKIFG) // 等候数据的应对
{
I2CTCTL |= I2CSTP; // 无应对,完毕总线
return(0);
}
ps++;
}

// I2CTCTL |= I2CSTP; //I2CRM =1形式中,在发送最终一个数据之前将中止方位位,*特别留意,有必要
I2cBusSendByte(*ps);
while((I2CIFG & 0x02) == NACKIFG) // 等候数据的应对
{
I2CTCTL |= I2CSTP; // 无应对,完毕总线
return(0);
}
return(1);
}

//——————写at24c512—-
void I2cWrAt24c512(void)
{
unsigned int j=0;
unsigned char m,n;
unsigned int tempAddr;

for (j = 0;j <462;j++) // page write
{
tempAddr = 128*j;
while (I2CBUSY & I2CDCTL); // I2C ready?
I2cSendSubAddr(tempAddr,0x80);
I2cSendStr(FPGACfg+tempAddr,0x80);
P4OUT ^= 0x80;
for (m=0;m<125;m++) {for (n=0;n<110;n++);}
P4OUT ^= 0x80;
}

tempAddr = 128*j;
while (I2CBUSY & I2CDCTL); // I2C ready?
I2cSendSubAddr(tempAddr,0x4f);
I2cSendStr(FPGACfg+tempAddr,0x4f);
P4OUT ^= 0x80;
for (m=0;m<125;m++) {for (n=0;n<110;n++);}
P4OUT ^= 0x80;
}

void I2cReceSubAddr(unsigned int suba) // test 09-25 读之前先写地址
{
unsigned char Hi_suba,Lo_suba;
P3OUT = 0x00; // clear P3 output register
P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
U0CTL |= I2C + SYNC; // select I2C mode;XA=0,7bit_addresing;
U0CTL &= ~I2CEN; // i2c功能使能无效
// I2CTCTL =I2CRM + I2CSSEL_2;//I2CRM = 1,自己设定字节个数

I2CTCTL =I2CSSEL_2; // I2CRM = 0,
I2CNDAT = 2; // I2CRM = 0,2byte地址 + 128byte数据,固定字节
I2CIFG = 0;
I2CPSC = 1; // set scl
// I2CSCLH = 0x02; // High period of SCL
//I2CSCLL = 0x02; // Low period of SCL
I2CSA = 0x50; // Slave address of At24c512
U0CTL |= I2CEN; // enable I2C module, 7 bit addr,master mode 08-26
U0CTL |= MST;


Hi_suba = ( unsigned char )( suba >> 8 );
Lo_suba = ( unsigned char )( suba & 0x00ff );

while (I2CBUSY & I2CDCTL); // I2C ready? 在闲暇状况:0,闲暇;1:忙
I2CTCTL |= I2CTRX + I2CSTT + I2CSTP ;// I2CRM =0,发动总线,发送从器材地址
while((I2CIFG & NACKIFG) == 0x02) // ack 为低电平,等候地址应对位 ,判别无应对NACKIFG = 1
{
I2CTCTL |= I2CSTP; // 完毕总线
}

I2cBusSendByte( Hi_suba ); //发送器材子地址
while((I2CIFG & NACKIFG) == 0x02) // ack 为低电平,等候地址应对位 ,判别无应对NACKIFG = 1
{
I2CTCTL |= I2CSTP; // 完毕总线
}
I2cBusSendByte( Lo_suba ); //发送器材子地址
while((I2CIFG & NACKIFG) == 0x02) // ack 为低电平,等候地址应对位 ,判别无应对NACKIFG = 1
{
I2CTCTL |= I2CSTP; // 完毕总线
}
DelayTime10us(250);
DelayTime10us(250);
DelayTime10us(250);
DelayTime10us(250);
}

void InitI2cRece(unsigned char num)
{
P3OUT = 0x00; // clear P3 output register
P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
U0CTL |= I2C + SYNC; // select I2C mode;XA=0,7bit_addresing;
U0CTL &= ~I2CEN; // i2c功能使能无效
// I2CTCTL = I2CRM + I2CSSEL_2;// I2CRM=1 x(x>256)字节形式

I2CTCTL =I2CSSEL_2; // I2CRM=0
I2CNDAT = num; // 1个字节
I2CPSC = 1; // set scl
//I2CSCLH = 0x01; // High period of SCL
//I2CSCLL = 0x01; // Low period of SCL
I2CIFG = 0;
I2CSA = 0x50; // Slave address of At24c512
I2CTCTL &= ~I2CTRX;
U0CTL |= I2CEN; // enable I2C module, 7 bit addr,master mode 08-26
}

unsigned char I2cReceSlaByte(void)
{
unsigned char ch;
DelayTime10us(15);
U0CTL |=MST; //主形式
while (I2CBUSY & I2CDCTL); // I2C ready? 在闲暇状况:0,闲暇;1:忙
I2CTCTL |= I2CSTT + I2CSTP; // I2CRM =0; Receive, ST, SP (clears MST)
while((I2CIFG & RXRDYIFG) != RXRDYIFG);// 等候地址应对位 ,判别
ch = I2CDRB;
return(ch);
}

void I2cReceSlaStr(unsigned char num)
{
unsigned char i;
DelayTime10us(15);
U0CTL |=MST; //主形式
while (I2CBUSY & I2CDCTL); // I2C ready? 在闲暇状况:0,闲暇;1:忙
I2CTCTL |= I2CSTT + I2CSTP; // I2CRM =0; Receive, ST, SP (clears MST)
for (i=0;i
{
while((I2CIFG & RXRDYIFG) != RXRDYIFG);// 等候地址应对位 ,判别
rxDataArray = I2CDRB;
}
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部