always@(posedge clk or negedge rst_n)
begin
if(!rst_n) clk_div_counter<=0;
else if(clk_div_counter==clk_div_factor) clk_div_counter<=0;
else clk_div_counter<=clk_div_counter+1'b1;
end
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
state<=CH451_Idle;
shift_state<=Shift12_out_idle;
CH451_DIN<=1'b1;
CH451_DCLK<=1'b1;
CH451_LOAD<=1'b1;
transfer_count<=0;
shift_counter<=0;
display_data<=0;
decemial_point_sel<=0;
display_led_sel<=0;
CH451_cmd<=12'h0;
trans_over_flag<=1'b0;
ACK<=1'b0;
end
else
if(clk_div_counter==clk_div_factor)
begin
case(state)
CH451_Idle:begin
CH451_DIN<=1'b0;
state<=CH451_Enable_serial_port;
end
CH451_Enable_serial_port:begin
CH451_DIN<=1'b1;
trans_over_flag<=1'b0;
CH451_cmd<=`CH451_RESET;
state<=CH451_Reset;
end
CH451_Reset:begin
if(trans_over_flag==1'b0)
shift12_out;//调用任务函数
else
begin
trans_over_flag<=1'b0;
CH451_cmd<=`CH451_BCD;
state<=CH451_Set_display_mode;
end
end
CH451_Set_display_mode:begin
if(trans_over_flag==0)
shift12_out;
else
begin
trans_over_flag<=1'b0;
CH451_cmd<=`CH451_SYSON1;
state<=CH451_Open_display;
end
end
CH451_Open_display:begin
if(trans_over_flag==0)
shift12_out;
else begin
trans_over_flag<=1'b0;
state<=CH451_Get_data;
end
end
CH451_Get_data:begin
display_data[5*digit_On_num-1:0]<=data[5*digit_on_num-1:0];
decemial_point_sel<=point_i[7:8-digit_on_num];
display_led_sel<=CH451_DISPLAY[4*digit_on_num-1:0];
state<=CH451_Display_ready;
end
CH451_Display_ready:begin
CH451_cmd<={
display_led_sel[3:0],
decemial_point_sel[0].
2'b00,
display_data[4:0]
};
state<=CH451_Display;
end
CH451_Display:begin
if(trans_over_flag==0)
shift12_out;
else begin
trans_over_flag<=1'b0;
state<=CH451_Display_temp;
end
end
CH451_Display_temp:begin
if(trans_count==digit_on_num-1) begin
state<=CH451_Get_data;
transfer_count<=0;
end
else begin
state<=CH451_Display_ready;
transfer_count<=transfer_count+1'b1;
display_data<={
display_data[4:0]
display_data{5*digit_on_num-1:5]
};
decemial_point_sel<={
display_led_sel[3:0],
display_led_sel[4*digit_on_num-1:4]
};
display_led_sel<={
display_led_sel[3:0],
display_led_sel[4*digit_on_num-1:4]
};
end
end
default:state<=CH451_Idle;
endcase
end
end
end
task shift12_out;//12位数据输出
begin
casex(shift_state_
Shift12_out_idle:begin
shift_state<=Pull_down_ch451_load;
ACK<=1'b0;
end
Pull_down_ch451_load:begin
shift_state<=Get_shift_out_data;
CH451_LOAD<=1'b0;
shift_state<=Pull_up_ch451_dclk;
end
Get_shift_out_data:begin
shift_state<=Pull_down_ch451_dclk;
CH451_DIN<=CH451_cmd[0];
end
Pull_down_ch451_dclk:begin
CH451_DCLK<=1'b0;
shift_state<=Pull_up_ch451_dclk;
end
Pull_up_ch451_dclk:begin
CH451_DCLK<=1'b1;
shift_state<=Shift_paralell_data;
end
Shift_paralell_data:begin
if(shift_counter==11)begin
shift_state<=Pull_up_ch451_load;
shift_counter<=0;
end
else begin
shif_counter<=shift_counter+1'b1;
CH451_cmd<={1'b0,CH451_cmd[11:1]};
shift_state<=Get_shift_out_data;
ACK<=1'b1;
end
end
default:begin
shift_state<=Shift12_out_idle;
end
endcase
end
endtask