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

QQ登錄

只需一步,快速開始

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

AVR單片機(jī)按鍵控制數(shù)碼管

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:77937 發(fā)表于 2015-5-11 20:15 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
AVR單片機(jī)按鍵控制數(shù)碼管+1、-1、我已經(jīng)寫好了,但是遞增跟遞減不會(huì)寫,求教
#include <iom88v.h>   
unsigned char output[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};       //定義一個(gè)有小數(shù)點(diǎn)的數(shù)組給個(gè)位
unsigned char output1[]={0x3f,0x06,0xd5b,0xc4f,0xe66,0xe6d,0x7d,0x07,0x7f,0x6f};  //定義一個(gè)沒有數(shù)組的給百十位
unsigned int shuzhi=100;
unsigned char dispaynum=1;
unsigned int System_Time = 0;  
unsigned char key;
unsigned char flag;
unsigned char flag1;
unsigned char flag2;
unsigned char flag3;
void delay(unsigned int yanshi)
{
        unsigned int x,z;
        for(x=yanshi;x>0;x--)
        {
                for(z=110;z>0;z--);
        }
}
void port_init(void)  //端口初始化
{
   PORTD = 0xff;   
   DDRD  = 0xff;   //端口D輸出
   PORTC = 0xff;   
   DDRC  = 0xff;   //端口C輸出
   PORTB = 0x0f;   
   DDRB  = 0xf0;
}

/*void yy(void)
{
       

        unsigned char i;
         DDRB  = 0xf0;   //端口B輸入
         PORTB = 0x0f;
         i=PINB;
         i&0x0f;
         if(i!=0x0f) key=1;
         else key=0;   
}
*/
void keyy(void)
{
        unsigned int i;
       
//---------------------------------------------K1
    if(!(PINB&(1<<0)))
          {
                 
                  if(!(PINB&(1<<0)))
                          {
                           if(flag==0){shuzhi++;flag=1;}
                      }                       
          }
          if(PINB&(1<<0)){flag=0;}
//-----------------------------------------K2
           if(!(PINB&0x02))
          {
                   if(!(PINB&0x02))
                          {
               if(flag1==0){shuzhi--;flag1=1;}
                          }
          }
          if(PINB&0x02){flag1=0;}
         
//------------------------------------------K3
           if(!(PINB&0x04))
          {
                  delay(2);
                  if(!(PINB&0x04))
                          {
                          if(flag2==0)
                      {
                              while(1)
                                  {shuzhi++;
                                          if(dispaynum==1)
                {
                        PORTD=output[shuzhi%10];  //取數(shù)值的個(gè)位數(shù)
                        PORTC=0xfb;               //使LED3使能
                        dispaynum=2;
                }
    else if(dispaynum==2)
            {
                    PORTD=output1[(shuzhi%100)/10];  //取數(shù)值的十位數(shù)
                    PORTC=0xfd;                     //使LED2使能
                    dispaynum=3;
            }
    else if(dispaynum==3)
            {
                    PORTD=output1[shuzhi/100];      //取數(shù)值的百位數(shù)
                    PORTC=0xfe;                     //使LED1使能
                    dispaynum=1;
            }       
                                 
                               flag2=1;}
                          }
                          }
          }
    if(PINB&0x04){flag2=0;}
}
void dispay (void)   
{
        if(dispaynum==1)
                {
                        PORTD=output[shuzhi%10];  //取數(shù)值的個(gè)位數(shù)
                        PORTC=0xfb;               //使LED3使能
                        dispaynum=2;
                }
    else if(dispaynum==2)
            {
                    PORTD=output1[(shuzhi%100)/10];  //取數(shù)值的十位數(shù)
                    PORTC=0xfd;                     //使LED2使能
                    dispaynum=3;
            }
    else if(dispaynum==3)
            {
                    PORTD=output1[shuzhi/100];      //取數(shù)值的百位數(shù)
                    PORTC=0xfe;                     //使LED1使能
                    dispaynum=1;
            }       
}
void main(void)
{
        port_init();     
       
        //yy();
        while(1)
        {
        dispay();        
        keyy();
        /*System_Time++;       //防止數(shù)據(jù)跳動(dòng)過快,延時(shí)一段時(shí)間數(shù)值再減一
                if (System_Time > 1000)
                {
                        System_Time = 0;
                shuzhi--;
            }
               
                if(shuzhi==0)        //循環(huán)數(shù)值再到999
                {
                        PORTC=0xef;
                        shuzhi=999;        
                }*/
       
        }
       
}

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的積分獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:77937 發(fā)表于 2015-5-11 20:16 | 只看該作者
#include <iom88v.h>   
unsigned char output[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};       //定義一個(gè)有小數(shù)點(diǎn)的數(shù)組給個(gè)位
unsigned char output1[]={0x3f,0x06,0xd5b,0xc4f,0xe66,0xe6d,0x7d,0x07,0x7f,0x6f};  //定義一個(gè)沒有數(shù)組的給百十位
unsigned int shuzhi=100;
unsigned char dispaynum=1;
unsigned int System_Time = 0;  
unsigned char key;
unsigned char flag;
unsigned char flag1;
unsigned char flag2;
unsigned char flag3;
void delay(unsigned int yanshi)
{
        unsigned int x,z;
        for(x=yanshi;x>0;x--)
        {
                for(z=110;z>0;z--);
        }
}
void port_init(void)  //端口初始化
{
   PORTD = 0xff;   
   DDRD  = 0xff;   //端口D輸出
   PORTC = 0xff;   
   DDRC  = 0xff;   //端口C輸出
   PORTB = 0x0f;   
   DDRB  = 0xf0;
}

/*void yy(void)
{
       

        unsigned char i;
         DDRB  = 0xf0;   //端口B輸入
         PORTB = 0x0f;
         i=PINB;
         i&0x0f;
         if(i!=0x0f) key=1;
         else key=0;   
}
*/
void keyy(void)
{
        unsigned int i;
       
//---------------------------------------------K1
    if(!(PINB&(1<<0)))
          {
                 
                  if(!(PINB&(1<<0)))
                          {
                           if(flag==0){shuzhi++;flag=1;}
                      }                       
          }
          if(PINB&(1<<0)){flag=0;}
//-----------------------------------------K2
           if(!(PINB&0x02))
          {
                   if(!(PINB&0x02))
                          {
               if(flag1==0){shuzhi--;flag1=1;}
                          }
          }
          if(PINB&0x02){flag1=0;}
         
//------------------------------------------K3
           if(!(PINB&0x04))
          {
                  delay(2);
                  if(!(PINB&0x04))
                          {
                          if(flag2==0)
                      {
                              while(1)
                                  {shuzhi++;
                                          if(dispaynum==1)
                {
                        PORTD=output[shuzhi%10];  //取數(shù)值的個(gè)位數(shù)
                        PORTC=0xfb;               //使LED3使能
                        dispaynum=2;
                }
    else if(dispaynum==2)
            {
                    PORTD=output1[(shuzhi%100)/10];  //取數(shù)值的十位數(shù)
                    PORTC=0xfd;                     //使LED2使能
                    dispaynum=3;
            }
    else if(dispaynum==3)
            {
                    PORTD=output1[shuzhi/100];      //取數(shù)值的百位數(shù)
                    PORTC=0xfe;                     //使LED1使能
                    dispaynum=1;
            }       
                                 
                               flag2=1;}
                          }
                          }
          }
    if(PINB&0x04){flag2=0;}
}
void dispay (void)   
{
        if(dispaynum==1)
                {
                        PORTD=output[shuzhi%10];  //取數(shù)值的個(gè)位數(shù)
                        PORTC=0xfb;               //使LED3使能
                        dispaynum=2;
                }
    else if(dispaynum==2)
            {
                    PORTD=output1[(shuzhi%100)/10];  //取數(shù)值的十位數(shù)
                    PORTC=0xfd;                     //使LED2使能
                    dispaynum=3;
            }
    else if(dispaynum==3)
            {
                    PORTD=output1[shuzhi/100];      //取數(shù)值的百位數(shù)
                    PORTC=0xfe;                     //使LED1使能
                    dispaynum=1;
            }       
}
void main(void)
{
        port_init();     
       
        //yy();
        while(1)
        {
        dispay();        
        keyy();
        /*System_Time++;       //防止數(shù)據(jù)跳動(dòng)過快,延時(shí)一段時(shí)間數(shù)值再減一
                if (System_Time > 1000)
                {
                        System_Time = 0;
                shuzhi--;
            }
               
                if(shuzhi==0)        //循環(huán)數(shù)值再到999
                {
                        PORTC=0xef;
                        shuzhi=999;        
                }*/
       
        }
       
}

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 共享資料的積分獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

板凳
ID:1 發(fā)表于 2015-5-11 21:47 | 只看該作者
你好  能分享一下電路圖嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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