你的位置:首页 > 操作系统

[操作系统]单片机4*4按键数码管显示0

 

单片机4*4按键数码管显示0-9

 

#include<reg51.h>#define KEY  P1 //键盘#define SEG  P0 //数码管sbit COM=P2^0; // 数码管comsbit OC = P2^1;//573锁存器 char code TAB[]={//0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xa7,0xa1,0x84,0x84,0xbf,0x7f};//共阳极        0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7D,0x07,0x7f,0x6F}; //共阴极unsigned char scan[4]={0xef,0xdf,0xbf,0x7f}; //扫描码 低电平动作扫描void delay1ms(int x);void main(void){    unsigned char col,row;      //定义列行  unsigned char rowkey,kcode;   //定义按键,按键码   COM=0; //点亮数码管   OC=0; // 低电平使能  while(1)  {    for(col=0;col<4;col++)   //4列按键    {    KEY =scan[col];    rowkey = ~KEY&0X0F;     //例如 读入低4位 反相清除高4位,求出行键值    while(rowkey!=0)     //如果按下键    {       if(rowkey==0x01)  row = 0;   //如果第0行被按下      if(rowkey ==0x02) row = 1;   //如果第1行被按下      if(rowkey == 0x04) row = 2;   //如果第2行被按下      if(rowkey == 0x08) row = 3;   //如果第3行被按下      kcode = 4*col+row;   //算出按键号码      SEG = TAB[kcode];   //显示      while(rowkey!=0)   //如果未放开      rowkey = ~ KEY&0x0f; //再次读入       }          delay1ms(1);  //消除抖动1ms    }  }}   void delay1ms(int x) //延迟消抖{   int i,j;   for(i=0;i<x;i++)   for(j=0;j<120;j++); // 120 * 8 = 960us = 1ms }