1: /* 2: * SPI interface whith the ST M95160 EEPROM 3: */ 4: 5: 6: #include "pic1687x.h" 7: static bit CHIP_SELECT @ ((unsigned)&PORTB*8+5); // CHIP SELECT 8: int 9: spi_read(long int addres) 10: { 11: int data; 12: SSPBUF = 0x03; 13: CHIP_SELECT = 0; 14: do{} 15: while(!STAT_BF); 16: SSPBUF = 0x0; 17: do{} 18: while(!STAT_BF); 19: SSPBUF = addres; 20: do{} 21: while(!STAT_BF); 22: CKP = 0; 23: SSPBUF = 0; 24: do{} 25: while(!STAT_BF); 26: data = SSPBUF; 27: CKP = 1; 28: CHIP_SELECT = 1; 29: return(data); 30: } 31: 32: 33: void 34: spi_write(unsigned char addres, unsigned char data) 35: { 36: /* instruction */ 37: SSPBUF = 0x02; 38: CHIP_SELECT = 0; 39: do{} 40: while(!STAT_BF); 41: 42: /* 16 bit addres */ 43: SSPBUF = 0x00; 44: do{} 45: while(!STAT_BF); 46: 47: SSPBUF = addres; 48: do{} 49: while(!STAT_BF); 50: 51: /* 8 bit data */ 52: SSPBUF = data; 53: do{} 54: while(!STAT_BF); 55: CHIP_SELECT = 1; 56: } 57: 58: 59: void 60: spi_RDSR(unsigned unsigned char data) 61: { 62: SSPBUF = 0x05; 63: CHIP_SELECT = 0; 64: do{} 65: while(!STAT_BF); 66: CKP = 0; 67: SSPBUF = 0; 68: do{} 69: while(!STAT_BF); 70: data = SSPBUF; 71: CKP = 1; 72: CHIP_SELECT = 1; 73: 74: } 75: 76: 77: void 78: spi_WREN() 79: { 80: SSPBUF = 0x06; 81: CHIP_SELECT = 0; 82: do{} 83: while(!STAT_BF); 84: CHIP_SELECT = 1; 85: }; 86: 87: 88: void 89: spi_WRDI() 90: { 91: SSPBUF = 0x04; 92: CHIP_SELECT = 0; 93: do{} 94: while(!STAT_BF); 95: CHIP_SELECT = 1; 96: }; 97: 98: 99: 100: void InitSPI(void) 101: { 102: // SSPSTAT 103: STAT_SMP = 0; // INPUT DATA SAMPLED AT MIDDLE OF DATA 104: STAT_CKE = 0; // DATA TRANSMITTED ON RISING EDGE 105: STAT_BF = 0; // BF READY? 106: // SSPCON 107: SSPEN = 1; 108: CKP = 1; 109: };