mình có 1 đoạn code Pic viết cho con RFM73 nhưng mình không hiểu. ai có thể giải thích giúp mình không ạ. sau đó mình muốn sửa đoạn code đó để viết cho con 8051
copyright(c) 2012
Title: RFM73 simple example based on PIC c
Current version: v1.0
Function: RFM73 demo
processor: PIC16F690
Clock: internal RC 8M
Author: baiguang(ISA)
Company: Hope microelectronic Co.,Ltd.
Contact: +86-0755-82973805-846
E-MAIL: rfeng@hoperf.com
Data: 2012-11-10
************************************************** ********/
/************************************************** *******
---------------
|VDD VSS|
IRQ---- |RA5 RA0| ----CE
MOSI---- |RA4 RA1| ----CSN
MISO---- |RA3 RA2| ----SCK
---- |RC5 RC0| ----
---- |RC4 RC1| ----
---- |RC3 RC2| ----
---- |RC6 RB4| ----GREEN_LED
---- |RC7 RB5| ----RED_LED
---- |RB7 RB6| ----sensitive_LED
---------------
pic16F690
************************************************** *******/
#include "RFM73.h"
#include <pic.h>
#define GREEN_LED RB4
#define RED_LED RB5
#define Sensitive_LED RB6
#define GREEN_LED_OUT() TRISB4=0;
#define RED_LED_OUT() TRISB5=0;
#define Sensitive_LED_OUT() TRISB6=0;
UINT8 count_50hz;
typedef struct
{
unsigned char reach_1s : 1;
unsigned char reach_5hz : 1;
} FlagType;
FlagType Flag;
void init_mcu(void);
void init_port(void);
void timer2_init(void);
void power_on_delay(void);
void delay_200ms(void);
void delay_50ms(void);
void delay_5ms(void);
void delay_1ms(void);
void delay_20us(void);
void sub_program_1hz(void);
void Send_Packet(UINT8 type,UINT8* pbuf,UINT8 len);
void Send_NACK_Packet(void);
void Receive_Packet(void);
void SPI_Bank1_Write_Reg(UINT8 reg, UINT8 *pBuf);
void SPI_Bank1_Read_Reg(UINT8 reg, UINT8 *pBuf);
void Carrier_Test(UINT8 b_enable); //carrier test
extern void RFM73_Initialize(void);
extern void SwitchToTxMode(void);
extern void SwitchToRxMode(void);
const UINT8 tx_buf[17]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x3 9,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x78};
UINT8 rx_buf[MAX_PACKET_LEN];
extern const UINT8 RX0_Address[];
extern const unsigned long Bank1_Reg0_13[];
UINT8 test_data;
__CONFIG(0x3fd4);
main()
{
unsigned char i, j, chksum;
OSCCON = 0X70; // 8M RC
WDTCON = 0X00;
power_on_delay();
init_mcu();
count_50hz = 0;
Flag.reach_1s = 0;
INTCON = 0xc0; // enable interrupt
RFM73_Initialize();
while(1)
{
sub_program_1hz();
Receive_Packet();
}
}
/************************************************** *******
Function: init_mcu();
Description:
initialize mcu.
************************************************** *******/
void init_mcu(void)
{
init_port();
timer2_init();
}
/************************************************** *******
Function: init_port();
Description:
initialize port.
************************************************** *******/
void init_port(void)
{
ANSEL = 0;
ANSELH = 0;
WPUB = 0;
CE_OUT();
CSN_OUT();
SCK_OUT();
MISO_IN();
MOSI_OUT();
IRQ_IN();
RED_LED_OUT();
GREEN_LED_OUT();
Sensitive_LED_OUT();
CE = 0;
CSN = 1;
SCK = 0;
MOSI = 0;
RED_LED = 0;
GREEN_LED = 0;
Sensitive_LED = 0;
}
/************************************************** *******
Function: timer2_init();
Description:
initialize timer.
************************************************** *******/
void timer2_init(void)
{
T2CON = 0x7f; // timer2 on and 16 pre, postscale
PR2 = 156; // 50hZ, 4m/4/16/16/50
TMR2IE = 1;
}
/************************************************** *******
Function: interrupt ISR_timer()
Description:
************************************************** *******/
void interrupt ISR_timer(void)
{
// di();
unsigned char i;
if(TMR2IF)
{
count_50hz++;
if(count_50hz==50) // REACH 1S
{
count_50hz=0;
Flag.reach_1s = 1;
}
else if(count_50hz == 5)
{
Flag.reach_5hz = 1;
}
TMR2IF=0;
}
}
/************************************************** *******
Function: power_on_delay()
Description:
************************************************** *******/
void power_on_delay(void)
{
unsigned int i;
for(i = 0; i<1000; i++)
{
delay_1ms();
}
}
/************************************************** ******
************************************************** *******/
void delay_200ms(void)
{
unsigned char j;
for(j = 0; j<40; j++)
{
delay_5ms();
}
}
/************************************************** *******
Function: delay_50ms();
Description:
************************************************** *******/
void delay_50ms(void)
{
unsigned char j;
for(j = 0; j<10; j++)
{
delay_5ms();
}
}
/************************************************** *******
Function: delay_5ms();
Description:
************************************************** *******/
void delay_5ms(void)
{
int i;
for(i = 0; i<650; i++) // 85*5
{
;
}
}
/************************************************** *******
Function: delay_1ms();
Description:
************************************************** *******/
void delay_1ms(void)
{
unsigned char i;
for(i = 0; i<130; i++)
{
;
}
}
/************************************************** *******
Function: delay_20us();
Description:
************************************************** *******/
void delay_20us(void)
{
unsigned char i;
for(i = 0; i<3; i++)
{
;
}
}
/************************************************** *******
Function: sub_program_1hz()
Description:
************************************************** *******/
void sub_program_1hz(void)
{
UINT8 i;
UINT8 temp_buf[32];
if(Flag.reach_1s)
{
Flag.reach_1s = 0;
for(i=0;i<17;i++)
{
temp_buf[i]=tx_buf[i];
}
Send_Packet(W_TX_PAYLOAD_NOACK_CMD,temp_buf,17);
SwitchToRxMode(); //switch to Rx mode
}
}
/**************************************************
Function: Send_Packet
Description:
fill FIFO to send a packet
Parameter:
type: WR_TX_PLOAD or W_TX_PAYLOAD_NOACK_CMD
pbuf: a buffer pointer
len: packet length
Return:
None
**************************************************/
void Send_Packet(UINT8 type,UINT8* pbuf,UINT8 len)
{
UINT8 fifo_sta;
SwitchToTxMode(); //switch to tx mode
fifo_sta=SPI_Read_Reg(FIFO_STATUS); // read register FIFO_STATUS's value
if((fifo_sta&FIFO_STATUS_TX_FULL)==0)//if not full, send data (write buff)
{
RED_LED = 1;
SPI_Write_Buf(type, pbuf, len); // Writes data to buffer
delay_50ms();
RED_LED = 0;
delay_50ms();
}
}
/**************************************************
Function: Receive_Packet
Description:
read FIFO to read a packet
Parameter:
None
Return:
None
**************************************************/
void Receive_Packet(void)
{
UINT8 len,i,sta,fifo_sta,value,chksum,aa;
UINT8 rx_buf[MAX_PACKET_LEN];
sta=SPI_Read_Reg(STATUS); // read register STATUS's value
if((STATUS_RX_DR&sta) == 0x40) // if receive data ready (RX_DR) interrupt
{
do
{
len=SPI_Read_Reg(R_RX_PL_WID_CMD); // read len
if(len<=MAX_PACKET_LEN)
{
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,len);// read receive payload from RX_FIFO buffer
}
else
{
SPI_Write_Reg(FLUSH_RX,0);//flush Rx
}
fifo_sta=SPI_Read_Reg(FIFO_STATUS); // read register FIFO_STATUS's value
}while((fifo_sta&FIFO_STATUS_RX_EMPTY)==0); //while not empty
chksum = 0;
for(i=0;i<16;i++)
{
chksum +=rx_buf[i];
}
if(chksum==rx_buf[16]&&rx_buf[0]==0x30)
{
GREEN_LED = 1;
delay_50ms();
delay_50ms();
GREEN_LED = 0;
//Send_Packet(W_TX_PAYLOAD_NOACK_CMD,rx_buf,17);
SwitchToRxMode();//switch to RX mode
}
}
SPI_Write_Reg(WRITE_REG|STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
}
copyright(c) 2012
Title: RFM73 simple example based on PIC c
Current version: v1.0
Function: RFM73 demo
processor: PIC16F690
Clock: internal RC 8M
Author: baiguang(ISA)
Company: Hope microelectronic Co.,Ltd.
Contact: +86-0755-82973805-846
E-MAIL: rfeng@hoperf.com
Data: 2012-11-10
************************************************** ********/
/************************************************** *******
---------------
|VDD VSS|
IRQ---- |RA5 RA0| ----CE
MOSI---- |RA4 RA1| ----CSN
MISO---- |RA3 RA2| ----SCK
---- |RC5 RC0| ----
---- |RC4 RC1| ----
---- |RC3 RC2| ----
---- |RC6 RB4| ----GREEN_LED
---- |RC7 RB5| ----RED_LED
---- |RB7 RB6| ----sensitive_LED
---------------
pic16F690
************************************************** *******/
#include "RFM73.h"
#include <pic.h>
#define GREEN_LED RB4
#define RED_LED RB5
#define Sensitive_LED RB6
#define GREEN_LED_OUT() TRISB4=0;
#define RED_LED_OUT() TRISB5=0;
#define Sensitive_LED_OUT() TRISB6=0;
UINT8 count_50hz;
typedef struct
{
unsigned char reach_1s : 1;
unsigned char reach_5hz : 1;
} FlagType;
FlagType Flag;
void init_mcu(void);
void init_port(void);
void timer2_init(void);
void power_on_delay(void);
void delay_200ms(void);
void delay_50ms(void);
void delay_5ms(void);
void delay_1ms(void);
void delay_20us(void);
void sub_program_1hz(void);
void Send_Packet(UINT8 type,UINT8* pbuf,UINT8 len);
void Send_NACK_Packet(void);
void Receive_Packet(void);
void SPI_Bank1_Write_Reg(UINT8 reg, UINT8 *pBuf);
void SPI_Bank1_Read_Reg(UINT8 reg, UINT8 *pBuf);
void Carrier_Test(UINT8 b_enable); //carrier test
extern void RFM73_Initialize(void);
extern void SwitchToTxMode(void);
extern void SwitchToRxMode(void);
const UINT8 tx_buf[17]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x3 9,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x78};
UINT8 rx_buf[MAX_PACKET_LEN];
extern const UINT8 RX0_Address[];
extern const unsigned long Bank1_Reg0_13[];
UINT8 test_data;
__CONFIG(0x3fd4);
main()
{
unsigned char i, j, chksum;
OSCCON = 0X70; // 8M RC
WDTCON = 0X00;
power_on_delay();
init_mcu();
count_50hz = 0;
Flag.reach_1s = 0;
INTCON = 0xc0; // enable interrupt
RFM73_Initialize();
while(1)
{
sub_program_1hz();
Receive_Packet();
}
}
/************************************************** *******
Function: init_mcu();
Description:
initialize mcu.
************************************************** *******/
void init_mcu(void)
{
init_port();
timer2_init();
}
/************************************************** *******
Function: init_port();
Description:
initialize port.
************************************************** *******/
void init_port(void)
{
ANSEL = 0;
ANSELH = 0;
WPUB = 0;
CE_OUT();
CSN_OUT();
SCK_OUT();
MISO_IN();
MOSI_OUT();
IRQ_IN();
RED_LED_OUT();
GREEN_LED_OUT();
Sensitive_LED_OUT();
CE = 0;
CSN = 1;
SCK = 0;
MOSI = 0;
RED_LED = 0;
GREEN_LED = 0;
Sensitive_LED = 0;
}
/************************************************** *******
Function: timer2_init();
Description:
initialize timer.
************************************************** *******/
void timer2_init(void)
{
T2CON = 0x7f; // timer2 on and 16 pre, postscale
PR2 = 156; // 50hZ, 4m/4/16/16/50
TMR2IE = 1;
}
/************************************************** *******
Function: interrupt ISR_timer()
Description:
************************************************** *******/
void interrupt ISR_timer(void)
{
// di();
unsigned char i;
if(TMR2IF)
{
count_50hz++;
if(count_50hz==50) // REACH 1S
{
count_50hz=0;
Flag.reach_1s = 1;
}
else if(count_50hz == 5)
{
Flag.reach_5hz = 1;
}
TMR2IF=0;
}
}
/************************************************** *******
Function: power_on_delay()
Description:
************************************************** *******/
void power_on_delay(void)
{
unsigned int i;
for(i = 0; i<1000; i++)
{
delay_1ms();
}
}
/************************************************** ******
************************************************** *******/
void delay_200ms(void)
{
unsigned char j;
for(j = 0; j<40; j++)
{
delay_5ms();
}
}
/************************************************** *******
Function: delay_50ms();
Description:
************************************************** *******/
void delay_50ms(void)
{
unsigned char j;
for(j = 0; j<10; j++)
{
delay_5ms();
}
}
/************************************************** *******
Function: delay_5ms();
Description:
************************************************** *******/
void delay_5ms(void)
{
int i;
for(i = 0; i<650; i++) // 85*5
{
;
}
}
/************************************************** *******
Function: delay_1ms();
Description:
************************************************** *******/
void delay_1ms(void)
{
unsigned char i;
for(i = 0; i<130; i++)
{
;
}
}
/************************************************** *******
Function: delay_20us();
Description:
************************************************** *******/
void delay_20us(void)
{
unsigned char i;
for(i = 0; i<3; i++)
{
;
}
}
/************************************************** *******
Function: sub_program_1hz()
Description:
************************************************** *******/
void sub_program_1hz(void)
{
UINT8 i;
UINT8 temp_buf[32];
if(Flag.reach_1s)
{
Flag.reach_1s = 0;
for(i=0;i<17;i++)
{
temp_buf[i]=tx_buf[i];
}
Send_Packet(W_TX_PAYLOAD_NOACK_CMD,temp_buf,17);
SwitchToRxMode(); //switch to Rx mode
}
}
/**************************************************
Function: Send_Packet
Description:
fill FIFO to send a packet
Parameter:
type: WR_TX_PLOAD or W_TX_PAYLOAD_NOACK_CMD
pbuf: a buffer pointer
len: packet length
Return:
None
**************************************************/
void Send_Packet(UINT8 type,UINT8* pbuf,UINT8 len)
{
UINT8 fifo_sta;
SwitchToTxMode(); //switch to tx mode
fifo_sta=SPI_Read_Reg(FIFO_STATUS); // read register FIFO_STATUS's value
if((fifo_sta&FIFO_STATUS_TX_FULL)==0)//if not full, send data (write buff)
{
RED_LED = 1;
SPI_Write_Buf(type, pbuf, len); // Writes data to buffer
delay_50ms();
RED_LED = 0;
delay_50ms();
}
}
/**************************************************
Function: Receive_Packet
Description:
read FIFO to read a packet
Parameter:
None
Return:
None
**************************************************/
void Receive_Packet(void)
{
UINT8 len,i,sta,fifo_sta,value,chksum,aa;
UINT8 rx_buf[MAX_PACKET_LEN];
sta=SPI_Read_Reg(STATUS); // read register STATUS's value
if((STATUS_RX_DR&sta) == 0x40) // if receive data ready (RX_DR) interrupt
{
do
{
len=SPI_Read_Reg(R_RX_PL_WID_CMD); // read len
if(len<=MAX_PACKET_LEN)
{
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,len);// read receive payload from RX_FIFO buffer
}
else
{
SPI_Write_Reg(FLUSH_RX,0);//flush Rx
}
fifo_sta=SPI_Read_Reg(FIFO_STATUS); // read register FIFO_STATUS's value
}while((fifo_sta&FIFO_STATUS_RX_EMPTY)==0); //while not empty
chksum = 0;
for(i=0;i<16;i++)
{
chksum +=rx_buf[i];
}
if(chksum==rx_buf[16]&&rx_buf[0]==0x30)
{
GREEN_LED = 1;
delay_50ms();
delay_50ms();
GREEN_LED = 0;
//Send_Packet(W_TX_PAYLOAD_NOACK_CMD,rx_buf,17);
SwitchToRxMode();//switch to RX mode
}
}
SPI_Write_Reg(WRITE_REG|STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
}