/* * SPI interface whith the ST M95160 EEPROM */ #include "pic1687x.h" static bit CHIP_SELECT @ ((unsigned)&PORTB*8+5); // CHIP SELECT int spi_read(long int addres) { int data; SSPBUF = 0x03; CHIP_SELECT = 0; do{} while(!STAT_BF); SSPBUF = 0x0; do{} while(!STAT_BF); SSPBUF = addres; do{} while(!STAT_BF); CKP = 0; SSPBUF = 0; do{} while(!STAT_BF); data = SSPBUF; CKP = 1; CHIP_SELECT = 1; return(data); } void spi_write(unsigned char addres, unsigned char data) { /* instruction */ SSPBUF = 0x02; CHIP_SELECT = 0; do{} while(!STAT_BF); /* 16 bit addres */ SSPBUF = 0x00; do{} while(!STAT_BF); SSPBUF = addres; do{} while(!STAT_BF); /* 8 bit data */ SSPBUF = data; do{} while(!STAT_BF); CHIP_SELECT = 1; } void spi_RDSR(unsigned unsigned char data) { SSPBUF = 0x05; CHIP_SELECT = 0; do{} while(!STAT_BF); CKP = 0; SSPBUF = 0; do{} while(!STAT_BF); data = SSPBUF; CKP = 1; CHIP_SELECT = 1; } void spi_WREN() { SSPBUF = 0x06; CHIP_SELECT = 0; do{} while(!STAT_BF); CHIP_SELECT = 1; }; void spi_WRDI() { SSPBUF = 0x04; CHIP_SELECT = 0; do{} while(!STAT_BF); CHIP_SELECT = 1; }; void InitSPI(void) { // SSPSTAT STAT_SMP = 0; // INPUT DATA SAMPLED AT MIDDLE OF DATA STAT_CKE = 0; // DATA TRANSMITTED ON RISING EDGE STAT_BF = 0; // BF READY? // SSPCON SSPEN = 1; CKP = 1; };