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

📄 main.c

📁 Tea5767的3线控制程序
💻 C
字号:
// IAR 3.2
// ATmega8     
// clock: internal 8Mhz
//Hewei

#include <iom8.h>
#include <inavr.h>
#include <string.h>
#include "tea5767.h"
#include "key.h"
#include "led.h"
#include "global.h"

#define T0INTERVAL           10L          // 定时器0的中断周期 单位: 0.06mS
#ifndef FOSC
#define FOSC                 8000000L    // 在此定义CPU时钟频率
#endif

#define FALSE 0x0
#define TRUE 0xFF

unsigned char Search_Flag ;

//读键值
unsigned char Key_Process;
unsigned char Key_Push_Flag;
unsigned char Key_Code;



//工作状态选择
#define   AUTO_TURNING     0xF1
#define   Seltct_TURNING   0xF2
#define   Seltct_FREQ      0xF3

unsigned char Work_STAT;
unsigned char Dis_Flag;


unsigned char temp;
//当前频率
unsigned long Freq,PLL;     //频率单位Khz

static unsigned char DisBuffer[5] ;


#define uchar unsigned char
#define uint  unsigned int

uchar WriteData[5] ={0x29,0xc2,0x20,0x11,0x00};
uchar ReadData[5] ={0,0,0,0,0};


//////////////////////////////////////////////////////////
//static unsigned char 	key_counter1 = 0;	
//----------------------------------------------------
#pragma vector=TIMER0_OVF_vect
__interrupt void Timer0_Int_Handler(void)
{
	static unsigned char 	old_key,		// 老化键值
				key_counter;		// 按键年龄计数器(用于键盘消抖)
	unsigned char 		new_key;		// 新键值

//	MCUCR &= 0x7F;//(1<<SE);
	
	TCNT0  = 256 - (T0INTERVAL*FOSC)/1024000L;
	//--------------------------------------------
	// 用户在下面添加周期性处理程序,如定时等
	if (Dis_Flag)
	{
	    Display_LED(DisBuffer);
	}
/*	
	key_counter1++;
	
	if (key_counter1 == 50)
	{
	     PORTC ^= 0x08;
	     key_counter1 = 0;
         }
*/	
	//--------------------------------------------
	// 读键盘示范---------------------------------
	new_key = Read_Key();
	if ((new_key & 0xF0) == 0x00)
	{
		if (new_key != old_key)			// 有新的按键被按下
		{
			key_counter = 0;		// 计时开始
			Key_Process = FALSE;
		}
		else
		{
			if ((key_counter >= 100) && !Key_Process)		// 按下键超过50ms且未处理 
			{
				Key_Push_Flag = TRUE;
				Key_Code = new_key;
			}
			else key_counter++;		// 连续计数
		}
	}
	old_key = new_key;				// 保存键值
}

//-------------------------------

void init(void)
{

__disable_interrupt();

  OSCCAL=0x9d;                        // 8M系统内部时钟校准    
  
  delay_ms(100);

  DDRD = 0xFF;
  PORTD = 0x00;
  DDRC = 0xFF ;
  PORTC = 0x00;
  DDRB = 0xFF;
  PORTB = 0x00;  

  TIMSK  |= 0x01;
  TCNT0  = 100;
  TCCR0  = 0x03;
  SREG|= 0x80;
  
__enable_interrupt();
  

}

//////////////////////////////////////////////////////////////////
void main(void)
{
	init();
/////////////////////////////////	
        __disable_interrupt();
        temp = EEPROM_read( 2 );
        __enable_interrupt();
        
        if ((temp == Seltct_TURNING)||(temp == Seltct_FREQ))
        {
          Work_STAT = temp;
        }
        else Work_STAT = Seltct_TURNING;
        __disable_interrupt();
        Freq = Load_Freq();
	__enable_interrupt();
        
	Key_Push_Flag = FALSE;
	Key_Process = FALSE;
	Search_Flag = FALSE;
	Dis_Flag = TRUE;
	

	DisBuffer[4] = 0x0; //LED点亮,Seltct_TURNING
        if ((Freq<88000)||(Freq>108000))
	{
	  Freq = 88000;
	  Work_STAT = Seltct_TURNING;
 	}
 	else if (Work_STAT == Seltct_FREQ)
 	{
 	    DisBuffer[4] = 0x01;   //LED关闭,Seltct_FREQ
 	}
	Freq_2_Buffer(Freq,DisBuffer);  //初始化显示频率值	

 	
	Display_LED( DisBuffer );	


	Init_Tea5767( Freq );
	delay_ms(10);

	
	while(1)
	{
		if ( Key_Push_Flag )
		{
//			PORTC |= 0x01;
			
			if(Key_Code == 3)
                        {
                              if ( Work_STAT == Seltct_FREQ )
                              {
                                  Work_STAT = Seltct_TURNING;
                                  DisBuffer[4] = 0x0;    //"P"
                                  PORTC |= 0x01;
                              }
                              else    
                              {
                                  Work_STAT = Seltct_FREQ;
                                  DisBuffer[4] = 0x0F;
                                  PORTC &= 0xFE;
                              }
                              __disable_interrupt();
                              EEPROM_write( 2 , Work_STAT );
                              __enable_interrupt();
                              
                              
//                             	if (Dis_Flag)
//                             	  Dis_Flag = FALSE;
//                             	else Dis_Flag = TRUE;
  
                        }
                        if(Key_Code == 2)   //+++
                        {
                              if ( Work_STAT == Seltct_FREQ )
                              {
                                  Freq += 100;
                                  if (Freq >108000)
                                      Freq = 88000;
                                  Set_Freq(Freq);
                                  
                                  __disable_interrupt();
                                  Save_Freq(Freq);
                                  __enable_interrupt();
                                  
                                  Freq_2_Buffer(Freq,DisBuffer);
                                  
                              }
                              else
                              {
                                  Search_up(Freq);
                                  Search_Flag = TRUE;
                                  while (Search_Flag)
                                  {
                                      Read_5767(ReadData);
                                      PLL = (ReadData[0] & 0x3f)*256+ReadData[1];
                                      Freq = PLL_2_Freq(PLL);
                                      Freq_2_Buffer(Freq,DisBuffer);

                                      if ( ReadData[0] & 0x80)   //has been found a station
                                      {
                                              
                                              Set_Freq(Freq);
                                       	      Freq_2_Buffer(Freq,DisBuffer);
                                
                                              __disable_interrupt();
                                              Save_Freq(Freq);
                                              __enable_interrupt();
                                              
                                              Search_Flag = FALSE;
                                      }
                                      if ( ReadData[0] & 0x40 )  //reach band end
			              {
					      Freq = 88000;
					      Freq_2_Buffer(Freq,DisBuffer);
                                              
                             //                 Search_up(Freq);
                                      }
                                      
                                      
                                  }
                             }
                        }
                        if(Key_Code == 1)   //----
                        {
                              if ( Work_STAT == Seltct_FREQ )
                              {
                                  Freq -= 100;
                                  if (Freq<88000)
                                    Freq = 108000;
                                  Set_Freq(Freq);
                                  
                                  __disable_interrupt();
                                  Save_Freq(Freq);
                                  __enable_interrupt();
                                  
                                  Freq_2_Buffer(Freq,DisBuffer);
                              }
                              else
                              {
                                  Search_down(Freq);
                                  Search_Flag = TRUE;
                                  while (Search_Flag)
                                  {
                                      Read_5767(ReadData);
                                      
                                      PLL = (ReadData[0] & 0x3f)*256+ReadData[1];
                                      Freq = PLL_2_Freq(PLL);
                                      Freq_2_Buffer(Freq,DisBuffer);
                                  
                                      if ( ReadData[0] & 0x80)   //has been found a station
                                      {
                                              Set_Freq(Freq);
                                  
                                              __disable_interrupt();
                                              Save_Freq(Freq);
                                              __enable_interrupt();
                                  
                                              Search_Flag = FALSE;
                                      }
                                      if ( ReadData[0] & 0x40 )  //reach band end
			              {
					      Freq = 108000;
					      Freq_2_Buffer(Freq,DisBuffer);
                                  
                                      }
                                  }
                              }
                        }

                        Key_Push_Flag = FALSE;
                        Key_Process = TRUE;
		}
		
//		sleep();
	}
}

⌨️ 快捷键说明

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