10.07.2015 Views

MSSPを使用したi2cプロトコル関数

MSSPを使用したi2cプロトコル関数

MSSPを使用したi2cプロトコル関数

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

#define_LEGACY_HEADERS"lcd.h" #include "mssp_i2c.h" mssp_i2cTRUE 0x0; #define FALSE 0x1; /**************************************************MSSP 使 用 マスターモード 専 用 I2C 関 数教 育 ・ホビー 用 、 営 利 目 的 ・ 商 用 への 利 用 は 禁 止詳 しいタイミングはNXPの 資 料 やPICドキュメント * 参 照 のこと **************************************************/* msspはSDAとSCL 信 号 をスタート 状 態 にします * 衝 突 有 るとMSSPはリセットされる */void*/SSP1CON2bits.SEN = 1; SSP1CON2:0 initiate I2C START condition while (SSP1CON2bits.SEN == 1); // wait until START bit finishes } /*i2c_start(void){信 号 をRESTART 状 態 にします * 衝 突 有 るとBCL1IFが 立 つ */void i2c_restart( void) { msspはSDAとSCL= 1; // SSP1CON2:1 Repeated Start while (SSP1CON2bits.RSEN==1); } /* 関 数 の 説 明DATAをSSP1BUFに 書 きスレーブに 転 送 かける SSP1CON2bits.RSEN転 送 中 はR_nWが1になっている */void i2c_write( unsigned char DataByte ){= DataByte; while (SSP1STATbits.R_nW == 1); }関 数 の 説 明 * 受 信 可 にしてDATAを 待 つ。 /*RSEN0になったら、SSP1BUFのデータを 読 み 持 って 返 る */unsigned char i2c_read( void) { unsigned char data; SSP1CON2bits.RCEN = 1; // SSP1CON2:3 Receive enable while (SSP1CON2bits.RCEN == 1); *return data; } /* 関 数 の 説 明msspはSDAとSCL 信 号 をstop 状 態 にします data=SSP1BUF;PENが0になったら、かえる。 衝 突 あるとBCL1IFが 立 つ */void i2c_stop( void) { *= 1; // SSP1CON2:2 initiate I2C STOP condition while (SSP1CON2bits.PEN); } /* 関 数 の 説 明ACK(ACKDT:0)かNACK(ACKDR:1)を 送 る SSP1CON2bits.PENACKENしたらかえる。 */void i2c_sendack(unsigned char status) { SSP1CON2bits.ACKDT status; SSPCON2:5 set to ACK:0,NACK:1 SSP1CON2bits.ACKEN = 1; SSPCON2:4 initiate I2C condition while ( SSP1CON2bits.ACKEN); // wait until ACK sequence is over */* 関 数 の 説 明 ページ(1) }


ACK(ACKDT:0)かNACK(ACKDR:1)をチェックする * ACK(ACKSTAT:0)かNACK(ACKSTAT:1)でかえる。 */unsigned char i2c_readack(void) { if (SSP1CON2bits.ACKSTAT == { mssp_i2c0;detectedelse { return 1; // ACK not detected } }関 数 の 説 明指 定 アドレスにWRITEモードでアドレスを 書 く * ACK 有 ればTRUEで、 無 ければFALSEでかえる /*char i2c_writeto(unsigned char address){ i2c_start(); i2c_write(address | 0x0); if(i2c_readack()==0) return TRUE; return FALSE; */unsigned/* 関 数 の 説 明指 定 アドレスにREADモードでアドレスを 書 く * ACK 有 ればTRUEで、 無 ければFALSEでかえる */unsigned}i2c_readfrom(unsigned char address){ i2c_start(); i2c_write(address | 0x1); if(i2c_readack()==0) return TRUE; char}return FALSE;ページ(2)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!