欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2319|回復(fù): 6
打印 上一主題 下一主題
收起左側(cè)

求大神幫忙看一下單片機(jī)程序,為什么按下keyK后不開始倒數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:816162 發(fā)表于 2020-9-20 23:34 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
150黑幣
#include <reg51.h>
#include <math.h>
#include <intrins.h>
#define uchar unsigned char                                      
#define uint unsigned int                              
#define ulong unsigned long
#define CH451_NOP              0x0000                                   // 空操作
#define CH451_RESET     0x0201                                   // 復(fù)位
#define CH451_LEVEL              0x0100                                   // 加載光柱值,需另加7位數(shù)據(jù)
#define CH451_CLR_BIT       0x0180                                   // 段位清0,需另加6位數(shù)據(jù)
#define CH451_SET_BIT       0x01C0                                   // 段位置1,需另加6位數(shù)據(jù)
#define CH451_SLEEP              0x0202                                   // 進(jìn)入睡眠狀態(tài)
#define CH451_LEFTMOV   0x0300                          // 設(shè)置移動(dòng)方式-左移
#define CH451_LEFTCYC   0x0301                          // 設(shè)置移動(dòng)方式-左循環(huán)
#define CH451_RIGHTMOV  0x0302                          // 設(shè)置移動(dòng)方式-右移
#define CH451_RIGHTCYC  0x0303                          // 設(shè)置移動(dòng)方式-右循環(huán)      
#define CH451_SELF_BCD       0x0380                                   // 自定義BCD碼,需另加7位數(shù)據(jù)
#define CH451_SYSOFF    0x0400                                   // 關(guān)閉顯示、關(guān)閉鍵盤
#define CH451_SYSON1    0x0401                                   // 開啟顯示
#define CH451_SYSON2    0x0403                                   // 開啟顯示、鍵盤
#define CH451_SYSON2W   0x0423                                   // 開啟顯示、鍵盤, 真正2線接口
#define CH451_NO_BCD    0x0500                                   // 設(shè)置默認(rèn)顯示方式,可另加3位掃描極限
#define CH451_BCD       0x0580                                   // 設(shè)置BCD譯碼方式,可另加3位掃描極限
#define CH451_TWINKLE   0x0600                          // 設(shè)置閃爍控制,需另加8位數(shù)據(jù)
#define CH451_GET_KEY       0x0700                                   // 獲取按鍵,返回按鍵代碼
#define CH451_DIG0      0x0800                                   // 數(shù)碼管位0顯示,需另加8位數(shù)據(jù)
#define CH451_DIG1      0x0900                          // 數(shù)碼管位1顯示,需另加8位數(shù)據(jù)
#define CH451_DIG2      0x0a00                          // 數(shù)碼管位2顯示,需另加8位數(shù)據(jù)
#define CH451_DIG3      0x0b00                          // 數(shù)碼管位3顯示,需另加8位數(shù)據(jù)
#define CH451_DIG4      0x0c00                          // 數(shù)碼管位4顯示,需另加8位數(shù)據(jù)
#define CH451_DIG5      0x0d00                                   // 數(shù)碼管位5顯示,需另加8位數(shù)據(jù)
#define CH451_DIG6      0x0e00                                   // 數(shù)碼管位6顯示,需另加8位數(shù)據(jù)
#define CH451_DIG7      0x0f00                          // 數(shù)碼管位7顯示,需另加8位數(shù)據(jù)1
sbit   CH451_DCLK = P2^4;                      // 串行數(shù)據(jù)時(shí)鐘上升沿激活
sbit   CH451_DIN = P2^5;                        // 串行數(shù)據(jù)輸出,接CH451的數(shù)據(jù)輸入
sbit   CH451_LOAD = P2^6;                     // 串行命令加載,上升沿激活
sbit   CH451_DOUT = P2^7;                     // INT1,鍵盤中斷和鍵值數(shù)據(jù)輸入,接CH451的數(shù)據(jù)輸出
sfr P5          =   0xC8;   //xxxx,1111 端口5
sfr P4          =   0xC0;   //1111,1111 端口4
sbit sp=P4^5;  //蜂鳴器
uint Data=9999;      // 顯示初值,Data的值決定開機(jī)顯示數(shù)值
uchar count = 0;
uint  z = 0;
uint temp = 0;
uchar  ch451_key=0;                 // 存放鍵盤中斷中讀取的鍵值
uchar  key=0x00;
code uchar table[]={0xBD,0x18,0xD5,                 //0,1,2,
                       0xD9,0x78,0xE9,                //3,4,5,
                       0xED,0x98,0xFD,0xF9};  //6,7,8,9
void speaker(void)//蜂鳴器響起
{
uint i,j;
for(j=0;j<250;j++)
{
  for(i=0;i<2000;i++)
  {
    sp=0;
  }
  for(i=0;i<3000;i++)
  {
    sp=1;
  }
}
  
   sp=1;
}
void delayms(uint xms)  //延時(shí)子程序
{
   uint i,j;
   for(i=xms;i>0;i--)
   for(j=110;j>0;j--);        //延時(shí)
}
/*****************寫命令時(shí)序************************************/
void CH451_write(uint command) //定義一無符號(hào)整型變量存儲(chǔ)12字節(jié)的命令字
{
  uchar i;     
  CH451_LOAD=0;   
    for(i=0;i<12;i++)
         {                             
            CH451_DIN = command&1;                                
            CH451_DCLK =0;
            command>>=1;
            CH451_DCLK=1;                             
            delayms(10);
         }
    CH451_LOAD=1;
}
/*****************初始化子程序************************************/
void CH451_init()     
{
    CH451_write(CH451_RESET);    //CH451復(fù)位
    CH451_write(CH451_SYSON2 );//開啟顯示、鍵盤
    CH451_write(CH451_NO_BCD);// 設(shè)置默認(rèn)顯示方式
    CH451_write(CH451_TWINKLE);            //設(shè)置閃爍控制--正常顯示
}
/*****************顯示數(shù)碼管************************************/
void display()        
{
temp = Data / 1000;
CH451_write(CH451_DIG4 | table[temp]);
temp = (Data / 100)%10;
CH451_write(CH451_DIG5 | table[temp]);
temp = (Data / 10)%10;
CH451_write(CH451_DIG6 | table[temp]);
temp = Data % 10;
CH451_write(CH451_DIG7 | table[temp]);
}
/********************讀取按鍵中斷子程序*****************************/

void gerkey()            
{   
  uchar i;                                                                   //定義循環(huán)變量
  uchar command,keycode;                                         //定義控制字寄存器,和中間變量定時(shí)器
  command=0x07;                                                    //讀取鍵值命令的高4位0111B
  CH451_LOAD=0;                                                    //命令開始
  for(i=0;i<4;i++)
   {
    CH451_DIN=command&1;                                        //低位在前,高位在后
    CH451_DCLK=0;
    command>>=1;                                                          //右移一位
    CH451_DCLK=1;                                                   //產(chǎn)生時(shí)鐘上升沿鎖通知CH451輸入位數(shù)據(jù)
   }
    CH451_LOAD=1;                                                   //產(chǎn)生加載上升沿通知CH451處理命令數(shù)據(jù)
    keycode=0;                                                           //清除keycode
    for(i=0;i<7;i++)
     {
    keycode<<=1;                                                   //數(shù)據(jù)作移一位,高位在前,低位在后
    keycode|=CH451_DOUT;                                          //從高到低讀入451的數(shù)據(jù)
    CH451_DCLK=0;                                                   //產(chǎn)生時(shí)鐘下升沿通知CH451輸出下一位
    CH451_DCLK=1;
     }
  ch451_key=keycode;                                                 //保存上次的鍵值
}
/********************按鍵掃描子程序*****************************/
void keyscan()  //鍵盤掃描子程序
{   
   while(1)
     {
       gerkey();
       switch(ch451_key)//按鍵判斷
            {
                 case 0x47://keyG
                  {
                       Data++; //Data+1
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                       display();
                      while(ch451_key==0x47) gerkey();
                   }break;      
                case  0x46://keyS
                  {
                       Data=Data+10; //Data+10
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                        display();
                       while(ch451_key==0x46) gerkey();
                   }break;   
               case  0x45://keyB
                  {
                       Data=Data+100; //Data+100
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                        display();
                       while(ch451_key==0x45)gerkey();
                   }break;   
             case 0x44://keyQ
                  {
                       Data=Data+1000; //Data+1000
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                       display();
                       while(ch451_key==0x44)gerkey();
                   } break;  
             case 0x4c://keyF
                  {
                     Data=0;   //復(fù)位,顯示0000
                     display();
                     while(ch451_key==0x4c)gerkey();
                  }break;
              case 0x4d://keyU
                {
                z=1;
                while(ch451_key==0x4e)gerkey();
                } break;
              case  0x4e://keyK
                  while(ch451_key==0x4e)gerkey();
            break;
         }
    }
}
void start_timer0()                     //開中斷子程序
{
    TMOD=0x01;                         //定時(shí)器0工作方式1     
    EA = 1;  //開啟中斷總開關(guān)
    TR0 = 1;//開啟T0
    ET0 = 1;//打開T0中斷開關(guān)
}
void stop_timer0()                     //關(guān)中斷子程序
{
    TR0 = 0;
    ET0 = 0;
    TF0 = 0;//定時(shí)器/計(jì)數(shù)器0的溢出中斷標(biāo)志位,當(dāng)T0從初值開始加一計(jì)數(shù)到產(chǎn)生溢出時(shí),由硬件使TF0置1,直到CPU響應(yīng)中斷由硬件復(fù)位
}

void timer0() interrupt 1    //倒計(jì)時(shí)中斷程序timer0,不用調(diào)用stop_timer0函數(shù),TF0會(huì)自動(dòng)置0,interrupt 后面的數(shù)字表示使用的寄存器組
{
      TH0=(65536-9216)/256;  //50ms產(chǎn)生一次中斷   
      TL0=(65536-9216)%256;   
      while (Data!=0)
            {
    if(z==1)//單位為分
      {
          display();
                         count++;
                         if(count==1200)
                            {
                                count=0;
                 Data--;
              }
       }
    else//默認(rèn)單位為秒
        {
            display();
                           count++;
                           if(count==20)
                               {
     count=0;
     Data--;   
                 }
        }
            }
}

void main()
{     
      CH451_init();                         //數(shù)碼管中斷初始化
       display();
      while(1)
          {
              gerkey();
              keyscan();        //鍵盤輸入初值
              stop_timer0();
              start_timer0();
              TR0=0;
              if(Data == 0)
                  {
      speaker();  
                    }
          }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:213173 發(fā)表于 2020-9-21 06:18 | 只看該作者
由于按鍵函數(shù)里用了死循環(huán)while(1)就再也出不來了。
回復(fù)

使用道具 舉報(bào)

板凳
ID:342822 發(fā)表于 2020-9-21 07:27 | 只看該作者
把大量的中文“;”替換為英文";"
以下已替換:
/* Main.c file generated by New Project wizard
*
* Created:   周日 9月 20 2020
* Processor: AT89C52
* Compiler:  Keil for 8051
*/

#include <reg51.h>
#include <math.h>
#include <intrins.h>
#define uchar unsigned char                                      
#define uint unsigned int                              
#define ulong unsigned long
#define CH451_NOP              0x0000                                   // 空操作
#define CH451_RESET     0x0201                                   // 復(fù)位
#define CH451_LEVEL              0x0100                                   // 加載光柱值,需另加7位數(shù)據(jù)
#define CH451_CLR_BIT       0x0180                                   // 段位清0,需另加6位數(shù)據(jù)
#define CH451_SET_BIT       0x01C0                                   // 段位置1,需另加6位數(shù)據(jù)
#define CH451_SLEEP              0x0202                                   // 進(jìn)入睡眠狀態(tài)
#define CH451_LEFTMOV   0x0300                          // 設(shè)置移動(dòng)方式-左移
#define CH451_LEFTCYC   0x0301                          // 設(shè)置移動(dòng)方式-左循環(huán)
#define CH451_RIGHTMOV  0x0302                          // 設(shè)置移動(dòng)方式-右移
#define CH451_RIGHTCYC  0x0303                          // 設(shè)置移動(dòng)方式-右循環(huán)      
#define CH451_SELF_BCD       0x0380                                   // 自定義BCD碼,需另加7位數(shù)據(jù)
#define CH451_SYSOFF    0x0400                                   // 關(guān)閉顯示、關(guān)閉鍵盤
#define CH451_SYSON1    0x0401                                   // 開啟顯示
#define CH451_SYSON2    0x0403                                   // 開啟顯示、鍵盤
#define CH451_SYSON2W   0x0423                                   // 開啟顯示、鍵盤, 真正2線接口
#define CH451_NO_BCD    0x0500                                   // 設(shè)置默認(rèn)顯示方式,可另加3位掃描極限
#define CH451_BCD       0x0580                                   // 設(shè)置BCD譯碼方式,可另加3位掃描極限
#define CH451_TWINKLE   0x0600                          // 設(shè)置閃爍控制,需另加8位數(shù)據(jù)
#define CH451_GET_KEY       0x0700                                   // 獲取按鍵,返回按鍵代碼
#define CH451_DIG0      0x0800                                   // 數(shù)碼管位0顯示,需另加8位數(shù)據(jù)
#define CH451_DIG1      0x0900                          // 數(shù)碼管位1顯示,需另加8位數(shù)據(jù)
#define CH451_DIG2      0x0a00                          // 數(shù)碼管位2顯示,需另加8位數(shù)據(jù)
#define CH451_DIG3      0x0b00                          // 數(shù)碼管位3顯示,需另加8位數(shù)據(jù)
#define CH451_DIG4      0x0c00                          // 數(shù)碼管位4顯示,需另加8位數(shù)據(jù)
#define CH451_DIG5      0x0d00                                   // 數(shù)碼管位5顯示,需另加8位數(shù)據(jù)
#define CH451_DIG6      0x0e00                                   // 數(shù)碼管位6顯示,需另加8位數(shù)據(jù)
#define CH451_DIG7      0x0f00                          // 數(shù)碼管位7顯示,需另加8位數(shù)據(jù)1
sbit   CH451_DCLK = P2^4;                      // 串行數(shù)據(jù)時(shí)鐘上升沿激活
sbit   CH451_DIN = P2^5;                        // 串行數(shù)據(jù)輸出,接CH451的數(shù)據(jù)輸入
sbit   CH451_LOAD = P2^6;                     // 串行命令加載,上升沿激活
sbit   CH451_DOUT = P2^7;                     // INT1,鍵盤中斷和鍵值數(shù)據(jù)輸入,接CH451的數(shù)據(jù)輸出
sfr P5          =   0xC8;   //xxxx,1111 端口5
sfr P4          =   0xC0;   //1111,1111 端口4
sbit sp=P4^5;  //蜂鳴器
uint Data=9999;      // 顯示初值,Data的值決定開機(jī)顯示數(shù)值
uchar count = 0;
uint  z = 0;
uint temp = 0;
uchar  ch451_key=0;                 // 存放鍵盤中斷中讀取的鍵值
uchar  key=0x00;
code uchar table[]={0xBD,0x18,0xD5,                 //0,1,2,
                       0xD9,0x78,0xE9,                //3,4,5,
                       0xED,0x98,0xFD,0xF9};  //6,7,8,9
void speaker(void)//蜂鳴器響起
{
uint i,j;
for(j=0;j<250;j++)
{
  for(i=0;i<2000;i++)
  {
    sp=0;
  }
  for(i=0;i<3000;i++)
  {
    sp=1;
  }
}
  
   sp=1;
}
void delayms(uint xms)  //延時(shí)子程序
{
   uint i,j;
   for(i=xms;i>0;i--)
   for(j=110;j>0;j--);        //延時(shí)
}
/*****************寫命令時(shí)序************************************/
void CH451_write(uint command) //定義一無符號(hào)整型變量存儲(chǔ)12字節(jié)的命令字
{
  uchar i;     
  CH451_LOAD=0;   
    for(i=0;i<12;i++)
         {                             
            CH451_DIN = command&1;                                
            CH451_DCLK =0;
            command>>=1;
            CH451_DCLK=1;                             
            delayms(10);
         }
    CH451_LOAD=1;
}
/*****************初始化子程序************************************/
void CH451_init()     
{
    CH451_write(CH451_RESET);    //CH451復(fù)位
    CH451_write(CH451_SYSON2 );//開啟顯示、鍵盤
    CH451_write(CH451_NO_BCD);// 設(shè)置默認(rèn)顯示方式
    CH451_write(CH451_TWINKLE);            //設(shè)置閃爍控制--正常顯示
}
/*****************顯示數(shù)碼管************************************/
void display()        
{
temp = Data / 1000;
CH451_write(CH451_DIG4 | table[temp]);
temp = (Data / 100)%10;
CH451_write(CH451_DIG5 | table[temp]);
temp = (Data / 10)%10;
CH451_write(CH451_DIG6 | table[temp]);
temp = Data % 10;
CH451_write(CH451_DIG7 | table[temp]);
}
/********************讀取按鍵中斷子程序*****************************/

void gerkey()            
{   
  uchar i;                                                                   //定義循環(huán)變量
  uchar command,keycode;                                         //定義控制字寄存器,和中間變量定時(shí)器
  command=0x07;                                                    //讀取鍵值命令的高4位0111B
  CH451_LOAD=0;                                                    //命令開始
  for(i=0;i<4;i++)
   {
    CH451_DIN=command&1;                                        //低位在前,高位在后
    CH451_DCLK=0;
    command>>=1;                                                          //右移一位
    CH451_DCLK=1;                                                   //產(chǎn)生時(shí)鐘上升沿鎖通知CH451輸入位數(shù)據(jù)
   }
    CH451_LOAD=1;                                                   //產(chǎn)生加載上升沿通知CH451處理命令數(shù)據(jù)
    keycode=0;                                                           //清除keycode
    for(i=0;i<7;i++)
     {
    keycode<<=1;                                                   //數(shù)據(jù)作移一位,高位在前,低位在后
    keycode|=CH451_DOUT;                                          //從高到低讀入451的數(shù)據(jù)
    CH451_DCLK=0;                                                   //產(chǎn)生時(shí)鐘下升沿通知CH451輸出下一位
    CH451_DCLK=1;
     }
  ch451_key=keycode;                                                 //保存上次的鍵值
}
/********************按鍵掃描子程序*****************************/
void keyscan()  //鍵盤掃描子程序
{   
   while(1)
     {
       gerkey();
       switch(ch451_key)//按鍵判斷
            {
                 case 0x47://keyG
                  {
                       Data++; //Data+1
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                       display();
                      while(ch451_key==0x47) gerkey();
                   }break;      
                case  0x46://keyS
                  {
                       Data=Data+10; //Data+10
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                        display();
                       while(ch451_key==0x46) gerkey();
                   }break;   
               case  0x45://keyB
                  {
                       Data=Data+100; //Data+100
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                        display();

                       while(ch451_key==0x45)gerkey();
                   }break;   
             case 0x44://keyQ
                  {
                       Data=Data+1000; //Data+1000
                       if(Data>=10000)   //限制Data的取值范圍
                            {   
                                Data=9999;  //最大顯示9999
                            }
                       display();
                       while(ch451_key==0x44)gerkey();
                   } break;  
             case 0x4c://keyF
                  {
                     Data=0;   //復(fù)位,顯示0000
                     display();
                     while(ch451_key==0x4c)gerkey();
                  }break;
              case 0x4d://keyU
                {
                z=1;
                while(ch451_key==0x4e)gerkey();
                } break;
              case  0x4e://keyK
                  while(ch451_key==0x4e)gerkey();
            break;
         }
    }
}
void start_timer0()                     //開中斷子程序
{
    TMOD=0x01;                         //定時(shí)器0工作方式1     
    EA = 1;  //開啟中斷總開關(guān)
    TR0 = 1;//開啟T0
    ET0 = 1;//打開T0中斷開關(guān)
}
void stop_timer0()                     //關(guān)中斷子程序
{
    TR0 = 0;
    ET0 = 0;
    TF0 = 0;//定時(shí)器/計(jì)數(shù)器0的溢出中斷標(biāo)志位,當(dāng)T0從初值開始加一計(jì)數(shù)到產(chǎn)生溢出時(shí),由硬件使TF0置1,直到CPU響應(yīng)中斷由硬件復(fù)位
}

void timer0() interrupt 1    //倒計(jì)時(shí)中斷程序timer0,不用調(diào)用stop_timer0函數(shù),TF0會(huì)自動(dòng)置0,interrupt 后面的數(shù)字表示使用的寄存器組
{
      TH0=(65536-9216)/256;  //50ms產(chǎn)生一次中斷   
      TL0=(65536-9216)%256;   
      while (Data!=0)
            {
    if(z==1)//單位為分
      {
          display();
                         count++;
                         if(count==1200)
                            {
                                count=0;
                 Data--;
              }
       }
    else//默認(rèn)單位為秒
        {
            display();
                           count++;
                           if(count==20)
                               {
     count=0;
     Data--;   
                 }
        }
            }
}

void main()
{     
      CH451_init();                         //數(shù)碼管中斷初始化
       display();
      while(1)
          {
              gerkey();
              keyscan();        //鍵盤輸入初值
              stop_timer0();
              start_timer0();
              TR0=0;
              if(Data == 0)
                  {
      speaker();  
                    }
          }
}
回復(fù)

使用道具 舉報(bào)

地板
ID:584814 發(fā)表于 2020-9-21 17:11 | 只看該作者
那么多的中文符號(hào)居然能通過編譯
回復(fù)

使用道具 舉報(bào)

5#
ID:816162 發(fā)表于 2020-9-21 21:44 | 只看該作者
taotie 發(fā)表于 2020-9-21 07:27
把大量的中文“;”替換為英文";"
以下已替換:
/* Main.c file generated by New Project w ...

那無法倒數(shù)的部分怎么解決啊
回復(fù)

使用道具 舉報(bào)

6#
ID:816162 發(fā)表于 2020-9-21 21:45 | 只看該作者
wulin 發(fā)表于 2020-9-21 06:18
由于按鍵函數(shù)里用了死循環(huán)while(1)就再也出不來了。

那應(yīng)該是怎么解決啊
回復(fù)

使用道具 舉報(bào)

7#
ID:213173 發(fā)表于 2020-9-22 07:05 | 只看該作者
jyhbdhs 發(fā)表于 2020-9-21 21:45
那應(yīng)該是怎么解決啊

樓主的程序硬傷太多,三言兩語無法說清,網(wǎng)上示例很多,可自行搜索參考。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表