PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 22, 2007 2:09 am |
|
|
You could write a substitute routine for the bad library code. Only the
i2c_write() function has a problem and only in the 2nd SSP module.
One easy way to do this is to look at the ASM code in the .LST file
and write C code that does the same thing. I've done this below.
Here's the corrected ASM code for the i2c_write() function for the 2nd
i2c port on the 18F8722. I've fixed the register address and bit position
for the SSP2IF bit, and I've added comments:
Code: | /
/*
... #use i2c(master, sda=PIN_D5, SCL=PIN_D6, FORCE_HW)
00004: BCF F63.7 // Set WCOL2 = 0
00006: BCF FA4.7 // Set SSP2IF = 0
00008: MOVFF 07,F66 // Put byte into SSP2BUF
0000C: MOVLW 02
0000E: BTFSC F63.7 // Test WCOL2
00010: BRA 001C // Return 2 if WCOL2 = 1.
00012: BTFSS FA4.7 // Test SSP2IF
00014: BRA 0012 // Wait in loop if SSP2IF = 0
00016: MOVLW 00
00018: BTFSC F62.6 // Test ACKSTAT
0001A: MOVLW 01 // Return state of ACKSTAT
0001C: MOVWF 01
*/
|
Here's a functional translation of the above ASM code to C. It doesn't
compile to exactly the same ASM code, but it performs the same functions.
Code: |
// Define registers and bits used by the routine.
#byte SSP2CON1 = 0xF63
#bit WCOL2 = SSP2CON1.7
#byte PIR3 = 0xFA4
#bit SSP2IF = PIR3.7
#byte SSP2CON2 = 0xF62
#bit ACKSTAT2 = SSP2CON2.6
#byte SSP2BUF = 0xF66
// Here is the new routine. Call it instead of i2c_write()
// in the CCS library code for the 2nd SSP module.
int8 i2c_write_ssp2(int8 value)
{
int8 retval;
WCOL2 = 0;
SSP2IF = 0;
SSP2BUF = value;
if(WCOL2)
{
retval = 2;
}
else
{
while(!SSP2IF);
retval = ACKSTAT2;
}
return(retval);
} |
|
|