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

📄 transmit.c

📁 采用大规模专用射频集成电路nRF2401A设计、实现短距离无线数据传输设备
💻 C
字号:

#include <iom16v.h>
#include <macros.h>


#define	SETBIT(x,y) (x|=(1<<y))      //set bit y in byte x
#define	CLRBIT(x,y) (x&=(~(1<<y)))   //clear bit y in byte x
#define	CHKBIT(x,y) ((x&(1<<y))>>y)       //check bit y in byte x

#define PWR_UP  	 PD7
#define CE      	 PD6
#define CS			 PD5
#define CLK1		 PD4
#define DATA   		 PD3
#define DR1			 PD2

//使能第二频道
#define RX2_EN  	 15		

//通信方式设置	 
#define CM      	 14		

//发射数据速率		 
#define RFDR_SB		 13

//晶振频率 3位
#define XO_F		 10

//发射输出电源 2位
#define RF_PWF		 8

//频道设置 7位
#define RF_CH#   	 1

//接受或发射操作 RXEN
#define RX_EN	     0

#define set_pwr_up()	SETBIT(PORTD,PWR_UP)
#define clr_pwr_up()    CLRBIT(PORTD,PWR_UP)
#define set_ce()  	    SETBIT(PORTD,CE)
#define clr_ce()  		CLRBIT(PORTD,CE)
#define set_cs()   		SETBIT(PORTD,CS)
#define clr_cs()        CLRBIT(PORTD,CS)
#define set_clk1()   	SETBIT(PORTD,CLK1)
#define clr_clk1()      CLRBIT(PORTD,CLK1)
#define set_data()  	SETBIT(PORTD,DATA)
#define clr_data()  	CLRBIT(PORTD,DATA)
#define chk_dr1()  		CHKBIT(PIND,DR1)
#define chk_data()  	CHKBIT(PIND,DATA)

unsigned char buffer[32]="";

unsigned char adrdata[9]={0xaa,0xaa,0x1a,0x1,0x2,0x3,0x4,0x5,0x6};
unsigned char buf_cnt=0;
volatile unsigned int num=0;//发送次数
void delay_us(unsigned int i)//8.000M晶振 延时i+2us
{
 if(i==0)
 return;
 while(i--)
 {
 NOP();
 NOP();
 }
 
}
;

unsigned char config[15]=

{		
	0x20,			     			 	  //接受频道2有效数据的长度
    0x30,					 	 		  //接受频道1有效数据的长度  48bit
  	0x0a,0x0b,0x0c,0x0d,0x0e,			  //接受频道2的地址,最高为5个字节
    0xab,0xac,0x28,0x12,0x1a,			  //接受频道1的地址,最高为5个字节
    0x79,								  //接受频道地址位数(6位)30bit;8位或16位CRC校验(1位);使能CRC校验(1位),
    0x47,0x64			 				  //常用器件配置
}
;
//TIMER1 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1000mSec
// actual value: 1000.000mSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x85; //setup
 TCNT1L = 0xee;
 /*TCNT1H = 0xf3; //0.1s
 TCNT1L = 0xcb;*/
 TCCR1A = 0x00;
 TCCR1B = 0x04; //start Timer
}

void trans(unsigned char *p,unsigned char len)//发送len个字节的数据
{
 signed char  i;
 unsigned char len1;
 for(len1=0;len1<len;len1++)
 {
	 for(i=7;i>=0;i--)
	 {
		 if(CHKBIT(p[len1],i)!=0)
		 set_data();
		 else
		 clr_data(); 		  
		   NOP();	
		   NOP();
		   NOP();		 
		   NOP();		   // Ts 延时大于500ns
		 set_clk1(); 
		   NOP(); 
		   NOP();	
		   NOP();
		   NOP();    //延时大约500ns
		 clr_clk1();
	 }
 }
}
void trans6(unsigned char p)//发送1个字节的低六位数据
{
 signed char  i;

 {
	 for(i=5;i>=0;i--)
	 {
		 if(CHKBIT(p,i)!=0)
		 set_data();
		 else
		 clr_data(); 		  
		   NOP();	
		   NOP();
		   NOP();		 
		   NOP();		   // Ts 延时大于500ns
		 set_clk1(); 
		   NOP(); 
		   NOP();	
		   NOP();
		   NOP();    //延时大约500ns
		 clr_clk1();
	 }
 }
}
void transmit()//发送地址与数据
{
 DDRD=0XF8;
 set_ce();
 delay_us(5);
 trans6(0xaa);
 trans(adrdata,9);
 clr_ce();
 
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)//定时器中断函数,每隔1s发送一次,共发射200次
{
 //TIMER1 has overflowed
TCNT1H = 0x85; //setup
 TCNT1L = 0xee;



 PORTB=PORTB^BIT(PB1);
 transmit();
 num++;
 if(num>=200)
 TIMSK = 0x00;
 
 
}

void init_conf()
{
 DDRD=0XF8;
 clr_ce();
 set_cs();
 delay_us(5);
 trans(config,15); 
 clr_cs(); 
 }
 

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x0f;
 PORTB = 0x00;
 DDRB  = 0x02;
 PORTC = 0x00; 
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0xF8;
}

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

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x04; //timer interrupt sources
 
 //all peripherals are now initialized
}

//
void main(void)
{ 
 CLI();
 init_devices();
 set_pwr_up();
 delay_us(3000);
 init_conf();
  
timer1_init();
SEI();
 //transmit(); 
while(1);
 //insert your functional code here...
}

⌨️ 快捷键说明

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