i2c.c

来自「pic16f877a使用iic总线方式驱动低价温度芯片tc74」· C语言 代码 · 共 42 行

C
42
字号
#include"pic.h"
unsigned char jdate;
unsigned char code;
void i2cint(void)//iic设置
{

TRISC3=1;//input setting for iic work
TRISC4=1;// same as above
SSPCON=0X28;//iic setting word
STAT_SMP=1;//iic setting bit
SSPADD=0X05;//iic bit rate setting
GIE=0; //turn off the interrupt
SSPIF=0; //clear mssp flag bit
}
void tem_read()//读温度
{
STAT_CKE=1;
SEN=1;     //start bit
while(SEN==1);
SSPBUF=0X9A;//address for writing
while(STAT_RW==1); //wait for sending finished
while(ACKSTAT==1);//wait for the slave acknowledge
SSPBUF=code;    //send the control word
while(STAT_RW==1);//wait for finishing
while(ACKSTAT==1);//wait for the slave acknowledge
RSEN=1;
while(RSEN==1); //send the start bit again
SSPBUF=0X9B;    // address for reading
while(STAT_RW==1);//wait for finishing
while(ACKSTAT==1);//wait for the slave acknowledge
RCEN=1;         //receive enable
while(RCEN==1); //wait for getting all the receive date
jdate=SSPBUF; //
ACKDT=1; // the non-ack bit
ACKEN=1; //  non-ack bit sending enable
while(ACKEN==1);//wair for finish
PEN=1;//send stop bit
while(PEN==1);//wait for finish
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?