⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 动态led与键盘(优).txt

📁 AVR 单片机中动态led显示程序 经测试完全能用
💻 TXT
字号:
/********************************************************
*  函数名称: ①void led(void) 与void                    *
*            ②get_key(其返回值为0xff时无键值)          *
*  函数功能:延时函数			 		                *
*  调用前设全局变量	unsigned char countnum[4]={0,0,0,0};*
*  只需将要显示的数据赋予以上数组即可countnum[0]=1;	    *
********************************************************/
#include <avr/io.h>
#define  	uchar	unsigned char															 
#define 	uint	unsigned int
unsigned char countnum[4]={0,0,0,0};
unsigned const char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};



	 
/****************************************
*  函数名称: void delay_ms(uint i)		*
*  函数功能:延时函数			 		*
*  晶振频率:7.3728MHZ					*
*  入口参数:i mS						*
****************************************/
void delay_us(unsigned int i)
	{
	while(i--)
	 	{
		asm("nop");
		asm("nop");
		}
	}
	 
/****************************************
*  函数名称: void delay_ms(uint i)		*
*  函数功能:延时函数			 		*
*  晶振频率:7.3728MHZ					*
*  入口参数:i mS						*
****************************************/
void delay_ms(unsigned int i)
	{
	unsigned int a;
	for(;i;i--)
		{
		for(a=570;a;a--)
			{;}
		}
	}

//显示函数
void led(void)
{
    unsigned char i;
    static unsigned char lednum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3f};
    //unsigned char countnum[4]={0,0,0,0};
    DDRB=0xff;
    PORTB=0xff;
    DDRD=0x0f;
    PORTD=0x0ff;
//显示计数值
   for(i=0;i<4;i++)
   {
       PORTD&=~(1<<i);
	   if(2==i)
	      PORTB=lednum[countnum[i]]+0x80;
		  else
		      PORTB=lednum[countnum[i]];
          delay_ms(1);
		  PORTD|=0xff;
      }

       //if(countnum[0]==9 && countnum[1]==9 && countnum[2]==9 && countnum[3]==9)
       //  break;
  // }
  //}
}



/*键盘扫描函数 读取当前键盘的状态 有按键按下返回相应按键值 无按键按下返回"0x00"*/
unsigned char key_read(void)
{ 
unsigned char i; 
 DDRC = 0xF0;/*获取列地址*/
 PORTC = 0x0F;
 delay_ms(2);
 i = PINC; 
 DDRC = 0x0f;/*获取行地址*/
 PORTC = 0xF0;
 delay_ms(2);
 i |= PINC; 
 DDRC = 0xff;/*输出复位*/
 PORTC = 0xFF;
// return i; //测试键值用
switch (i)
   {       /*将按键码转换成键值*/ 
  case  0xEE: return 1; 
  case  0xDE: return 2; 
  case  0xBE: return 3;
  case  0x7E: return 4;
  case  0xED: return 5;
  case  0xDD: return 6;
  case  0xBD: return 7;
  case  0x7D: return 8;
  case  0xEB: return 9;
  case  0xDB: return 10;
  case  0xBB: return 11;
  case  0x7B: return 12;
  case  0xE7: return 13;
  case  0xD7: return 14;
  case  0xB7: return 15;
  case  0x77: return 16;
  default : return 0xff; 
     } 
}



/*按键获取函数 获取按键信号,其中包含有状态记录及按键去颤抖。 有正确按键按下返回相应按键值 无正确按键按下返回"0x00"*/
unsigned char get_key(void)
{ 
unsigned char i; 
static unsigned char j;/*按键记录*/ 
i = key_read(); 
if (i == 0xff)
  {/*无有效按键按下*/  
   j = 0x00;/*清除按键记录*/  
   return 0xff;/*程序退出*/  
   } 
if(j==0x00) 
{    /*为新按键*/  
j = i;/*保存本次采样结果*/  
delay_ms(2);/*去按键颤抖*/  
i = key_read();  
if(i == j) 
{   
return i;   
}  
} 
return 0xff;
}




void main(void)
{
int i=0,a1=0,b1=0;
 DDRB=0XFF;
 DDRD=0XFF;
 while(1)
 {
i=get_key();
//i=key_read();//测试键值
if(i==0xff)
{
led();
continue;}
else
{
a1=i;
a1&=0x0f;
//b1=i;
//b1&=0xf0;
//b1=b1>>4;

countnum[0]=countnum[1];
countnum[1]=countnum[2];
countnum[2]=countnum[3];
countnum[3]=a1;
}
led();

 }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -