木槿呀 2024-03-24 15:02 采纳率: 72.2%
浏览 3
已结题

蓝桥杯第九届彩灯控制器,按下S5会给选中的模块+1或者+100,但是我这段代码显示在数码管上却不对

蓝桥杯第九届的彩灯控制器,我只写了S6设置界面的功能,当在设置界面时,按下S5会给mode值+1或者给times值加100,但是我这个按下后在数码管上显示不对,mode值是在1和3之间跳变,times值在乱变,为什么啊?
main.c

#include<STC15F2K60S2.H>
#include"hardware.h"
bit sz=0;//设置界面
unsigned char stat=0;//s6
unsigned char flag=0;//数码管闪烁
unsigned char mode=2;
unsigned int times=1200;
//-------------------------------------------------------------------------
void Workingkey()
{
 if(timer10ms>=10)
 {
   timer10ms=0;
   Scankey();
   if(key_value==6 && key_stat==2 )
   {
      sz=1;
      switch(stat)
      {
         case 0:
           stat=1;          
         break;
         case 1:
           stat=2;//运行模式
           timer400ms=0;
         break;
         case 2:
           stat=3;//流转间隔
            timer400ms=0;
         break;
         case 3:
           stat=0;
           sz=0;
         break;
      }
   }
  }
  else if(key_value==5 && key_stat==2 && sz==1)//设置+
  {
    if(stat==2)//运行
    {
      mode++;
      if(mode==5)
        mode=1;
    }
    else if(stat==3)//流转
    {
      if(times==1200)
        times=400;
      else 
        times=times+100;
    }   
  }
}
//---------------------------------------------------------------------------
void SMG_Display()
{

    if(stat==0)
    {
     SMG_buf[0]=16;
     SMG_buf[1]=16;
     SMG_buf[2]=16;
     SMG_buf[3]=16;
     SMG_buf[4]=16;
     SMG_buf[5]=16;
     SMG_buf[6]=16;
     SMG_buf[7]=16;
    }
  if(sz==1)
    {
     if(stat==1)
     {
     SMG_buf[0]=17;
     SMG_buf[1]=mode;
     SMG_buf[2]=17;
     SMG_buf[3]=16;
      if(times/1000==0)
       {
        SMG_buf[4]=16;
        SMG_buf[5]=times/100;
        SMG_buf[6]=times%100/10;
        SMG_buf[7]=times%10;
       }
      else if(times/1000!=0)
      {
        SMG_buf[4]=times/1000;
        SMG_buf[5]=times%1000/100;
        SMG_buf[6]=times%100/10;
        SMG_buf[7]=times%10; 
      }
     }
     else if(stat==2)
     {
     SMG_buf[0]=17;
     SMG_buf[2]=17;
     SMG_buf[3]=16;
     SMG_buf[4]=16;
     SMG_buf[5]=times/100;
     SMG_buf[6]=times%100/10;
     SMG_buf[7]=times%10;
     }
     else if(stat==3)
     {
     SMG_buf[0]=17;
     SMG_buf[1]=mode;
     SMG_buf[2]=17;
     SMG_buf[3]=16;
     SMG_buf[4]=16;
     }
  }
}
//---------------------------------------------------------------------------
void SMG_ss()
{
  if(sz==1)
  {
    if(stat==2)
    {
      if(bz400ms==1)
      {
       bz400ms=0;
       switch(flag)
       {
         case 0:
           SMG_buf[1]=16;
           flag=1;
         break;
         case 1:
           SMG_buf[1]=mode;
           flag=0;
         break;
       }
      }
    }
    else if(stat==3)
    {
      if(bz400ms==1)
      {
       bz400ms=0;
       switch(flag)
       {
         case 0:
           SMG_buf[4]=16; 
           SMG_buf[5]=16;
           SMG_buf[6]=16;
           SMG_buf[7]=16;
           flag=1;
         break;
         case 1:
         if(times/1000==0)
         {
           SMG_buf[5]=times/100;
           SMG_buf[6]=times%100/10;
           SMG_buf[7]=times%10;
         }
         else if(times/1000!=0)
         {
          SMG_buf[4]=times/1000;
          SMG_buf[5]=times%1000/100;
          SMG_buf[6]=times%100/10;
          SMG_buf[7]=times%10;  
         }
           flag=0;
         break;
       }
      } 
    }
  }
}
//----------------------------------------------------------------------------
void main()
{
  led_output();
  ws_output();
  Timer1Init();
  while(1)
  {
    Workingkey();
    SMG_Display();
    SMG_ss(); 
  }
}

hardware.c

#include<STC15F2K60S2.H>
#include"hardware.h"
/*************  本地常量声明    **************/
unsigned char code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码
unsigned char led_buf=0xff;
unsigned char ws_buf=0x00;
unsigned char SMG_buf[]={16,16,16,16,16,16,16,16};
unsigned int timer10ms=0;
unsigned int timer400ms=0;
bit bz400ms=0;
unsigned char key_stat=0;
unsigned char key_value=0;
//----------------------------------------------------------------------
void Timer1Init()        //1毫秒@12.000MHz
{
    AUXR |= 0x40;        //定时器时钟1T模式
    TMOD &= 0x0F;        //设置定时器模式
    TL1 = 0x20;        //设置定时初值
    TH1 = 0xD1;        //设置定时初值
    TF1 = 0;        //清除TF1标志
    TR1 = 1;        //定时器1开始计时
    ET1=1;
    EA=1;
}
//---------------------------------------------------------------------
void SelectHC573(unsigned char channel)
{
    switch(channel)
    {
       case 4:
         P2=(P2 & 0x1f)|0x80;
       break;
       case 5:
         P2=(P2 & 0x1f)|0xa0;
       break;
       case 6:
         P2=(P2 & 0x1f)|0xc0;
       break;
       case 7:
         P2=(P2 & 0x1f)|0xe0;
       break;
       case 0:
         P2=(P2 & 0x1f)|0x00;
       break;
    }
}
//---------------------------------------------------------------------
void led_output()
{
       
    P0=led_buf;
    SelectHC573(4);
    SelectHC573(0);
}
//---------------------------------------------------------------------
void ws_output()
{
   
    P0=ws_buf;
    SelectHC573(5);
    SelectHC573(0);
}
//---------------------------------------------------------------------
void SMG_output()
{
   static unsigned char SMG_com=0;

  
    P0=0x00;
     SelectHC573(6);
    SelectHC573(0);

    
    P0=T_COM[SMG_com];
    SelectHC573(6);
    SelectHC573(0);

    
    P0=~t_display[SMG_buf[SMG_com]];
    SelectHC573(7);
    SelectHC573(0);

    if(++SMG_com>=8)
    {
      SMG_com=0;
    }
}
//---------------------------------------------------------------------
void ServiceTimer()interrupt 3
{
   SMG_output();
   timer10ms++;
   timer400ms++;
   if(timer400ms>=400)
   {
     timer400ms=0;
     bz400ms=1;
   }
}
//---------------------------------------------------------------------
void Scankey()
{
  P3|=0x0f;
  if(~P3&0x0f)
  {
     if(key_stat<255)
       key_stat++;
     if(P33==0)
       key_value=4;
     if(P32==0)
       key_value=5;
     if(P31==0)
       key_value=6;
     if(P30==0)
       key_value=7;
  }
  else 
  {
    key_stat=0;
    key_value=0;
  }
}

  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2024-03-24 15:03
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据你提供的代码,我发现一些可能的问题。首先,mode 变量在 case 2: 的情况下应该是在1和2之间变化,而不是1和3。这可能是由于你在代码中使用了 mode++ 后没有做边界检查。其次,times 的增加逻辑看起来是正确的,但数码管显示部分可能存在逻辑错误。

    SMG_Display() 函数中,对于 times 的显示部分,你有两个条件分支,一个是 stat==2,另一个是 stat==3。但是,在 stat==2 的情况下,你没有处理 times 是1000的倍数的情况。这可能导致在 times 达到1200后,数码管显示不正确。

    修改后的 SMG_Display() 函数如下:

    void SMG_Display()
    {
      if(stat==0)
      {
        SMG_buf[0]=16;
        SMG_buf[1]=16;
        SMG_buf[2]=16;
        SMG_buf[3]=16;
        SMG_buf[4]=16;
        SMG_buf[5]=16;
        SMG_buf[6]=16;
        SMG_buf[7]=16;
      }
      if(sz==1)
      {
        if(stat==1)
        {
          SMG_buf[0]=17;
          SMG_buf[1]=mode;
          SMG_buf[2]=17;
          SMG_buf[3]=16;
          if(times/1000==0)
          {
            SMG_buf[4]=16;
            SMG_buf[5]=times/100;
            SMG_buf[6]=times%100/10;
            SMG_buf[7]=times%10;
          }
          else if(times/1000!=0)
          {
            SMG_buf[4]=times/1000;
            SMG_buf[5]=times%1000/100;
            SMG_buf[6]=times%100/10;
            SMG_buf[7]=times%10; 
          }
        }
        else if(stat==2) // 这里添加一个对times的显示处理
        {
          SMG_buf[0]=17;
          SMG_buf[2]=17;
          SMG_buf[3]=16;
          SMG_buf[4]=16;
          SMG_buf[5]=times/100;
          SMG_buf[6]=times%100/10;
          SMG_buf[7]=times%10;
        }
        else if(stat==3)
        {
          SMG_buf[0]=17;
          SMG_buf[1]=mode;
          SMG_buf[2]=17;
          SMG_buf[3]=16;
          SMG_buf[4]=16;
        }
      }
    }
    

    请注意,这里我没有实际运行你的代码,所以可能还需要你进一步调试。确保 mode 变量在适当的地方进行边界检查(例如,if (mode == 5) mode = 1;),以确保它在1和2之间正确切换。同时,对于 times 的显示,确保所有可能的情况都得到了处理。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月1日
  • 已采纳回答 3月24日
  • 创建了问题 3月24日

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大