//SHIFT COUNTER
always @(negedge RESET or posedge CLOCK ) begin
if (!RESET) SD_COUNTER=5'b11111;
else begin
if (GO==0)
SD_COUNTER=0;
else
if (SD_COUNTER < 5'b11111) SD_COUNTER=SD_COUNTER+1;
end
end
//----
always @(negedge RESET or posedge CLOCK ) begin
if (!RESET) begin SCLK=1;SDO=1;STO=1; end
else
case (SD_COUNTER)
5'd0 : begin SDO=1; SCLK=1;end
//start
5'd1 : begin SD=HC595_DATA;SDO=0;end
5'd2 : SCLK=0;
//SLAVE ADDR
5'd3 : SDO=SD[15];
5'd4 : SDO=SD[14];
5'd5 : SDO=SD[13];
5'd6 : SDO=SD[12];
5'd7 : SDO=SD[11];
5'd8 : SDO=SD[10];
5'd9 : SDO=SD[9];
5'd10 : SDO=SD[8];
6'd11 : SDO=SD[7];
reg [7:0]cntcode;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n) begin cntcode<=0;code<=0; end
else if(cnt==8'hff)
begin
cntcode<=cntcode+1;
if(cntcode==8'hff) code<=code+1;
end
end
reg[15:0] dis_code;
reg [11:0]decode;
always @(posedge clk or negedge rst_n)
if(!rst_n) GO=1;
else if(cnt>=8'h20 && cnt<=8'h24) begin decode={code[3:0],8'hf7};GO=0; end
else if(cnt>=8'h60 && cnt<=8'h64) begin decode={code[7:4],8'hfb};GO=0; end
else if(cnt>=8'ha0 && cnt<=8'ha4) begin decode={code[11:8],8'hfd};GO=0; end
else if(cnt>=8'he0 && cnt<=8'he4) begin decode={code[15:12],8'hfe};GO=0; end
else GO=1;