|
//這是我編寫的 #include <iom16v.h> #include <macros.h> unsigned char num=0; unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; void delay_ms(unsigned int k) { unsigned int i,j; for (i=0;i<k;i++) for (j=0;j<1140;j++); } void delay_500us() { unsigned int j; for (j=0;j<570;j++); } void Beep() { unsigned int i; for (i=0;i<300;i++) { PORTD=~BIT(6); delay_500us(); PORTD=0xff; delay_500us(); } } void main() { DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //數碼管和蜂鳴器為輸出 PORTB|=BIT(4); //矩陣鍵盤為輸入(帶上拉) DDRB&=~BIT(4); //數碼管顯示0 PORTC=~BIT(7); //7為右邊最後一位數碼管 PORTA=s8[0]; while (1) { if ((PINB&BIT(4)&0x10)==0) //必須打括號!!! { delay_ms(15); if ((PINB&BIT(4)&0x10)==0) { Beep(); num++; if (num==16) num=0; } while (!(PINB&BIT(4)&0x10)); } PORTA=s8[num]; //顯示到數碼管上 } }
|
|
#include <iom16v.h> #include <macros.h> unsigned long num=0; unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; void delay_ms(unsigned int k) { unsigned int i,j; for (i=0;i<k;i++) for (j=0;j<1140;j++); } void delay_500us() { unsigned int j; for (j=0;j<570;j++); } void Beep() { unsigned int i; for (i=0;i<300;i++) { PORTD=~BIT(6); delay_500us(); PORTD=0xff; delay_500us(); } } unsigned long power(unsigned char times) { unsigned long num=10; unsigned char i; if (times==0) return 1; else { for (i=1;i<times;i++) num*=10; return num; } } void main() { unsigned char i; DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //數碼管和蜂鳴器為輸出 PORTB|=BIT(4); //矩陣鍵盤為輸入(帶上拉) DDRB&=~BIT(4); //數碼管顯示0 //PORTC=~BIT(7); //7為右邊最後一位數碼管 //PORTA=s8[0]; while (1) { if ((PINB&BIT(4)&0x10)==0) //必須打括號!!! { delay_ms(15); if ((PINB&BIT(4)&0x10)==0) { Beep(); num++; if (num>99999999) num=0; } while (!(PINB&BIT(4)&0x10)); } //PORTA=s8[num]; //顯示到數碼管上 for (i=0;i<8;i++) //i+1為從右數的位數 { PORTC=~BIT(7-i); PORTA=s8[num%power(i+1)/power(i)]; delay_ms(1); PORTC=0xff; PORTA=0xff; } } }
這是改進版,可以顯示8位數字
|
|
|
|
那個&0x10可以改成:
if ((PINB&BIT(4)&BIT(4))==0)
|
|
回復:4樓
if ((PINB&BIT(4)/*&BIT(4)*/)==0)
少與一次bit4也行
看來視頻教程忘了#define K5裡面已經與了一次了
|
|
以後要判斷某一位是多少直接PINn&BIT(n)就行了
用不着與兩次
|
|
由於蜂鳴器響的時候會耽誤時間,所以這裡用不着加按鍵消抖程序。
#include <iom16v.h> #include <macros.h> unsigned long num=0; unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; void delay_ms(unsigned int k) { unsigned int i,j; for (i=0;i<k;i++) for (j=0;j<1140;j++); } void delay_500us() { unsigned int j; for (j=0;j<570;j++); } void Beep() { unsigned int i; for (i=0;i<300;i++) { PORTD=~BIT(6); delay_500us(); PORTD=0xff; delay_500us(); } } unsigned long power(unsigned char times) { unsigned long num=10; unsigned char i; if (times==0) return 1; else { for (i=1;i<times;i++) num*=10; return num; } } void main() { unsigned char i; DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //數碼管和蜂鳴器為輸出 PORTB|=BIT(4); //矩陣鍵盤為輸入(帶上拉) DDRB&=~BIT(4); //數碼管顯示0 //PORTC=~BIT(7); //7為右邊最後一位數碼管 //PORTA=s8[0]; while (1) { if ((PINB&BIT(4)/*&BIT(4)*/)==0) //必須打括號!!! { //delay_ms(15); //if ((PINB&BIT(4)/*&BIT(4)*/)==0) //{ Beep(); num++; if (num>99999999) num=0; //} //while (!(PINB&BIT(4)/*&BIT(4)*/)); } //PORTA=s8[num]; //顯示到數碼管上 for (i=0;i<8;i++) //i+1為從右數的位數 { PORTC=~BIT(7-i); PORTA=s8[num%power(i+1)/power(i)]; delay_ms(1); PORTC=0xff; PORTA=0xff; } } }
|