aboutsummaryrefslogtreecommitdiffstats
path: root/i2c.c
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2019-07-27 22:16:27 -0500
committerWilliam Harrington <kb0iic@berzerkula.org>2019-07-27 22:16:27 -0500
commite6bffe23c87a9f6de8abdec747600f674b9cab62 (patch)
tree1da4dc68a918f969f3354f1d70c2aa58b588fabd /i2c.c
parent1dd364ccc6fb4447d89cbc965655b895def8e97b (diff)
Copy project files into repo
Diffstat (limited to 'i2c.c')
-rwxr-xr-xi2c.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/i2c.c b/i2c.c
new file mode 100755
index 0000000..5b3b707
--- /dev/null
+++ b/i2c.c
@@ -0,0 +1,62 @@
+
+//PIN 18 -> RC3 -> SCL
+//PIN 23 -> RC4 ->SDA
+
+#include "i2c.h"
+#include "conf.h"
+
+void I2C_Master_Init(const unsigned long freq_K) //Begin IIC as master
+{
+ TRISC3 = 1; TRISC4 = 1; //Set SDA and SCL pins as input pins
+
+ SSPCON = 0b00101000; //pg84/234
+ SSPCON2 = 0b00000000; //pg85/234
+
+ SSPADD = (_XTAL_FREQ/(4*freq_K*100))-1; //Setting Clock Speed pg99/234
+ SSPSTAT = 0b00000000; //pg83/234
+}
+
+void I2C_Master_Wait()
+{
+ while ( (SSPCON2 & 0b00011111) || (SSPSTAT & 0b00000100) ) ; //check the bis on registers to make sure the IIC is not in progress
+}
+
+void I2C_Master_Start()
+{
+ I2C_Master_Wait(); //Hold the program if I2C is busy
+ SEN = 1; //Begin IIC pg85/234
+}
+
+void I2C_Master_Repeated_Start()
+{
+ I2C_Master_Wait();
+ RSEN = 1; //Initiate repeated start condition
+}
+
+void I2C_Master_Stop()
+{
+ I2C_Master_Wait(); //Hold the program is I2C is busy
+ PEN = 1; //End IIC pg85/234
+}
+
+void I2C_Master_Write(unsigned data)
+{
+ I2C_Master_Wait(); //Hold the program is I2C is busy
+ SSPBUF = data; //pg82/234
+}
+
+unsigned short I2C_Master_Read(unsigned short ack)
+{
+ unsigned short incoming;
+ I2C_Master_Wait();
+ RCEN = 1;
+
+ I2C_Master_Wait();
+ incoming = SSPBUF; //get the data saved in SSPBUF
+
+ I2C_Master_Wait();
+ ACKDT = (ack)?0:1; //check if ack bit received
+ ACKEN = 1; //pg 85/234
+
+ return incoming;
+} \ No newline at end of file