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

📄 频率发生器__.c

📁 有LCD菜单的 频率发生器制作资料
💻 C
📖 第 1 页 / 共 2 页
字号:
				LCD_Write(3);
			}
			else if (CurrentMenuItem==MenuCount-1)
			{
				LCD_Write(1);
			}
			else
			{
				LCD_Write(2);
			}
			
			//LCD_Write(MenuNo(CurrentMenuItem));
			LCD_Write(' ');
			LCD_Write('=');
			LCD_PutStrF(Caption);
		}
		
		Key=GetKey();
		
		if (Key!=0)
		{
			if (Key==BT_UP)
			{
				if (CurrentMenuItem>0)
				{
					CurrentMenuItem--;
				} 
			}
			else if (Key==BT_DOWN)
			{	
				if (CurrentMenuItem<MenuCount-1)
				{
					CurrentMenuItem++;
				}
			}
			else if (Key==BT_APPLY)
			{
				*Value=CurrentMenuItem;
			}
			else if (Key==BT_LEFT)
			{
				Key=BT_APPLY;
			}
			
			delay_ms(10);
			SOUND_STOP;  //Sound Stop
		}
		
	}
}

void ShowMenu(void)
{
	flash unsigned char *pMenu;
	flash unsigned char *pDefine;
	flash unsigned char *Caption;
	unsigned char Key;
	unsigned int TopMenuItem;
	unsigned int CurrentMenuItem;
	unsigned char i;
	unsigned char Stack[3][5];
	unsigned char StackIndex;
	unsigned char DrawMenuItem[6]={LCD_SET_DDRAM,0xC0,1,0,0,1};
	unsigned char MenuCount;
	unsigned char j;
	//unsigned char MenuItem;
	
	
	pMenu=Menu;
	pDefine=SubMenuDefine;
	
	TopMenuItem=0;
	CurrentMenuItem=0;
	//MenuItem=0;

	Key=1;
	WriteCGRAM(0x80,0,0);
	WriteCGRAM(0x81,1,0);
	StackIndex=0;
	Stack[0][0]=0;
	Stack[1][0]=0;
	Stack[2][0]=0;
	MenuCount=SubMenuDefine[1];

	while (1)
	{
		if (Key!=0)
		{
			for (j=0;j<2;j++)
			{
				LCD_Ctrl(DrawMenuItem[0+j]);
				
				if (DrawMenuItem[(unsigned char)(j+4)]<MenuCount)
				{
					Caption=Menu+(CAPTION_LENGTH+2)*(TopMenuItem+DrawMenuItem[(unsigned char)(j+4)]);
					
					if (DrawMenuItem[(unsigned char)(j+2)])
					{
						LCD_PutStrF("     ");
						LCD_Write(0);
						LCD_Write(MenuNo(DrawMenuItem[(unsigned char)(j+4)]+1));
						LCD_Write('.');
						LCD_PutStrF(Caption);
					}
					else
					{
						LCD_PutStrF("      ");
						LCD_Write(MenuNo(DrawMenuItem[(unsigned char)(j+4)]+1));
						LCD_Write('.');
						LCD_PutStrF(Caption);
					}
				}
				else
				{
					for (i=0;i<16;i++) LCD_Write(' ');
				}		
			}
		}
		
		Key=GetKey();
		
		if (Key!=0)
		{
			if (Key==BT_UP)
			{
				if (DrawMenuItem[3])
				{
					DrawMenuItem[2]=1;
					DrawMenuItem[3]=0;
				} 
				else if (DrawMenuItem[4]>0)
				{
					DrawMenuItem[4]--;
					DrawMenuItem[5]--;
				}
			}
			else if (Key==BT_DOWN)
			{	
				if (DrawMenuItem[2])
				{
					DrawMenuItem[2]=0;
					DrawMenuItem[3]=1;
				}
				else if (DrawMenuItem[5]<MenuCount-1)
				{
					DrawMenuItem[4]++;
					DrawMenuItem[5]++;
				}
			}
			else if ((Key==BT_APPLY)||(Key==BT_RIGHT))
			{
				
				if (DrawMenuItem[2])
				{
					CurrentMenuItem=TopMenuItem+DrawMenuItem[4];
				}
				else
				{
					CurrentMenuItem=TopMenuItem+DrawMenuItem[5];
				}
				
				if (CurrentMenuItem<MENU_COUNT)
				{
					pMenu=Menu+(CAPTION_LENGTH+2)*CurrentMenuItem+CAPTION_LENGTH+1;
					if (*pMenu>MENU_SUBITEM)
					{
						Stack[0][StackIndex]=pDefine-SubMenuDefine;
						Stack[1][StackIndex]=DrawMenuItem[2];
						Stack[2][StackIndex]=DrawMenuItem[4];
						StackIndex++;
						
						pDefine=SubMenuDefine+(unsigned int)(*pMenu-MENU_SUBITEM)*2;
						TopMenuItem=*pDefine -1;
						MenuCount=pDefine[1];
						DrawMenuItem[2]=1;
						DrawMenuItem[3]=0;
						DrawMenuItem[4]=0;
						DrawMenuItem[5]=1;
					}
					else if (*pMenu<=MENU_SUBITEM)
					{
						ExecuteMenu(CurrentMenuItem+1);
					}
					
				}
			}
			else if (Key==BT_LEFT)
			{
				if (StackIndex>0)
				{
					StackIndex--;
					pDefine=SubMenuDefine+(unsigned int)(Stack[0][StackIndex]);
					TopMenuItem=*pDefine -1;
					MenuCount=pDefine[1];
					DrawMenuItem[2]=Stack[1][StackIndex];
					DrawMenuItem[3]=1-Stack[1][StackIndex];
					DrawMenuItem[4]=Stack[2][StackIndex];
					DrawMenuItem[5]=Stack[2][StackIndex]+1;
				}
			}
			
			delay_ms(10);
			SOUND_STOP;  //Sound Stop
		}
		
	}
}

interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
	/*
	    利用 Timer1 的输入捕获中断来取得红外的调频
		
		红外接收管的IR#接到MCU的ICP脚,下降沿触发。
		
		当ICP触发时,Timer1的数据保存到ICR1里,只需取低8位则可,ICR1的低8位为ICR1L
		
		Timer1的分频为8K左右.
    */

	unsigned char ICR;
  
	ICR=ICR1L;    //保存ICR1的低8位
	TCNT1H=0x00;  //复位计时器
	TCNT1L=0x00;
	
	//TransmitByte(ICR);
	//TransmitByte(IRTiger);
	
	IR_PressTimer(ICR);
	
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{	
	IR_Reset();
}

#define MAXCMDBUF 17

void PORT_Init(void)
{
	DDRB=0B11101110;
	PORTB=0B11101100;
	DDRC=0xFF;
	DDRD=0B10011100;
}

void TIMER_Init(void)
{
	TCCR1A=0x00;
	//TCCR1B=0x05;   //Click IO / 1024 预分频为1024,7.3728mHz大概为7.204kHz左右 /正常IR应该为7.1428
	TCCR1B=0x0E;     //利用NE555做出7.143~7.184之间的频率来支持
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0x03;   //比较匹配最高值为 108ms ,  108ms / 140us = 771 = 0x02FF,
	OCR1AL=0x0F;   //0x030F是为了比0x02FF稍高一些,预仿延时问题
	OCR1BH=0x00;
	OCR1BL=0x00;
	
	TIMSK=(1<<TICIE1)|(1<<OCIE1A);
}

void LCD_WriteCaption(unsigned char ParamCaptionID,unsigned char ValueCaptionID)
{
	flash unsigned char *Caption;
	unsigned char i=0;
	
	LCD_Ctrl(LCD_CLEAR);
	LCD_Delay(500);
	
	for (i=0;i<2;i++)
	{	
		if (!i)
		{
			LCD_Ctrl(LCD_SET_DDRAM);
			LCD_PutStrF("      Set ");
			if (ParamCaptionID>0)
			{
				Caption=Menu+(unsigned int)(CAPTION_LENGTH+2)*((unsigned int)(ParamCaptionID-1));
			}
			else
			{
				Caption=0;
			}
		}
		else
		{
			LCD_Ctrl(0xC0);
			LCD_PutStrF("      ");
			if (ValueCaptionID>0)
			{
				Caption=Menu+(unsigned int)(CAPTION_LENGTH+2)*((unsigned int)(ValueCaptionID-1));
			}
			else
			{
				Caption=0;
			}
		}
		
		if (Caption>0)
		{
			while (*Caption)
			{
				if (*Caption!=0x20)
				{
					LCD_Write(*Caption++);
				}
				else
				{
					if ((Caption[1]==0x20)||(Caption[1]==0)) 
					{
						break;
					}
					else
					{
						LCD_Write(*Caption++);
					}	
				}
			}
		}
	}
}

void SaveSetup(void)
{
	unsigned char i;
	
	SOUND_STOP;
	RED_LED_ON;
	LCD_Ctrl(LCD_CLEAR);
	LCD_PutStrF("    Save Setup");
	LCD_Ctrl(0xC0);
	LCD_PutStrF("    waiting...");
	
	for (i=0;i<SETUP_COUNT;i++)
	{
		eSetup[i]=mSetup[i];
		delay_ms(10);
	}
	
	LCD_Ctrl(0xC0);
	LCD_PutStrF("    done      ");

	for (i=0;i<5;i++)
	{
		SOUND_START;
		delay_ms(50);
		SOUND_STOP;
		delay_ms(50);
	}
	
	RED_LED_OFF;
}

void RestartClient(void)
{
	unsigned char i;
	unsigned char Flag=0;
	
	SOUND_STOP;
	YLW_LED_ON;
	LCD_Ctrl(LCD_CLEAR);
	LCD_PutStrF("    Restarting");
	LCD_Ctrl(0xC0);
	LCD_PutStrF("    waiting...");
	
	while ((UCSRA&(1<<RXC))) 
	{
		i=UDR;
		delay_us(100);
	}
	
	
	RST_CLIENT_ON;
	delay_ms(100);
	
	LCD_Ctrl(0xC0);
	LCD_PutStrF("    sending...");
	RST_CLIENT_OFF;
	delay_ms(4);
	
	if ((ReceiveByte()==0x55)&&(ReceiveByte()==0xAA))
	{
		for (i=0;i<SETUP_COUNT;i++)
		{
			TransmitByte(mSetup[i]);
		}
		
		for (i=0;i<SETUP_COUNT;i++)
		{
			mSetup[i]=ReceiveByte();
		}		
		
		if ((ReceiveByte()==0xF0)&&(ReceiveByte()==0x0F))
		{
			Flag=1;
		}
	}
	
	if (Flag==1)
	{
		LCD_Ctrl(0xC0);
		LCD_PutStrF("    done      ");
		
		for (i=0;i<3;i++)
		{
			SOUND_START;
			delay_ms(50);
			SOUND_STOP;
			delay_ms(50);
		}
	}
	else
	{
		LCD_Ctrl(0xC0);
		LCD_PutStrF("    Faild     ");
		
		for (i=0;i<10;i++)
		{
			SOUND_START;
			delay_ms(50);
			SOUND_STOP;
			delay_ms(50);
		}
	}
	
	YLW_LED_OFF;
	
}

void ByteToBin(unsigned char Bin,unsigned char * Buffer)
{
	unsigned char i;
	
	for (i=0;i<8;i++)
	{
		if (Bin&(1<<i)) 
		{
			Buffer[7-i]='1';
		}
		else
		{
			Buffer[7-i]='0';
		}
	}
}

void ViewSetup()
{
	flash unsigned char *Caption;
	flash unsigned char *pDefine;
	unsigned char Key;
	//unsigned char Pos;
	unsigned int TopMenuItem;
	unsigned int CurrentMenuItem;
	//unsigned char i;
	unsigned char MenuCount;
	unsigned char Buffer[12];

    delay_ms(10);
    SOUND_STOP;
	
	//LCD_WriteCaption(ParamCaptionID,0);
	//Pos=LCD_Position()-1;
	
	WriteCGRAM(0x82,1,0);
	WriteCGRAM(0x83,2,0);
	WriteCGRAM(0x84,3,0);
	
	pDefine=SubMenuDefine+(unsigned int)2*(unsigned int)(SD_VIEW);
	MenuCount=pDefine[1];
	TopMenuItem=pDefine[0];
	CurrentMenuItem=0;
	if (CurrentMenuItem>MenuCount-1)
	{
		CurrentMenuItem=MenuCount-1;
	}
	

	Key=1;
	while (Key!=BT_APPLY)
	{
		if (Key!=0)
		{
			LCD_Ctrl(LCD_SET_DDRAM);
			LCD_PutStrF("      ");
			
			Caption=Menu+(CAPTION_LENGTH+2)*(TopMenuItem+CurrentMenuItem-1);
			
			if (CurrentMenuItem==0)
			{
				LCD_Write(3);
			}
			else if (CurrentMenuItem==MenuCount-1)
			{
				LCD_Write(1);
			}
			else
			{
				LCD_Write(2);
			}
			
			LCD_Write(MenuNo(CurrentMenuItem+1));
			LCD_Write('.');
			LCD_PutStrF(Caption);

			ByteToHexBuffer(mSetup[(unsigned char)CurrentMenuItem],Buffer);
			ByteToBin(mSetup[(unsigned char)CurrentMenuItem],&Buffer[3]);
			Buffer[2]=',';
			Buffer[11]=0;
			LCD_Ctrl(0xC0);
			LCD_PutStrF("    =");
			LCD_PutStr(Buffer);
			
		}
		
		Key=GetKey();
		
		if (Key!=0)
		{
			if (Key==BT_UP)
			{
				if (CurrentMenuItem>0)
				{
					CurrentMenuItem--;
				} 
			}
			else if (Key==BT_DOWN)
			{	
				if (CurrentMenuItem<MenuCount-1)
				{
					CurrentMenuItem++;
				}
			}
			else if (Key==BT_LEFT)
			{
				Key=BT_APPLY;
			}
			
			delay_ms(10);
			SOUND_STOP;  //Sound Stop
		}
		
	}
}


void ExecuteMenu(unsigned char MenuID)
{
	unsigned char Temp;
	
	if (MenuID==MN_INITKEY)
	{
		SaveSetup();
	}
	else if (MenuID==MN_RESTART)
	{
		RestartClient();
	}
	else if (MenuID==MN_VIEW)
	{
		ViewSetup();
	}
	else if (MenuID==MN_CLOCK0)
	{   //Timer0 Clock0
		ChooseRadioMenu(MenuID,SD_CLOCK0,&mTCCR0);		
	}
	else if (MenuID==MN_TOIE0)
	{   //Timer0 TOIE0
		GetInputBoolean(MN_TIMER0,MenuID,&mTIMSK,TOIE0);			
	}
	else if (MenuID==MN_CLOCK1)
	{   //Timer1 Clock1
		Temp=mTCCR1B&0x07;
		ChooseRadioMenu(MenuID,SD_CLOCK1,&Temp);
		mTCCR1B=(mTCCR1B&0xF8)|(Temp);
	}	
	else if (MenuID==MN_COM1A)
	{   //Timer1 COM1A
		Temp=(mTCCR1A&((1<<COM1A1)|(1<<COM1A0)))>>COM1A0;
		ChooseRadioMenu(MenuID,SD_COM1A,&Temp);
		mTCCR1A=(mTCCR1A&(~((1<<COM1A1)|(1<<COM1A0))))|((Temp)<<COM1A0);
	}	
	else if (MenuID==MN_COM1B)
	{   //Timer1 COM1B
		Temp=(mTCCR1A&((1<<COM1B1)|(1<<COM1B0)))>>COM1B0;
		ChooseRadioMenu(MenuID,SD_COM1B,&Temp);
		mTCCR1A=(mTCCR1A&(~((1<<COM1B1)|(1<<COM1B0))))|((Temp)<<COM1B0);
	}	
	else if (MenuID==MN_WGM1)
	{   //Timer1 WGM1
		Temp=(mTCCR1A&((1<<WGM11)|(1<<WGM10)))>>WGM10;
		Temp|=((mTCCR1B&((1<<WGM13)|(1<<WGM12)))>>(WGM12-2));
		ChooseRadioMenu(MenuID,SD_WGM1,&Temp);
		mTCCR1A=(mTCCR1A&(~((1<<WGM11)|(1<<WGM10))))|((Temp&0x03)<<WGM10);
		mTCCR1B=(mTCCR1B&(~((1<<WGM13)|(1<<WGM12))))|((Temp>>2)<<WGM12);
	}	
	else if (MenuID==MN_OCR1A)
	{   //Time1 OCR1A
		GetInput(MN_TIMER1,MenuID,0,&mOCR1AL);
	}
	else if (MenuID==MN_OCR1B)
	{   //Timer1 OCR1B
		GetInput(MN_TIMER1,MenuID,0,&mOCR1BL);
	}
	else if (MenuID==MN_ICR1)
	{   //Timer1 ICR1
		GetInput(MN_TIMER1,MenuID,0,&mICR1L);
	}
	else if (MenuID==MN_TICIE1)
	{   //Timer1 TICIE1
		GetInputBoolean(MN_TIMER1,MenuID,&mTIMSK,TICIE1);
	}
	else if (MenuID==MN_OCIE1A)
	{   //Timer1 OCIE1A
		GetInputBoolean(MN_TIMER1,MenuID,&mTIMSK,OCIE1A);
	}
	else if (MenuID==MN_OCIE1B)
	{   //Timer1 OCIE1B
		GetInputBoolean(MN_TIMER1,MenuID,&mTIMSK,OCIE1B);
	}
	else if (MenuID==MN_TOIE1)
	{   //Timer1 TOIE1
		GetInputBoolean(MN_TIMER1,MenuID,&mTIMSK,TOIE1);
	}
	else if (MenuID==MN_ICNC1)
	{   //Timer1 ICNC1
		GetInputBoolean(MN_TIMER1,MenuID,&mTCCR1B,ICNC1);
	}		
	else if (MenuID==MN_ICES1)
	{   //Timer1 ICES1
		GetInputBoolean(MN_TIMER1,MenuID,&mTCCR1B,ICES1);
	}
	else if (MenuID==MN_CLOCK2)
	{   //Timer2 Clock2
		Temp=mTCCR2&0x07;
		ChooseRadioMenu(MenuID,SD_CLOCK2,&Temp);
		mTCCR2=(mTCCR2&0xF8)|(Temp);
	}
	else if (MenuID==MN_WGM2)
	{   //Timer2 WGM2
		Temp=(mTCCR2&(1<<WGM20))>>WGM20;
		Temp|=(mTCCR2&(1<<WGM21))>>(WGM21-1);
		ChooseRadioMenu(MenuID,SD_WGM2,&Temp);
		mTCCR2= (mTCCR2&(~(1<<WGM20)))|((Temp&(0x01))<<WGM20);
		mTCCR2= (mTCCR2&(~(1<<WGM21)))|(((Temp>>1)&(0x01))<<WGM21);
	}
	else if (MenuID==MN_COM2)
	{   //Timer2 COM2
		Temp=(mTCCR2&((1<<COM20)|(1<<COM21)))>>COM20;
		ChooseRadioMenu(MenuID,SD_COM2,&Temp);
		mTCCR2= (mTCCR2&(~((1<<COM20)|(1<<COM21))))|(Temp<<COM20);
	}
	else if (MenuID==MN_OCIE2)
	{  //Timer2 OCIE2
		GetInputBoolean(MN_TIMER2,MenuID,&mTIMSK,OCIE2);
	}
	else if (MenuID==MN_TOIE2)
	{   //Timer2 TOIE2
		GetInputBoolean(MN_TIMER2,MenuID,&mTIMSK,TOIE2);
	}
	else if (MenuID==MN_OCR2)
	{   //Timer2 OCR2
		GetInput(MN_TIMER2,MenuID,&mOCR2,0);
	}
	else if (MenuID==MN_AS2)
	{   //Timer2 AS2
		GetInputBoolean(MN_TIMER2,MenuID,&mASSR,AS2);
	}
}

void InitKey(void)
{
	flash unsigned char *Caption;
	unsigned char IRCmd[4];
	unsigned char Buffer[8];
	unsigned char IRKeyPress;
	unsigned int TopMenuItem;
	unsigned char MenuCount;
	unsigned char i,j;
	
	
	MenuCount=SubMenuDefine[SD_INITKEY*2+1];
	TopMenuItem=SubMenuDefine[SD_INITKEY*2];
	
	i=0;
	
	RED_LED_ON;
	
	while (i<MenuCount)
	{
		Caption=Menu+(CAPTION_LENGTH+2)*(TopMenuItem+i-1);
		
		LCD_Ctrl(LCD_CLEAR);
		LCD_Delay(500);
		
		LCD_Ctrl(LCD_SET_DDRAM);
		LCD_PutStrF("Set Key:");
		LCD_PutStrF(Caption);
		LCD_Ctrl(0xC0);
		//LCD_PutStrF("       ");
		//LCD_PutStrF("Key:");
		
		while (!(IR_GetBuffer(IRCmd,&IRKeyPress)));
		
		SOUND_START;  //Sound Start
		
		BinToHex(IRCmd,Buffer,4);
		
		for (j=0;j<8;j++)
		{
			LCD_Write(Buffer[j++]);
			LCD_Write(Buffer[j]);
			LCD_Write(' ');
		}
		
		if ((~IRCmd[0]==IRCmd[1])&&(~IRCmd[2]==IRCmd[3])&&(IRKeyPress>0))
		{
			eConfig[i] = IRCmd[2];
			mConfig[i++] = IRCmd[2];
			LCD_PutStrF("OK ");
		}
		else
		{
			LCD_PutStrF("Err");
		}
		
		delay_ms(10);
		SOUND_STOP;
		
		IR_Complete();
		delay_ms(500);
	}
	
	LCD_Ctrl(0xC0);
	LCD_PutStrF("    done       ");
	
	for (i=0;i<SETUP_COUNT;i++)
	{
		eSetup[i]=0;
		mSetup[i]=0;
	}
	
	for (i=0;i<5;i++)
	{
		SOUND_START;
		delay_ms(50);
		SOUND_STOP;
		delay_ms(50);
	}	
	
	RED_LED_OFF;

}

void LoadConfig(void)
{
	unsigned char i;
	
	for (i=0;i<CONFIG_COUNT;i++)
	{
		mConfig[i] = eConfig[i];
	}
}

void main (void)
{
   unsigned char cnt,i;
   unsigned char BUFFER[MAXCMDBUF];
   unsigned char Index;
   unsigned char Cmd;
   //flash unsigned char *sp2

  for (i=1;i<2;i++)
   delay_ms(1000);  //LCD上电中,延迟2秒
  
  
  PORT_Init();
  LCD_Init();
  USART_Init(0x33);
  TIMER_Init();
  
  RED_LED_OFF;
  YLW_LED_OFF;
  RST_CLIENT_OFF;
  
  Index=0;
  Cmd=0;
 
  cnt=1;
  LCD_Ctrl(LCD_HOME);
  LCD_Delay(150);

  LCD_Ctrl(LCD_SET_DDRAM);
  LCD_PutStrF("  Timer Menu");

  LCD_Ctrl(0xC0);
  LCD_PutStrF("  Initialing...");
  
   
  #asm
  sei
  #endasm

  Cmd=0;
  for (i=0;i<100;i++)
  {
	if (PINB.PB4==0) Cmd++;
  }
  
  if (Cmd>80)
	InitKey();
  else
	LoadConfig();
  
  for (i=0;i<SETUP_COUNT;i++)
  {
	mSetup[i]=eSetup[i];
  }
  
  while (1)
  {
		if (UCSRA&(1<<RXC)) //串口有信息吗?
		{
			Cmd=UDR;    //如果有,接收一个字节
			if ((Cmd>=32) && (Cmd<=127))
			{
				BUFFER[Index]=Cmd;
				if (Index++ >MAXCMDBUF ) Index=0;
			}     
		}
		
		ShowMenu();
	}
}


⌨️ 快捷键说明

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