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

📄 main._c

📁 用AVR atmega16做的一个完整彩色LED控制程序
💻 _C
📖 第 1 页 / 共 5 页
字号:
 	   PORTD=0x7f;
	   PORTA=DispBuffer[4];
	   Delay(10);

#ifdef VERSION_CONTROL
	/* WDT 复位*/
	_WDR();
#endif
	   
}
void   Display_Fast(void)
{	
 	   PORTD=0xf7;
	   PORTA=DispBuffer[0];
	   Delay(1);
 	   PORTD=0xef;
	   PORTA=DispBuffer[1];
	   Delay(1);
 	   PORTD=0xdf;
	   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=6;
		break;
	case	SPEED2:
		tim=9;
		break;
	case	SPEED3:
		tim=12;
		break;
	case	SPEED0:
	default:
		tim=3;
		break;
	}

	for(i=0;i<tim;i++)
	{
		Display();
	   	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;

	putSPIchar(kind);
	DelayNs(150);
	putSPIchar(kind);
	DelayNs(150);

	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++)
	{
		putSPIchar(BC);
		DelayNs(150);
		putSPIchar(BC);
		DelayNs(150);
	}

	putSPIchar(kind);
	DelayNs(150);
	putSPIchar(kind);
	DelayNs(150);
	for(i=i+1;i<BLenghtCount;i++)
	{
		putSPIchar(BC);
		DelayNs(150);
		putSPIchar(BC);
		DelayNs(150);
	}

	SendSS(ss_flag);
	SendSI(si_flag);

	if(SpeedDelay(mode & 0x03)) return 1;
	else return 0;
}
char		SendMiddleToEdge(uchar kind,uchar mode,uchar BC,uchar class)
{
	uchar	tim,i;
	for(i=1;i<left;i++)
	{
		putSPIchar(BC);
		DelayNs(150);
		putSPIchar(BC);
		DelayNs(150);
	}

	SendSS(1);
	SendSI(class);
	
	if(SpeedDelay(mode & 0x03)) return 1;
	else return 0;
}
char		SendEdgeToMiddle(uchar kind,uchar mode,uchar BC,uchar class)
{
	uchar	tim,i;
	for(i=1;i<left;i++)
	{
		putSPIchar(BC);
		DelayNs(150);
		putSPIchar(BC);
		DelayNs(150);
	}
	SendSS(1);
	SendSI(class);
	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)
		{
//			SendFirstToEnd(value,mode & 0x0f,CLASSNUMBER,1);
			for(tim=0;tim<val_lenght ;tim++)
			{
				if(SendFirstToEnd(value,mode & 0x0f,0,0)==1) return;
			}
			SendSS(1);
//			Delay(250);
			Display();
			Display();
			Display();
			Display();
			Display();
			
//			for(tim=0;tim<200;tim +=2) //20070715 modify
			for(tim=0;tim<200;tim +=4) 
			{
//				SendSI(2); //20070715 modify
				SendSI(4);
//				Delay(30);
				if(SpeedDelay(mode & 0x03)) 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,mode & 0x0f,0,0)==1) return;
			}
			SendSS(1);
			Display();
			Display();
			Display();
			Display();
			Display();
			
//			for(tim=0;tim<200;tim +=2) //20070715 modify
			for(tim=0;tim<200;tim +=4) 
			{
//				SendSI(2);//20070715 modify
				SendSI(4);
//				Delay(30);
//				if(SpeedDelay(mode & 0x03)) return ;
				if(SpeedDelay(0)) return ;
				
			}
		}
	}
}


void		GetMiddleColor(uchar value)
{
	switch(value)
	{
	case	ALLSTAR1:
		color1=BDATA | (SDATA2 <<GBIT)| (SDATA1 <<BBIT)| (SDATA1 <<RBIT);
		color2=BDATA | (SDATA3 <<GBIT)| (SDATA1 <<BBIT)| (SDATA1 <<RBIT);
		break;
	case	ALLSTAR2:
		color1=BDATA | (SDATA1 <<GBIT)| (SDATA2 <<BBIT)| (SDATA1 <<RBIT);
		color2=BDATA | (SDATA1 <<GBIT)| (SDATA3 <<BBIT)| (SDATA1 <<RBIT);
		break;
	case	ALLSTAR3:
		color1=BDATA | (SDATA1 <<GBIT)| (SDATA2 <<BBIT)| (SDATA2 <<RBIT);
		color2=BDATA | (SDATA1 <<GBIT)| (SDATA3 <<BBIT)| (SDATA3 <<RBIT);
		break;
	case	ALLSTAR4:
		color1=BDATA | (SDATA2 <<GBIT)| (SDATA1 <<BBIT)| (SDATA2 <<RBIT);
		color2=BDATA | (SDATA3 <<GBIT)| (SDATA1 <<BBIT)| (SDATA3 <<RBIT);
		break;
	case	ALLSTAR5:
		color1=BDATA | (SDATA2 <<GBIT)| (SDATA2 <<BBIT)| (SDATA1 <<RBIT);
		color2=BDATA | (SDATA3 <<GBIT)| (SDATA3 <<BBIT)| (SDATA1 <<RBIT);
		break;
	case	ALLSTAR0:
	default:
		color1=BDATA | (SDATA1 <<GBIT)| (SDATA1 <<BBIT)| (SDATA2 <<RBIT);
		color2=BDATA | (SDATA1 <<GBIT)| (SDATA1 <<BBIT)| (SDATA3 <<RBIT);
		break;
	}
}

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++)
	{
		putSPIchar(da1);
		DelayNs(50);
		putSPIchar(da1);
		DelayNs(50);
		SendSS(1);
		SendSI(loop1);		
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		putSPIchar(da2);
		DelayNs(50);
		putSPIchar(da2);
		DelayNs(50);
		SendSS(1);
		SendSI(loop1);		
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		putSPIchar(da3);
		DelayNs(50);
		putSPIchar(da3);
		DelayNs(50);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		putSPIchar(da4);
		DelayNs(50);
		putSPIchar(da4);
		DelayNs(50);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		putSPIchar(da5);
		DelayNs(50);
		putSPIchar(da5);
		DelayNs(50);
		SendSS(1);
		SendSI(loop1);
		i++;
		if(i>val_lenght)break;
		if(SpeedDelay(mode & 0x03)==1) return 1;

		putSPIchar(da6);
		DelayNs(50);
		putSPIchar(da6);
		DelayNs(50);
		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:
			putSPIchar(da5);
			DelayNs(50);
			putSPIchar(da5);
			DelayNs(50);
		case	4:
			putSPIchar(da4);
			DelayNs(50);
			putSPIchar(da4);
			DelayNs(50);
		case	3:
			putSPIchar(da3);
			DelayNs(50);
			putSPIchar(da3);
			DelayNs(50);
		case	2:
			putSPIchar(da2);
			DelayNs(50);
			putSPIchar(da2);
			DelayNs(50);
		case	1:
			putSPIchar(da1);
			DelayNs(50);
			putSPIchar(da1);
			DelayNs(50);
			break;
		default:
			break;
		}	
		if(FLenghtCount>val1)
		{
			i=val1;
			for(;i<FLenghtCount;i+=6)
			{
				putSPIchar(da6);
				DelayNs(50);
				putSPIchar(da6);
				DelayNs(50);
				putSPIchar(da5);
				DelayNs(50);
				putSPIchar(da5);
				DelayNs(50);
				putSPIchar(da4);
				DelayNs(50);
				putSPIchar(da4);
				DelayNs(50);
				putSPIchar(da3);
				DelayNs(50);
				putSPIchar(da3);
				DelayNs(50);
				putSPIchar(da2);
				DelayNs(50);
				putSPIchar(da2);
				DelayNs(50);
				if(BLenghtCount==0 && (i+6)>=FLenghtCount)break;
				else{
				putSPIchar(da1);
				DelayNs(50);
				putSPIchar(da1);
				DelayNs(50);
					}
			}
		}
		
	}

	i=init;
	for(;i<BLenghtCount;i++)
	{
		putSPIchar(UNLOCKDATA);
		DelayNs(50);
		putSPIchar(UNLOCKDATA);
		DelayNs(50);
	}	


}

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;

⌨️ 快捷键说明

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