当前位置:网站首页>Blue Bridge Cup SCM module code (matrix key) (code + comments)

Blue Bridge Cup SCM module code (matrix key) (code + comments)

2022-06-25 06:44:00 tuygre

/* The explanation in the previous chapter will not be repeated */
#include "STC15F2K60S2.H"

typedef unsigned char u8;
typedef unsigned int u16;

u8 org[9],tran[9],old,new,wei;
u16 count;

void close()
{
  P0=0;
  P2=P2&0X1F|0XA0;
  P2=P2&0X1F;
  P0=0XFF;
  P2=P2&0X1F|0X80;
  P2=P2&0X1F;
}

void Timer0Init(void)		
{
  AUXR |= 0x80;		
  TMOD &= 0xF0;		
  TL0 = 0x20;		
  TH0 = 0xD1;		
  TF0 = 0;		
  TR0 = 1;		
}

void open()
{
  EA=1;
  ET0=1;
}

void translate(u8 org[],u8 tran[])
{
  u8 tran1,j,k;
  for(j=0,k=0;k<8;j++,k++)
  {
    switch(org[j])
	{
	  case '0': tran1 = 0xc0; break;  
	  case '1': tran1 = 0xf9; break;  
	  case '2': tran1 = 0xa4; break;  
	  case '3': tran1 = 0xb0; break;  
 	  case '4': tran1 = 0x99; break;  
	  case '5': tran1 = 0x92; break;  
  	  case '6': tran1 = 0x82; break;  
	  case '7': tran1 = 0xf8; break;  
	  case '8': tran1 = 0x80; break;  
   	  case '9': tran1 = 0x90; break;
	  case 'A': tran1 = 0x88; break;
	  case 'B': tran1 = 0x83; break;
	  case 'C': tran1 = 0xc6; break;
	  case 'D': tran1 = 0xa1; break;
	  case 'E': tran1 = 0x86; break;  
	  case 'F': tran1 = 0x8E; break;
	  default: tran1 = 0xff; 
	}
	if(org[j+1]=='.')
	{
	  tran1&=0x7f;
	  j++;
	}
	tran[k]=tran1;
  }
}

void display(u8 tran[],u8 wei)
{
  P0=0XFF;
  P2=P2&0X1F|0XE0;
  P2=P2&0X1F;
  P0=1<<wei;
  P2=P2&0X1F|0XC0;
  P2=P2&0X1F;
  P0=tran[wei];
  P2=P2&0X1F|0XE0;
  P2=P2&0X1F;
}

u8 search()
{
  u16 key;
  u8 key_return;
  P44=0;P42=1;P35=1;P34=1;
  key=key|(P3&0X0F);
  P44=1;P42=0;P35=1;P34=1;
  key=(key<<4)|(P3&0X0F);
  P44=1;P42=1;P35=0;P34=1;
  key=(key<<4)|(P3&0X0F);
  P44=1;P42=1;P35=1;P34=0;
  key=(key<<4)|(P3&0X0F);
/* According to the schematic diagram , We control P44、P42、P35、P34 Four pins , Set any one of them 0, The implementation of a matrix key 
 Column by column scanning , Then through observation P30、P31、P32、P33 Four pins , Determine which line the pressed key is on . That's a little 
 abstract , for instance : Such as P44=0;P42=1;P35=1;P34=1; What is being scanned is S7、S6、S5、S4 This column , Such as P32 by 0,
 It means S5 Where the circuit is connected ,S5 Pressed . Parameters key The result of the scan is recorded , adopt switch function , Confirm the pressed press 
 key . notes :J5 To put 2,3 Connect , Scanning will work ; At the same time, according to the user manual 3.3 We are here ,P3 The initial value of 1111 11
11, therefore P30、P31、P32、P33 Change with the external state , Specific learnable 《 Single chip microcomputer principle and interface technology 》*/ 
  switch(~key)
  {
    case 0x8000: key_return = 4; break; // S4  
	case 0x4000: key_return = 5; break; // S5  
	case 0x2000: key_return = 6; break; // S6  
	case 0x1000: key_return = 7; break; // S7  
	case 0x0800: key_return = 8; break; // S8  
	case 0x0400: key_return = 9; break; // S9  
	case 0x0200: key_return = 10; break; // S10  
	case 0x0100: key_return = 11; break; // S11  
	case 0x0080: key_return = 12; break; // S12  
	case 0x0040: key_return = 13; break; // S13  
	case 0x0020: key_return = 14; break; // S14  
	case 0x0010: key_return = 15; break; // S15  
	case 0x0008: key_return = 16; break; // S16  
	case 0x0004: key_return = 17; break; // S17  
	case 0x0002: key_return = 18; break; // S18  
	case 0x0001: key_return = 19; break; // S19  
	default: key_return = 0; 
  }
  return key_return;
}
/* The return value of this function is the number of the key pressed . Do not consider the case that two keys are pressed at the same time ,
 Because the MCU runs very fast , Even if the visual inspection is simultaneously pressed , For MCU 
 There are still two different moments */

void key_translate()
{
  new=search();
/* Collect key information */ 
  if(new!=old&&new!=0)
/* Each time you press a key, you only acknowledge it once and rule out the case that no key is pressed */ 
  {
    if(new>=14)
	org[7]=new-14+'A';
	else org[7]=new-4+'0';
/* The array records ASCII value */
  }
  old=new;
/* Don't forget */
}
 

void main()
{
  close();
  open();
  Timer0Init();
  while(1)
  {
    translate(org,tran);
	key_translate();
  }
}

void time0() interrupt 1
{
  display(tran,wei);
  if(++wei==8) wei=0;
}


Official schematic diagram , The download address of the user manual is as follows :

link :https://pan.baidu.com/s/1y8lRYHxLKojL4_r0PZPYRw 
Extraction code :19so

Note cannot insert picture , Relevant information readers themselves look for in the folder .

Study notes for undergraduate students of Nanjing University of Information Engineering , For your reference .

If there is a mistake , contact QQ3182097183.

原网站

版权声明
本文为[tuygre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200605293797.html