ホーム > ブログ> 4X4 keyboard matrix keyboard program

4X4 keyboard matrix keyboard program

October 10, 2023
Single chip microcomputer STM32L151CCU6
1206RGB (single)
>

//The scan result of the button and the button satisfies the following relationship:
/ / button scan results (result) button scan results
//K100XE7K180XB7
//K110XEBK190XBB
//K120XEDK200XBD
//K130XEEK210XBE
//K140XD7K220X77
//K150XDBK230X7B
//K160XDDK240X7D
//K170XDEK250X7E

#include / / Contains the internal resources of the microcontroller predefined
__CONFIG(0x1832);
/ / Chip configuration word, watchdog off, power-on delay on, power-down detection off, low-voltage programming off, encryption, 4M crystal HS oscillation

Intresult;
Voiddelay();//delay function declaration
Voidinit();//I/O port initialization function declaration
voidsCAN();//Keystroke Scanner Declaration
Voiddisplay(intx);//Display function declaration
//------------------------------------------------ ---
/ / Main program
Voidmain()
{
While(1)//loop work
{
Init();//call initialization subroutine
Scan();//call button scan subroutine
Display(result);//call result display subroutine
}
}

//------------------------------------------------ ---
/ / initialization function
Voidinit()
{
ADCON1=0X07;//Set A port to normal I/O port
TRISA=0X0f; / / set the A port high 2 bits for the output, the lower 4 bits for the input
TRISC=0XF0; / / set C port high 4 bits for input, low 4 bits for output
TRISD=0X00; / / set D port as output
PORTA=0XFF;
PORTD=0XFF; / / clear all displays first
}

//------------------------------------------------ ---
/ / button scan program
Voidscan()
{
PORTC=0XF7; / / C3 output low level, the other three bits output high level
Asm("nop");//Insert a certain delay to ensure stable levels
Result=PORTC;//Read back C port high 4 results
Result=result&0xf0;//Clear the lower 4 bits
If (result! = 0xf0) / / determine whether the high 4 is all 1 (all 1 means no button press)?
{
Result=result|0x07;//No, plus the lower 4 bits 0x07, as a result of the key scan
}
Else / / Yes, change the low 4 bit output, re-determine whether there is a button press
{
PORTC=0XFb; / / C2 output low level, the other three bits output high level
Asm("nop");//Insert a certain delay to ensure stable levels
Result=PORTC;//Read back C port high 4 results
Result=result&0xf0;//Clear the lower 4 bits
If(result!=0xf0)//Check if the high 4 digits are all 1 (all 1 means no button press)
{
Result=result|0x0b;//No, plus the lower 4 bits 0xb, as a result of the key scan
}
Else / / Yes, change the low 4 bit output, rescan
{
PORTC=0XFd; / / C1 output low level, the other three bits output high level
Asm("nop");//Insert a certain delay to ensure stable levels
Result=PORTC;//Read back C port high 4 results
Result=result&0xf0;//Clear the lower 4 bits
If(result!=0xf0)//Check if the high 4 digits are all 1 (all 1 means no button press)
{
Result=result|0x0d;//No, plus the lower 4 bits 0x0d, as a result of the key scan
}
Else / / Yes, change the output of the lower 4 bits, rescan
{
PORTC=0XFe; / / C0 output low level, the other three bits output high level
Asm("nop");//Insert a certain delay to ensure stable levels
Result=PORTC;//Read back C port high 4 results
Result=result&0xf0;//Clear the lower 4 bits
If(result!=0xf0)//Check if the upper four digits are all 1 (all 1 means no button press)
{
Result=result|0x0e;//No, plus the lower 4 bits 0x0e, as a result of the key scan
}
Else / / Yes, all button scan ends, no button press, no button press flag
{
Result=0xff;//The scan result is 0xff, as a flag without button press
}
}
}
}
}

//------------------------------------------------ ----------
/ / Display program
Voiddisplay(intx)
{
Switch(result)
{
Case0xe7:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xc0;PORTA=0X1F;delay();break;//K10
Case0xeb:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xf9;PORTA=0X1F;delay();break;//K11
Case0xed:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xa4;PORTA=0X1F;delay();break;//K12
Case0xee:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0xb0;PORTA=0X1F;delay();break;//K13
Case0xd7:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0x99;PORTA=0X1F;delay();break;//K14
Case0xdb:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0x92;PORTA=0X1F;delay();break;//K15
Case0xdd:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X82;PORTA=0X1F;delay();break;//K16
Case0xde:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0XF8;PORTA=0X1F;delay();break;//K17
Case0xb7:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X80;PORTA=0X1F;delay();break;//K18
Case0xbb:
PORTD=0xf9;PORTA=0X2F;delay();PORTD=0X90;PORTA=0X1F;delay();break;//K19
Case0xbd:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xc0;PORTA=0X1F;delay();break;//K20
Case0xbe:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xf9;PORTA=0X1F;delay();break;//K21
Case0x77:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xa4;PORTA=0X1F;delay();break;//K22
Case0x7b:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0xb0;PORTA=0X1F;delay();break;//K23
Case0x7d:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0x99;PORTA=0X1F;delay();break;//K24
Case0x7e:
PORTD=0xa4;PORTA=0X2F;delay();PORTD=0x92;PORTA=0X1F;delay();break;//K25
Case0xff:
PORTD=0x8e;PORTA=0X2F;delay();PORTD=0x8e;PORTA=0X1F;delay();//no button press
}
}

//------------------------------------------------ ------------------
/ / delay program
Voiddelay()//delay program
{
Inti; / / define the shaping variable
For(i=0x100;i--;);//delay
}

お問い合わせ

Author:

Ms. Zoe Zhong

Phone/WhatsApp:

+8618617178558

人気商品
You may also like
Related Categories

この仕入先にメール

タイトル:
携帯電話:
イーメール:
メッセージ:

Your message must be betwwen 20-8000 characters

2010年に設立されたYetnorson Antenna Co.、Ltdは、コミュニケーションアンテナの研究、開発、販売、サービスに従事する専門企業です。 Yetnorsonは、2 g/3 g/4 g/Wifi/TVアンテナ、およびさまざまなRFコネクタとアンテナケーブルアセンブリに特化しています。 5人のエンジニアがいるR&Dチームがあり、7日以内にクライアントの仕様に応じてプロジェクトを完了し、オプション用に500種類以上の現在のアンテナ製品があります。その間、36の製品特許と8つの商標があり、ISO 9 0 0 1品質システム認証、ISO 1 4 0 0...
Newsletter
イーメール
sales07@ynxantenna.com
住所
Room #101,201,301,5BLDG , No.4 of XinWuCun New Area ,Shabo community Maluan Street Pingshan District, Shenzhen ,Guangdong , China, Shenzhen, Guangdong China

Copyright ©2024 Yetnorson Antenna Co., Ltd.著作権を有します

We will contact you immediately

Fill in more information so that we can get in touch with you faster

Privacy statement: Your privacy is very important to Us. Our company promises not to disclose your personal information to any external company with out your explicit permission.

送信