//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// Create Date: 15:49:43 10/29/2014
// Design Name:
// Module Name: commuication
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module light_tx(
sys_clk,
key_done,
sys_rstn,
data_tx,
send_data
);
//////////////////////////////////////////////////////////////////////////////////
input sys_clk;
input key_done;
input sys_rstn;
output [7:0]send_data;
output data_tx;
reg [7:0]send_data;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//閸掑棝顣剁拋鈩冩殶 50m t=20ns 1101_1011_1011_1010_0000_0
reg [20:0]count;
reg clear;
parameter t9ms = 21'd449_999; //450000
parameter t4_5ms = 21'd224_999; //225000
parameter t1_125ms = 21'd56_249; //56250
parameter t0_56ms = 21'd27_999; //28000
parameter t2_25ms = 21'd112_499; //112500
parameter t1_678ms = 21'd83_899; //83900
reg [2:0]n;
reg [2:0]m;
always @(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
count <= 21'd0;
else if(clear)
count <= 21'd0;
else
count <= count + 1'b1;
end
//////////////////////////////////////////////////////////////////////////////////
//div 38k 1/38 2.63157894 * 10^-5 s 26.3157us n=760 1110_1111_0110_0
//50Mhz 20ns
//1khz 1ms 1100_0011_0101_0000
parameter t1k = 15'd25000;
parameter t66k = 15'd379;
parameter t38k = 15'd657;
reg [15:0]cnt_div;
reg div_switch;
reg clk_frequence;
always @(posedge sys_clk)
begin
if(!sys_rstn)
begin
cnt_div <= 16'd0;
clk_frequence <= 1'b1;
end
else if(div_switch)
begin
if(cnt_div == t38k)
clk_frequence = ~clk_frequence;
else
cnt_div <=cnt_div + 1'b1;
end
else
cnt_div <= 16'd0;
end
//////////////////////////////////////////////////////////////////////////////////
reg [5:0]state;
//////////////////////////////////////////////////////////////////////////////////
reg [7:0]user_data;
reg [2:0]i;
reg [2:0]j;
reg data;
always @(posedge sys_clk)
begin
if(!sys_rstn)
state <= 6'd0;
else
case(state)
6'd0:
//if(key_done)
begin
clear <= 1'b1;
user_data <= 8'b1111_1111;
state <= state + 1'b1;
send_data <= 8'b0000_0001;
div_switch <= 1'b0;
i <= 3'd0;
j <= 3'd0;
n <= 3'd0;
m <= 3'd0;
end
//else
//state <= 6'd0;
6'd1:
begin
if(count == t9ms)
begin
clear <= 1'b1;
state <= state + 1'b1;
div_switch <= 1'b0;
end
else
begin
data <= 1'd1;
clear <= 1'b0;
div_switch <= 1'b0;
end
end
6'd2:
begin
if(count == t4_5ms)
begin
state <= state + 1'b1;
clear <= 1'b1;
div_switch <= 1'b0;
end
else
begin
clear <= 1'b0;
data <= 1'd0;
div_switch <= 1'b0;
end
end
6'd3,6'd4,6'd5,6'd6,6'd7,6'd8,6'd9,6'd10: //8娴e秵鏆熼幑
begin
if(user_data == 1'b0)
begin
if(count == t1_125ms)
begin
clear <= 1'b1;
i <= i + 1'b1;
state <= state + 1'b1;
div_switch <= 1'b0;
end
else if(count >= t0_56ms)
begin
clear <= 1'b0;
data <= 1'b0;
div_switch <= 1'b0;
end
else
begin
clear <= 1'b0;
div_switch <= 1'b1;
end
end
else
begin
if(count == t2_25ms)
begin
clear <= 1'b1;
i <= i + 1'b1;
state <= state + 1'b1;
div_switch <= 1'b0;
end
else if(count >= t0_56ms)
begin
data <= 1'b0;
clear <= 1'b0;
div_switch <= 1'b0;
end
else
begin
div_switch <= 1'b1;
clear <= 1'b0;
end
end
end
6'd11,6'd12,6'd13,6'd14,6'd15,6'd16,6'd17,6'd18:
begin
if(user_data[j] == 1'b1)
begin
if(count == t1_125ms)
begin
clear <= 1'b1;
j <= j + 1'b1;
state <= state + 1'b1;
div_switch <= 1'b0;
end
else if(count >= t0_56ms)
begin
clear <= 1'b0;
data <= 1'b0;
div_switch <= 1'b0;
end
else
begin
clear <= 1'b0;
div_switch <= 1'b1;
end
end
else
begin
if(count == t2_25ms)
begin
clear <= 1'b1;
j <= j + 1'b1;
state <= state + 1'b1;
div_switch <= 1'b0;
end
else if(count >= t0_56ms)
begin
data <= 1'b0;
clear <= 1'b0;
div_switch <= 1'b0;
end
else
begin
div_switch <= 1'b1;
clear <= 1'b0;
end
end
end
6'd19,6'd20,6'd21,6'd22,6'd23,6'd24,6'd25,6'd26:
begin
if(send_data[n] == 1'b0)
begin
if(count == t1_125ms)
begin
clear <= 1'b1;
div_switch <= 1'b0;
n <= n + 1'b1;
state <= state + 1'b1;
end
else if(count >= t0_56ms)
begin
clear <= 1'b0;
data <= 1'b0;
div_switch <= 1'b0;
end
else
begin
clear <= 1'b0;
div_switch <= 1'b1;
end
end
else
begin
if(count == t2_25ms)
begin
clear <= 1'b1;
n <= n + 1'b1;
div_switch <= 1'b0;
state <= state + 1'b1;
end
else if(count >= t0_56ms)
begin
data <= 1'b0;
div_switch <= 1'b0;
clear <= 1'b0;
end
else
begin
div_switch <= 1'b1;
clear <= 1'b0;
end
end
end
6'd27,6'd28,6'd29,6'd30,6'd31,6'd32,6'd34,6'd35:
begin
if(send_data[m] == 1'b1)
begin
if(count == t1_125ms)
begin
clear <= 1'b1;
div_switch <= 1'b0;
m <= m + 1'b1;
state <= state + 1'b1;
end
else if(count >= t0_56ms)
begin
clear <= 1'b0;
div_switch <= 1'b0;
data <= 1'b0;
end
else
begin
clear <= 1'b0;
div_switch <= 1'b1;
end
end
else
begin
if(count == t2_25ms)
begin
clear <= 1'b1;
div_switch <= 1'b0;
m <= m + 1'b1;
state <= state + 1'b1;
end
else if(count >= t0_56ms)
begin
data <= 1'b0;
div_switch <= 1'b0;
clear <= 1'b0;
end
else
begin
div_switch <= 1'b1;
clear <= 1'b0;
end
end
end
6'd36:
begin
if(count == t1_678ms)
begin
clear <= 1'b1;
div_switch <= 1'b0;
data <= 1'b0;
state <= 6'd0;
end
else
begin
div_switch <= 1'b1;
clear <= 1'b0;
end
end
default:
state <= 6'd0;
endcase
end
assign data_tx = (div_switch == 1'b1)? clk_frequence : data;
endmodule
文章评论(0条评论)
登录后参与讨论