#include <stdarg.h>
#include <stdio.h>
+#ifdef __SDCC
+#include <delay.h>
#include <pic18fregs.h>
+#endif
#include "i2c_io.h"
{
// unsigned char b;
// b = _SSPEN | _SSPM3;
- SSPCON1bits.SSPEN = 1;
- SSPCON1bits.SSPM3 = 1;
- SSPADD = 4; // Fosc:8000[KHz] / (4 * I2C:Clock:400[KHz]) - 1
- SSPSTAT = 0x80;
+ // SSPCON1bits.SSPEN = 1;
+// SSPCON1bits.SSPM3 = 1;
+// SSPADD = 4; // Fosc:8000[KHz] / (4 * I2C:Clock:400[KHz]) - 1
+// SSPSTAT = 0x80;
PIR1bits.SSPIF = 0;
PIE1bits.SSPIE = 0;
IPR1bits.SSPIP = 1;
{
}
#endif
+// I2C_IO
+void i2c_send_byte(unsigned char addr, unsigned char reg, unsigned char data)
+{
+#ifdef __SDCC
+ i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
+ I2C_START();
+ i2c_writechar(addr);
+ i2c_writechar(reg);
+ i2c_writechar(data);
+ I2C_STOP();
+ i2c_close();
+// delay100tcy(2);
+#else
+ OpenI2C(MASTER, SLEW_ON);
+ SSPADD = 0x5;
+// IdleI2C();
+ StartI2C();
+ while (SSPCON2bits.SEN);
+ WriteI2C(addr);
+ while(SSPCON2bits.SEN);
+ WriteI2C(reg);
+ while(SSPCON2bits.SEN);
+ WriteI2C(data);
+ StopI2C();
+ while (SSPCON2bits.PEN);
+ CloseI2C();
+#endif // i2c_idle();
+}
+
+unsigned char i2c_read_byte(unsigned char addr, unsigned char reg)
+{
+ unsigned char c;
+ #ifdef __SDCC
+ i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
+ I2C_START();
+ i2c_writechar(addr);
+ i2c_writechar(reg);
+ I2C_STOP();
+ i2c_idle();
+// delay100tcy(2);
+ I2C_START();
+ i2c_writechar(addr | 1);
+ c = i2c_readchar();
+ I2C_ACK();
+ I2C_STOP();
+ i2c_close();
+#else
+ OpenI2C(MASTER, SLEW_ON);
+ SSPADD = 0x5;
+ StartI2C();
+ while(SSPCON2bits.SEN);
+ WriteI2C(addr);
+ // delay1ktcy(8);
+ while(SSPCON2bits.SEN);
+ WriteI2C(reg);
+ // delay1ktcy(8);
+ StopI2C();
+ IdleI2C();
+ StartI2C();
+ WriteI2C(addr | 1);
+ if (!SSPCON2bits.ACKSTAT){
+ SSPCON2bits.RCEN = 1;
+ while(SSPCON2bits.RCEN);
+ NotAckI2C();
+ while (SSPCON2bits.ACKEN);
+ StopI2C();
+ while (SSPCON2bits.PEN);
+ }
+ c = SSPBUF;
+ // delay1ktcy(8);
+ // delay1ktcy(8);
+ CloseI2C();
+#endif
+ // CLOSEASMASTER();
+ return c;
+
+}
+
#endif
\ No newline at end of file