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

📄 rx_data.c

📁 本软件是基于nRf2401的无线节点
💻 C
字号:
#include <avr/io.h>
#include <avr/delay.h> 
#include <avr/signal.h>
#include <avr/interrupt.h>

#define SET_PWR PORTA|=(1<<7)
#define CLR_PWR PORTA&=~(1<<7)

#define SET_CE PORTA|=(1<<6)
#define CLR_CE PORTA&=~(1<<6)

#define SET_CS PORTA|=(1<<5)
#define CLR_CS PORTA&=~(1<<5)

#define SET_DR1 PORTA|=(1<<4)
#define CLR_DR1 PORTA&=~(1<<4)

#define SET_CLK1 PORTA|=(1<<1)
#define CLR_CLK1 PORTA&=~(1<<1)

#define SET_DATA PORTA|=(1<<0)
#define CLR_DATA PORTA&=~(1<<0)

//////////////变量定义/////////////////////////////////

unsigned char InitData[18];            //存储配置字
unsigned char temp,RXData;
unsigned int i,j;

////////////////////函数声明//////////////////////////////

void init_io();    //初始化io口
void nrf2401_on();    //2401上电
void build_init_word_RX();       //建立配置字
void init_nrf2401();         //配置2401
void read_data();            //读取数据
  

///////////////////////函数定义///////////////////////////

int main()
{
	MCUCR = 0x00;
	GICR  = 0x00;
	init_io();
	nrf2401_on();
	build_init_word_RX();
	init_nrf2401();
	
	DDRA&=~(1<<0);
	while(1)
	{	
		SET_CE;
		_delay_us(202);	
		if(PINA&(1<<4))		
		{
			read_data();
			CLR_CE;
	  
			_delay_ms(500);
		}
	}
	return 0;
}

void init_io()
{
	PORTA=0x00;         //pA口初始化为低电平
	DDRA=0xEF;         //pA4为输入,其他为输出

	PORTB=0x00;
	DDRB=0xFF;             //pB为输出

//	DDRC=0x00;
//	PORTC=0x02;

	DDRD=0xFF;
	PORTD=0x00;
}


void nrf2401_on()
{
	SET_PWR;
	_delay_ms(3);	//延时3ms后完成上电
}

void build_init_word_RX()
{
	InitData[0]=0x8E;// 
	InitData[1]=0x08;// 
	InitData[2]=0x1C;// 
	InitData[3]=0x08;		//RX receiver 2, Datalength 8bit(1 byte)
	InitData[4]=0x08;		//RX receiver 1, Datalength 8bit(1 byte)
	InitData[5]=0x00;
	InitData[6]=0x1c;
	InitData[7]=0xcc;
	InitData[8]=0xcc;
	InitData[9]=0xcc;
	InitData[10]=0x00;
	InitData[11]=0xcc; 		//RX receiver 1 address   High byte
	InitData[12]=0xcc;		//RX receiver 1 address   High byte
	InitData[13]=0xcc;		//RX receiver 1 address   Low byte
	InitData[14]=0xcc;		//RX receiver 1 address   Low byte
	InitData[15]=0x83;		//this is for 32 bit address(Bit7-Bit2), 16bit CRC(Bit1), CRC enable(Bit0) for final chip	
	InitData[16]=0x4f;		//One receiver(Bit7), shock Mode(Bit6), Datarate(Bit5) 250K, crystal(Bit4-Bit2), RF Power(Bit1-Bit0)
	InitData[17]=0x05;		//Channel Number(Bit7-Bit1), RX/TX mode(Bit0)
}

void init_nrf2401()
{
	CLR_CE;						
	SET_CS;						//进入配置模式
	_delay_us(5);
	for (i=0;i<18;i++)     //装入配置字,最高为先进
	{
		temp=InitData[i];
		for (j=0;j<8;j++)
		{
			if(0x80&temp)
				SET_DATA;
			else 
				CLR_DATA;
			_delay_us(500);		
			SET_CLK1;
			_delay_us(500);
			CLR_CLK1;
			temp=temp<<1;
		}
	}
	_delay_us(500);
	CLR_CS;						//cs为低完成配置
	CLR_DATA;
	
}
 
void read_data()
{      
			temp=0;
			CLR_CLK1;
			for(j=0;j<8;j++)
			{	
				temp=temp<<1;	
				SET_CLK1;
				_delay_us(100);
				CLR_CLK1;	
				if(PINA&(1<<0))				
					temp|=(1<<0);
				_delay_us(100);

				
				/*temp=temp<<1;	
				SET_CLK1;
				_delay_us(500);
					
				if(PINA&(1<<0))				
					temp|=(1<<0);
				CLR_CLK1;
				_delay_us(500);	*/											
			}
			RXData=temp;
			PORTB = RXData;
	
}

















⌨️ 快捷键说明

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