led燈 1-99顯示

來源:互聯網
上載者:User

//********************************************************************
/*按一次PICDEMO2實驗板SB3鍵(RB3口),顯示增加1。*/
/*源檔案名稱18F552_PORTB_C18.c
      /*樂的
      led迴圈顯示01-99,選用PIC18F4520器件。*/
      /*使用C18編譯器,MPLAB v8.2調試通過。*/
      /************************************************************************
  Name:          流水燈顯示1—99
  Copyright:     唐湘衡
  Author:        唐湘衡
  Date: 22-10-09 18:46
  Description:
               功能:用c連接埠實現流水燈的1到99顯示
               器件:pic18f4520晶片一片,pic-DEMO工程板一塊,picMCD2模擬器,台式電腦
               MPLAB9.6測試通過
               要求:選用xt晶振,關閉看門狗定時器和低電壓編程
**************************************************************************/
#include<pic18.h>   /*標誌標頭檔*/
     
//********************************************************************
#define uchar unsigned char
#define uint unsigned int
//********************************************************************
void initPORTC();
void initPORTB();
void uinttouchar(uchar x);              //函式宣告
uchar connectbyte(uchar y1,uchar y2);   //合并BCD高、低位元組。
void delay();                           // 聲明按鍵延遲函數。
//********************************************************************
uchar counter;            //按鍵計數。
uchar x1,x2;              //x1是BCD高4位,x2是BCD低4位。
//********************************************************************
void main (void)
{
    initPORTB();
    initPORTC();
    counter = 0;          //設定初值
  while (1)               //永久迴圈掃描鍵盤
  {
    while (1)             //永久迴圈掃描鍵盤
    {
       if(RB3==0)   //是否有鍵按下
      {
         delay();              /*延遲10ms*/
         if(RB3==0)
         {
           counter++;          //索引值增加1
           uinttouchar(counter);     //十六進位數轉化成十進位數     
           PORTC=connectbyte(x1,x2); //合并BCD碼的高、低四位
           break;                    //退出。
          }
      }
    }
    while(1)   
    {
     if(RB3==1)    //按鍵鬆開否?   
     delay();                //延遲10ms。
     if(RB3==1)    //按鍵鬆開。
     break;                  //退出。     
     }
  }
}
//********************************************************************
// 初始化 PORTc。
//********************************************************************
void initPORTC()
{
    TRISC = 0x00;         //設定C口為輸出
    PORTC=0x00;           //清C口熄滅。
}
//********************************************************************
//    初始化 PORTb。
//********************************************************************
void initPORTB()
{
    PORTB=0x00;           //清A口。
    INTCON=0x00;          //關閉所有中斷        
    ADCON1=0x07;          //設定B口為開關量I/O使用。
    TRISB=0xff;           //設定B口為輸入。
    //PORTA=0xff;
}
//********************************************************************
//    延遲10ms。
//********************************************************************
void delay()
{
     uint d=1000;
     while(--d)
          {;}
}
//********************************************************************
//    16進位轉換成10進位。
//********************************************************************
void uinttouchar(uchar x)
{
     uchar mid,y;
     mid=x;
     y=x;
     x2=(uchar)(y%10);
     y=mid;
     x1=(uchar)(((y-x2)%100)/10);
}
//********************************************************************
//    合并位元組。
//********************************************************************
uchar connectbyte(uchar y1,uchar y2)
{
     return(((0xf0)&(y1<<4))|((0x0f)&y2));
}
//********************************************************************

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.