3*4矩阵键盘程序,帮我改一下

3*4矩阵键盘程序,帮我改一下,第1张

//4*4矩阵键盘----------------------------------------------------------------------------------- module key44( code, col, valid, row, sys_clk, rst )//------------------------------------------------------------------------------------------------- /枯银/ Port declaration output [3:0] col output valid output [3:0] code input [3:0] row input sys_clk,rst //巧败高------------------------------------------------------------------------------------------------- // reg [3:0] col,codereg [5:0] state,next_stateparameter S_0 = 6'b000001, S_1 = 6'b000010, S_2 = 6'b000100, S_3 = 6'b001000, S_4 = 6'b010000, S_5 = 6'b100000reg S_row reg [3:0] count,row_reg,col_regreg clk2,clk4 reg [4:0] Mega_cntwire clk//------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- /孝尺/ always @( posedge sys_clk, negedge rst ) begin if(!rst) Mega_cnt<=0else Mega_cnt<=Mega_cnt+1end assign clk = Mega_cnt[4]//------------------------------------------------------------------------------------------------- // Frequency Division Two always @( posedge clk ) clk2 <= ~clk2// A quarter of the clk always @( posedge clk2 ) clk4 <= ~clk4//------------------------------------------------------------------------------------------------- // Check the Key //------------------------------------------------------------------------------------------------- always @( posedge clk4, negedge rst ) if(!rst) begin count <= 0S_row <= 0end else begin if(!(row[0]&row[1]&row[2]&row[3])) begin if(count <'d4) count <= count + 1// Filter else S_row <= 1end // else if(state[5]||state[0]) else if((state == S_0) || (state == S_5)) begin count <= 0S_row <= 0end end assign valid = ((state == S_1)||(state == S_2)||(state == S_3)||(state == S_4)) &&(!(row[3]&row[2]&row[1]&row[0]))//------------------------------------------------------------------------------------------------- // Save the value of row and col always @( negedge clk ) if( valid ) begin row_reg <= row col_reg <= col end /* else begin row_reg <= row_reg col_reg <= col_reg end*/ //------------------------------------------------------------------------------------------------- // Decode the Key always @( row_reg, col_reg, clk ) case( {row_reg,col_reg} ) 8'B1110_1110: code = 4'hd8'B1110_1101: code = 4'h98'B1110_1011: code = 4'h58'B1110_0111: code = 4'h1 8'B1101_1110: code = 4'he8'B1101_1101: code = 4'ha8'B1101_1011: code = 4'h68'B1101_0111: code = 4'h2 8'B1011_1110: code = 4'hf8'B1011_1101: code = 4'hb8'B1011_1011: code = 4'h78'B1011_0111: code = 4'h3 8'B0111_1110: code = 4'h08'B0111_1101: code = 4'hc8'B0111_1011: code = 4'h88'B0111_0111: code = 4'h4default : code = 4'h0endcase //------------------------------------------------------------------------------------------------- // State Machine : Mealy //------------------------------------------------------------------------------------------------- always @( posedge clk4, negedge rst ) if( !rst ) state <= S_0 else state <= next_state //------------------------------------------------------------------------------------------------- always @( state, row, S_row ) begin col = 0//---------------------------------------------- case( state ) S_0 : begin col = 4'b0000if(S_row) next_state = S_1else next_state = S_0end //---------------------------------------------- // Decoding... S_1 : begin col = 4'b1110if(row!='hf) next_state = S_5else next_state = S_2end S_2 : begin col = 4'b1101if(row!='hf) next_state = S_5else next_state = S_3end S_3 : begin col = 4'b1011if(row!='hf) next_state = S_5else next_state = S_4end S_4 : begin col = 4'b0111if(row!='hf) next_state = S_5else next_state = S_0end //---------------------------------------------- S_5 : begin col = 4'b0000if(row == 4'b1111) next_state = S_0else next_state = S_5end default: next_state = S_0endcase end endmodule

#include<stdio.h>

#include<reg51.h>

#define uchar unsigned char

uchar key

int i=0

char b[4]={'0','0','0','0'}//输入的密码放这里和初始的比较来判断是否密码正确

void keyscan() //扫描按键是否按下

{

uchar temp

P3=0xfe //扫描第一行

temp=P3

temp=temp&0xf0

if(temp!=0xf0) //判断按键是否按毁迟下

{

delayms(10)//去抖纤虚李动

temp=P3

temp=temp&0xf0

if(temp!=0xf0)//确认按键按下

{

temp=P3

switch(temp)

{

case 0xee:key='0'break

case 0xde:key='1'break

case 0xbe:key='2'break

case 0x7e:key='3'break

}

while(temp!=0xf0) //判断按键是否松开

{

temp=P3

temp=temp&0xf0

}

b[i]=keyi++if(i==3)i=0

}

}

P3=0xfd //扫描第二行

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

delayms(10)

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

temp=P3

switch(temp)

{

case 0xed:key='4'break

case 0xdd:key='5'break

case 0xbd:key='6'break

case 0x7d:key='7'break

}

while(temp!=0xf0)

{

temp=P3

temp=temp&0xf0

}

b[i]=keyi++if(i==3)i=0

}

}

P3=0xfb //扫描第三行

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

delayms(10)

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

temp=P3

switch(temp)

{

case 0xeb:key='8' break

case 0xdb:key='誉档9' break

case 0xbb:key='A'break

case 0x7b:key='B'break

}

while(temp!=0xf0)

{

temp=P3

temp=temp&0xf0

}

b[i]=keyi++if(i==3)i=0

}

}

P3=0xf7 //扫描第四行

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

delayms(10)

temp=P3

temp=temp&0xf0

if(temp!=0xf0)

{

temp=P3

switch(temp)

{

case 0xe7:key='C'break

case 0xd7:key='D'break

case 0xb7:key='E'break

case 0x77:key='F'break

}

while(temp!=0xf0)

{

temp=P3

temp=temp&0xf0

}

b[i]=keyi++if(i==3)i=0

}

}

}

main()

{

char a[4]={'3','5','A','C'}//这个做初始密码

while(1)//无限循环,保证了b 0-3 四个数有值

{

keyscan()

if(a[0]==b[0]&&a[1]==b[1]&&a[2]==b[2]&&a[3]==b[3])

{

//这里写密码正确程序要怎么处理

}

}

}

#define uint unsigned int

#define uchar unsigned char

#define Key_Scanf P1 //4X4按键显示;

#define Print_Out P0 //显示数字P0;

#define PORT P2 /茄绝/数码选择;

#define SYSLED P0^7//系统指示灯;

uchar code table[]={0xff,0x4f,0x12,0x06,0x4c,0x24,0x20,0x0f,0x00,0x0c,0x01} //0~9数字

void display(uchar) //显示

void delay_50us(uint t) //延时50微秒,防抖动

uchar keyscan(void) //键盘

void SYS_LED(void) //指示灯

uchar a=0

uchar keynum

void delay_50us(uint t)

{

uchar j

for(t>0t--)

for(j=19j>0j--)

}

void SYS_LED()

{

SYSLED = 0

delay_50us(100)

SYSLED = 1

}

void display( uchar keynum) //根据keynum返回值显示数字

{

PORT=0xfe

Print_Out=table[keynum]

}

uchar keyscan()

{

Key_Scanf=0xbf // 扫描第一横行,后四位为1,前四位线与,然后跟原来数值比较,如果不等,则把对应的数字赋给keynum

if((Key_Scanf&0xbf)!=0xbf)

{

delay_50us(100)

if((Key_Scanf&0xbf)!=0xbf)//消抖

{

SYS_LED()

a=Key_Scanf&0xbf

switch(a)

{

case 0x77:keynum=1break

case 0x7b:keynum=2break

case 0x7d:keynum=3break

default:break

}

}

}

Key_Scanf=0xdf//扫描第二横行,后四位为1,前四位线激知与;

if((Key_Scanf&0xdf)!=0xdf)

{

delay_50us(100)

if((Key_Scanf&0xdf)!=0xdf)

{

a=Key_Scanf&0xdf

switch(a)

{

case 0x7e:keynum=4break

case 0xb7:keynum=5break

case 0xbb:keynum=6break

default: break

}

}

}

Key_Scanf=0xef //扫描第三横行,后四位为1,前四位线明纳消与;

if((Key_Scanf&0xef)!=0xef)

{

delay_50us(100)

if((Key_Scanf&0xef)!=0xef)

{

a=Key_Scanf&0xef

switch(a)

{

case 0xbd:keynum=7break

case 0xbe:keynum=8break

case 0xe7:keynum=9break

default: break

}

}

}

Key_Scanf=0xf7 //扫描第四横行,后四位为1,前四位线与;

if((Key_Scanf&0xf7)!=0xf7)

{

delay_50us(100)

if((Key_Scanf&0xf7)!=0xf7)

{

a=Key_Scanf&0xf7

switch(a)

{

case 0xeb:keynum=10break

default: break

}

}

}

return keynum

}

void main(void)

{

while(1)

{

keyscan()

display(keynum)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12443757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存