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

📄 coding.c

📁 自定简单无线协议.由AVR编码发送,51中断接收解码, 误码率低, 但通信速率较低
💻 C
字号:
//ICC-AVR application builder : 2008-5-2 16:02:25
// Target : M8
// Crystal: 7.3728Mhz

#include <iom8v.h>
#include <macros.h>
unsigned int ms;

void delay(unsigned int t)
{	unsigned int i;unsigned char j;
	for(i=0;i<t;i++)
		for(j=80;j>0;j--);
}

void port_init(void)
{
 PORTB = 0x01;
 DDRB  = 0x01;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 
}

//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 500uSec
// actual value: 499.946uSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xF1; //setup
 TCNT1L = 0x9A;
 OCR1AH = 0x0E;
 OCR1AL = 0x66;
 OCR1BH = 0x0E;
 OCR1BL = 0x66;
 ICR1H  = 0x0E;
 ICR1L  = 0x66;
 TCCR1A = 0x00;
 TCCR1B = 0x01; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
 TCNT1H = 0xF1; //reload counter high value
 TCNT1L = 0x9A; //reload counter low value
 ms++;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer1_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x05; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void signal_0(void)
{	
	 ms=0;
	 while(ms==0) PORTB=PORTB&0xfe;
	 ms=0;
	 while(ms==0) PORTB=PORTB|0X01;
	
}

void signal_1(void)
{	 
	 ms=0;
	 while(ms==0) PORTB=PORTB&0xfe;
	 ms=0;
	 while(ms<3) PORTB=PORTB|0x01;
	
}

void signal_init(void)
{	
	 ms=0;
	 while(ms<4) PORTB=PORTB&0xfe;
	 ms=0;
	 while(ms<4) PORTB=PORTB|0x01;
	
}

void signal_end(void)
{	 ms=0;
	 while(ms==0) PORTB=PORTB&0xfe;
	
	 PORTB=PORTB|0x01;
} 

void coding(unsigned char num)
{	 unsigned char i,j,m;
	 for(j=0;j<6;j++)
	 {
	  	signal_init();
	 }
	 
	 for(m=0;m<1;m++)
   	 {
	 
	  	signal_0();
	 	signal_1();
		signal_0();
		signal_1();
	 
	 	for(i=0;i<8;i++)
	 	{	
	   
			if( ( num&(0x01<<i) )!=0)
				signal_1();
			else
				signal_0();
		 		
		}	
    	signal_init();
		signal_init();
	 }
		
	 signal_end();
}
//
void main(void)
{unsigned code;
 unsigned t=0;
 init_devices();
 DDRD=0x00;
 PORTD=0x01;

 //insert your functional code here...
 
 while(1)
 {	
 	
	/*
	
	while(PIND!=0x01)  ;
	while(PIND==0x01)  coding(0x11);
	coding(0xff);
	delay(2000);
	*/
	t++;
		if(t==0xff)  t=0;
		coding(t);
		delay(50000);
		
		
	
 }
 
 
 
}

⌨️ 快捷键说明

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