`timescale 1ns / 1ps module i2c ( input STATUSin, input config_rst, input clk_i2c, input RESET, inout SDA, output SCL, output reg LED, output reg DONE ); wire [3 : 0] i2c_reset; wire SDA_W; reg [2 : 0] Bit_Counter; reg [4 : 0] Byte_Counter; reg IO; reg STARTCLK; reg Clk_Enable; wire STARTI2C; wire ACK; FDS flop_reg000 (.D(1'b0), .C(clk_i2c), .Q(i2c_reset[0]), .S(1'b0)); FD flop_reg001 (.D(i2c_reset[0]), .C(clk_i2c), .Q(i2c_reset[1])); FD flop_reg010 (.D(i2c_reset[1]), .C(clk_i2c), .Q(i2c_reset[2])); FD flop_reg011 (.D(i2c_reset[2]), .C(clk_i2c), .Q(i2c_reset[3])); SRL16E #( /////START REGISTER .INIT(16'hFFFF) // Initial Value of Shift Register ) SRL16E_inst9 ( .Q(SDA_W), // SRL data output .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_8) // SRL data input ); SRLC16E #( .INIT(16'h5600) // Initial Value of Shift Register ) SRLC16E_inst8 ( .Q(), // SRL data output .Q15(Q15_8), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_7) // SRL data input ); SRLC16E #( .INIT(16'h0538) // Initial Value of Shift Register ) SRLC16E_inst7 ( .Q(), // SRL data output .Q15(Q15_7), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_6) // SRL data input ); SRLC16E #( .INIT(16'h0000) // Initial Value of Shift Register //Test Data ) SRLC16E_inst6 ( .Q(), // SRL data output .Q15(Q15_6), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_5) // SRL data input ); SRLC16E #( .INIT(16'h0000) // Initial Value of Shift Register ) SRLC16E_inst5 ( .Q(), // SRL data output .Q15(Q15_5), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable &&!(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_4) // SRL data input ); SRLC16E #( .INIT(16'h0000) // Initial Value of Shift Register ) SRLC16E_inst4 ( .Q(), // SRL data output .Q15(Q15_4), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input .CLK(clk_i2c), // Clock input .D(Q15_3) // SRL data input ); SRLC16E #( /////STOP REGISTER .INIT(16'h00CB) // Initial Value of Shift Register ) SRLC16E_inst3 ( .Q(), // SRL data output .Q15(Q15_3), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input //2MORE SRLS .CLK(clk_i2c), // Clock input .D(Q15_2) // SRL data input ); SRLC16E #( .INIT(16'h8A09) // Initial Value of Shift Register ) SRLC16E_inst2 ( .Q(), // SRL data output .Q15(Q15_2), // Carry output (connect to next SRL) .A0(1'b1), // Select[0] input .A1(1'b1), // Select[1] input .A2(1'b1), // Select[2] input .A3(1'b1), // Select[3] input .CE(Clk_Enable && !(Bit_Counter == 7)), // Clock enable input //2MORE SRLS .CLK(clk_i2c), // Clock input .D(Q15_1) // SRL data input ); SRLC16E #( .INIT(16'h2A00) // Initial Value of Shift Register ) SRLC16E_inst1 ( .Q(), .Q15(Q15_1), .A0(1'b1), .A1(1'b1), .A2(1'b1), .A3(1'b1), .CE(Clk_Enable && !(Bit_Counter == 7)), .CLK(clk_i2c), .D(1'b0) ); reg [4 : 0] counter; reg SCLreg; always @ (posedge clk_i2c) begin if(RESET || (|i2c_reset)) begin counter <= 0; SCLreg <= 0; Clk_Enable <= 0; end else if (DONE) begin counter <= 0; SCLreg <= 0; Clk_Enable <= 0; end else begin counter <= counter + 1; SCLreg <= counter[4]; if(counter == 31) //Dividing clock by 32 Clk_Enable <= 1; else Clk_Enable <= 0; end end always @ (posedge clk_i2c) begin if(RESET || (|i2c_reset) || STATUSin || config_rst) begin IO <= 0; Bit_Counter <= 0; Byte_Counter <=0; STARTCLK <=0; DONE <= 0; LED <=1; end else if(Clk_Enable) begin if(IO==0) begin Bit_Counter <= Bit_Counter + 1; //Bit_Counter will remain at zero for two clock cycles. if(Byte_Counter == 17) begin STARTCLK <= 0; DONE <= 1; end if(Bit_Counter == 7) begin IO <= 1; Byte_Counter <= Byte_Counter + 1; end end else //IO==1 begin IO <= 0; if (Byte_Counter == 2) STARTCLK <= 1; end end end assign STARTI2C = (Byte_Counter == 2); assign SDA = IO? (STARTI2C? 1'b0 : 1'bz) : STARTCLK? (SDA_W) : 1'b1 ; assign SCL = STARTCLK? SCLreg : 1'b1; endmodule