#include #include #include #include "i2cmsu.h" #define DATA_EEMEM_PROTECT_DISABLE 0x0100 #define XT_OSC 0x0001 #define DISABLE_DEBUG 0x0800 #define CP0 0x1010 #define CP1 0x2020 /* CP0,CP1 code protect off*/ /* other bits zero which means WDT disabled Low Voltage prm disabled, brownout disabled power up timer enabled */ __CONFIG((CP1 | CP0) | DATA_EEMEM_PROTECT_DISABLE | XT_OSC | DISABLE_DEBUG); #define bitset(var,bitno) ((var) |= (1 << (bitno))) #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) #define bittst(var,bitno) (var & (1 << (bitno))) // Either dump previously written text from ROM to interface, or capture bytes // from serial port and dump to memory. // do the writes one block at a time // capture to 64 byte buffer before writing char buf[64]; #define ROM 0xA0 /* I2C EEPROM */ bank1 unsigned char received_char; bank1 unsigned char got_char_flag; /* return 8 bit char from Recieve port */ /* interrupt driven version */ unsigned char getch () { /* while (!RCIF) */ /* while (!bittst(PIR1,5));*/ /* check RCIF bit */ while (!got_char_flag); got_char_flag = 0; return(received_char); } /* send 8 bit char to Recieve port */ void putch (c) char c; { /* wait until transmit reg empty */ /* while (!TXIF) */ while (!bittst(PIR1,4)); /* check RCIF bit */ TXREG = c; } void pcrlf () { putch(0x0a); putch(0x0d); } /* interrupt service routine */ void interrupt pic_isr(void) { /* see if this interrupt was generated by a receive character */ if (bittst(PIR1,5)) { /* check RCIF bit */ received_char = RCREG; /* reading this register clears interrupt bit */ got_char_flag = 1; } } // i2c clock freq = Fosc/4(SSPAD+1) // 100000(SSPPAD+1) = 3686400/4 // SSPAD = 8.2, use 8, will be close to 100 Khz bank1 char i; bank1 char inchar; main(void) { /* setup Async communication */ bitclr(TXSTA, 6); /* 8-bit transmit */ bitset(TXSTA, 5); /* transmit enable*/ bitclr(TXSTA, 4); /* async mode */ bitset(TXSTA, 2); /* high speed mode */ SPBRG = 95; /* 9600 in high baud rate mode, crystal = 14.7456 Mhz */ bitset(TRISC, 7); /* serial port enable */ bitset(TRISC, 6); /* serial port enable */ bitset(RCSTA, 7); /* serial port enable */ bitclr(RCSTA, 6); /* 8-bit reception */ bitset(RCSTA, 4); /* enable recieve */ /* enable I2C */ SSPCON = 0x28; //Master mode // SSPADD = 8; // close to 100 Khz rate on i2c bus for 3.6864 Mhz //SSPADD = 16; // try 50 Khz, verified to work //SSPADD = 1; // over 400Khz for 3.6864 Mhz, verified to work SSPADD = 8; // about 400Khz for 14.7456 Mhz, verified to work // slew control does not seem to matter much //bitset(SSPSTAT,7); //disable slew rate control for 100 Khz bitset(SSPCON,5); //SSPEN = 1; bitset(SSPCON,4); //CKP = 1; bitset(TRISC,3); bitset(TRISC,4); // SDA, SCL pins are inputs /* enable interrupts */ bitset(PIE1, 5); /* enable receive interrupt, RCIE */ bitset(INTCON,6); /* enable peripheral interrupts, PEIE */ bitset(INTCON,7); /* enable global interrupts, GIE */ printf("Enter 'w'(write), or 'r' (read): "); pcrlf (); inchar=getch(); if (inchar == 'w') { pcrlf (); printf("Capturing next 64 bytes for write to memory"); pcrlf (); for (i=0;i<64;i++) { buf[i] = getch(); } printf("Block write to memory"); pcrlf (); for (i=0;i<64;i++) { printf("Write at %d, val: %x (%c) \n",i,buf[i], buf[i]); pcrlf (); } block_mem_write(ROM,0,buf); printf("Mem Write complete"); pcrlf (); } else if (inchar == 'r') { printf("###Reading first 64 memory locations, via a block read"); pcrlf (); block_mem_read(ROM,0,buf); for (i=0;i<64;i++) { printf("Read %d, val: %x (%c) \n",i,buf[i], buf[i]); pcrlf (); } pcrlf (); printf("###Mem Read complete"); pcrlf (); } while(1); }