This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
尊敬的团队:
我是否可以使用 P1.0、P1.1 来实现 软件 I2C?
我使用 P1.2 P1.3 来实现 软件 I2C、它运行良好。
然后 使用与 P1.2 P1.3相同的代码,只需更改引脚(使用 P1.0、P1.1)。 这不能奏效。
请帮助。
#include "include.h" void Simulative_I2C_Init(void) { P1DIR|= BIT2; P1DIR|= BIT3; PM5CTL0 &= ~LOCKLPM5; SCL_H; SDA_H; } void I2C_Start(void) { SDA_H; SCL_H; delay_us(10); SDA_L; delay_us(10); SCL_L; delay_us(10); } void I2C_Stop(void) { SDA_OUT; SDA_L; SCL_H; delay_us(10); SDA_H; delay_us(10); SCL_L; } void I2C_Ack(void) { SDA_L; delay_us(10); SCL_H; delay_us(10); SCL_L; delay_us(10); } void I2C_NoAck(void) { SDA_H; delay_us(10); SCL_H; delay_us(10); SCL_L; delay_us(10); } unsigned char Wait_Ack(void) { unsigned char tiemout=0; SDA_H; delay_us(10); SDA_IN; SCL_H; delay_us(10); while(READ_SDA) { tiemout++; if(tiemout>250) { I2C_Stop(); return 1; } } SCL_L; SDA_OUT; return 0; } unsigned char Send_Byte(unsigned char l_u8Data) { unsigned char i; for(i=0; i<8; i++) { SCL_L; delay_us(10); if(l_u8Data & 0x80) { SDA_H; } else { SDA_L; } delay_us(10); SCL_H; delay_us(10); l_u8Data = l_u8Data << 1; } SCL_L; delay_us(10); if(Wait_Ack()) { return 1; } else { return 0; } } unsigned char Receive_Byte(void) { unsigned char l_u8Count, l_u8RecData=0; SCL_L; delay_us(10); SDA_H; SDA_IN; for(l_u8Count=0; l_u8Count<8; l_u8Count++) { SCL_H; delay_us(10); l_u8RecData <<= 1; if(READ_SDA) { l_u8RecData |= 0x01; } SCL_L; delay_us(10); } SDA_OUT; return l_u8RecData; } unsigned char I2C_WriteByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char l_u8Data) { I2C_Start(); if(Send_Byte(l_u8SlaveAddr&0xFE)) { return 1; } if(Send_Byte(l_u8Addr)) { return 1; } if(Send_Byte(l_u8Data)) { return 1; } I2C_Stop(); return 0; } unsigned char I2C_WriteNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length) { unsigned char i; I2C_Start(); if(Send_Byte(l_u8SlaveAddr&0xFE)) { return 1; } if(Send_Byte(l_u8Addr)) { return 1; } for(i=0; i<l_u8Length; i++) { if(Send_Byte(l_u8Data[i])) { return 1; } } I2C_Stop(); return 0; } unsigned char I2C_ReadByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data) { I2C_Start(); if(Send_Byte(l_u8SlaveAddr&0xFE)) { return 1; } if(Send_Byte(l_u8Addr)) { return 1; } I2C_Start(); if(Send_Byte(l_u8SlaveAddr|0x01)) { return 1; } *l_u8Data = Receive_Byte(); I2C_NoAck(); I2C_Stop(); return 0; } unsigned char I2C_ReadNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length) { unsigned char i; I2C_Start(); if(Send_Byte(l_u8SlaveAddr&0xFE)) { return 1; } if(Send_Byte(l_u8Addr)) { return 1; } I2C_Start(); if(Send_Byte(l_u8SlaveAddr|0x01)) { return 1; } for(i=0; i<l_u8Length-1; i++) { l_u8Data[i] = Receive_Byte(); I2C_Ack(); } l_u8Data[i] = Receive_Byte(); I2C_NoAck(); I2C_Stop(); return 0; }
#ifndef __SIMIIC_H #define __SIMIIC_H /******************************************IO宏定义******************************************/ #define SDA_IN (P1DIR &= ~ BIT2) #define SDA_OUT (P1DIR |= BIT2) #define READ_SDA P1IN & BIT2 #define SDA_H (P1OUT |= BIT2) //p1.2 SDA #define SDA_L (P1OUT &= ~BIT2) #define SCL_H (P1OUT |= BIT3) // P1.3 SCL #define SCL_L (P1OUT &= ~BIT3) /******************************************函数接口声明******************************************/ extern void Simulative_I2C_Init(void); extern void I2C_Start(void); extern void I2C_Stop(void); extern void I2C_Ack(void); extern void I2C_NoAck(void); extern unsigned char Wait_Ack(void); extern unsigned char Send_Byte(unsigned char l_u8Data); extern unsigned char Receive_Byte(void); extern unsigned char I2C_WriteByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char l_u8Data); extern unsigned char I2C_ReadByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data); extern unsigned char I2C_WriteNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length); extern unsigned char I2C_ReadNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length); #endif
你(们)好 Susan
软件 I2C 解决方案中用作 GPIO 的 P1.0、P1.1、P1.2和 P1.3没有区别。
请考虑使用 MSP430FR243x、MSP430FR253x、MSP430FR263x 代码示例的基本代码 msp430fr243x_p1_01/p1_03来测试 IO 数字输出和输入功能(修订版 E)
同时、FR2433的 eUSCI_B 支持 I2C 功能。 (P1.0-P1.3)、我认为您可以考虑使用 eUSCI_B 外设。
谢谢!
I2C 使用开漏输出。 当我想进行位 bang I2C 时、我将输出(P1OUT)设置为零、并更改方向寄存器以更改电平。 使引脚成为输入、让上拉电阻器将其变为高电平、如果我想将其拉至低电平、则使其成为输出。
但是、在具有硬件支持的部件上、我必须耗尽串行端口、然后才能考虑位拆裂。
如果您使用的是 Launchpad、请不要忘记从 J10和 J11 (LED1/2)上移除跳线。