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

📄 tex1.c

📁 用AVR M8控制PT2134音频芯片的程序,显示是LCM2402,还带有有红外遥控功能.
💻 C
字号:
//********************************************************************************
                          //project:text
                          //name   :PENG
                          //date   :2008-9-3
//********************************头文件******************************************
#include<avr/eeprom.h>
#include<avr/pgmspace.h>
#include<avr/interrupt.h>
#include<avr/io.h>
#include <util/delay.h> 
#include <util/twi.h> 
#include"LCD/2402.h"
#include"KEY/KEY.h"
#include"DELAY/DELAY.h"
#include"TWI/TWI.h"
#include"IR/IR.h"
//********************************************************************************
uchar votable[6]={0,1,2,3,4,5};  //0-VOICE/1-BASS/2-TREBLE/3-INPUT/4-DB/5-LOUD
const prog_uchar trbtable[15]={0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x7f,0x7e,0x7d,0x7c,0x7b,0x7a,0x79,0x78};//trbe and treble change date
const prog_uchar bastable[15]={0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x6f,0x6e,0x6d,0x6c,0x6b,0x6a,0x69,0x68};//bass and treble change date
uchar file_return;  //菜单返回标志,当5秒无按键按下返回主界面
uchar timecount;    //中断计数,当5秒无按键按下返回主界面
uchar dobit=1;
uchar ke,ike;          //key num
uchar model=1,dbloud, mute,lond; 
#define POWER_OFF PORTD&=~(1<<PD0)
#define POWER_ON PORTD|=(1<<PD0)
//********************************************************************************
void system_int(void);
SIG_OVERFLOW0(void);
void voice_change();
void BL1(void);
//-------------------------------------main------------------------------------------------------------
//函数名:main
//返回值:无
//-----------------------------------------------------------------------------------------------------
void main()
{

while(1)
 {

  cli();
  delay10ms(10);
  system_int();
  ke=0;
  while(1)
   {
    ke=0;
	while(!(ke||ike))ke=key();           //取按键值
	if((ike!=0)||(ke!=0)||(file_return==0)){voice_change();}
    if(dobit==0){timecount=0;dobit=1;TIMSK|=0X01;}//开定时中断按键计时   
	}
 }
}
//---------------------------------------------------------------------------------
//函数名:voice_change
//返回值:无
//---------------------------------------------------------------------------------
void voice_change( )
{ 
           
//----------------------VOICE---------------------------		  	   		   
		   if((ike==10||ke==10)&&model==1)
		      {
			    votable[0]--;if(votable[0]>63)votable[0]=0;
				LCD_write_char(18,0,votable[0]/10+48);
				LCD_write_char(19,0,votable[0]%10+48);
				while( !write_nbyte1(0x88,63-votable[0]));
				ke=100;  ike=100;
			   }
		   if((ke==9||ike==9)&&model==1)
		      {			    
			    votable[0]++;if(votable[0]>63)votable[0]=63;
				LCD_write_char(18,0,votable[0]/10+48);
				LCD_write_char(19,0,votable[0]%10+48);
				while( !write_nbyte1(0x88,63-votable[0]));
				ke=100;  ike=100;
			   }
//----------------------------BASS------------------------
		   if((ike==10||ke==10)&&model==2)     
		      {
			    votable[1]--;if(votable[1]>14)votable[1]=0;
				 if(votable[1]>7)LCD_write_string(18,0,"+");
				  else if(votable[1]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[1]>7)LCD_write_char(19,0,votable[1]+48-7);
				  else if(votable[1]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[1]+48);
                while( !write_nbyte1(0x88,pgm_read_byte(&bastable[votable[1]])));
				ke=100;  ike=100;
			   }
		   if((ke==9||ike==9)&&model==2)
		      {			    
			    votable[1]++;if(votable[1]>14)votable[1]=14;
				if(votable[1]>7)LCD_write_string(18,0,"+");
				  else if(votable[1]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[1]>7)LCD_write_char(19,0,votable[1]+48-7);
				  else if(votable[1]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[1]+48);
				while( !write_nbyte1(0x88,pgm_read_byte(&bastable[votable[1]])));
				ke=100;  ike=100;
			   }
//-------------------------TREB----------------------------
		   if((ike==10||ke==10)&&model==3)
		      {
			    votable[2]--;if(votable[2]>14)votable[2]=0;
				 if(votable[2]>7)LCD_write_string(18,0,"+");
				  else if(votable[2]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[2]>7)LCD_write_char(19,0,votable[2]+48-7);
				  else if(votable[2]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[2]+48);
				while( !write_nbyte1(0x88,pgm_read_byte(&trbtable[votable[2]])));
				ke=100;  ike=100;
			   }
		   if((ke==9||ike==9)&&model==3)
		      {			    
			    votable[2]++;if(votable[2]>14)votable[2]=14;
				 if(votable[2]>7)LCD_write_string(18,0,"+");
				  else if(votable[2]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[2]>7)LCD_write_char(19,0,votable[2]+48-7);
				  else if(votable[2]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[2]+48);
				while( !write_nbyte1(0x88,pgm_read_byte(&trbtable[votable[2]])));
				ke=100;  ike=100;
			   }
//------------------------INPUT--------------------------
		   if((ike==10||ke==10)&&model==4)
		      {
			    votable[3]--;if(votable[3]>3)votable[3]=0;
				LCD_write_char(18,0,votable[3]/10+48);
				LCD_write_char(19,0,votable[3]%10+48);
				while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号
				ke=100;  ike=100;
			   }
		   if((ke==9||ike==9)&&model==4)
		      {			    
			    votable[3]++;if(votable[3]>3)votable[3]=3;
				LCD_write_char(18,0,votable[3]/10+48);
				LCD_write_char(19,0,votable[3]%10+48);
				while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号
				  ke=100;  ike=100;
			   }
//----------------------D  B------------------------------
		   if((ike==10||ke==10)&&model==5)
		      {
			    votable[4]--;if(votable[4]>4)votable[4]=0;
				LCD_write_char(18,0,votable[4]/10+48);
				LCD_write_char(19,0,votable[4]%10+48);
		        dbloud=(3-votable[4])*8+votable[5];
                while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号(loud)
				  ke=100;  ike=100;
			   }
		   if((ke==9||ike==9)&&model==5)
		      {			    
			    votable[4]++;if(votable[4]>3)votable[4]=3;
				LCD_write_char(18,0,votable[4]/10+48);
				LCD_write_char(19,0,votable[4]%10+48);
		       dbloud=(3-votable[4])*8+votable[5];
               while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号(loud)
				  ke=100;  ike=100;
			   }
        
//----------------------LOUD------------------------------
        if(ike==8||ke==8)
	     {                  //LOUD
		     	 
         if(lond)
           {
		    votable[5]=0;
		    dbloud=(3-votable[4])*8+votable[5];
            while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号(loud)
            LCD_write_string(0,1,"LOUD");
			lond=0;
			   ke=100;  ike=100;
		    }
           if(lond==0&&(ike==8||ke==8))
	        {
			 votable[5]=4;
			 dbloud=(3-votable[4])*8+votable[5];
			 while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号(loud)
             LCD_write_string(0,1,"    ");
			 lond=1;
		    } 		
			 ke=100;  ike=100;		
		  }
//----------------------MUTE------------------------------
        if(ike==7||ke==7)
	     {

		                //静音
         if(mute)
           {
            while( !write_nbyte1(0x88,223));//
            while( !write_nbyte1(0x88,255));//
            LCD_write_string(16,1,"MUTE");
			mute=0;
			   ke=100;  ike=100;
			}
         if(mute==0&&(ike==7||ke==7))
	       {
			 while( !write_nbyte1(0x88,192));//向声音处理器发送信号(左右平衡)
             while( !write_nbyte1(0x88,224));//向声音处理器发送信号(左右平衡)
             LCD_write_string(16,1,"    ");
			 mute=1;
		    } 
		     ke=100;  ike=100;
         }
	
//----------------------关机----------------------------
     if(ke==11||ike==17)
	  {	       
		unsigned char key_um;  unsigned int keytime;	   

		 keytime=0;	

		for(PORTC|=0x0f;(key_um!=0xff)&&(keytime<=100);PORTC|=0x0f,key_um=PINC,key_um|=0xf0)//关机长按判断
			{
			   _delay_ms(10);
               if(keytime<=100){keytime++;ke=11;}
			   if(keytime>100){ke=18;write_command(0x01);delay10ms(2);}	
			     
             }
		
			   
		if(ke==18||ike==17)
		  {
		    while( !write_nbyte1(0x88,223));
			while( !write_nbyte1(0x88,255));
			write_command(0x01);delay10ms(2);
			LCD_write_string(6,0,"POWER OFF");delay10ms(200);
			ke=0;
            eeprom_write_byte (0x01,votable[0]);  //储存当前音调值
            eeprom_write_byte (0x02,votable[1]);
            eeprom_write_byte (0x03,votable[2]);
		    eeprom_write_byte (0x04,votable[3]);
		    eeprom_write_byte (0x05,votable[4]);
		    eeprom_write_byte (0x06,votable[5]);
			SETBIT(DDRD,PD6);
            CLRBIT(PORTD,PD6);
			while(1);
			}
		
	  }  
//----------------------MODEL-----------------------------
      
            if((ike==11||ke==11)||file_return==0||(ike==12||ke==12)||(ike==13||ke==13))
			  {
			    if(file_return==0)							
				 {
				   TIMSK&=0xfe; //关定时中断按键到时
				   model=1;
				   file_return=1;

				  }
			    if(ike==11||ke==11){model++;if(model>5)model=1;   ke=100; ike=100; }
				if(ike==12||ke==12){model++;if(model>5)model=1;   ke=100; ike=100; }
				if(ike==13||ke==13){model--;if(model<1)model=5;   ke=100; ike=100; }				
                if(model==1)
				  {
				   LCD_write_char(18,0,votable[0]/10+48);
				   LCD_write_char(19,0,votable[0]%10+48);
				   LCD_write_string(0,0,"VOIC ------------ ");
				   }
				if(model==2)
				  {
				 if(votable[1]>7)LCD_write_string(18,0,"+");
				  else if(votable[1]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[1]>7)LCD_write_char(19,0,votable[1]+48-7);
				  else if(votable[1]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[1]+48);
				   LCD_write_string(0,0,"BASS ------------ ");
				   }
				if(model==3)
				   {
				 if(votable[2]>7)LCD_write_string(18,0,"+");
				  else if(votable[2]==7)LCD_write_string(18,0," ");
				   else LCD_write_string(18,0,"-");

				 if(votable[2]>7)LCD_write_char(19,0,votable[2]+48-7);
				  else if(votable[2]==7)LCD_write_string(19,0,"0");
				   else LCD_write_char(19,0,7-votable[2]+48);
				    LCD_write_string(0,0,"TREB ------------ ");
				   }
				if(model==4)
				   {
    				LCD_write_char(18,0,votable[3]/10+48);
			     	LCD_write_char(19,0,votable[3]%10+48);
				    LCD_write_string(0,0,"INPU ------------ ");
					}
				if(model==5)
				   {
 			    	LCD_write_char(18,0,votable[4]/10+48);
				    LCD_write_char(19,0,votable[4]%10+48);
				    LCD_write_string(0,0,"D  B ------------ ");
				    }				
               }
       		 if(ke==100&&ike==100){dobit=0;BL1( ); }
			 
	         ke=0;ike=0;
            
      
}
//----------------------------system int-------------------------------------------------------------
//函数名:system_int
//返回值:无
//---------------------------------------------------------------------------------------------------
void system_int(void)
{
        LCD_init(); //初始化LCD1602
        GICR|=0X40;
		MCUCR|=0X02;
		TCCR0|=0X05;//1024分频   CPU=1M
        TCNT0 =61;  //time=1024*(256-158)=100ms

        DDRD|=(1<<PD0);
	    DDRC|=0x00;
        PORTC=0x0f;
	    CLRBIT(DDRD,PD6);
        SETBIT(PORTD,PD6);
		file_return=1;
		timecount=0;
        ike=0;
		ke=0;
             
		POWER_OFF;
		while((PINC|0xf0)!=0xff);
		_delay_ms(5);
		while((PINC|0xf0)!=0xff);
		
		
		sei();              //开机
	    while(!(ike==17||ke==11))if((PINC|0xf0)!=0xff)ke=key( ); 

		BL1( );

		cli();		         
        LCD_write_string(6,0,"H");
        delay10ms(40);
		LCD_write_string(8,0,"E");
		delay10ms(40); 
		LCD_write_string(10,0,"L");
		delay10ms(40); 
		LCD_write_string(12,0,"L");
		delay10ms(40); 
		LCD_write_string(14,0,"O");
		delay10ms(200);
	    write_command(0x01);  //清屏--对应清屏指令 
        delay10ms(2);         //delay  

		POWER_ON; 
		ike=0;
		                
		votable[0]=eeprom_read_byte (0x01);
		votable[1]=eeprom_read_byte (0x02);
		votable[2]=eeprom_read_byte (0x03);
		votable[3]=eeprom_read_byte (0x04);
		votable[4]=eeprom_read_byte (0x05);
		votable[5]=eeprom_read_byte (0x06);
		LCD_write_string(0,0,"VOIC ------------ ");

		if(votable[5]!=04)LCD_write_string(0,1,"LOUD");
		LCD_write_char(18,0,votable[0]/10+48);
		LCD_write_char(19,0,votable[0]%10+48);
		while( !write_nbyte1(0x88,63-votable[0]));
		dbloud=(3-votable[4])*8+votable[5];
		while( !write_nbyte1(0x88,votable[3]+dbloud+64));//向声音处理器发送信号(loud)
		while( !write_nbyte1(0x88,pgm_read_byte(&bastable[votable[1]])));
		while( !write_nbyte1(0x88,pgm_read_byte(&trbtable[votable[2]])));
        while( !write_nbyte1(0x88,224));
	    while( !write_nbyte1(0x88,192));     		 
        sei(); 
}		
//----------------------------timer0 interrupt--------------------------------------------------------
//函数名:SIG_OVERFLOW0
//返回值:
//----------------------------------------------------------------------------------------------------
SIG_OVERFLOW0(void)
{
        cli();
		timecount++;
        if(timecount>200){timecount=0;file_return=0;} 
		TCNT0 =61;

		sei();
}
//-------------------------------------------------------------------------------------
void BL1(void)
{
  unsigned int tt;
 cli();
 SETBIT(DDRD,PD7);
 for(tt=40;tt>0;tt--)
  {
   CLRBIT(PORTD,PD7);
   _delay_us(100);
   _delay_us(100);
   _delay_us(100);
   _delay_us(100);
   _delay_us(100);
   _delay_us(100);
   SETBIT(PORTD,PD7);
   _delay_us(110);
   }
   CLRBIT(DDRD,PD7);
   SETBIT(PORTD,PD7);
   sei();
}

⌨️ 快捷键说明

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