使用到RF_module
也怕自已忘掉
所以就改寫到89S51上 XDDD
/*
RF_module user TR24A
TR24A MCU = EMI198810 3.3V
需注意io 可使用在16 or 32 bit mcu
By Brian @Taiwan
*/
#include "AT89X52.H"
#define Port_B P2
#define RF_DI 0x01 //P2.0 low
#define RF_CSB 0x02 //P2.1 high
#define RF_CLK 0x04 //P2.2 low
#define FIFO_FLAG 0x08 //P2.3 low
#define PKT_FLAG 0x10 //P2.4 low
#define RF_RESET 0x20 //P2.5 high
#define RF_DO 0x80 //P2.7 high
#define MaxLength 4
void Init_RF(void);
void INTO_TX(void);
void INTO_RX(void);
void WRITE_FIFO(unsigned char tx_leng,unsigned char tx_data);
void READ_FIFO(void);
void SpiTxReg(unsigned char rf_com,unsigned char rf_hb,unsigned char rf_lb);
void Write_FIFO_Start(unsigned char rf_com,unsigned char tx_leng);
void Write_FIFO_data(unsigned char tx_data);
void Write_FIFO_End(void);
unsigned char Read_FIFO_Start(unsigned char rf_com);
unsigned char Read_FIFO_data(void);
void Read_FIFO_End(void);
unsigned int SpiRxReg(unsigned char rf_com); //debug user
void Spi_Tx_Loop(unsigned char tx_data);
unsigned int Spi_Rx_Loop(void);
void sys_init(void);
void msDelay(unsigned int MaxDelay);
unsigned int DelayCNT;
unsigned int TimeCNT;
unsigned char Revice_Data[MaxLength];
void main(void)
{
sys_init();
Port_B &= ~RF_RESET; //RESET LOW ACT
msDelay(30);
Port_B |= RF_RESET; //RESET HIGH
msDelay(30);
Init_RF();
while(1)
{
//IN YOU CODE TX OR RX
//EX: TX
WRITE_FIFO(0x04,0x99); //SEND 4BYTE LENGTH 0x99 0x99 0x99 0x99
//EX: RX
INTO_RX(); //RESET RX STATUS
READ_FIFO(); // RECEIVE DATA IN Revice_data[]
}
}
void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
TR0=1; //time enabled
TimeCNT=0;
P2 = 0xA6; //RF port default
}
void Time0_int(void)interrupt 1
{
// TIME 0 MODE2 AUTORELOAD USER UNTERUPT 1
// 1000 = 0.1S, 10000 = 1S, 100=0.01S ,10=0.001S
TimeCNT++;
if(TimeCNT>=5)
{
DelayCNT++;
TimeCNT=0;
}
}
void msDelay(unsigned int MaxDelay)
{
DelayCNT = 0x0000;
while(DelayCNT < MaxDelay) {}
}
void Init_RF(void)
{
//unsigned int rec_data; debug
//INITIAL REG48~REG58
SpiTxReg(0x30,0x98,0x00); //reg48
// rec_data=SpiRxReg(0x30);
SpiTxReg(0x31,0xFF,0x0F); //reg49
// rec_data=SpiRxReg(0x31);
SpiTxReg(0x32,0x80,0x28); //reg50
SpiTxReg(0x33,0x80,0x56); //reg51
SpiTxReg(0x34,0x4E,0xF6); //reg52
SpiTxReg(0x35,0xF6,0xF5); //reg53
SpiTxReg(0x36,0x18,0x5C); //reg54
SpiTxReg(0x37,0xD6,0x51); //reg55
SpiTxReg(0x38,0x44,0x04); //reg56
SpiTxReg(0x39,0xE0,0x00); //reg57
SpiTxReg(0x3a,0x00,0x00); //reg58
msDelay(20);
//INITIAL REG0~REG28
SpiTxReg(0x00,0x35,0x4F); //reg00
SpiTxReg(0x02,0x1F,0x01); //reg02
SpiTxReg(0x04,0x38,0xD8); //reg04
SpiTxReg(0x05,0x00,0xA1); //reg05
SpiTxReg(0x07,0x12,0x4C); //reg07
SpiTxReg(0x08,0x80,0x08); //reg08
SpiTxReg(0x09,0x21,0x01); //reg09
SpiTxReg(0x0A,0x00,0x04); //reg10
SpiTxReg(0x0B,0x40,0x41); //reg11
SpiTxReg(0x0C,0x7E,0x00); //reg12
SpiTxReg(0x0D,0x00,0x00); //reg13
SpiTxReg(0x0E,0x16,0x9D); //reg14
SpiTxReg(0x0F,0x80,0x2F); //reg15
SpiTxReg(0x10,0xB0,0xF8); //reg16
// SpiTxReg(0x11,0x00,0x66); //reg17
SpiTxReg(0x12,0xE0,0x00); //reg18
SpiTxReg(0x13,0xA1,0x14); //reg19
SpiTxReg(0x14,0x81,0x91); //reg20
SpiTxReg(0x15,0x69,0x62); //reg21
SpiTxReg(0x16,0x00,0x02); //reg22
SpiTxReg(0x17,0x00,0x02); //reg23
SpiTxReg(0x18,0xB1,0x40); //reg24
SpiTxReg(0x19,0x78,0x0F); //reg25
SpiTxReg(0x1A,0x3F,0x04); //reg26
// SpiTxReg(0x1B,0x80,0x00); //reg27
SpiTxReg(0x1C,0x58,0x00); //reg28
//完成進入閒置模式
SpiTxReg(0x07,0x00,0x00);
}
//==========================================================================
/*void INTO_TX(void)
{
SpiTxReg(0x52,0x80,0x00); //RESET TX
msDelay(5);
SpiTxReg(0x07,0x01,0x10); //INTO TX
msDelay(5);
} */
//==========================================================================
void INTO_RX(void)
{
SpiTxReg(0x52,0x00,0x80); //RESET RX
msDelay(5);
SpiTxReg(0x07,0x00,0x90); //INTO RX
msDelay(5);
}
//==========================================================================
void WRITE_FIFO(unsigned char tx_leng,unsigned char tx_data)
{
int i;
SpiTxReg(0x52,0x80,0x00); //RESET TX
msDelay(5);
Write_FIFO_Start(0x50,tx_leng); //WRITE FIFO
for(i=0;i<tx_leng;i++)
Write_FIFO_data(tx_data);
Write_FIFO_End();
SpiTxReg(0x07,0x01,0x10); //INTO TX
msDelay(5);
/* i = 0;
while( i == 0) //若PKT_FLAG = 0則等待,等於1則往下執行
{
i = Port_B; //此處沒有作用 但 Application Notes 說可讀取此io來確定data是否傳送完成
i &= PKT_FLAG; //檢查pkt_flag
} */
SpiTxReg(0x52,0x80,0x00); //RESET TX
msDelay(5);
SpiTxReg(0x07,0x00,0x00); //TX完成,進入閒置模式
msDelay(5);
}
//==========================================================================
void READ_FIFO(void)
{
char i;
unsigned char length;
for(i=0;i<MaxLength;i++) //clear receive data
Revice_Data[i]=0;
i = 0;
while( i == 0)
{
i = Port_B;
i &= PKT_FLAG; //wait pkt_flag io
}
length = Read_FIFO_Start(0xD0); //send 0xD0 and receive length
if(length<=MaxLength) //確定傳送的資料是無誤的 可在自已加上check code
{
for(i=0;i<length;i++)
{
Revice_Data[i]= Read_FIFO_data(); //接收資料
}
}
Read_FIFO_End(); //io set
SpiTxReg(0x52,0x00,0x80);
msDelay(5);
SpiTxReg(0x07,0x00,0x90);
msDelay(5);
}
void SpiTxReg(unsigned char rf_com,unsigned char rf_hb,unsigned char rf_lb)
{
unsigned char Port_status;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
Port_status &= ~RF_CSB;
Port_B = Port_status;
Spi_Tx_Loop(rf_com);
Spi_Tx_Loop(rf_hb);
Spi_Tx_Loop(rf_lb);
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
}
void Write_FIFO_Start(unsigned char rf_com,unsigned char tx_leng)
{
unsigned char Port_status;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
Port_status &= ~RF_CSB;
Port_B = Port_status;
Spi_Tx_Loop(rf_com);
Spi_Tx_Loop(tx_leng);
}
void Write_FIFO_data(unsigned char tx_data)
{
Spi_Tx_Loop(tx_data);
}
void Write_FIFO_End(void)
{
unsigned char Port_status;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
}
unsigned char Read_FIFO_Start(unsigned char rf_com)
{
unsigned char Port_status;
unsigned char Rx_leng;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
Port_status &= ~RF_CSB;
Port_B = Port_status;
Spi_Tx_Loop(rf_com);
Rx_leng = Spi_Rx_Loop();
return Rx_leng;
}
unsigned char Read_FIFO_data(void)
{
return Spi_Rx_Loop();
}
void Read_FIFO_End(void)
{
unsigned char Port_status;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
}
void Spi_Tx_Loop(unsigned char tx_data)
{
unsigned char Port_status;
unsigned char temp;
int i;
for(i=0;i<8;i++)
{
Port_status = Port_B;
Port_status |= RF_CLK; //SPI_CLK = HIGH
Port_B = Port_status;
temp = tx_data & RF_DO;
Port_status = Port_B;
if(temp)
Port_status |= RF_DO;
else
Port_status &= ~RF_DO;
Port_B = Port_status;
tx_data<<=1;
msDelay(1);
Port_status = Port_B;
Port_status &= ~RF_CLK; //SPI_CLK = LOW
Port_B = Port_status;
msDelay(1);
}
}
unsigned int Spi_Rx_Loop(void)
{
unsigned char Port_status;
unsigned char rx_data;
unsigned char ret_data;
unsigned char temp;
int i;
ret_data = 0;
for(i=0;i<8;i++)
{
Port_status = Port_B;
Port_status |= RF_CLK; //SPI_CLK = HIGH
Port_B = Port_status;
rx_data = Port_B & RF_DI;
if(rx_data)
temp = 1;
else
temp = 0;
ret_data |= temp;
if(i<7)
ret_data <<= 1;
msDelay(1);
Port_status = Port_B;
Port_status &= ~RF_CLK; //SPI_CLK = LOW
Port_B = Port_status;
msDelay(1);
}
return ret_data;
}
unsigned int SpiRxReg(unsigned char rf_com)
{
unsigned char Port_status;
unsigned int rec_data;
Port_status = Port_B;
Port_status |= RF_CSB;
Port_status &= ~RF_CLK;
Port_B = Port_status;
Port_status &= ~RF_CSB;
Port_B = Port_status;
Spi_Tx_Loop(rf_com|0x80);
rec_data = Spi_Rx_Loop();
rec_data<<=8;
rec_data |= Spi_Rx_Loop();
return rec_data;
}
沒有留言:
張貼留言