?? i2c.c.bak
字號:
#include <pic.h>
#include "..\inc\include.h"
#include "..\lib\I2C.h"
#define LED RB0
#define LED1 RB0
#define LED2 RB1
#define LED3 RB2
#define SCL RC3
#define SDA RC4
void I2C_Init(void)
{
TRISC3 =0; //SCL output
TRISC4 =0; //SDA output
SCL = 1;
SDA = 1;
SSPCON &= ~(0x0F);
SSPCON |= 0x08; //I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
/*
SSPM3 =1; //master mode
SSPM2 =0;
SSPM1 =0;
SSPM0 =0;*/
SMP = 1; //100KHz
CKE = 0; //not smbus
SSPADD = 0x13; //100KHz
SSPEN = 1; //enable I2C
}
// ========= read 1 byte data from 24LC256 (15 bit word address) ==============
unsigned char EEPROM_ReadByte(unsigned int Addr, unsigned char *DataBuf)
{
unsigned char cnt=0;
while((SSPCON2 & 0x1F) || RW ){ //if not in idle mode, reinitial the serial bus
SSPEN = 0;
NOP();
SSPEN = 1;
}
SEN = 1; //send the start condition
while(SEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
while(ACKSTAT){ //if return no ACK, resend the start bit and control byte
RSEN = 1; //send the restart condition
while(RSEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
if(cnt++>200) return NACK; //repeat 200 times
}
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = (unsigned char)(Addr>>8); //send high address
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = (unsigned char)Addr; //send low address
while(!SSPIF);
if(ACKSTAT) return NACK;
RSEN = 1; //send the restart condition
while(RSEN);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = 0b10100001; //send control byte
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
RCEN = 1; //enable master for 1 byte reception???
//while(RCEN); //Automatically cleared by hardware
while(!SSPIF);
//if(!RCEN) LED1 = 1;
ACKDT = 1;
ACKEN = 1; //send NACK
while (ACKEN ==1);
PEN = 1; //send the stop condition
while(PEN);
*DataBuf = SSPBUF; //return byte data wanted to read
return NOERR;
}
// ========= write 1 byte data to 24LC256 (15 bit word address) ===============
unsigned char EEPROM_WriteByte(unsigned int Addr, unsigned char cData)
{
unsigned char cnt=0;
while((SSPCON2 & 0x1F) || RW ){ //if not in idle mode, reinitial the serial bus
SSPEN = 0;
NOP();
SSPEN = 1;
}
SEN = 1; //send the start condition
while(SEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
while(ACKSTAT){ //if return no ACK, resend the start bit and control byte
RSEN = 1; //send the restart condition
while(RSEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
if(cnt++>200) return NACK; //repeat 200 times
}
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = (unsigned char)(Addr>>8); //send high address
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = (unsigned char)Addr; //send low address
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = cData; //send data that want to be write
while(!SSPIF);
PEN = 1; //send the stop condition
while(PEN);
return NOERR;
}
// ========= read 1 byte data from 24LC01/02 (7/8 bit word address) ==============
unsigned char EEPROM_ReadByte1(unsigned char Addr, unsigned char *DataBuf)
{
unsigned char cnt=0;
while((SSPCON2 & 0x1F) || RW ){ //if not in idle mode, reinitial the serial bus
SSPEN = 0;
NOP();
SCL = 1;
SDA = 1;
SSPEN = 1;
}
SEN = 1; //send the start condition
while(SEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte, write
while(!SSPIF);
while(ACKSTAT){ //if return no ACK, resend the start bit and control byte
RSEN = 1; //send the restart condition
while(RSEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
NOP();
NOP();
NOP();
if(cnt++>200) return NACK; //repeat 200 times
}
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = Addr; //send word address
while(!SSPIF);
if(ACKSTAT) return NACK;
RSEN = 1; //send the restart condition
while(RSEN);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = 0b10100001; //send control byte, read
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
RCEN = 1; //enable master for 1 byte reception???
//while(RCEN); //Automatically cleared by hardware
while(!SSPIF);
//if(!RCEN) LED1 = 1;
ACKDT = 1;
ACKEN = 1; //send NACK
while (ACKEN ==1);
PEN = 1; //send the stop condition
while(PEN);
*DataBuf = SSPBUF; //return byte data wanted to read
return NOERR;
}
// ========= write 1 byte data to 24LC01/02 (7/8 bit word address) ===============
unsigned char EEPROM_WriteByte1(unsigned char Addr, unsigned char cData)
{
unsigned char cnt=0;
while((SSPCON2 & 0x1F) || RW ){ //if not in idle mode, reinitial the serial bus
SSPEN = 0;
NOP();
SSPEN = 1;
}
SEN = 1; //send the start condition
while(SEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
while(ACKSTAT){ //if return no ACK, resend the start bit and control byte
RSEN = 1; //send the restart condition
while(RSEN);
SSPIF = 0;
SSPBUF = 0b10100000; //send control byte
while(!SSPIF);
if(cnt++>200) return NACK; //repeat 200 times
}
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = Addr; //send word address
while(!SSPIF);
if(ACKSTAT) return NACK;
SSPIF = 0;
SSPBUF = cData; //send data that want to be write
while(!SSPIF);
PEN = 1; //send the stop condition
while(PEN);
return NOERR;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -