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

📄 cw1.c

📁 实现无线控制功能
💻 C
字号:
#include "iom32v.h"
#include "macros.h"
#define wuxian_js    PA3 //无线接收
#define power_led    PC6 //电源指示灯
#define study_key    PC1  //学习键
#define   UDRE  5	     //发送缓冲区为空
#define   DATA_REGISTER_EMPTY        (1<<UDRE)
#define  uchar  unsigned char
#define  ulong  unsigned long
ulong    count,count1;  //定时器2计数器
uchar    code[3],code1[3];
ulong data=0;
uchar forward[14]={0x00,0x7f,0x7f,0xff,0xff,0xfb,0x00,0x00,0x7f,
                    0xff,0xff,0xff,0xfd,0x00};
uchar back[14]={0x00,0x7f,0x7f,0xff,0xff,0xfd,0x00,0x00,0x7f,0xff,
                     0xff,0xff,0xfd,0x00};
uchar UP[7]={0xff,0x01,0x00,0x08,0x00,0x2f,0x38};
uchar DOWN[7]={0xff,0x01,0x00,0x10,0x00,0x2f,0x40};
uchar left[7]={0xff,0x01,0x00,0x04,0x3f,0x00,0x44}; 
uchar right[7]={0xff,0x01,0x00,0x02,0x3f,0x00,0x42};
uchar STOP[7]={0xff,0x01,0x00,0x00,0x00,0x00,0x01};
void delay_ms(void)                         //毫秒延时  
{     
     unsigned int i; 
     for (i=0;i<140;i++)  
          { 
          } 
} 

void delay_nms(unsigned int n)              //延时周期 
{ 
     unsigned int i; 
     for (i=0;i<n;i++) 
          { 
           delay_ms(); 
          } 
} 
void  port_init(void)
      {
	   PORTA = 0xff;
       DDRA  = 0x00;
       PORTB = 0x00;
       DDRB  = 0x00;
       PORTC = 0x01; //m103 output only
       DDRC  = 0x00;
       PORTD = 0x00;
       DDRD  = 0x00; 
	  }
void uart0_init(void)
      {
       UCSRB = 0x00; //disable while setting baud rate
       UCSRA = 0x00;
       UCSRC = BIT(URSEL) | 0x06;
       UBRRL = 0x67; //set baud rate lo6719
       UBRRH = 0x00; //set baud rate hi
       UCSRB = 0x08;
      }    
//发送单字节字符
void  uart_transmit_byte( unsigned char data)
     {
       while(!(UCSRA&DATA_REGISTER_EMPTY));
       UDR=data;
     }	
	 
//TIMER2 initialize - prescale:8
// desired value: 100uSec
// actual value: 100.000uSec (0.0%)
void timer2_init(void)
{
 TCCR2 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT2 = 0xCE; //setup
 OCR2  = 0x32;
 TCCR2 = 0x02; //start
}

#pragma interrupt_handler timer2_ovf_isr:6
void timer2_ovf_isr(void)
{
 TCNT2 = 0xCE; //reload counter value
 if(count<65535){++count;}
 if(count1<65535){++count1;}
}  
//==============================================================================
void eeprom_writ(uchar dz,uchar sj) //写EEPROM  
{
     CLI(); //清总中断
	 while(EECR & (1<<EEWE)){;}
	 EEARH=0x00;
	 EEARL=dz;
	 EEDR=sj;
	 EECR |= (1<<EEMWE);
	 EECR |= (1<<EEWE);
     SEI();
}
//==============================================================================
uchar eeprom_read(uchar dz)     //读EEPROM
{
    CLI(); //清总中断
	while(EECR & (1<<EEWE)){;}
	EEARH=0x00;
	EEARL=dz;
	EECR |= (1<<EERE);
	SEI();
	return(EEDR); 
}  
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer2_init();
 uart0_init();
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x40; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
uchar decode(void)
     {
	   uchar pin_var,i;
	   uchar flag=0;
	   signed char j;
	   ulong data=0;
	   count1=0;
	   DDRA&=~(1<<wuxian_js);PORTA|=(1<<wuxian_js);
	   while(1)
	       {
		    if(count1>250){return(0xff);}
		    pin_var=PINA&(1<<wuxian_js);
			if(flag==1){break;}
			while(pin_var==0)
			     {
				   count=0;
				   if(count1>250){return(0xff);}
				   while(1)
				   {
				     pin_var=PINA&(1<<wuxian_js);if(count1>250){return(0xff);}
					 if(pin_var!=0)
					 {
				        if((count>130)&&(count<155))  {flag=1;break;}
					    else  {flag=0;break;}
					 }	
				   }	
				   break;
				 }
				 if(flag==1) break;
			} 
			      
				   for(i=0;i<24;i++)
				       {
					      
					      count=0;
						  data<<=1;                       //数据左移
					      while (1)           //高位计数 
                            {   
							   pin_var=PINA&(1<<wuxian_js);
							   if(pin_var==0) {break;}
							   if(count>200)  return 0xff; 
							 }
							 if(count>1&&count<8)  data+=0;
							 else //if(count>13&&count<15)    
							  data|=1;
                            
							 count=0;
			              while(1)
			                {
    			             WDR();
				             pin_var=PINA&(1<<wuxian_js);
				             if(pin_var!=0){break;}
				             if(count>200)
    			               {
        			              return(0xff);
    			               }
			                } 
					   }	
			if(i==24)
			   {		   
				for(j=2;j>=0;j--)
				   {
				     code[j]=data;
					 data>>=8;
				   }  
			  }	   
		 return 1;	 	   		 
	 }
	 
	void ana_decode(void)
   {
	uchar key_var;
	uchar i,flag=0;
	uchar  code_buf[7];
	DDRC&=~(1<<study_key);PORTC|=(1<<study_key);
	key_var=PINC&(1<<study_key);
	if(key_var==0)
	   {
	      while(1)
		     {
	          while(1)
    		   {
    		       flag=decode();
				   if(flag==1)  {flag=0;break;}
    		   }
			   for(i=0;i<2;++i){code1[i]=code[i];}
			   while(1)
    		   {
    		       flag=decode();
				   if(flag==1)  {flag=0;break;}
			
    		   }
			   for(i=0;i<2;++i){if(code1[i]!=code[i]){break;}}
			   if(i==2){flag=1;break;}
	        }
			 if(flag==1)	
			    {    
				    for(i=0;i<2;++i)
    		           {
   				         eeprom_writ(i,code[i]);
   			            } 
				flag=0;	
				}	
			  return;	 
		}		  
	 flag=decode(); 
	 if(flag==1)
	    {
		     for(i=0;i<2;++i)
    		           {
   				         if(eeprom_read(i)!=code[i])  {flag=0xff;break;}
   			            } 
	        if(flag==1)	
	          {       
	             flag==0;
	             if(code[2]==0xc0)
				       {
					     for(i=0;i<14;i++)
                         {  
                          uart_transmit_byte(forward[i]);
                         }
					   }
			     else if(code[2]==0x30)
				       {
					     for(i=0;i<14;i++)
                         {  
                        uart_transmit_byte(back[i]);
                         }
					   }		   
	             else if(code[2]==0xf0)   //上
			           {
			          for(i=0;i<7;i++)
                         {  
                       uart_transmit_byte(UP[i]);
					       code_buf[i]=UP[i];
                         }
	                  }
			      else if(code[2]==0x0c)//下
			          {
			            for(i=0;i<7;i++)
                            {  
                              uart_transmit_byte(DOWN[i]);
							  code_buf[i]=DOWN[i];
                            }
			           }
			
    		      else if(code[2]==0xcc)//左
    		          {
			           for(i=0;i<7;i++)
                          {  
                            uart_transmit_byte(left[i]);
							code_buf[i]=left[i];
                          } 
    		         }
    		     else if(code[2]==0x3c)//右
    		         {
    		          for(i=0;i<7;i++)
                         {  
                          uart_transmit_byte(right[i]);
                         } 
						 code_buf[i]=right[i];
                     } 
					while(1)
					   {
					      flag=decode();
					      while(flag==0xff)
						       {
							     flag=0;
							     delay_nms(25);
								 flag=decode();
								 while(flag==0xff)
								      {
										for(i=0;i<7;i++) 
                                        uart_transmit_byte(STOP[i]);
										if(i==7)  {flag=1;break;}
									  }
								  if(flag==1)  {flag=0;return;}	  
							   }
							for(i=0;i<7;i++) 
                              uart_transmit_byte(code_buf[i]);   
					   }
		         }	
		  }
		   else if(flag==0xff)
                {
	              flag=0;
	           for(i=0;i<7;i++) 
                    uart_transmit_byte(STOP[i]);
	            }	  
  }   
void   main()
    {
	   init_devices();
	   //DDRA&=~(1<<wuxian_js);
	  // PORTA|=(1<<wuxian_js);
	   DDRD=0x02; 
	   //DDRC|=(1<<power_led);
	   //PORTC&=~(1<<power_led);
	   DDRA |=0x40;                      //485使能
       PORTA|=0x40;               // 
	  while(1)
	    {
		 ana_decode();
		}
	}	 
	 

⌨️ 快捷键说明

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