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

📄 main.c

📁 用AVR atmega16做的一个完整彩色LED控制程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	   PORTA=DispBuffer[2];
	   Delay(1);
 	   PORTD=0xbf;
	   PORTA=DispBuffer[3];
	   Delay(1);
 	   PORTD=0x7f;
	   PORTA=DispBuffer[4];
	   Delay(1);
#ifdef VERSION_CONTROL
	/* WDT 复位*/
	_WDR();
#endif	   
}
char   PressKeyProcess(void)
{
 	   char		temp1,temp2;
	   temp1=PINC;
	   temp1 &=0xc3;//get pc0,pc1,pc6,pc7
	   if(temp1==0xc3)return 0;
	   Display();
	   Delay(50);
	   Display();
	   Delay(50);
	   Display();
	   Delay(50);
//
	   temp2=PINC;
	   temp2 &=0xc3;//get pc0,pc1,pc6,pc7
	   if(temp2==0xc3)return 0;
	   Display();
	   Delay(50);
	   Display();
	   Delay(50);
//
	   temp1=PINC;
	   temp1 &=0xc3;//get pc0,pc1,pc6,pc7
	   temp2 &=temp1;
	   if(temp2==0xc3)return 0;
	   if(temp2==KEY_K1K4RST ||temp2==KEY_K3K4RST )
    	   {
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
	   	Delay(250);
 	   	return	temp2;
	   }
	   Display();
	   Delay(50);
	   Display();
	   Delay(50);
 	   return	temp2;
}

//#pragma interrupt_handler spi_stc_isr:18 //atmega128 ,SPI_vecter
#pragma interrupt_handler spi_stc_isr:11  
//spi_stc_isr:11  ;atmega16 ,SPI_vecter 

void spi_stc_isr(void) 
{ 

  SPI_rx_buff[0] = SPDR; //从ISP口读出收到的字节 
} 
unsigned char getSPIchar(void) 
{ 
  unsigned char data; 
return 0;  
  while (rx_counter == 0); //无接收数据,等待 
  data = SPI_rx_buff[rx_rd_index]; //从接收缓冲区取出一个SPI收到的数据 
  if (++rx_rd_index == SIZE) rx_rd_index = 0; //调整指针 
  CLI(); 
  --rx_counter; 
    SEI(); 
    return data; 
} 
void putSPIchar(char c) 
{ 
  	 CLI(); 
     	  SPDR = c; //发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送 
	 SEI(); 
} 
void spi_init(void) 
{ 
  	 unsigned char temp; 
//  	 DDRB |= 0x10; //SS = output 
  	 DDRB = (1<<7)|(1<<5)|(1<<4)|(1<<3); //SS = output ,PB3 ==>output S_I use
	 PORTB = 0x00; //SS 平时为low 电平 
//  	 SPCR = 0xD5; //SPI允许,主机模式,MSB,允许SPI中断,极性方式01,1/16系统时钟速率 
  	 SPCR = 0x55; //SPI允许,主机模式,MSB,disable SPI中断,极性方式01,1/16系统时钟速率 
  	 SPSR = 0x00; 
  	 temp = SPSR; 
  	 temp = SPDR; //清空SPI,和中断标志,使SPI空闲 

	PORTB &=(~0x10); //SS  output  low 电平 
	PORTB &=(~0x08); //S_I  output  low 电平 
  	 
} 

void WatchDog(char  tim)
{
	/* WDT 复位*/
	_WDR();
	/* 置位 WDTOE 和 WDE*/
	WDTCR |= (1<<WDTOE) | (1<<WDE);
	/* run WDT  16.3ms*/
	WDTCR = (1<<WDE) |(tim);
}

void WDT_off(void)
{
	/* WDT 复位*/
	_WDR();
	/* 置位 WDTOE 和 WDE*/
	WDTCR |= (1<<WDTOE) | (1<<WDE);
	/* 关闭WDT */
	WDTCR = 0x00;
}
char		SpeedDelay(uchar  speed)
{
	uchar	tim,i;
	switch(speed)
	{
	case	SPEED1:
		tim=10;//6;
		break;
	case	SPEED2:
		tim=15;//9;
		break;
	case	SPEED3:
		tim=20;//12;
		break;
	case	SPEED0:
	default:
		tim=5;//3;
		break;
	}

	for(i=0;i<tim;i++)
	{
		Display_Fast();
	   	KeyCode=PressKeyProcess();
		if(KeyCode!=0) return 1 ;
		Display_Fast();
	}
	return 0;
	
}

uchar	SendFirstToEnd(uchar kind,uchar mode,uchar si_flag,uchar ss_flag)
{
	uchar	tim,i;
	Send_Data(kind,2);

	SendSS(ss_flag);
	SendSI(si_flag);
	if(SpeedDelay(mode & 0x03)) return 1;
	else
		return 0;
}

char		SendEndToFirst(uchar kind,uchar mode,uchar BC,uchar si_flag,uchar ss_flag)
{
	uchar	tim,i;
	for(i=0;i<FLenghtCount;i++)
	{
		Send_Data(BC,2);
	}

	Send_Data(kind,2);
	
	for(i=i+1;i<BLenghtCount;i++)
	{
		Send_Data(BC,2);
	}

	SendSS(ss_flag);
	SendSI(si_flag);

	if(SpeedDelay(mode & 0x03)) return 1;
	else return 0;
}

void		AllColorProcess(uchar kind,uchar mode)
{
	uchar	tim,i,value;

	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
			for(tim=0;tim<val_lenght ;tim++)
			{
				Send_Data(value,2);
//				if(SendFirstToEnd(value,mode & 0x0f,0,0)==1) return;
			}
			SendSS(1);
			Display();
			Display();
			Display();
			Display();
			Display();
			
			for(tim=0;tim<128;tim ++) 
			{
				SendSI(1);
				Delay(1);
				if(SpeedDelay(0x0)) return ;
			}
		}
	}
}

void		Three1ColorProcess(uchar kind,uchar mode)
{
	uchar	tim,i,value;

	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
			SendFirstToEnd(value,mode & 0x0f,CLASSNUMBER,1);
			for(tim=1;tim<val_lenght ;tim++)
			{
				if(SendFirstToEnd(UNLOCKDATA,mode & 0x0f,CLASSNUMBER,1)==1) return;
			}

		}
	}	
}
void		Three1ColorProcess_1(uchar kind,uchar mode)
{
	uchar	tim,i,value;

	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
			FLenghtCount=0;
			SendEndToFirst(value,mode & 0x0f,UNLOCKDATA,CLASSNUMBER,1);
			for(tim=1;tim<val_lenght;tim++)
			{
				FLenghtCount++;
				if(SendEndToFirst(value,mode & 0x0f,UNLOCKDATA,CLASSNUMBER,1)==1) return;
			}
		}
	}	
}

void		ThreeColorProcess(uchar kind,uchar mode)
{
	uchar	tim,i,value;

	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
			for(tim=0;tim<val_lenght ;tim++)
			{
//				if(SendFirstToEnd(value, 0,0,0)==1) return;
				Send_Data(value,2);
			}
			SendSS(1);
			Display();
			Display();
			Display();
			Display();
			Display();
			
//			for(tim=0;tim<200;tim +=2) //20070715 modify
			for(tim=0;tim<128;tim ++) 
			{
//				SendSI(2);//20070715 modify
				SendSI(1);
				Delay(1);
//				if(tim==127) break ;
				if(SpeedDelay(0)) return ;
				
			}
		}
	}
}

uchar		UniteColor(uchar da1,uchar da2,uchar da3,uchar da4,uchar da5,uchar da6,uchar mode)
{
	uchar	tim,i=0;//,mode;
	uchar	loop1=16,loop2=70;

	for(i=0;i<val_lenght;i++)
	{
		Send_Data(da1,2);
		SendSS(1);
		SendSI(loop1);		
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		Send_Data(da2,2);
		SendSS(1);
		SendSI(loop1);		
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		Send_Data(da3,2);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		Send_Data(da4,2);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		Send_Data(da5,2);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		Send_Data(da6,2);
		SendSS(1);
		SendSI(loop1);		
		if(SpeedDelay(mode & 0x03)==1) return 1;
		
	}


//	if(SpeedDelay(mode & 0x03)==1) return 1;
	return 0;
}

void		UniteColorProcess(uchar kind,uchar mode)
{
	uchar	tim,i,value;

	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
//			GetMiddleColor(value);
			if(UniteColor( value, UNITEC1,UNITEC2,UNITEC3,  UNITEC4,  UNITEC5,mode)==1) return;
		}
	}
}
void		UniteColorRight(uchar da1,uchar da2,uchar da3,uchar da4,uchar da5,uchar da6,uint init)
{
//	uchar	tim;//,i;
	uchar	loop1=16,loop2=70;
	uint 		val1=0x00,i;

	if(FLenghtCount!=0)
	{
		val1=hmod(FLenghtCount,6);
		switch(val1)
		{
		case	5:
			Send_Data(da5,2);
		case	4:
			Send_Data(da4,2);
		case	3:
			Send_Data(da3,2);
		case	2:
			Send_Data(da2,2);
		case	1:
			Send_Data(da1,2);
			break;
		default:
			break;
		}	
		if(FLenghtCount>val1)
		{
			i=val1;
			for(;i<FLenghtCount;i+=6)
			{
				Send_Data(da6,2);
				Send_Data(da5,2);
				Send_Data(da4,2);
				Send_Data(da3,2);
				Send_Data(da2,2);

				if(BLenghtCount==0 && (i+6)>=FLenghtCount)break;
				else
				{
					Send_Data(da1,2);
				}
			}
		}
		
	}

	i=init;
	for(;i<BLenghtCount;i++)
	{
		Send_Data(UNLOCKDATA,2);
	}	


}

void		UniteColorProcess_1(uchar kind,uchar mode)
{
	uchar	i,value,j;
		
	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
//			GetMiddleColor(value);

			FLenghtCount=0;
			BLenghtCount=val_lenght;
			for(j=0;j<val_lenght;j++)
			{
				FLenghtCount++;
				UniteColorRight(value, UNITEC1,UNITEC2,UNITEC3,  UNITEC4,  UNITEC5,FLenghtCount);

				SendSS(1);
				SendSI(16);
	
				if(SpeedDelay(mode & 0x03)) return;;		
			}
		}
	}
}
void		UniteColorLeft(uchar da1,uchar da2,uchar da3,uchar da4,uchar da5,uchar da6,uint  loop)
{
	uint	tim,i;//,mode;
//	uchar	loop1=16,loop2=70;
	if(loop>6)
	{
//		tim=loop;
		for(;loop>=6;loop-=6)
		{
			Send_Data(da1,2);
			Send_Data(da2,2);
			Send_Data(da3,2);
			Send_Data(da4,2);
			Send_Data(da5,2);
			Send_Data(da6,2);
		}
	}
	i=0;
	if(i<loop){
			Send_Data(da1,2);
	}

	if(i+1<loop){
			Send_Data(da2,2);
	}

	if(i+2<loop){
			Send_Data(da3,2);
	}

	if(i+3<loop){
			Send_Data(da4,2);
	}
	if(i+4<loop){
			Send_Data(da5,2);
	}
	if(i+5<loop){
			Send_Data(da6,2);
	}

}
uchar		UniteColorMiddleToEdge(uchar da1,uchar da2,uchar da3,uchar da4,uchar da5,uchar da6,uchar mode)
{
	uint 	tim,i,diff,j;
	//芯片数为双数, 为单数
	HalfLenght=val_lenght/2;
	FLenghtCount=1;
	BLenghtCount=HalfLenght+1;
	
	Send_Data(da1,2);
	for(i=1;i<BLenghtCount;i++)
	{
			Send_Data(UNLOCKDATA,2);
	}	

	SendSS(1);
	
///	if(SpeedDelay(mode & 0x03)) return 1;
	
	for(i=1;i<BLenghtCount;i++)
	{
		UniteColorLeft(da1, da2,  da3,  da4,da5,da6,i);
		FLenghtCount++;
		UniteColorRight(da1, da2,  da3,  da4,da5,da6,FLenghtCount);
		SendSS(1);
		SendSI(16);
	}
//	if(SpeedDelay(mode & 0x03)) return 1;		
	return 0;
}

//从中间向两边。
void		UniteColorProcess_2(uchar kind,uchar mode)
{
	uchar	i,value;
		
	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{
			if(UniteColorMiddleToEdge(value, UNITEC1,UNITEC2,UNITEC3,  UNITEC4,  UNITEC5,mode)==1) break;
		}
		if(SpeedDelay(mode & 0x03)) return;		
		
	}
}

uchar		UniteColorEdgeToMiddle(uchar da1,uchar da2,uchar da3,uchar da4,uchar da5,uchar da6,uchar mode)
{
	uint 	tim,i,diff,j;
	//芯片数为双数, 为单数
	HalfLenght=val_lenght/2;
	FLenghtCount=1;
	BLenghtCount=val_lenght-2;
	
	for(i=1;i<=HalfLenght;i++)
	{
	       UniteColorRight(da1, da2,  da3,  da4,da5,da6,0);
		FLenghtCount++;
		UniteColorLeft(da1, da2,  da3,  da4,da5,da6,i);
		
		SendSS(1);
		SendSI(16);
	
		if(BLenghtCount==1)
		{
			BLenghtCount=0;
		       UniteColorRight(da1, da2,  da3,  da4,da5,da6,0);
			UniteColorLeft(da1, da2,  da3,  da4,da5,da6,i+1);
			SendSS(1);
			SendSI(16);
//			if(SpeedDelay(mode & 0x03)) return 1;
			return 0;
		}		
		else
			BLenghtCount-=2;
	}
//	if(SpeedDelay(mode & 0x03)) return 1;
	return 0;
}

//从两边向中间.
void		UniteColorProcess_3(uchar kind,uchar mode)
{
	uchar	i,value;
		
	for(i=0;i<7;i++)
	{	
		value=KindGroudData[kind][i];
		if(value!=0x00)
		{

⌨️ 快捷键说明

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