case(state) idle_state://stage = 1, begin sda_link <= 1'b1; scl_link <= 1'b1; if(timer_cnt == 26'd49999999) //定时1S开始测量 begin state <= start_state; i2c_sda_reg <= 1'b1; //sda置1 ,为start_state做准备 end else state <= idle_state; end start_state://stage = 2, begin if(`SCL_HIG) begin i2c_sda_reg <= 1'b0; //ic2 data pull down.mean begin to transfer data state <= address_write_state; byte_to_send <= 8'b10000000; bit_counter <= 'd0; end else state = start_state; end address_write_state://stage = 3, begin if(`SCL_LOW) begin if(bit_counter == 8) //send_8-bits data finish begin bit_counter <= 'd0; sda_link <= 1'b0; state <= wait_ack1_state; i2c_sda_reg <= 1'b1;//8位数据传输结束 end else begin bit_counter <= bit_counter + 1'b1; i2c_sda_reg <= byte_to_send[7-bit_counter]; state <= address_write_state; end end else state <= address_write_state; end wait_ack1_state://stage=4 begin if(`SCL_HIG) begin if(!i2c_sda) //receive ack signal begin state <= command_send_state; byte_to_send = 8'b11100011; sda_link <= 1'b1; end else //if no receice ack signal , means communication is wrong , back to idle state begin state <= idle_state; end end else state <= wait_ack1_state; end command_send_state://stage = 5, begin if(`SCL_LOW) begin if(bit_counter == 8) //send_8-bits data finish begin bit_counter <= 'd0; state <= wait_ack2_state; sda_link <= 1'b0; i2c_sda_reg <= 1'b1;//8位数据传输结束 end else begin bit_counter <= bit_counter + 1'b1; i2c_sda_reg <= byte_to_send[7-bit_counter]; state <= command_send_state; end end else state <= command_send_state; end wait_ack2_state://stage=6 begin if(`SCL_HIG) begin if(!i2c_sda) //receive ack signal begin state <= address_read_state; byte_to_send <= 8'b10000001; sda_link <= 1'b1; end else //if no receice ack signal , means communication is wrong , back to idle state begin state <= idle_state; end end else state <= wait_ack2_state; end
address_read_state://stage = 7, begin if(`SCL_LOW) begin if(bit_counter == 8) //send_8-bits data finish begin bit_counter <= 'd0; state <= wait_ack3_state; sda_link <= 1'b0; i2c_sda_reg <= 1'b1;//8位数据传输结束 end else begin bit_counter <= bit_counter + 1'b1; i2c_sda_reg <= byte_to_send[7-bit_counter]; state <= address_read_state; end end else state <= address_read_state; end wait_ack3_state://stage=8 begin if(`SCL_HIG) begin // if(!i2c_sda) //receive ack signal // begin state <= hold_state; scl_link <= 1'b0; /* end else //if no receice ack signal , back to idle state begin state <= idle_state; end */ end else state <= wait_ack3_state; end hold_state:// stage=9 begin if(`SCL_LOW) begin if(i2c_scl) //receive measurement finish signal begin state <= data_msb_state; scl_link <= 1'b1; end else state <= hold_state; end else state <= hold_state; end data_msb_state://stage = 10, begin if(`SCL_HIG) begin bit_counter <= bit_counter + 1'b1; t_msb_reg <= {t_msb_reg[6:0],i2c_sda}; state <= data_msb_state; end else if((bit_counter == 'd8)&&(`SCL_NEG)) //read_8-bits data begin bit_counter <= 'd0; state <= ack_state; sda_link <= 1'b1; end else state <= data_msb_state; end ack_state://stage = 11, begin if(`SCL_LOW) i2c_sda_reg <= 1'b0; else if(`SCL_NEG) begin state <= data_lsb_state; sda_link <= 1'b0; end else state <= ack_state; end data_lsb_state://stage = 12 begin if(`SCL_HIG) begin bit_counter <= bit_counter + 1'b1; t_lsb_reg <= {t_lsb_reg[6:0],i2c_sda}; state <= data_lsb_state; end else if((bit_counter == 'd8)&&(`SCL_NEG)) //read_8-bits data begin bit_counter <= 'd0; state <= nack_state; sda_link <= 1'b1; end else state <= data_lsb_state; end nack_state://stage = 13 begin if(`SCL_LOW) i2c_sda_reg <= 1'b1; else if(`SCL_NEG) begin state <= stop_state; i2c_sda_reg <= 1'b0; end else state <= nack_state; end stop_state://stage = 14 begin if(`SCL_HIG) begin i2c_sda_reg <= 1'b1; state <= idle_state; readfinish_reg <= ~readfinish_reg;//////////////////////////////////////////////////////////////////////////for test end else state <= stop_state; end default: state <= idle_state; endcase end endmodule