Dom> Blog> 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
}

Skontaktuj się z nami

Author:

Ms. Zoe Zhong

Phone/WhatsApp:

+86 18617178558

Wszystkie produkty
You may also like
Related Categories

Wyślij je do tym dostawcy

Przedmiot:
Mobile Phone:
Email:
wiadomość:

Your message must be betwwen 20-8000 characters

Skontaktować

  • Tel: 86-186-17178558
  • Whatsapp: +86 18617178558
  • Email: sales07@ynxantenna.com
  • Adres: Room #101,201,301,5BLDG , No.4 of XinWuCun New Area ,Shabo community Maluan Street Pingshan District, Shenzhen ,Guangdong , China, Shenzhen, Guangdong China

Send Inquiry

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.

Wysłać