//********************************************************************
/*按一次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));
}
//********************************************************************