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

📄 main.c

📁 这一个用ATTINY26做的433M无线遥控解码程序
💻 C
字号:

//ICC-AVR application builder : 2007-11-18 19:20:12
// Target : T26
// Crystal: 2.0000Mhz



#include <iot26v.h>
#include <macros.h>



#define		uchar		unsigned char
#define		uint		unsigned int

#define 	UP			0xC0
#define 	STOP		0xA0
#define 	DOWN		0x60


#define		SET_LED1		PORTB |=  (1<<PB5)
#define		CLR_LED1		PORTB &= ~(1<<PB5)

#define		SET_LED2		PORTB |=  (1<<PB4)
#define		CLR_LED2		PORTB &= ~(1<<PB4)

#define 	SET_RLY1		PORTA |=  (1<<PA1)
#define 	CLR_RLY1 		PORTA &= ~(1<<PA1)

#define 	SET_RLY2		PORTA |=  (1<<PA0)
#define 	CLR_RLY2 		PORTA &= ~(1<<PA0)



uchar	flag_4ms;
uchar	led_time;
uint	key,key1,key2;

uchar	flag,rxd_flag;
uchar	start,bits;
uchar	timeH,timeL;
uchar	datas;
uchar	addrH,addrL;
uint	addr;
uchar	rxd_data;


void port_init(void)
{
	PORTB = 0x0F;
	DDRB  = 0x30;
	 
	PORTA = 0xE0;
	DDRA  = 0x03;
}


void timer0_init(void)
{
	 TCCR0 = 0x00; //stop
	 TCNT0 = 0x83; //set count
	 TCCR0 = 0x03; //start timer
}

void timer1_init(void)
{
	 TCCR1B = 0x00; //stop
	 TCNT1 = 0x06;
	 OCR1A = 0xFA;
	 OCR1B = 0xFA;
	 OCR1C = 0x80;
	 TCCR1A = 0x00;
	 //TCCR1B = 0x05; //start Timer
}


void init_eeprom(void)
{
 	 EECR = 0x07;
}



void EEPROM_write(unsigned char Address, unsigned char Datas)
{
	while(EECR & (1<<EEWE));// 等待上一次写操作结束 
	
	EEARL= Address;			// 设置地址和数据寄存器 
	EEDR = Datas;
	EECR |= (1<<EEMWE);		// 置位EEMWE
	EECR |= (1<<EEWE);		// 置位EEWE 以启动写操作E
}


unsigned char EEPROM_read(unsigned char uiAddress)
{
	while(EECR&(1<<EEWE));	//等待上一次写操作结束
	EEARL = uiAddress;		// 设置地址寄存器
	EECR |= (1<<EERE);		// 设置EERE 以启动读操作
	return EEDR;			// 自数据寄存器返回数据
}







//call this routine to initialize all peripherals
void init_devices(void)
{
	 CLI(); 		//disable all interrupts
	 
	 port_init();
	 timer0_init();
	 timer1_init();
	 init_eeprom();
	 
	 MCUCR = 0x02;
	 TIMSK = 0x06; 	//timer interrupt sources
	 GIMSK = 0x40; 	//interrupt sources
	 PLLCSR = 0x00; //PLL
	 WDR(); 		//this prevents a timout on enabling
 	 WDTCR = 0x08; 	//WATCHDOG ENABLED - dont forget to issue WD
 	 
	 SEI(); 		//re-enable interrupts
}


/*---------------------------------------------------------------
   		按键扫描
----------------------------------------------------------------*/

void key_scan(void)
{
	uchar temp;
	
	temp = PINA & 0xE0;

	if(temp==UP) 	
	{	
		SET_LED2;
		//if(key<500) 	
		{
			key ++;
			if(key>=5)
			{
				CLR_RLY2;
				asm("nop"); 
				asm("nop");
				SET_RLY1;
			}
		}	
	}

	else if(temp==STOP) 	// k1
	{	
		SET_LED2;
		//if(key1<500) 	
		{	
			key1 ++;
			if(key1>=5)		// 短按
			{
				CLR_RLY1;
				CLR_RLY2;
			}
		}
	}

	else if(temp==DOWN) 	// k2
	{	
		SET_LED2;
		//if(key2<500) 	
		{	
			key2 ++;
			if(key2>=5)		// 短按
			{
				CLR_RLY1;
				asm("nop"); 
				asm("nop");
				SET_RLY2;
				
			}
		}
	}
	else 
	{
		key = key1 = key2 = 0;
		//CLR_LED2;
	}
}



void Remote_set(void)
{
	if(led_time<50) led_time ++;
 	else CLR_LED2;
	
	if(rxd_flag)
	{
		rxd_flag = 0;
		if(addr==0xaaaa)	// 地址正确
		{
			SET_LED2;
			led_time = 0;
			if(rxd_data==0x03) 
			{
				CLR_RLY2;
				asm("nop"); 
				asm("nop");
				SET_RLY1;
			}	
			else if(datas==0xc0) 
			{
				CLR_RLY1;
				asm("nop"); 
				asm("nop");
				SET_RLY2;
			}
			else if(datas==0x0c || datas==0x30) 
			{
				CLR_RLY1;
				CLR_RLY2;
			}
		}
	}
}



void main(void)
{
	init_devices();
	
	SET_LED1;
	
	while(1)
	{
		if(flag_4ms)
		{
			flag_4ms = 0;
			asm("WDR");	
			Remote_set();
			key_scan();
		}
	}
}



#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
	uchar	temp;
	uint	i;

	if(!start)
	{
		if(MCUCR==0x02)
		{
			MCUCR  = 0x03;
			TCNT1  = 0x00;
 			timeH  = 0x00;
 			timeL  = 0x00;
 			TCCR1B = 0x05;
 			
		}
		else if(MCUCR==0x03)
		{
			TCCR1B = 0x00;
			MCUCR  = 0x02;

			i = timeH*256 + TCNT1;
			if(i>1620 && timeH<1800) 	// 同步头
			{
				start  = 1;
				TCNT1  = 0x00;
	 			timeH  = 0x00;
	 			timeL  = 0x00;
	 			TCCR1B = 0x05;
	 			
	 			bits   = 0;
	 			addrL  = 0;
				addrH  = 0;
	 			datas  = 0;
	 			flag   = 0;
			}	
		}
	}
	else 
	{
 		if(MCUCR==0x02)		// 下降沿读时间
 		{	
 			TCCR1B = 0x00;
 			MCUCR  = 0x03;
 			if(timeH>0) start = 0; // 超时
 			else 
 			{
	 			timeL = TCNT1;
	 			if(timeL>40 && timeL<60) temp = 0x00;
	 			else if(timeL>150 && timeL<170)  temp = 0x80;

	 			if(flag==0)			// 地址低位
	 			{
		 			addrL >>= 1;
		 			addrL |= temp;
		 		}
		 		else if(flag==1)	// 地址高位
		 		{
		 			addrH >>= 1;
		 			addrH |= temp;
		 		}
		 		else if(flag==2) 	// 数据
		 		{
		 			datas >>= 1;	
		 			datas |= temp;
		 		}
	
	 			bits ++;
	 			if(bits==8)  	  flag = 1;	
				else if(bits==16) flag = 2;	
	 			else if(bits>=24) 
	 			{
	 				start = 0;
	 				rxd_flag = 1;
	 				rxd_data = datas;
	 				addr = addrH*256 + addrL;
	 			}
			}
 		}
 		else if(MCUCR==0x03)	// 上升沿开始计时
 		{
 			MCUCR  = 0x02;
 			
 			TCNT1  = 0x00;
 			timeH  = 0x00;
 			timeL  = 0x00;
 			TCCR1B = 0x05;
 		}
	}	
}




#pragma interrupt_handler timer1_ovf_isr:6
void timer1_ovf_isr(void)
{
	TCNT1 = 0x00; //reload counter	
	timeH ++;
}



#pragma interrupt_handler timer0_ovf_isr:7
void timer0_ovf_isr(void)
{
 	TCNT0 = 0x83; //reload counter value
 	
 	flag_4ms = 1;
}


⌨️ 快捷键说明

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