From 29771df0c9fc3612ce02a1d51a37c94f16e7dda8 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Wed, 7 Aug 2019 22:27:48 -0500 Subject: Adjust formatting --- i2c.c | 81 +++++++++++++++++++++++++++++++------------------------------------ 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'i2c.c') diff --git a/i2c.c b/i2c.c index 04dcda1..51a93a1 100755 --- a/i2c.c +++ b/i2c.c @@ -7,60 +7,55 @@ 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 - SSPADD = 49; - - //SSPSTAT: SMP:CKE:D/!A:P:S:R/!W/:U:BF *U unimplemented read as 0 - SMP = 1; // Disable slew rate - CKP = 0; // Data transmitted on rising edge of SCK + 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 + SSPADD = 49; + + //SSPSTAT: SMP:CKE:D/!A:P:S:R/!W/:U:BF *U unimplemented read as 0 + SMP = 1; // Disable slew rate + CKP = 0; // Data transmitted on rising edge of SCK } -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_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_Start() { + I2C_Master_Wait(); //Hold the program if I2C is busy + SEN = 1; //Begin IIC pg85/234 } -void I2C_Master_Repeated_Start() -{ +void I2C_Master_Repeated_Start() { I2C_Master_Wait(); - RSEN = 1; //Initiate repeated start condition + 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_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 +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; +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 -- cgit v1.2.3-54-g00ecf