|  | 【程序】AVR独立键盘 | 
                
          |   一派掌门 二十级 | 
              //这是我编写的#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;
 }
 }
 }
 
 | 
|