STC单片机矩阵按键在12232液晶上显示,程序有点问题,求大婶看看

2019-07-15 16:57发布

#include "STC15F2K60S2.h"
#include"intrins.h"
#define uchar unsigned char
#define uint  unsigned int
sbit CS=P1^0;
sbit SID=P1^1;
sbit SCLK=P1^2;
uchar num2;
uchar jj=0;
uchar flag=0;
//uchar code table[]={'0','1','2','3','4','5','6,'7','8','9','*','#'};          //显示字符表
uchar code table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2a,0x23};          //显示字符表
//uchar code table[]={0xa3b0,0xa3b1,0xa3b2,0xa3b3,0xa3b4,0xa3b5,0xa3b6,0xa3b7,0xa3b8,0xa3b9,0xa3aa,0xa3a3};          //显示字符表
//=====================延时函数==========================  
void delayms(uint xms)
{
  uint i,j;
  for(i=xms;i>0;i--)          //i=xms,即延时约xms毫秒      
   for(j=110;j>0;j--);
}
void send_command(uchar command_data)
{
  uchar i;
  uchar i_data;
  i_data=0xf8;
  CS=1;
  SCLK=1;
  for(i=0;i<8;i++)
  {         
        SID=(bit)(i_data&0x80);
        SCLK=0;
        delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  i_data=command_data;
  i_data&=0xf0;
  for(i=0;i<8;i++)
  {   
        SID=(bit)(i_data&0x80);
        SCLK=0;
        delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  i_data=command_data;
  i_data<<=4;
  for(i=0;i<8;i++)
  {
    SID=(bit)(i_data&0x80);
        SCLK=0;
                delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  CS=0;
  delayms(10);
}
void send_data(uchar command_data)
{
  uchar i;
  uchar i_data;
  i_data=0xfa;
  CS=1;
  SCLK=1;
  for(i=0;i<8;i++)
  {
        SID=(bit)(i_data&0x80);
        SCLK=0;
                delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  i_data=command_data;
  i_data&=0xf0;
  for(i=0;i<8;i++)
  {
        SID=(bit)(i_data&0x80);
        SCLK=0;
                delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  i_data=command_data;
  i_data<<=4;
  for(i=0;i<8;i++)
  {
        SID=(bit)(i_data&0x80);
        SCLK=0;
                delayms(1);
        SCLK=1;
        i_data=i_data<<1;
  }
  CS=0;
  delayms(10);
}
//=====================按键扫描==========================
uchar matrixkeyscan()
{
  uchar temp,key;
  P1=0xfe;                    //P1口赋值为 1111,1110即将P1.0置低
  temp=P1;                    //将P1口赋给temp,等待按键按下
  temp=temp&0xf0;             //判断temp的高四位是否为0,即判断是否有键按下
  if(temp!=0xf0)                          //当有键按下时
  {
           delayms(10);             //延时去抖
         temp=P1;                 //重新读一次P1口的数据
         temp=temp&0xf0;          //判断temp的高四位是否为0,即判断是否有键按下
         if(temp!=0xf0)                          //确实有键按下时
         {
                 temp=P1;
                if(temp==0xee)
                   key=3;
        if(temp==0xde)
                   key=2;
                if(temp==0xbe)
                   key=1;
                flag=1;
                while(temp!=0xf0)     //等待按键被释放
                {
                  temp=P1;                          //不断读取P1口的值。然后赋给temp
                  temp=temp&0xf0;     //判断高四位,只要结果不等于0xf0,说明按键没有释放,等待按键释放
                  delayms(30);
                }          
         }
  }
  P1=0xfd;
  temp=P1;
  temp=temp&0xf0;
  if(temp!=0xf0)
  {
          delayms(10);
        temp=P1;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
         temp=P1;
         if(temp==0xed)
            key=6;
         if(temp==0xdd)
            key=5;
         if(temp==0xbd)
            key=4;
         flag=1;
         while(temp!=0xf0)
          {
                 temp=P1;
                temp=temp&0xf0;
                delayms(30);
          }
        }
  }
  P1=0xfb;
  temp=P1;
  temp=temp&0xf0;
  if(temp!=0xf0)
  {
          delayms(10);
        temp=P1;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
          temp=P1;
          if(temp==0xeb)
                key=9;
          if(temp==0xdb)
                key=8;
      if(temp==0xbb)
                key=7;
         flag=1;               
          while(temp!=0xf0)
          {
                  temp=P1;
                temp=temp&0xf0;
                delayms(30);
          }
        }
  }
  P1=0xf7;
  temp=P1;
  temp=temp&0xf0;
  if(temp!=0xf0)
  {
          delayms(10);
        temp=P1;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
          temp=P1;
          if(temp==0xe7)
                key=11;
          if(temp==0xd7)
                key=0;
          if(temp==0xb7)
            key=10;
          flag=1;
          while(temp!=0xf0)
          {
                 temp=P1;
                temp=temp&0xf0;
                delayms(30);
          }
        }
  }
return key;
}
//=====================串口初始化==========================
void uart_init()
{
  AUXR=0x40;                                    //定时器1速度是传统8051的12倍
  TMOD&=0x0f;                                   //设置T1定时器为工作模式0 16位自动装载  
  TL1=0xe0;                                     //设置波特率为9600
  TH1=0xfe;
  SM0=0;                                        //设置SCON 串口模式1 8位UART
  SM1=1;            
  TR1=1;                                                //开启T1定时器
  REN=1;                                                //允许串行接收
  ET1=0;                                                                 //关闭T1定时器
}
//=====================12232初始化==========================
void lcd_init()
{
  delayms(100);
  send_command(0x30);                         //设置8位数据口,基本指令模式
  send_command(0x02);
  send_command(0x06);                                 //整体显示开,游标关,反白关
  send_command(0x0c);                                 //游标及显示右移一位
  send_command(0x01);                                 //写入空格清屏指令
  send_command(0x80);                                 //设定首次显示位置
}
void main()
{
   lcd_init();                                       //lcd初始化
   uart_init();                                  //串口初始化
  while(1)
  {
    num2=matrixkeyscan();
    if(flag==1)
        {
          flag=0;
                  SBUF=num2;
        while(!ti);
        TI=0;  
          send_data(table[num2]);
        }
  }
}
现在矩阵按键肯定没问题了,问题是有时候显示不出来数,或者显示位置不对,比如说我按1 ,有时候在第一个显示,有时候在第二个显示,或者不一定在哪显示了。请大家看看问题出哪了

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
11条回答
武力戡乱
1楼-- · 2019-07-15 19:19
按键释放!
天使一般爱你
2楼-- · 2019-07-15 21:49
 精彩回答 2  元偷偷看……
武力戡乱
3楼-- · 2019-07-15 23:07
天使一般爱你 发表于 2015-5-31 21:21
不是键盘的事。。我把按键发串口了。。发出来的都对。。

        while(temp!=0xf0)
          {
                 temp=P1;
                temp=temp&0xf0;
                delayms(30);
          }
这个算按键等待释放吗?
天使一般爱你
4楼-- · 2019-07-16 02:44
武力戡乱 发表于 2015-5-31 21:26
while(temp!=0xf0)
          {
                 temp=P1;

嗯!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!是的  为啥得超过20字?????
天使一般爱你
5楼-- · 2019-07-16 06:24
武力戡乱 发表于 2015-5-31 21:26
while(temp!=0xf0)
          {
                 temp=P1;

好像是12232的指针出问题了,总乱走了,!! 唉,郁闷死了。。。。。。
武力戡乱
6楼-- · 2019-07-16 10:38
天使一般爱你 发表于 2015-5-31 21:34
好像是12232的指针出问题了,总乱走了,!! 唉,郁闷死了。。。。。。

指针乱走?多少行?说来看看!

一周热门 更多>