|
這個是我自己diy的單片機(jī)開發(fā)板:http://www.torrancerestoration.com/bbs/dpj-42590-1.html
- /*
- 為自已寫的獨立鍵盤掃描
- 犯的錯:unsigned密寫成了unsigner一直提示找不到a,b
- PORTD0=1;不行,要寫成PORTD=0x01;才可以,如果一定要寫,應(yīng)寫為RD0=1;
- PORTD=0x03;不是第三個燈亮,而是0000 0011是兩個燈亮
- PORTD=0x8則是0000 1000是第4個燈亮。。。。。出錯因原進(jìn)制轉(zhuǎn)化,剛開始寫程序進(jìn)制轉(zhuǎn)化混掉了。
- */
- #include <pic.h>
- #define uchar unsigned char
- #define uint unsigned int
- __CONFIG(0x3B31);
- const unsigned char table[] = {0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7c,0x7,0x7f,0x6f}; //數(shù)碼管數(shù)組
- void delay(uchar x) //延時函數(shù)
- {
- uint a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
- void scan(); //掃描鍵盤函數(shù) 聲明
- void init() //初始化函數(shù)
- {
- TRISB=0xff;
- TRISD=0x00;
- TRISC=0X00;
- TRISD=0x00;
- PORTC=0Xff;
- PORTD=0x00;
- }
- //-------------------主函數(shù)-----------------------------------------------------------
- void main()
- {
- init ();
- while(1)
- {
- scan(); //掃描鍵盤
- }
- }
- //-------------------------------------------------------------------------------------
- void scan()
- {
- if(RB0==0)
- {
- delay(10);
- while(!RB0) PORTD=0x01;
- }
- if(RB1==0)
- {
- delay(10);
- while(!RB1) PORTD=0x02;
- }
- if(RB2==0)
- {
- delay(10);
- while(!RB2) PORTD=0x04;
- }
- if(RB3==0)
- {
- delay(10);
- while(!RB3) PORTD=0x08;
- }
- }
復(fù)制代碼
|
|