main.c如下:
#include "LPC21xx.h"
#include <stdio.h>
extern void init_serial (void); /* initialize serial interface */
//³£Á¿¶¨Òå
const unsigned char LEDSEGCODE[]=
{
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
};
unsigned char KSCANCODE[]={0x01,0x02,0x04,0x08};
unsigned char keytab[4][4]= //¼üÖµ±í£¬±£´æÁË16¸ö°´¼üµÄ¶ÔÓ¦¼üÖµ
{
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05
};
void Delay_250uS(void) //ÑÓʱº¯Êý
{
unsigned int i;
i=50000;//CCLK=60MHZ
while(i--);
}
int main(void)
{
unsigned int keyval;
unsigned int row,col,x1,x2;
init_serial( );
printf("xvvvv1");
//³õʼ»¯LPC2124µÄP0
PINSEL0=0x00050000;//0x00000000;
PINSEL1=0x00000000;
PINSEL2=0x00000000;
IO0DIR=0x000000FF;//
IO1DIR=0x000F0000;//KR1-KR4¶¨ÒåΪÊä³ö KC1-KC4¶¨ÒåΪÊäÈë
IO0CLR=0x000000FF;//³õʼ»¯LEDÃð
keyval=0;
//ÎÞÏÞÑ»·Ìå
printf("xvvvv2");
while (1)
{
//Ñ»·´ÓP1.16-P1.19Êä³öÐÐɨÃèÐźţ¨Ö»ÓÐ1ÌõÐÐÏßÊä³öµÍµçƽ£¬ÆäÓà3ÌõΪ¸ß£©
for (row=0; row<4; row++) //·¢³öɨÃèÂëµÄʱºò£¬È·¶¨±¾´Î¼ì²â°´¼üµÄÐкÅ
{
IO1SET=(0x0F)<<16;
IO1CLR=(KSCANCODE[row])<<16; //¸ù¾ÝÐкŲé±í£¬µÃµ½²¢·¢³ö±¾´Î¼üÅÌɨÃèÂë
x1= IO1PIN;
if ((x1 & 0x00F00000)!=0x00F00000) //¼ì²âµ½¼üÅÌ°´Ï¶¯×÷
{
Delay_250uS(); //ÑÓʱȥ¶¶¶¯
x2=IO1PIN;
if ((x2 & 0x00F00000)!=0x00F00000) //È·ÈÏÊÇÄÄÁеļü°´ÏÂ
{ //È·¶¨°´¼üµÄÁкÅ
if ((IO1PIN & 0x00E00000)==0x00E00000) col=0;
else if ((IO1PIN & 0x00D00000)==0x00D00000) col=1;
else if ((IO1PIN & 0x00B00000)==0x00B00000) col=2;
else col=3;
keyval=keytab[row][col]; //¸ù¾ÝÐкźÍÁкţ¬²é±íÇó¼üÖµ
IO0CLR|=0xFF; //ÇåÏÔʾ
IO0SET=(LEDSEGCODE[keyval]<<0); //ÏÔʾ±¾´Î°´ÏµļüÂëÖµ
printf("R=%x c=%x\n",(x2>>16)&0x000f,(x2>>20)&0xf);
}
}
}
}
}
要求如下:
1 加入对串口的支持。将printf函数的输出重定向到串口,然后在Proteus中观测。每次按键,将P1口值在串口打印出来。
2 源程序有一个问题,即一次按键可记录为多次。修改程序,使得一次按键,只记录一次,而不管按键的时长。
求解》