我使用pic16f1704微控制器,我的編譯器是xc8。我一直在試圖獲得控制器與地址0x00的I2C通信。我的SCL引腳是RC0,SDA引腳是RC1。我發現xc8編譯器中的內置函數可以與pic16一起使用。我根據數據表做了以下代碼。但編譯我的代碼後,我無法獲得SCL和SDA波形。使用xc8編譯器的pic16f1704的I2C通信
請看我的代碼是否正確,請讓我知道我哪裏出錯了。
#include<xc.h>
#pragma config FOSC=INTOSC, PLLEN=OFF, MCLRE=ON, WDTE=OFF
#pragma config LVP=OFF, CLKOUTEN=OFF
#define SCL TRISC0
#define SDA TRISC1
#define SCL_IN RC0
#define SDA_IN RC1
void init()
{
SDA = SCL = 1;
SCL_IN = SDA_IN = 1; //SDA & SCL at high state
}
void i2c_start(void)
{
SDA = 1; // i2c start bit sequence
SCL = 1;
_delay(10000);
SDA = 0;
SCL = 1;
_delay(10000);
}
void i2c_stop(void)
{
SDA = 0; // i2c stop bit sequence
_delay(10000);
SCL = 1;
_delay(1000);
SDA = 1;
_delay(10000);
}
void I2CWait()
{
while ((SSPCON2 & 0x1F) || (SSPSTAT & 0x04));
}
void main()
{
OSCCONbits.SCS = 0x02; //set the SCS bits to select internal oscillator block
OSCCONbits.IRCF = 0x0F; //set OSCCON IRCF
BAUD1CONbits.BRG16=0; //BRGH IS NOT VALID IN SYNCHRONOUS TRANSMISSION
SPBRGH=0X00;
SPBRGL=0X19;
SLRCONC=0xff; //Slew rate IS LIMITED
ANSELC=0x00; //Digital mode is selected
SSPCLKPPS=0x10;
SSPDATPPS=0X11; //PPL for input pins
RC0PPS=0X10;
RC1PPS=0X11; //PPL for output pins
SSPCONbits.SSPM0=0;
SSPCON1bits.SSPM1=0;
SSPCON1bits.SSPM2=0;
SSPCON1bits.SSPM3=1; //selecting I2C master mode
SSP1STATbits.SMP=1; //SLEW RATE CONTROL
SSP1STATbits.CKE=1; //CLOCK EDGE SELECT BIT
SPEN=1;
ACKEN=1;
SSPCON1=0X28; //sync. serial port enable bit is set along with I2C mastermode
SSPADD=0X27; //calculated value
GIE=1;
PIE1=1;
TXIE=1;
init();
SEN=1; //start enable
i2c_start();
I2CWait();
_delay(10000);
SSPBUF=0X00;
_delay(10000);
PEN=1; //stop enable
i2c_stop();
}
另請參閱啓動條件所需的這些類型的初始化。
void i2c_start(void)
{
SDA = 1; // i2c start bit sequence
SCL = 1;
_delay(10000);
SDA = 0;
SCL = 1;
_delay(10000);
}
我認爲這是沒有必要的。當SEN(啓動使能)位置位時,控制器驅動SDA爲低電平,SCL保持高電平。