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

📄 mytest.c

📁 avr twi 从接受
💻 C
字号:
/* #include <macros.h>     avr/interrupt.h avr/signal.h为AVR GCC中的头文件*/
#include <avr/io.h>
#include <avr/interrupt.h>
#pragma data:code 
const char table[]={
            0x73,
			0x3f, /*0*/
			0x06, /*1*/
			0x5b, /*2*/
			0x4f, /*3*/
			0x66, /*4*/
			0x6d, /*5*/
			0x7d, /*6*/
			0x07, /*7*/
			0x7f, /*8*/
			0x6f, /*9*/
			0x77, /*A*/
			0x7c, /*B*/
			0x39, /*C*/
			0x5e, /*D*/
			0x79, /*E*/
			0x71  /*F*/
                };

unsigned char LedBuffer[4]={0x3f,
			0x3f, /*0*/
			0x3f, /*1*/
			0x3f, /*2*/};

void LedGetValue(i)
{  int j;
    for(j=0;j<4;j++)
    LedBuffer[j]=table[i++];

}


void Delay(int ms)
{
    int i,j;
	for(i=0;i<ms;i++)
	   {
	   for(j=0;j<110;j++);
       }
}



/*
************************************************************
*名称:unsigned int GetADCValue(viod)
*功能:获取按键ADC值
*参数:无
*返回:按键ADC值
************************************************************
*/
unsigned int GetADCValue(viod)
{
  unsigned int KeyValue=0;
  ADCSRA |= _BV(ADSC); /*ADC开始转换*/

  while(ADCSRA&_BV(ADSC));

  KeyValue = ADCL;
  KeyValue = KeyValue+ADCH*256; /*ADC取数据*/
  
  return KeyValue; 
}

/*
************************************************************
*名称:unsigned char MCUGetKeyValue(viod)
*功能:获取按键值
*参数:无
*返回:按键值
************************************************************
*/
unsigned char MCUGetKeyValue(viod)
{
  unsigned int ADCKeyValue;
  unsigned char MCUKeyValue=0;
  ADCKeyValue = GetADCValue(); 
  
  if(ADCKeyValue != 0)
  {
    Delay(5);
	ADCKeyValue = GetADCValue(); /*获取按键ADC值*/ 
	if(ADCKeyValue < 600)
	 {
	   MCUKeyValue=1; /*MENU*/ 
	 }
	else if(ADCKeyValue <750)
	       {
		     MCUKeyValue = 2; /*OK*/ 
		   }
		 else if(ADCKeyValue < 790)
		       {
		         MCUKeyValue = 3; /*UP*/ 
		       }
			   else if(ADCKeyValue <840)
		             {
		               MCUKeyValue = 4; /*DOWN*/ 
		             }
					 else if(ADCKeyValue < 865)
		                   {
		                     MCUKeyValue = 5; /*LEFT*/ 
		                   }
					       else if(ADCKeyValue < 888) 
		                         {
		                           MCUKeyValue = 6; /*RIGHT*/ 
		                         }
								 else if(ADCKeyValue < 930) 
				                         {
				                           MCUKeyValue = 7; /*RIGHT*/ 
				                          }

  }

  return MCUKeyValue;  
}



int main()
{   
    //unsigned char Wable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
    int i=0,temp=0,Key,temp1=0;
    DDRC |= 0x07;
	DDRB |= 0xFF;
	DDRA |= 0xF0;
	//DDRD |= _BV(PC2);
    //PORTD |= _BV(PC2);
	PORTA |= 0xf0;
		//	Delay(5);
	PORTB = 0x00;
//	cli();

    DDRA &= ~(1<<PA0);  
    PORTA &= ~(1<<PA0); 

    ADMUX = 0X0;  /*ADC0单端输入,ADC参考电压为VREF,内部Vref关闭*/
    ADCSRA |= _BV(ADEN)|_BV(ADPS1)|_BV(ADPS0); /*ADC置位使能,预分频因子为8*/

  

	MCUCR |= _BV(ISC01);
    GICR |= _BV(INT0);   //INT0上升沿下降沿触发
   

	TCCR0 |= (1<<CS00);   /*1/8分频,ATtiny48为 TCCR0A,TIMSK0*/ 
    TIMSK |= (1<<TOIE0);
	   
  	sei();  
    TCNT0 =0;    
	while(1)
	{   
	   
		PORTC |= (1<<PC0);
		Delay(1);
		PORTC |= (1<<PC1);
	    Delay(1);
		PORTC |= (1<<PC2);
	 	Delay(1);
		PORTC &= ~((1<<PC0)|(1<<PC1)|(1<<PC2));
		Delay(1);


		Key=MCUGetKeyValue();
        switch(Key)
		{
		  case 1: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[0];
					break;
		
		  case 2: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[1];
					break;
		  case 3: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[2];
					break;
		  case 4: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[3];
					break;
		  case 5: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[4];
					break;
		  case 6: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[5];
					break;
		  case 7: for(temp1=0;temp1<4;temp1++)
		            LedBuffer[temp1]=table[6];
					break;
          default:break;
		
		}

	 /*  
       if( ++temp>10) 
	   {
		    if(++i<14)
			{
			   LedGetValue(i);
			}
			else
			 i=0; 

          temp=0;

        }
      */
       // else
	//	 LedGetValue(0);
		

     /*   for(i=0;i<4;i++)
         {
		    PORTB |= (1<<i);
	  	    //Delay(1);
			PORTA =0;
			PORTA |= table[i+2];
	        Delay(20);
		
	        PORTB &= ~(1<<i);

         }
      */ 
    }

}


volatile int LedNum,Time=0;
SIGNAL(SIG_OVERFLOW0)
{  
	  if(++Time>20)
	  {
	     //TCNT0 =100;
	     //cli();
	   for(LedNum=0;LedNum<4;LedNum++)
	     {
		    PORTA |= (1<<(LedNum+4));
		//	Delay(5);
		    PORTB =0;
			PORTB |= LedBuffer[LedNum];
	        Delay(20);
	        PORTA &= ~(1<<(LedNum+4));
        
	      }
	    // sei();
	     Time=0;

	   }
}

SIGNAL(SIG_INTERRUPT0)
{
   LedBuffer[0]=0;
   LedBuffer[1]=0x3f; 
   LedBuffer[2]=0x76;
   LedBuffer[3]=0;

  for(Time=0;Time<100;Time++)
    Delay(100); 
			
}

⌨️ 快捷键说明

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