//同步输入数据信号
always @(posedge clock)
begin
sdat_in_r <= sdat_in;
end
//**********************************************************
//时钟分频计数器
always @(posedge clock)
begin
if (reset == 1'b1)
clk_count <= 5'd0;
else
begin
if (clk_count < CLK_DIV_VALUE)
begin
clk_count <= clk_count + 1'b1;
div_clk <= 1'b0;
end
else
begin
clk_count <= 5'd0;
div_clk <= 1'b1;
end
end
end
//**********************************************************
//状态机ADC
always @(posedge clock)
begin
if (reset == 1'b1)
adc_state <= idle;
else
adc_state <= adc_next_state;
end
//ADC状态机转换逻辑
always @(adc_state or ready_done or rec_done or conv_done or enable)
begin
cs_n_r <= 1'b0;
bit_count_rst <= 1'b0;
data_ready_r <= 1'b0;
case (adc_state)
idle: //初始状态
begin
cs_n_r <= 1'b1;
bit_count_rst <= 1'b1; //复位移位计数器
if (enable == 1'b1)
adc_next_state <= adc_ready;
else
adc_next_state <= idle;
end
adc_ready: //准备接收
begin
if(ready_done == 1'b1)
adc_next_state <= adc_receive;
else
adc_next_state <= adc_ready;
end
adc_receive: //接收数据
begin
if(rec_done == 1'b1)
adc_next_state <= adc_conversion;
else
adc_next_state <= adc_receive;
end
adc_conversion: //转换前的采样的数据
begin
cs_n_r <= 1'b1;
if(conv_done == 1'b1)
adc_next_state <= adc_data_load;
else
adc_next_state <= adc_conversion;
end
adc_data_load:
begin
data_ready_r <= 1'b1; //数据输出标志
adc_next_state <= idle;
end
default : adc_next_state <= idle;
endcase
end
//**********************************************************
//位移位计数器
always @(posedge clock)
begin
if (reset == 1'b1)
bit_count <= 6'd0;
else if (bit_count_rst == 1'b1)
bit_count <= 6'd0;
else if (div_clk == 1'b1)
bit_count <= bit_count + 1'b1;
end