홈페이지> 블로그> 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 년에 설립 된 Entnorson Antenna Co., Ltd는 커뮤니케이션 안테나의 연구, 개발, 판매 및 서비스에 종사하는 전문 회사입니다. Yetnorson은 2 g/3 g/4 g/wifi/TV 안테나와 다양한 RF 커넥터 및 안테나 케이블 어셈블리를 전문으로했습니다. 우리는 5 명의 엔지니어가있는 R & D 팀이 7 일 이내에 고객의 사양에 따라 프로젝트를 완료 할 수 있으며 귀하의 옵션을 위해 500 가지 이상의 현재 안테나 제품을 제공합니다. 그 동안 36 개의 제품 특허와 8 개의 상표가 있으며 ISO...
Newsletter
회사 주소
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.판권소유

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.

송신