ch451检测按键c语言,FPGA:利用ch451驱动数码管,实现按键控制显示数字

这是一个用于EDA学生7段显示实验的代码,通过串行命令将数据传输到CH451。代码中定义了一个状态机,处理从SEDLED_COMMAND接收的并行命令,并通过SPI模块以3线串行方式(LDA, LCLK, LOAD)传输。在每个时钟的负边沿,状态机会根据命令开始标志进入不同的状态,如传输、判断和结束。同时,代码还设置了负载和时钟信号的分配。
摘要由CSDN通过智能技术生成

/****************************************/

//Code to transfer serial command to CH451, for EDA students in 7-Segments experiments

//3-line serial command:LDA,LCLK,LOAD,from CH451

//By you,2017.0922

//steps of transfer serial command:

// 1.recevie the parrel command from SEDLED_COMMAND

// 2.transfer the command via serial command

/****************************************************/

module CH451SPI_MODULE(rst,clk,command,command_idle,command_start,LDA,LCLK,LOAD);

parameter command_length=12;

input rst,clk; //system

input[command_length-1:0] command;

input command_start; //inidcate if the command start

//output

output LDA,LCLK,LOAD;

output command_idle;

//command

// state machine

reg[3:0] state;

parameter state_idle=4'h0;

parameter state_start=4'h1;// command start

parameter state_tran=4'h2;// transfer command

parameter state_judge=4'h3;// see if all command complete

parameter state_end=4'h4;

parameter state_rdy=4'h5;//load command

reg[7:0] count;// count number of commands

reg[command_length-1:0] current_command;

reg reg_LDA;

reg reg_LOAD;

/**************************************/

always@(negedge clk) // use negedge to avoid competion risk,

if(~rst)

begin

state<=state_idle;

count<=0;

reg_LDA<=1;

end

else

case(state)

state_idle: if(command_start) // if SPI module not busy,idle

state<=state_rdy;

else

state<=state_idle;

state_rdy: begin

current_command<=command;

state<=state_tran;

end// transfer the current command, one command only

state_tran:begin

if(count

begin

reg_LDA<=current_command[0];

current_command<=(current_command>>1);

count<=count+1'b1;

end

else

begin

state<=state_end;

count<=0;

reg_LDA<=1;

end

end

state_end:  state<=state_idle; // stop to here

default: state<=state_idle;

endcase

/*******************/

assign LDA=reg_LDA;

assign LCLK=clk;

always@(posedge clk)

if(~rst)

reg_LOAD<=1'b1;

else

begin

if(count==(command_length))

reg_LOAD<=1'b0;

else

reg_LOAD<=1'b1;

end

//assign LOAD=(count==(command_length-1))?1'b0:1'b1;

assign LOAD=reg_LOAD;// assign

assign command_idle=(state==state_idle)?1'b1:1'b0;

endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值