tumkayaonur
Junior Member level 2
I wrote a verilog code for I2C and tested it. It works properly in simulation results but i can't get any data from slave device's register.
Can anyone help me ?
Can anyone help me ?
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 module i2cio2( input wire clk, input wire reset, output reg sda, output wire scl, output reg [7:0] data, output reg [7:0] state ); localparam STATE_IDLE =0; localparam STATE_START =1; localparam STATE_ADDR =2; localparam STATE_RW =3; localparam STATE_WACK =4; localparam STATE_DATA =5; localparam STATE_WACK2 =6; localparam STATE_STOP =7; reg [6:0] addr; reg [7:0] count; reg ack1; reg ack2; reg scl_enable; reg rw; initial scl_enable<=0; i2c_clk_divider instance_name ( .clk(clk), .reset(reset), .i2c_clk(i2c_clk) ); assign scl=(scl_enable==0) ? 1:~i2c_clk; always @ (posedge i2c_clk)begin if(reset==1)begin scl_enable<=0; sda<=1; state<=STATE_IDLE; addr<=7'h0x96; rw<=1; count<=8'd0; data<=8'b01010101; ack1<=0; ack2<=0; end else begin case(state) STATE_IDLE: begin //IDDLE sda<=1; state<=STATE_START; scl_enable<=0; end STATE_START: begin //START sda<=0; state<=STATE_ADDR; scl_enable<=0; count<=6; end STATE_ADDR: begin //MSB ADDRESS BIT sda<=addr[count]; scl_enable<=1; if(count==0) state<=STATE_RW; else count<=count-1; end STATE_RW: begin //BIT 5 sda<=1; count<=7; state<=STATE_WACK; scl_enable<=1; end STATE_WACK: begin //BIT 4 sda<=0; state<=STATE_DATA; scl_enable<=1; end STATE_DATA: begin scl_enable<=1; sda<=data[count]; if(count==0) state<=STATE_WACK2; else count<=count-1; end STATE_WACK2: begin sda<=0; scl_enable<=1; state<=STATE_STOP; end STATE_STOP: begin sda<=1; scl_enable<=0; state<=STATE_IDLE; end endcase end//end else end // end always endmodule