1: //Project : I2CSubroutines.c 2: //Date : 5/23/2005 3: //Author : Curtis Rhodes w/ help from Dr. Song's example 4: //Purpose : I2C Memory Chip Interface Subroutines 5: // Synchronous Serial Port 6: // RC3 (pin 18) == SCL (Clock for Memory Chip) 7: // RC4 (pin 23) == SDA (Serial Data for Memory Chip) 8: // 9: //Chip type : PIC16F877A 10: //Clock frequency : 20 MHz 11: 12: #include 13: #include "memory_to_DAC.h" 14: 15: //====================================================================================================== 16: // initializeI2C() 17: //====================================================================================================== 18: //Initializes the I2C interface 19: void initializeI2C(){ 20: //Initialize I2C 21: SSPCON = 0b00111000; 22: SSPADD = 4; 23: ACKEN = 0; 24: ACKDT = 1; 25: SSPIF = 0; 26: } 27: 28: //====================================================================================================== 29: // send_Start_Bit() 30: //====================================================================================================== 31: //Sends a start bit through the I2C communication line 32: void send_Start_Bit(){ 33: // I2C Start 34: SEN = 1; 35: while(SSPIF == 0); 36: SSPIF = 0; 37: } 38: 39: //====================================================================================================== 40: // send_Stop_Bit() 41: //====================================================================================================== 42: //Sends a stop bit through the I2C communication line 43: void send_Stop_Bit(){ 44: //I2C STOP 45: PEN = 1; 46: while(SSPIF == 0); 47: SSPIF = 0; 48: } 49: 50: //====================================================================================================== 51: // acknowledge() 52: //====================================================================================================== 53: //Sends a stop bit through the I2C communication line 54: void acknowledge(){ 55: //I2C STOP 56: ACKDT = 0; 57: ACKEN = 1; 58: while(ACKEN == 1) {} 59: while(SSPIF == 0); 60: SSPIF = 0; 61: } 62: 63: //====================================================================================================== 64: // write_Data() 65: //====================================================================================================== 66: //Sends a byte through the I2C communication line 67: void write_Data(unsigned int write_data){ 68: SSPBUF = write_data; 69: while(ACKSTAT == 1) {} 70: while(SSPIF == 0) {} 71: SSPIF = 0; 72: } 73: 74: //====================================================================================================== 75: // receive_Data() 76: //====================================================================================================== 77: //Waits for a byte sent to the PIC through the I2C communication line 78: void receive_Data(){ 79: RCEN = 1; 80: while(SSPIF == 0) {} 81: SSPIF == 0; 82: //RCEN = 0; 83: } 84: 85: //====================================================================================================== 86: // wait_For_Memory_Write_Completion() 87: //====================================================================================================== 88: //Waits for the memory to complete its current write cycle 89: void wait_For_Memory_Write_Completion(int control_byte){ 90: // Wait Until Device is Ready 91: do{ 92: send_Start_Bit(); 93: write_Data(control_byte); 94: }while(ACKSTAT == 1); 95: send_Stop_Bit(); 96: } 97: 98: //====================================================================================================== 99: // memory_chip_write() 100: //====================================================================================================== 101: //Places desired data into passed memory chip / address 102: void memory_chip_write(unsigned char memory_chip, unsigned char address_high, unsigned char address_low, unsigned char data){ 103: send_Start_Bit(); 104: write_Data(memory_chip); 105: write_Data(address_high); 106: write_Data(address_low); 107: write_Data(data); 108: send_Stop_Bit(); 109: //wait_For_Memory_Write_Completion(memory_chip); 110: } 111: 112: //====================================================================================================== 113: // memory_chip_write() 114: //====================================================================================================== 115: //Places desired data into passed memory chip / address 116: unsigned char memory_chip_read(unsigned char memory_chip, unsigned char address_high, unsigned char address_low){ 117: unsigned char temp; 118: send_Start_Bit(); 119: write_Data((memory_chip & 0b11111110)); 120: write_Data(address_high); 121: write_Data(address_low); 122: send_Stop_Bit(); 123: send_Start_Bit(); 124: write_Data(memory_chip); 125: receive_Data(); 126: temp = SSPBUF; 127: send_Stop_Bit(); 128: return temp; 129: } 130: 131: //====================================================================================================== 132: // start_sequential_read() 133: //====================================================================================================== 134: //Places desired data into passed memory chip / address 135: void start_sequential_read(unsigned char memory_chip, unsigned char address_high, unsigned char address_low){ 136: send_Start_Bit(); 137: write_Data((memory_chip & 0b11111110)); 138: write_Data(address_high); 139: write_Data(address_low); 140: send_Stop_Bit(); 141: //READ FROM MEMORY 142: send_Start_Bit(); 143: write_Data(memory_chip); 144: } 145: 146: //====================================================================================================== 147: // sequential_memory_chip_read() 148: //====================================================================================================== 149: //Places desired data into passed memory chip / address 150: unsigned char sequential_memory_chip_read(){ 151: unsigned char temp; 152: receive_Data(); 153: temp = SSPBUF; 154: return temp; 155: } 156: 157: //====================================================================================================== 158: // end_sequential_read() 159: //====================================================================================================== 160: //Places desired data into passed memory chip / address 161: void end_sequential_read(){ 162: send_Stop_Bit(); 163: }