Learning the wireless module of NRF905 Based on stm32f103zet6

Source: Internet
Author: User

This wireless module was used because of the need of the competition. The module was also made by students. He said that he didn't need it now, so I took it and practiced it! I started my research yesterday afternoon. I found some problems during the test last night. I had a class this morning and I couldn't help it. It's worth debugging this afternoon. Fortunately, the debugging is successful. Thank God ~~~~ Okay. Now, let's sum up what we need to pay attention to in this wireless module. After all, we will be able to use it for remote control cars in the future ....

First, I would like to send you a technical reference manual, which is in Chinese, but if you think there is a problem in some areas, please refer to this manual. That's what I did!

The address is here

I. Hardware

We will not talk about the chip of nRF905. It is complicated to involve high frequency and RF. We will talk about how to use this module.

This is a pin map that involves programming. uclk is not used here. See the pin chart below.

Summarize the important information of this pin table as follows:

1. the SPI protocol is used for communication between the nRF905 and the single-chip microcomputer. I use the software to simulate SPI, and the hardware SPI has other functions.

2. There is no problem with power supply 3.3v. the I/O port voltage is fully compatible and the output current is also correct.

3. CD is the carrier detection signal, which means that when our module receives the signal from the same band of the transmitting module, the pin will be set to a high value of nRF905, usually low!

4. Am indicates address matching. When it is used as the receiving module, when the receiving address matches the transmitting address, the pin will be set to high by nRF905, which is usually low!

5. Dr indicates that the data is successfully received or sent! When a correct data packet is received, rf905 automatically removes the character check bit and sets the Dr pin to a high value, which is usually low!

Note: the status of the three pins CD, AM, and DRDs is very important during debugging. Therefore, make full use of these pins!

We have already finished what we need to pay attention to in terms of hardware. Next we will analyze my program!

Ii. Software

The first is the sending process:

1. When the Microcontroller has data to send, the address and data to be sent are sent to rf905 through the SPI protocol, and the speed of the SPI interface is determined during the communication protocol and device configuration;
2. The microcontroller sets the trx_ce and tx_en values to send data.
3. rf905 sending process:
(1) The RF register is automatically enabled;
(2) data packaging (plus header and CRC verification
(3) send data packets;
(4) when data is sent,

(1) (2) Two steps are automatically completed!

4. auto_retran is set to high, and rf905 continues resending until TRX _
5. When the trx_ce is set to low, the rf905 sending process is complete and enters idle mode automatically.
Note: The shockbursttm working mode ensures that once the data sending process starts
The trx_en and tx_en pins are high or low, and the sending process is complete. Only before
When packets are sent, rf905 can accept the next packet.

Then the receiving process is as follows:

1. When the trx_ce is high and the tx_en is low, rf905 enters the shockbursttm receiving mode;
2. rf905 continues to monitor and wait for receiving data after 650us;
3. When rf905 detects a carrier in the same frequency band, the carrier detection pin is set to high;
4. When a matched address is received, the am pin is set to high;
5. When a correct data packet is received, rf905 automatically removes the character header, address, and CRC check bit, and then sets the Dr pin to high.
6. The microcontroller sets trx_ce to a low level, and nRF905 enters the idle mode;
7. The microcontroller moves data to the microcontroller at a certain rate through the SPI port;
8. When all the data is received, set the Dr pin and am pin to a lower value in nRF905;
9. At this time, you can enter the shockbu rsttm receiving mode, shockbursttm sending mode, or shutdown mode. When receiving a packet, the status of the trx_ce or tx_en pin changes,
Rf905 immediately changes its working mode, and data packets are lost.

Note that when we set the receiving mode, we need to write the data to the nRF905 module before enabling the sending pin !!!

The next step is to analyze the specific program.

A. first look at my main function, which is very simple.

Int main (void) {// initialize the system timer systick_init (); usart+config (); nrf905_init (); nrf905_config (); settxmode (); While (1) {txpacket (txbuf); delay_ms (2000 );}}

Note This mode

B. Then there is the function for configuring the nRF905. This is the key. For specific data selection, it is best to look at the chip manual. I will post the code here.

/*************************************** ****************************************: NRF905_Init * function: configure the parameter number of the NRF905 register *: No * Call method: NRF905_Init (); * return value: no *************************************** **************************************** * ******/void NRF905_Config (void) {u8 I; NRF905_CSN_L; // Spi enable for write a spi commandNRF905_SPI_Write_Byte (WC); // Write config command write and put the configuration command for (I = 0; I <RxTxConf. n; I ++) // Write configration words Write and put the configuration word {NRF905_SPI_Write_Byte (RxTxConf. buf [I]) ;}for (I = 0; I <10; I ++) {printf ("% x \ r", RxTxConf. buf [I]);} NRF905_CSN_H; // Disable Spi}

In short, the content sent to it is:

/*************************************** ****************************************: Configure the content in the register! **************************************** **************************************** * *****/RFConfig RxTxConf = {10, 0x4c, // CH_NO [] together with CH_NO [8] of byte 1 and HFREQ_PLL control 905 Carrier Band 433MHZ0x0c, //: no use; 5: Automatic resend = 0; 4: Reduced reception sensitivity = 0; 3, 2: transmit power = 10dBm; 1: 433 MHz; CH_NO [8] = 00x44, // 7: no use; 6, 5, 4: sending address width 4; 3: no use; 320, 0: Receiving address width 40x20, // no use; 5-0: receiving data width x 20, // 7, 6 no use; 5-0: sending data width 320xcc, // Receiving address byte 0 (device ID) 0xcc, // Receiving address byte 1 (device ID) 0xcc, // received address byte 2 (device ID) 0xcc, // received address byte 3 (device ID) 0x58 // 7: 8 bits in CRC mode; 6: CRC verification permission; 5, 4, indicates 16 MHz; 2: UP_CLK_EN = 0 no external clock;: external clock frequency };

This is based on the meaning of the register format. In fact, there is nothing to say, but I always suspect that there are some problems in the chip manual, which should be 433.0 MHZ.

C. Next let's see how we can send data. I will paste my code and analyze it.

/*************************************** ****************************************: Txpacket * function: send data packet * parameter: content to be sent * Call method: txpacket (); * return value: miso value ************************************* **************************************** * *******/void txpacket (u8 * txbuf) {u8 I; nrf905_csn_l; // enable spinrf905_spi_write_byte (WTP); // write payload commandfor (I = 0; I <32; I ++) {nrf905_spi_write_byte (txbuf [I]); // Write 32 bytes TX data} for (I = 0; I <32; I ++) {printf ("% x \ r", txbuf [I]);} nrf905_csn_h; // SPI disabledelay_us (2); nrf905_csn_l; // SPI enable for write a spi partition (WTA); // write address commandfor (I = 0; I <4; I ++) // write 4 bytes address {nrf905_spi_write_byte (rxtxconf. buf [I + 5]) ;}for (I = 0; I <4; I ++) {printf ("% x \ r", rxtxconf. buf [I + 5]);} nrf905_csn_h; // SPI disablenrf905_trx_ce_h; // Set trx_ce high, start TX data transmission first write data and then enable sending delay_ms (100); // remember to give the response time while (! Dr_read () {printf ("Data send failed !!! \ N ");} printf (" Data send success !!! \ N "); nrf905_trx_ce_l; // set trx_ce low}

The above printf functions are all printed functions added to facilitate debugging!

You may wonder if there is a problem with the previous Mode settings. I thought that when it is set to send, both of them should be set to high. But here we will tell you that when we transfer data, this is what I understand.

Disable EN first. This statement is used only when data is written through SPI.

NRF905_TRX_CE_H; // Set TRX_CE high, start Tx data transmission first write data and then enable sending

This is to eliminate interference, I think so.

D. Add some driver functions.

/*************************************** ****************************************: SetTxMode * function: Set the sending status of NRF905 * parameter number: none * Call method: SetTxMode (); * return value: no *************************************** **************************************** * ******/void SetTxMode (void) {NRF905_TX_EN_H; NRF905_TRX_CE_L; // Why is it low? Write Data first and send it. Therefore, pull Delay_us (650 );} /*************************************** ****************************************: setRxMode * function: Set the sending status of NRF905 * parameter number: none * Call method: SetRxMode (); * return value: no *************************************** **************************************** * ******/void SetRxMode (void) {NRF905_TX_EN_L; NRF905_TRX_CE_H; Delay_us (650 );} /*************************************** ****************************************: CD_read * function: Read the CD value (carrier detection) * parameter number: none * Call method: CD_read (); * return value: CD value ************************************* **************************************** * ********/u8 CD_read (void) {u8 ReadValue; ReadValue = NRF905_CD_DATA; return ReadValue ;} /*************************************** ****************************************: AM_read * function: read AM value (address match AM) * parameter number: none * Call method: AM_read (); * return value: AM value ************************************* **************************************** * ********/u8 AM_read (void) {u8 ReadValue; ReadValue = NRF905_AM_DATA; return ReadValue ;} /*************************************** ****************************************: DR_read * function: Read the CD value (Data ready DR) * parameter number: none * Call method: DR_read (); * return value: DR value ************************************* **************************************** * ********/u8 DR_read (void) {u8 ReadValue; ReadValue = NRF905_DR_DATA; return ReadValue ;} /*************************************** ****************************************: NRF905_SPI_Read_Bit * function: Read MISO value * parameter number: none * Call method: NRF905_SPI_Read_Bit (); * return value: MISO value, 0 or 1 ************************************* **************************************** * *******/static u8 NRF905_SPI_Read_Bit (void) {u8 ReadValue; ReadValue = NRF905_MISO_DATA; return ReadValue ;} /*************************************** ****************************************: NRF905_SPI_Read_Byte * function: Read the MISO value, in the form of bytes * parameter number: none * Call method: NRF905_SPI_Read_Byte (); * return value: MISO value ************************************* **************************************** * ********/u8 NRF905_SPI_Read_Byte (void) {u8 I; u8 Temp_data; for (I = 0; I <8; I ++) // Setup byte circulation bits {Temp_data = Temp_data <1; // Right shift Temp_dataNRF905_SCK_H; // Set clock line higlif (NRF905_SPI_Read_Bit () {Temp_data | = 0x01; // Read data} NRF905_SCK_L; // Set clock line low} return Temp_data; // Return function parameter }/********************************* **************************************** * ************* Name: NRF905_SPI_Write_Byte * function: NRF905 write 1 byte function * parameter: DATA: bytes to be written * Call method: NRF905_SPI_Write_Byte (0xaa); * return value: no *************************************** **************************************** * *****/void NRF905_SPI_Write_Byte (u8 DATA) {u8 I, Temp_data; Temp_data = DATA; for (I = 0; I <8; I ++) {if (Temp_data & 0x80) // always send the highest bit {NRF905_MOSI_H;} else {NRF905_MOSI_L;} NRF905_SCK_H; Temp_data = Temp_data <1; NRF905_SCK_L ;}}

If you need a program, you can leave a message .....
Code uploaded to http://download.csdn.net/detail/king_bingge/5797627

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.