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

📄 cyrf6936.c

📁 无线芯片CYRF6936操作程序
💻 C
字号:
#include "iom88v.h"
#include "macros.h"
#include "spi.h"
#include "myconfig.h"

unsigned char cyrf_buf[16];
extern unsigned char send_length,*send_buf_pointer,recv_buf[16];
extern unsigned char cyrf6936_rst_counter;

unsigned char cyrf6936_send_buf[16],cyrf6936_recv_buf[16];
extern unsigned char SLEEP_STATUS,RSSI_DATA;
extern unsigned char eeprom_read_byte(unsigned int rd_addr);
extern void eeprom_write_byte(unsigned int wr_addr,unsigned char wr_data);
extern void watchdog_init_1s(void);
extern void timer1_init_10ms(void);
extern void timer1_init_100ms(void);

void cyrf6936_recv_pro(void);

void cyrf6936_init(void)
{
   unsigned char i;

    PORTB |=0x01;
	NOP();
    PORTB &=0xFE;	
	for(i=0;i<50;i++);
    CLR_SS;
	SpiWriteByte(0x9D);    //设置0x1D
	SpiWriteByte(0x01);
	SET_SS; 
	CLR_SS;
	SpiWriteByte(0x80);   //设置频道号,地址0x00
	SpiWriteByte(ch0_number);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0xB2);    //设置AUTO_CAL_OFFSET_ADR,0x32
	SpiWriteByte(0x3C);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0xB5);    //设置AUTO_CAL_OFFSET_ADR,0x35
	SpiWriteByte(0x14);
	SET_SS;	
	/*CLR_SS;	
	SpiWriteByte(0xA8);    //设置CLK_EN_ADR,0x28
	SpiWriteByte(0x02);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0xA7);    //设置CLK_OFFSET_ADR,0x27
	SpiWriteByte(0x02);
	SET_SS;*/
	CLR_SS;
	SpiWriteByte(0x9C);    //设置TX_OFFSET_MSB_ADR,0x1C
	SpiWriteByte(0x05);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x9B);    //设置TX_OFFSET_MSB_ADR,0x1B
	SpiWriteByte(0x55);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x92);   //DATA64_THOLD_ADR ,0x12
	SpiWriteByte(0x0E);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x90);   //0x10
	SpiWriteByte(0xEE);
	SET_SS;	
	CLR_SS;
	SpiWriteByte(0x86);  //0x06
	SpiWriteByte(0x8A);
	SET_SS;
	i=eeprom_read_byte(send_power);
	i&=0x07;
	i|=0x28;
	CLR_SS;
	SpiWriteByte(0x83);   //0x03
	SpiWriteByte(i);   //  64chip-code,8DR mode,-25dBm
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x8b);   //电沿管理,PMU EN ENABLE,PMU SEN DISABLE
	SpiWriteByte(0x80);
	SET_SS;
	
	CLR_SS;
	SpiWriteByte(0x8F);   //转换为接收状态
	SpiWriteByte(0x33);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x85);   //启动接收
	SpiWriteByte(0x82);
	SET_SS;
}

void cyrf6936_send(unsigned char send_length,unsigned char send_buf[16])
{
	unsigned char i;

	CLR_SS;
	SpiWriteByte(0x8F);   //转换为发送状态
	SpiWriteByte(0x27);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x82);
	SpiWriteByte(0x40);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0xA0);
	for(i=0;i<send_length;i++)
	{
	    SpiWriteByte(send_buf[i]);
	}
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x81);
	SpiWriteByte(send_length);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x82);
	SpiWriteByte(0x83);
	SET_SS;
}

void ext_int0_init(void)
{
    EICRA =0x02;   //下降沿产生外部中断0请求
	EIMSK =0x01;
	DDRD  &=0xFB;
	PORTD |=0x04;
}

void set_recv_status(void)
{
    unsigned char a;
	
    CLR_SS;
    SpiWriteByte(0x05);
	a=SpiReadByte();
	SET_SS;
	if((a&0x80)>0)
	  return;
	
	CLR_SS;
	SpiWriteByte(0x8F);   //转换为接收状态
	SpiWriteByte(0x33);
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x85);   //启动接收
	SpiWriteByte(0x82);
	SET_SS;
}
#pragma interrupt_handler cyrf6936_isr: 2
void cyrf6936_isr(void)     //外部中断0,处理来自CYRF6936的中断
{
   
   unsigned char temp1,temp2;
   
    CLI();	       //关闭所有中断
	CLR_SS;
	SpiWriteByte(0x04);
	temp1=SpiReadByte();
	SET_SS;
	CLR_SS;
	SpiWriteByte(0x07);
	temp2=SpiReadByte();
	SET_SS; 	
	if((temp1&0x02)>0)
	{
	   set_recv_status();
	}
	else if((temp2&0x02)>0)
	{
	   CLR_SS;
	   SpiWriteByte(0x0A);
	   temp2=SpiReadByte();
	   SET_SS;
	   temp2 %=17;
	   CLR_SS;
	   SpiWriteByte(0x21);
	   for(temp1=0;temp1<temp2;temp1++)
	   {
	       cyrf6936_recv_buf[temp1]=SpiReadByte();	       
	   }
	   SET_SS;
	   set_recv_status();
	   if(temp2>0)
	   {
	       cyrf6936_recv_pro();
	   }
	}
	SEI();    //开中断
}

/*   收到6936数据处理    */
void cyrf6936_recv_pro(void)
{	  
	  if((cyrf6936_recv_buf[0]!=0x55)||(cyrf6936_recv_buf[1]!=0xaa))    //判断数据头是否正确
	  {
	      return;
	  }
      if((cyrf6936_recv_buf[3]!=eeprom_read_byte(card_id))||(cyrf6936_recv_buf[4]!=eeprom_read_byte(card_id+1))||(cyrf6936_recv_buf[5]!=eeprom_read_byte(card_id+2))||(cyrf6936_recv_buf[6]!=eeprom_read_byte(card_id+3)))
	  {
	      return;
	  }
	  
	  if(cyrf6936_recv_buf[2]==0x13)	
	  {
			eeprom_write_byte(card_id,cyrf6936_recv_buf[7]);
			eeprom_write_byte(card_id+1,cyrf6936_recv_buf[8]);
			eeprom_write_byte(card_id+2,cyrf6936_recv_buf[9]);
			eeprom_write_byte(card_id+3,cyrf6936_recv_buf[10]);
			
			cyrf6936_send_buf[0]=0x55;
			cyrf6936_send_buf[1]=0xaa;
			cyrf6936_send_buf[2]=0x13;
			cyrf6936_send_buf[3]=eeprom_read_byte(card_id);
			cyrf6936_send_buf[4]=eeprom_read_byte(card_id+1);
			cyrf6936_send_buf[5]=eeprom_read_byte(card_id+2);
			cyrf6936_send_buf[6]=eeprom_read_byte(card_id+3);
			cyrf6936_send_buf[7]=cyrf6936_send_buf[3];
			cyrf6936_send_buf[8]=cyrf6936_send_buf[4];
			cyrf6936_send_buf[9]=cyrf6936_send_buf[5];
			cyrf6936_send_buf[10]=cyrf6936_send_buf[6];
			cyrf6936_send_buf[11]=(cyrf6936_send_buf[2]+cyrf6936_send_buf[3]+cyrf6936_send_buf[4]+cyrf6936_send_buf[5]+cyrf6936_send_buf[6]+cyrf6936_send_buf[7]+cyrf6936_send_buf[8]);
			cyrf6936_send_buf[11]=(cyrf6936_send_buf[15]+cyrf6936_send_buf[9]+cyrf6936_send_buf[10]);
			cyrf6936_send(12,cyrf6936_send_buf);
			if((TIFR1&0x02)>0)
	        TIFR1 |=0x02;
			timer1_init_10ms();
		}
        else if(cyrf6936_recv_buf[2]==0x12)	
		{
		    if(cyrf6936_recv_buf[7]<8)
            {
		        eeprom_write_byte(send_power,cyrf6936_recv_buf[7]);
            }
			cyrf6936_send_buf[0]=0x55;
			cyrf6936_send_buf[1]=0xaa;
			cyrf6936_send_buf[2]=0x12;
			cyrf6936_send_buf[3]=cyrf6936_recv_buf[3];
			cyrf6936_send_buf[4]=cyrf6936_recv_buf[4];
			cyrf6936_send_buf[5]=cyrf6936_recv_buf[5];
			cyrf6936_send_buf[6]=cyrf6936_recv_buf[6];
			cyrf6936_send_buf[7]=eeprom_read_byte(send_power);
			cyrf6936_send_buf[8]=(cyrf6936_send_buf[2]+cyrf6936_send_buf[3]+cyrf6936_send_buf[4]+cyrf6936_send_buf[5]);
			cyrf6936_send_buf[8]=(cyrf6936_send_buf[10]+cyrf6936_send_buf[6]+cyrf6936_send_buf[7]);
			cyrf6936_send(9,cyrf6936_send_buf);
			if((TIFR1&0x02)>0)
	        TIFR1 |=0x02;
			timer1_init_10ms();
		}
}

⌨️ 快捷键说明

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