STM8 nRF24L01 Program

Publisher:平静宁静Latest update time:2016-09-06 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
It took me more than a day of intermittent work to finally port nRF24L01 from C51 to STM8, paying main attention to the timing and I/O port configuration.
STM8 nRF24L01 Program - Xiaowen - Xiaowen Electronic Design

 

STM8 nRF24L01 Program - Xiaowen - Xiaowen Electronic Design

 

STM8 nRF24L01 Program - Xiaowen - Xiaowen Electronic Design

 Source program:

/************ STM8S103F learning************************/
/*Chip model: STM8S103F3PB6 */
/*Function description: nrf24l01 wireless module application */ /
*Author: ClimberWin */
/*Written time: 2011.9.25 */
/*Internal crystal oscillator */
/**************************************************/
//Modified time 2011.9.27
//The program was successfully transplanted from C51 to STM8
#include

#define   uint    unsigned int
#define   uchar    unsigned char

//********************************************IO port definition***************************************
//****************************************NRF24L01 port definition********************************

/////////////Pin definition//////////////

#define         CE         PD_ODR_ODR3 
#define         CSN       PD_ODR_ODR2 
#define         SCK       PC_ODR_ODR7
#define         MOSI     PC_ODR_ODR6
#define         MISO     PC_IDR_IDR5  //输入
#define         IRQ        PD_IDR_IDR4    //输入

/******************NRF24L01 partial program**********************/
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x9 0} ; //Local address
uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //Receive address
//****************************************NRF24L01 register instruction** ************************************************** ***
#define READ_REG 0x00 // Read register instruction
#define WRITE_REG 0x20 // Write register instruction
#define RD_RX_PLOAD 0x61 // Read receive data instruction
#define WR_TX_PLOAD 0xA0 // Write data to be sent instruction
#define FLUSH_TX 0xE1 // Flush Send FIFO instruction
#define FLUSH_RX 0xE2 // Flush receive FIFO instruction
#define REUSE_TX_PL 0xE3 // Define repeat load data instruction
#define NOP 0xFF // Reserved
//******************** ********************SPI(nRF24L01)Register Address**************************** ****************************
#define CONFIG 0x00 // Configure the receiving and sending status, CRC check mode and receiving and sending status response mode
#define EN_AA 0x01 // Automatic answer function setting
#define EN_RXADDR 0x02 // Available channel setting
#define SETUP_AW 0x03 // Send and receive address width setting
#define SETUP_RETR 0x04 // Automatic reset Transmit function settings
#define RF_CH 0x05 // Operating frequency settings
#define RF_SETUP 0x06 // Transmit rate, power consumption function settings
#define STATUS 0x07 // Status register
#define OBSERVE_TX 0x08 // Transmit monitoring function
#define CD 0x09 // Address detection           
#define RX_ADDR_P0 0x0A // Channel 0 receive data address
#define RX_ADDR_P1 0x0B // Channel 1 receive data address
#define RX_ADDR_P2 0x0C // Channel 2 receive data address
#define RX_ADDR_P3 0x0D // Channel 3 receive data address
#define RX_ADDR_P4 0x0E // Channel 4 receive data address
#define RX_ADDR_P5 0x0F // Channel 5 receive data address
#define TX_ADDR 0x10 // Transmit address register
#define RX_PW_P0 0x11 // Receive data length for receiving channel 0
#define RX_PW_P1 0x12 // Receive data length for receiving channel 0
#define RX_PW_P2 0x13 // Receive data length for receiving channel 0
#define RX_PW_P3 0x14 // Receive data length for receiving channel 0
#define RX_PW_P4 0x15 / / Receive data length for receiving channel 0
#define RX_PW_P5 0x16 // Receive data length for receiving channel 0
#define FIFO_STATUS 0x17 // FIFO stack push and pull status register settings

void init_NRF24L01(void);
volatile SPI_RW(volatile byte);
volatile SPI_Read(volatile reg);
void SetRX_Mode(void);
fly SPI_RW_Reg(fly reg, fly value);
fly SPI_Read_Buf(fly reg, fly *pBuf, fly num);
fly SPI_Write_Buf(fly reg, fly *pBuf, fly num);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);

void IO_config(void);

void delayms(unsigned int count);


flying sta;

#define   RX_DR  (sta & 0x40)
#define   TX_DS  (sta & 0x20)
#define   MAX_RT  (sta & 0x10)

void delayms(unsigned int count)
{
 unsigned int i,j;
 for(i=0;i  for(j=0;j<450;j++);
}

 

//NRF24L01 initialization

void init_NRF24L01(void)
{
    delayms(1);
  CE=0; // chip enable
  CSN=1; // Spi disable 
  SCK=0; // Spi clock line init high
 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Write local address 
 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // Write receiving address
 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Channel 0 automatic ACK response allowed 
 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Only channel 0 is allowed to receive address. If multiple channels are needed, please refer to Page21  
 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // Set the channel to work at 2.4GHZ, and the transmission and reception must be consistent
 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Set the length of the received data, this time it is set to 32 bytes
 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //Set the transmission rate to 1MHZ and the transmission power to the maximum value 0dB
}

//Function: uint SPI_RW(uint uchar)
//Function: SPI write timing of NRF24L01
uchar SPI_RW(uchar byte)
{
 uchar i;
    for(i=0;i<8;i++) // output 8-bit
    {
  if((byte & 0x80)==0)         

                {MOSI=0;}
                else
                {MOSI=1;}
                
  byte = (byte << 1);           // shift next bit into MSB..
  SCK = 1;                      // Set SCK high..
                if(MISO == 0)
  {byte |= 0;}           // capture current MISO bit
                else
                {byte |= 1;} 
  SCK = 0;                // ..then set SCK low again
    }
    return(byte);               // return read uchar
}

 /*uchar SPI_RW(uchar byte)
{
 uchar i;
    for(i=0;i<8;i++) // output 8-bit
    {
  MOSI = (byte & 0x80);         // output 'uchar', MSB to MOSI
  byte = (byte << 1);           // shift next bit into MSB..
  SCK = 1;                      // Set SCK high..
  byte |= MISO;           // capture current MISO bit
  SCK = 0;                // ..then set SCK low again
    }
    return(byte);               // return read uchar
}*/


//Function: uchar SPI_Read(uchar reg)
//Function: SPI timing of NRF24L01

uchar SPI_Read(uchar reg)
{
 uchar reg_val;
 
 CSN = 0;                // CSN low, initialize SPI communication...
 SPI_RW(reg);            // Select register to read from..
 reg_val = SPI_RW(0);    // ..then read registervalue
 CSN = 1;                // CSN high, terminate SPI communication
 
 return(reg_val);        // return register value
}

//Function: NRF24L01 read and write register function

uchar SPI_RW_Reg(uchar reg, uchar value)
{
 uint status;
 
 CSN = 0;                   // CSN low, init SPI transaction
 status = SPI_RW(reg);      // select register
 SPI_RW(value);             // ..and write value to it..
 CSN = 1;                   // CSN high again
 
 return(status);            // return nRF24L01 status uchar
}

//Function: uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//Function: used to read data, reg: register address, pBuf: address of data to be read, uchars: number of data to be read

uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)
{
 uchar status,i;
 
 CSN = 0;                      // Set CSN low, init SPI tranaction
 status = SPI_RW(reg);         // Select register to write to and read status uchar
 
 for(i=0;i   pBuf[i] = SPI_RW(0);    // 
 
 CSN = 1;                           
 
 return(status);                    // return nRF24L01 status uchar
}

//Function: uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//Function: used to write data: register address, pBuf: address of data to be written, uchars: number of data to be written
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)
{
 uchar status,i;
 
 CSN = 0; //SPI enable       
 status = SPI_RW(reg);   
 for(i=0; i   SPI_RW(*pBuf++);
 CSN = 1; //Close SPI
 return(status); // 
}

//Function: void SetRX_Mode(void)
//Function: Data receiving configuration

void SetRX_Mode(void)
{
 CE=0;
 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ reception and transmission completion interrupt response, 16-bit CRC, main reception
 CE = 1; 
 delayms(1);
}
/******************************************************************************************************/
//Function: unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//Function: After reading data, put it in the rx_buf receiving buffer

unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
    unsigned char revale=0;
 sta=SPI_Read(STATUS); // Read the status register to determine the data reception status
 if(RX_DR) // Determine whether data is received
 {
     CE = 0; //SPI enables
  SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
  revale =1; //Read data completion flag
 }
 SPI_RW_Reg(WRITE_REG+STATUS,sta); //After receiving the data, RX_DR,TX_DS,MAX_PT are all set high to 1, and the interrupt flag is cleared by writing 1
 return revale;
}

//Function: void nRF24L01_TxPacket(unsigned char * tx_buf)
//Function: Send data in tx_buf

void nRF24L01_TxPacket(unsigned char * tx_buf)
{
 CE=0; //StandBy I mode 
 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); //Load receiving end address
 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); //Load data 
 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); //IRQ transceiver completion interrupt response, 16-bit CRC, master send
 CE=1; //Set CE high to stimulate data transmission
 delayms(1);
}

 


//***********Port configuration******************/
void IO_config(void)
{
   PC_DDR_DDR5=0;
   PC_DDR_DDR6=1;
   PC_DDR_DDR7=1;
   
   PD_DDR_DDR2=1;
   PD_DDR_DDR3=1;
   PD_DDR_DDR4=0;
   
   
   PC_CR1_C15=1;//Input pull-up
   PC_CR1_C16=1;
   PC_CR1_C17=1;
   
   PD_CR1_C12=1;
   PD_CR1_C13=1;
   PD_CR1_C14=0;//Input pull-up

   PC_CR2 = 0x00; 
   PD_CR2 = 0x00; 
    
}
/***********************************/


/*****************主程序********************/
void main(void)
{
        unsigned char TxBuf[20]={0};  // 
 unsigned char RxBuf[20]={0};
 unsigned char led_num; 
       // unsigned char rx_temp; 
  
      delayms(100);
     IO_config(); 
      
       

    init_NRF24L01() ;
   TxBuf[1] =0x55 ;
 nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
 delayms(1000);
 led_num=0x00;
 while(1)
 {
     
  TxBuf[1] =led_num ;
  nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data   
  led_num++;
  delayms(500);       
//***************************************************************************************************
  SetRX_Mode(); // Start sending each time and then jump directly to receiving without pressing a button, and keep looping.
  nRF24L01_RxPacket(RxBuf);
  if(RX_DR==0)
                {
                 // rx_temp=0x55;
                  delayms(10);
                }
                else
                {
                   delayms(10);
                }
  
  
 }
     
}

Keywords:STM8 Reference address:STM8 nRF24L01 Program

Previous article:STM8S103F2 NOKIA5110+AD acquisition
Next article:STM8S103F---Nokia5110 LCD display

Recommended ReadingLatest update time:2024-11-16 00:05

STM8 study notes 1: CPU introduction
Written in front   Recently, the project involves some low-level driver designs with high real-time requirements. After writing them in C language under IAR, I always feel that the response is not fast enough. I usually see many embedded experts writing them directly through assembly on the Internet, and the effect is
[Microcontroller]
STM8 study notes 1: CPU introduction
How to set read protection and remove read protection using STVP programming program in STM8 series chips
First, open STVP, open the program to be burned (Open), if you want to read protect the chip, just set the protection option to ON. As shown in the figure below: Click Write All, and you will be prompted to set protection. Click "Yes" to write protection. After the above writing is completed, both reading and writin
[Microcontroller]
How to set read protection and remove read protection using STVP programming program in STM8 series chips
Initialization and simple use of stm8 hardware IIC
STM8 is a mainstream control chip commonly used by electronic engineers. IIC is a commonly used serial port protocol. However, people who have worked with 51 know that to perform IIC communication, pin simulation IIC is required. Although many engineers know that STM8 has hardware IIC, many engineers are still accusto
[Microcontroller]
STM8 defines a large array error #error clnk Debug\demo.lkf
Today, when I was programming STM8 again, I found that the compilation could not pass after defining a large array. After searching online, I finally got it compiled. This is for my own convenience. If there are any problems or shortcomings in my writing, please let me know. 1. Error example:    2. Open the dialog b
[Microcontroller]
STM8 defines a large array error #error clnk Debug\demo.lkf
STM8--STVD compilation tool installation and program download
1. Install STVD and download it from ST's official website. STVD only supports assembly language development. C language development requires the installation of the COSMIC C compiler, which is downloaded separately. 2. Download COSMIC C for STM8 from COSMIC official website: http://www.cosmic-software.com/downloa
[Microcontroller]
STM8--STVD compilation tool installation and program download
STM8 study notes---timer output 7-way PWM wave
The STM8S003F3P6 microcontroller has three timers: Timer 1, Timer 2, and Timer 4. Timer 1 is a 16-bit advanced timer, Timer 2 is a 16-bit general-purpose timer, and Timer 4 is an 8-bit basic timer. Timer 1 and Timer 2 can output PWM waves. Timer 1 has 4 channels, and Timer 2 has 3 channels. Now let all channels of T
[Microcontroller]
STM8 study notes---timer output 7-way PWM wave
Problems with single-step debugging of STM8
The following problems occur when using STM8 for single-step debugging: Sometimes the following error message appears: Error:swim error :swim communication error, as shown below: Sometimes the following error message appears: Error: swim error : comm timeout, as shown below: Cause: A hardware watchdog was set du
[Microcontroller]
stm8 halt low power mode
  STM8   The STM8 series is an 8-bit microcontroller produced by STMicroelectronics. This type of microcontroller is divided into three series: STM8A, STM8S, and STM8L. STM8A: automotive-grade application STM8S: standard series STM8L: ultra-low power MCU   Core Advanced STM8 core, Harvard architecture with 3-stage p
[Microcontroller]
stm8 halt low power mode
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号