本帖最后由 77114810 于 2016-11-24 15:59 编辑
接收程序:
#include <iom128v.h>
#include <macros.h>
#include "LpRegs.h"
#define CLR_SS PORTB&=~BIT(0)
#define SET_SS PORTB|=BIT(0)
#define F_CPU 7372800
#define CYCLES_PER_US ((F_CPU+500000)/1000000)
unsigned char cyrf6936_recv_buf[];
void delay_us(unsigned short time_us)//延时部分
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = (time_us+3)/5*CYCLES_PER_US;
for (i=0; i < delay_loops; i++) {};
}
void delay_ms(unsigned char time_ms)
{
unsigned short int i;
for (i=0; i < 10; i++) {delay_us(100);}
return;
}
void Delay_s(unsigned char Times)
{
unsigned char i;
for (i=0; i<Times; i++)
{ delay_ms(250);
delay_ms(250);
delay_ms(250);
delay_ms(250);
}
}
void spi_init(void) //spi部分
{
SPCR=0x50;
SPSR = 0x00;
}
void SpiWriteByte(unsigned char byte)
{
SPDR = byte;
while (!(SPSR & 0x80));
byte = SPDR;
}
unsigned char SpiReadByte(void)
{
SPDR = 0x00;
while (!(SPSR & 0x80));
return SPDR;
}
void write_byte(unsigned char addr,unsigned char data)
{
addr = addr|0x80;
CLR_SS;
SpiWriteByte(addr);
SpiWriteByte(data);
SET_SS;
}
unsigned char read_byte(unsigned char addr)
{
unsigned char buffer;
CLR_SS;
SpiWriteByte(addr);
buffer = SpiReadByte();
SET_SS;
return buffer;
}
void MyRadioInit() //cy6936的初始化
{
SET_SS;
write_byte(MODE_OVERRIDE_ADR, RST);
write_byte(AUTO_CAL_TIME_ADR, AUTO_CAL_TIME_MAX);
write_byte(AUTO_CAL_OFFSET_ADR, AUTO_CAL_OFFSET_MINUS_4);
write_byte(IO_CFG_ADR, IRQ_POL);
write_byte(XTAL_CFG_ADR, 0x08);
write_byte(RX_CFG_ADR,
((RX_CFG_RST | FASTTURN_EN | LNA_EN ) & ~( HI | RXOW_EN | AUTO_AGC_EN)));
write_byte(0x06,0x4A);
write_byte(TX_OFFSET_LSB_ADR, 0x55);
write_byte(TX_OFFSET_MSB_ADR, 0x05);
write_byte(TX_CFG_ADR, TX_CFG_RST | DATMODE_8DR);
}
void main(void)
{
PORTB=PORTB|0b11111111;
DDRB=0b11110111;
DDRD=0b11111010;
PORTD=0xff;
PORTE=0xFF;
DDRE=0b11111111;
spi_init();
MyRadioInit();
write_byte(0x00,0x48);
write_byte(0x22,0xB7);
write_byte(0x22,0x8F);
write_byte(0x22,0x2B);
write_byte(0x22,0x01);
write_byte(0x22,0xEF);
write_byte(0x22,0x76);
write_byte(0x22,0x0D);
write_byte(0x22,0x01);
while(1)
{
unsigned char RxState;
unsigned char i,NewData,Temp,Temp2,Temp3,Temp4,Number;
NewData=0;
Number=0;
MyRadioInit();
write_byte(0x00,0x48);
write_byte(0x22,0xB7);
write_byte(0x22,0x8F);
write_byte(0x22,0x2B);
write_byte(0x22,0x01);
write_byte(0x22,0xEF);
write_byte(0x22,0x76);
write_byte(0x22,0x0D);
write_byte(0x22,0x01);
write_byte(0x05,0x80);
Delay_s(100);
while((!NewData)&&(Number<10))
{
Number++;
Temp=read_byte(0x07); //读取RX_IRQ_STATUS_ADR
if(!read_byte(0x05)) //RX_GO发生了变化,变为0,自动退出接收状态
{
Temp2=read_byte(0x08); //读取RX_STATUS_ADR RX_GO自动清零后需要读一下
if((Temp&(0x02))&&(!((Temp)&(0x01)))) //如果RXC IRQ为1 且RXE IRQ为0
{
PORTE&=~BIT(4);
Delay_s(50);
PORTE|=BIT(4);
Delay_s(50);
if(((read_byte(0x07))&0x01)) //如果第二次读RXE为1 有错误发生
{
write_byte(0x07,0x80); //对RXOW IRQ写1 读receive buffer 之前要写1
Temp3=read_byte(0x09);
for(i=0;i<Temp3;i++)
Temp4=read_byte(0x21); //读出错误的数据 长度为RX_COUNT_ADR
NewData=0;
PORTE&=~BIT(7);
Delay_s(50);
PORTE|=BIT(7);
Delay_s(50);
}
else //无错误发生,读取有效数据
{
PORTE&=~BIT(3);
Delay_s(50);
PORTE|=BIT(3);
Delay_s(50);
write_byte(0x07,0x80); //对RXOW IRQ写1 读receive buffer 之前要写1
for (i=0; i<16; i++)
cyrf6936_recv_buf=read_byte(0x21); //读数据
NewData=1; //标志位置1 把收到的数据通过USB发送到上位机上去
}
}
else if((Temp&(0x02))&&(((Temp)&(0x01)))) //如果RXC IRQ为1 且RXE IRQ为1 有错误发生
{
write_byte(0x07,0x80); //对RXOW IRQ写1 读receive buffer 之前要写1
Temp3=read_byte(0x09);
for(i=0;i<Temp3;i++)
Temp4=read_byte(0x21);
PORTE&=~BIT(6);
Delay_s(50);
PORTE|=BIT(6);
Delay_s(50); //读出错误的数据
NewData=0; //标志位置0
}
else
{
NewData=0;
}
}
}
PORTE&=~BIT(5);
Delay_s(50);
PORTE|=BIT(5);
Delay_s(50);
}
}
发送程序:
#include <iom128v.h>
#include <macros.h>
#include "LpRegs.h"
#define CLR_SS PORTB&=~BIT(0)
#define SET_SS PORTB|=BIT(0)
#define F_CPU 7372800
#define CYCLES_PER_US ((F_CPU+500000)/1000000)
unsigned char cyrf6936_send_buf[16]={0x11,0x12,0x13,0x14,0x15,
0x16,0x17,0x18,0x19,0x20,
0x21,0x22,0x23,0x24,0x25,0x26};
void delay_us(unsigned short time_us)
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = (time_us+3)/5*CYCLES_PER_US;
for (i=0; i < delay_loops; i++) {};
}
void delay_ms(unsigned char time_ms)
{
unsigned short int i;
for (i=0; i < 10; i++) {delay_us(100);}
return;
}
void Delay_s(unsigned char Times)
{
unsigned char i;
for (i=0; i<Times; i++)
{ delay_ms(250);
delay_ms(250);
delay_ms(250);
delay_ms(250);
}
}
void spi_init(void)
{
SPCR=0x50;
SPSR = 0x00;
}
void SpiWriteByte(unsigned char byte)
{
SPDR = byte;
while (!(SPSR & 0x80));
byte = SPDR;
}
unsigned char SpiReadByte(void)
{
SPDR = 0x00;
while (!(SPSR & 0x80));
return SPDR;
}
void write_byte(unsigned char addr,unsigned char data)
{
addr = addr|0x80;
CLR_SS;
SpiWriteByte(addr);
SpiWriteByte(data);
SET_SS;
}
unsigned char read_byte(unsigned char addr)
{
unsigned char buffer;
CLR_SS;
SpiWriteByte(addr);
buffer = SpiReadByte();
SET_SS;
return buffer;
}
void MyRadioInit()
{
SET_SS;
write_byte(MODE_OVERRIDE_ADR, RST);
write_byte(AUTO_CAL_TIME_ADR, AUTO_CAL_TIME_MAX);
write_byte(AUTO_CAL_OFFSET_ADR, AUTO_CAL_OFFSET_MINUS_4);
write_byte(IO_CFG_ADR, IRQ_POL);
write_byte(XTAL_CFG_ADR, 0x08);
write_byte(RX_CFG_ADR,
((RX_CFG_RST | FASTTURN_EN | LNA_EN ) & ~( HI | RXOW_EN | AUTO_AGC_EN)));
write_byte(TX_OFFSET_LSB_ADR, 0x55);
write_byte(TX_OFFSET_MSB_ADR, 0x05);
write_byte(TX_CFG_ADR, TX_CFG_RST | DATMODE_8DR);
}
void PacketTX(unsigned char send_length,unsigned char send_buf[16])
{
unsigned char i;
//write_byte(0x0F,0x27);
write_byte(0X02,0x40);
CLR_SS;
SpiWriteByte(0x81);
SpiWriteByte(send_length);
SET_SS;
CLR_SS;
SpiWriteByte(0xA0);
for(i=0;i<send_length;i++)
{
SpiWriteByte(send_buf);
}
SET_SS;
write_byte(TX_CTRL_ADR, TXC_IRQ | TX_GO); // Start transmit
while (!(read_byte(TX_IRQ_STATUS_ADR) & TXC_IRQ))
{
PORTE&=~BIT(6);
Delay_s(30);
PORTE|=BIT(6);
Delay_s(30);
}
}
void main (void)
{
PORTB=PORTB|0b11111111;
DDRB=0b11110111;
PORTD=0xff;
DDRD=0b11111010;
PORTE=0xFF;
DDRE=0b11111111;
spi_init();
MyRadioInit();
write_byte(0x00,0x48);
while(1)
{
MyRadioInit();
write_byte(0x00,0x48);
write_byte(0x22,0xB7);
write_byte(0x22,0x8F);
write_byte(0x22,0x2B);
write_byte(0x22,0x01);
write_byte(0x22,0xEF);
write_byte(0x22,0x76);
write_byte(0x22,0x0D);
write_byte(0x22,0x01);
PacketTX(1,cyrf6936_send_buf);
Delay_s(50);
PORTE&=~BIT(2);
Delay_s(50);
PORTE|=BIT(2);
Delay_s(50);
}
}
现在的问题是什么都收不到啊!!!!!!!!! |
|