***************************************************
Keywords:MSP430 nRF24L01
Reference address:MSP430+nRF24L01 Program
Source: Internet
Rewritten by: bluefeel
Time: 10-03-13
Unit: Guilin University of Technology
/******************************************************/
Rewritten by: bluefeel
Time: 10-03-13
Unit: Guilin University of Technology
/******************************************************/
#include
#include
#include
#define uchar unsigned char
/***************************************************/
#define TX_ADR_WIDTH 5 // 5-byte width of the send/receive address
#define TX_PLOAD_WIDTH 4 // Data channel effective data width
#define LED P2
#define TX_ADR_WIDTH 5 // 5-byte width of the send/receive address
#define TX_PLOAD_WIDTH 4 // Data channel effective data width
#define LED P2
uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static transmit address
uchar RX_BUF[TX_PLOAD_WIDTH];
uchar TX_BUF[TX_PLOAD_WIDTH];
uchar flag;
uchar DATA = 0x01;
uchar bdata sta;
sbit RX_DR = sta^6;
sbit TX_DS = sta^5;
sbit MAX_RT = sta^4;
/***************************************************/
uchar RX_BUF[TX_PLOAD_WIDTH];
uchar TX_BUF[TX_PLOAD_WIDTH];
uchar flag;
uchar DATA = 0x01;
uchar bdata sta;
sbit RX_DR = sta^6;
sbit TX_DS = sta^5;
sbit MAX_RT = sta^4;
/***************************************************/
/**************************************************
Function: init_io()
Function: init_io()
Description:
Initialize IO
/************************************************/
void init_io(void)
{
CE = 0; // Standby
CSN = 1; // SPI disable
SCK = 0; // SPI clock low
IRQ = 1; // Interrupt reset
LED = 0xff; // Turn off the indicator light
}
/***************************************************/
Initialize IO
/************************************************/
void init_io(void)
{
CE = 0; // Standby
CSN = 1; // SPI disable
SCK = 0; // SPI clock low
IRQ = 1; // Interrupt reset
LED = 0xff; // Turn off the indicator light
}
/***************************************************/
/**************************************************
Function: delay_ms()
Function: delay_ms()
Description:
Delay x milliseconds
/************************************************/
void delay_ms(uchar x)
{
uchar i, j;
i = 0;
for(i=0; i
{
j = 250;
while(--j);
j = 250;
while(--j);
}
}
/***************************************************/
Delay x milliseconds
/************************************************/
void delay_ms(uchar x)
{
uchar i, j;
i = 0;
for(i=0; i
j = 250;
while(--j);
j = 250;
while(--j);
}
}
/***************************************************/
/**************************************************
Function: SPI_RW()
Function: SPI_RW()
Description:
According to the SPI protocol, write one byte of data to nRF24L01 and
read one byte from nRF24L01 at the same time
. /**************************************************/
uchar SPI_RW(uchar byte)
{
uchar i;
for(i=0; i<8; i++) // Loop 8 times
{
MOSI = (byte & 0x80); // byte outputs the highest bit to MOSI
byte <<= 1; // shift the lower bit to the highest bit
SCK = 1; // pull SCK high, nRF24L01 reads 1 bit of data from MOSI and outputs 1 bit of data from MISO
byte |= MISO; // read MISO to the lowest bit of byte
SCK = 0; // SCK is set low
}
return(byte); // return the read byte
}
/******************************************************/
According to the SPI protocol, write one byte of data to nRF24L01 and
read one byte from nRF24L01 at the same time
. /**************************************************/
uchar SPI_RW(uchar byte)
{
uchar i;
for(i=0; i<8; i++) // Loop 8 times
{
MOSI = (byte & 0x80); // byte outputs the highest bit to MOSI
byte <<= 1; // shift the lower bit to the highest bit
SCK = 1; // pull SCK high, nRF24L01 reads 1 bit of data from MOSI and outputs 1 bit of data from MISO
byte |= MISO; // read MISO to the lowest bit of byte
SCK = 0; // SCK is set low
}
return(byte); // return the read byte
}
/******************************************************/
/**************************************************
Function: SPI_RW_Reg()
Function: SPI_RW_Reg()
Description:
Write data value to register reg
/************************************************/
uchar SPI_RW_Reg(uchar reg, uchar value)
{
uchar status;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
SPI_RW(value); // Then write data to the register
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}
/******************************************************/
Write data value to register reg
/************************************************/
uchar SPI_RW_Reg(uchar reg, uchar value)
{
uchar status;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
SPI_RW(value); // Then write data to the register
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}
/******************************************************/
/**************************************************
Function: SPI_Read()
Function: SPI_Read()
Description:
Read one byte from register reg
/************************************************/
uchar SPI_Read(uchar reg)
{
uchar reg_val;
CSN = 0; // Set CSN low to start data transmission
SPI_RW(reg); // Select register
reg_val = SPI_RW(0); // Then read data from this register
CSN = 1; // Pull CSN high to end data transmission
return(reg_val); // Return register data
}
/******************************************************/
Read one byte from register reg
/************************************************/
uchar SPI_Read(uchar reg)
{
uchar reg_val;
CSN = 0; // Set CSN low to start data transmission
SPI_RW(reg); // Select register
reg_val = SPI_RW(0); // Then read data from this register
CSN = 1; // Pull CSN high to end data transmission
return(reg_val); // Return register data
}
/******************************************************/
/**************************************************
Function: SPI_Read_Buf()
Function: SPI_Read_Buf()
Description:
Read bytes bytes from the reg register, usually used to read the receiving channel
data or the receiving/sending address./****************************************************/
uchar
SPI_Read_Buf(uchar reg, uchar * pBuf, uchar bytes)
{
uchar status, i;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
for(i=0; i
pBuf[i] = SPI_RW(0); // Read bytes from nRF24L01 one by one
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}[page]
/******************************************************/
Read bytes bytes from the reg register, usually used to read the receiving channel
data or the receiving/sending address./****************************************************/
uchar
SPI_Read_Buf(uchar reg, uchar * pBuf, uchar bytes)
{
uchar status, i;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
for(i=0; i
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}[page]
/******************************************************/
/**************************************************
Function: SPI_Write_Buf()
Function: SPI_Write_Buf()
Description:
Write the data in the pBuf buffer to the nRF24L01, usually used to write the
transmit channel data or receive/send address
/**************************************************/
uchar SPI_Write_Buf(uchar reg, uchar * pBuf, uchar bytes)
{
uchar status, i;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
for(i=0; i
SPI_RW(pBuf[i]); // Write to nRF24L01 byte by byte
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}
/******************************************************/
Write the data in the pBuf buffer to the nRF24L01, usually used to write the
transmit channel data or receive/send address
/**************************************************/
uchar SPI_Write_Buf(uchar reg, uchar * pBuf, uchar bytes)
{
uchar status, i;
CSN = 0; // Set CSN low to start data transmission
status = SPI_RW(reg); // Select register and return status word
for(i=0; i
CSN = 1; // Pull CSN high to end data transmission
return(status); // Return to status register
}
/******************************************************/
/******************************************************
Function: RX_Mode()
Function: RX_Mode()
Description:
This function sets the nRF24L01 to receive mode, waiting to receive data packets from the transmitting device.
/**************************************************/
void RX_Mode(void)
{
CE = 0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // The receiving device receives channel 0 using the same transmit address as the transmitting device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable automatic response of receiving channel
0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable receiving channel 0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 0x40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select the same effective data width for receiving channel 0 as for transmitting channel
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // Data transmission rate 1Mbps, transmit power 0dBm, low noise amplifier gain
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // CRC enable, 16-bit CRC check, power on, receiving mode
CE = 1; // Pull CE high to start the receiving device
}
/******************************************************/
This function sets the nRF24L01 to receive mode, waiting to receive data packets from the transmitting device.
/**************************************************/
void RX_Mode(void)
{
CE = 0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // The receiving device receives channel 0 using the same transmit address as the transmitting device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable automatic response of receiving channel
0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable receiving channel 0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 0x40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select the same effective data width for receiving channel 0 as for transmitting channel
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // Data transmission rate 1Mbps, transmit power 0dBm, low noise amplifier gain
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // CRC enable, 16-bit CRC check, power on, receiving mode
CE = 1; // Pull CE high to start the receiving device
}
/******************************************************/
/**************************************************
Function: TX_Mode()
Function: TX_Mode()
Description:
This function sets nRF24L01 to transmit mode (CE=1 for at least 10us),
starts transmitting after 130us, and after data transmission is completed, the transmitting module automatically switches to receive
mode and waits for the response signal.
/******************************************************/
void TX_Mode(uchar * BUF)
{
CE = 0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Write transmit address
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // In order to respond to the receiving device, the receiving channel 0 address is the same as the transmit address
SPI_Write_Buf(WR_TX_PLOAD, BUF, TX_PLOAD_WIDTH); // Write data packet to TX FIFO
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable receive channel 0 to automatically respond
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable receive channel 0
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x0a); // Automatic retransmission delay wait 250us+86us, automatic retransmission 10 times
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 0x40
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // Data transmission rate 1Mbps, transmission power 0dBm, low noise amplifier gain
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // CRC enable, 16-bit CRC check, power-on
CE = 1;
}
/******************************************************/
This function sets nRF24L01 to transmit mode (CE=1 for at least 10us),
starts transmitting after 130us, and after data transmission is completed, the transmitting module automatically switches to receive
mode and waits for the response signal.
/******************************************************/
void TX_Mode(uchar * BUF)
{
CE = 0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Write transmit address
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // In order to respond to the receiving device, the receiving channel 0 address is the same as the transmit address
SPI_Write_Buf(WR_TX_PLOAD, BUF, TX_PLOAD_WIDTH); // Write data packet to TX FIFO
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable receive channel 0 to automatically respond
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable receive channel 0
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x0a); // Automatic retransmission delay wait 250us+86us, automatic retransmission 10 times
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 0x40
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // Data transmission rate 1Mbps, transmission power 0dBm, low noise amplifier gain
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // CRC enable, 16-bit CRC check, power-on
CE = 1;
}
/******************************************************/
/**************************************************
Function: Check_ACK()
Function: Check_ACK()
Description:
Check whether the receiving device has received the data packet, and set whether to resend if no acknowledgement
signal .
/************************************************/
uchar Check_ACK(bit clear)
{
while(IRQ);
sta = SPI_RW(NOP); // Return to status register
if(MAX_RT)
if(clear) // Whether to clear TX FIFO, if not clear, resend after resetting the MAX_RT interrupt flag
SPI_RW(FLUSH_TX);
SPI_RW_Reg(WRITE_REG + STATUS, sta); // Clear TX_DS or MAX_RT interrupt flag
IRQ = 1;
if(TX_DS)
return(0x00);
else
return(0xff);
}
/******************************************************/
Check whether the receiving device has received the data packet, and set whether to resend if no acknowledgement
signal .
/************************************************/
uchar Check_ACK(bit clear)
{
while(IRQ);
sta = SPI_RW(NOP); // Return to status register
if(MAX_RT)
if(clear) // Whether to clear TX FIFO, if not clear, resend after resetting the MAX_RT interrupt flag
SPI_RW(FLUSH_TX);
SPI_RW_Reg(WRITE_REG + STATUS, sta); // Clear TX_DS or MAX_RT interrupt flag
IRQ = 1;
if(TX_DS)
return(0x00);
else
return(0xff);
}
/******************************************************/
/**************************************************
Function: CheckButtons()
Function: CheckButtons()
Description:
Check if a button is pressed. If pressed, send a byte of data.
/******************************************************/
void CheckButtons()
{
P3 |= 0x00;
if(!(P3 & 0x01)) // Read P3^0 status
{
delay_ms(20);
if(!(P3 & 0x01)) // Read P3^0 status
{
TX_BUF[0] = ~DATA; // Data is sent to the buffer
TX_Mode(TX_BUF); // Set nRF24L01 to transmit mode and send data
LED = ~DATA; // Data is sent to LED for display
Check_ACK(1); // Wait for transmission to complete and clear TX FIFO
delay_ms(250);
delay_ms(250);
LED = 0xff; // Turn off LED
RX_Mode(); // Set to receive mode
while(!(P3 & 0x01));
DATA <<= 1;
if(!DATA)
DATA = 0x01;
}
}
}
/******************************************************/
Check if a button is pressed. If pressed, send a byte of data.
/******************************************************/
void CheckButtons()
{
P3 |= 0x00;
if(!(P3 & 0x01)) // Read P3^0 status
{
delay_ms(20);
if(!(P3 & 0x01)) // Read P3^0 status
{
TX_BUF[0] = ~DATA; // Data is sent to the buffer
TX_Mode(TX_BUF); // Set nRF24L01 to transmit mode and send data
LED = ~DATA; // Data is sent to LED for display
Check_ACK(1); // Wait for transmission to complete and clear TX FIFO
delay_ms(250);
delay_ms(250);
LED = 0xff; // Turn off LED
RX_Mode(); // Set to receive mode
while(!(P3 & 0x01));
DATA <<= 1;
if(!DATA)
DATA = 0x01;
}
}
}
/******************************************************/
/******************************************************
Function: main()
Function: main()
Description:
Main function
/******************************************************/
void main(void)
{
init_io(); // Initialize IO
RX_Mode(); // Set to receive modewhile
(1)
{
CheckButtons(); // Button scan
sta = SPI_Read(STATUS); // Read status registerif
(RX_DR) // Determine whether data is received
{
SPI_Read_Buf(RD_RX_PLOAD, RX_BUF, TX_PLOAD_WIDTH); // Read data from RX FIFOflag
= 1;
}
SPI_RW_Reg(WRITE_REG + STATUS, sta); // Clear RX_DS interrupt flagif
(flag) // Acceptance completed
{
flag = 0; // Clear flagLED
= RX_BUF[0]; // Data is sent to LED displaydelay_ms
(250);
delay_ms(250);
LED = 0xff; // Turn off LED
}
}
}
/******************************************************/
Main function
/******************************************************/
void main(void)
{
init_io(); // Initialize IO
RX_Mode(); // Set to receive modewhile
(1)
{
CheckButtons(); // Button scan
sta = SPI_Read(STATUS); // Read status registerif
(RX_DR) // Determine whether data is received
{
SPI_Read_Buf(RD_RX_PLOAD, RX_BUF, TX_PLOAD_WIDTH); // Read data from RX FIFOflag
= 1;
}
SPI_RW_Reg(WRITE_REG + STATUS, sta); // Clear RX_DS interrupt flagif
(flag) // Acceptance completed
{
flag = 0; // Clear flagLED
= RX_BUF[0]; // Data is sent to LED displaydelay_ms
(250);
delay_ms(250);
LED = 0xff; // Turn off LED
}
}
}
/******************************************************/
Previous article:ATMEGA16 drives internal EEPROM program
Next article:AMPIRE12864 Driver
Recommended ReadingLatest update time:2024-11-16 19:31
MSP430 BSL
For MSP430, simulation and programming can generally be done through the JTAG, SBW, and BSL interfaces. JTAG and SBW interfaces can be used for simulation, but BSL interfaces cannot be used for simulation. Programmers support all three interfaces. So it cannot be said that JTAG only supports simulation and not programm
[Microcontroller]
MSP430 button controls serial port to send data
#include msp430.h //MSP430G2553 - Use USCI_A0, Up Mode, DCO SMCLK // // Introduction: This program uses USCI_A0 to communicate with the computer and sends a data by pressing the button connected to the P1.3 port. // Baud rate 9600, data format 8N1 // // ACLK = 32768, SMCLK = 32768 MCLK = DCO-16M // //
[Microcontroller]
Is your MCU environmentally friendly enough?
As the concept of green energy conservation deepens, green design products are becoming popular. Embedded product developers need to ask semiconductor suppliers a question: "Is your MCU environmentally friendly?"
Since Intel successfully reduced the power consumption of the nearly 1,000-pin Pentium 4 to 150W in
[Microcontroller]
MSP430F5438 study notes DCO frequency multiplication to 8MHZ
1. Platform Description MS430F5438 // Clock defaults // FLL clock FLL select XT1 // Auxiliary clock ACLK selects XT1 32768Hz // Main system clock MCLK selection DCOCLKDIV 1048576Hz // Subsystem clock SMCLK selection DCOCLKDIV 1048576Hz #include msp430.h void clock_config(void); void select_xt
[Microcontroller]
Design and implementation of programmable filter based on MSP430 and MAX262
In electronic systems, filters are an indispensable and important link in data acquisition and signal processing, such as noise filtering before signal acquisition, "ladder-shaped" filtering of D/A conversion output, etc. The general active filter is composed of operational amplifiers and RC components, but the c
[Microcontroller]
Design of wireless remote control pointer based on MSP430F149
In traditional teaching, teachers use blackboard and chalk as the main teaching tools. This teaching method is single, the classroom efficiency is low, and it cannot arouse students' interest in learning. The rise of multimedia-assisted teaching mode makes up for the shortcomings of traditional teaching. In actual o
[Microcontroller]
msp430 button control LED light
#include "io430.h" /* There are two ways to control the on and off of the LED light. Method 1: Interrupt function. Method 2: Use if(P4IN&BIT2) to judge. When writing code, try to follow the steps to avoid wasting a lot of time looking for bugs due to negligence. */ int main( void ) { // Stop wat
[Microcontroller]
MSP430 Comprehensive Solution for Medical Electronics
Since the birth of MSP430 in the 1990s, the ultra-low power family has continued to expand, and this DNA will continue. They can always find the resources they need and complete technical solutions from TI's MSP430 product site.
In daily life, we are exposed to many medical electronic products, including blood
[Microcontroller]
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Linux kernel startup process - Xunwei IMX6ULL development board (I)
- Analysis of the whole process of C language compilation
- How to replace potentiometers with programmable devices in analog circuit design?
- BMW Cooling System and Electric Coolant Pump Components (Electronic Water Pump) Functional Characteristics and Standards
- Inline instructions for TMS320C66x study notes
- Electric mask solutions
- 【Project Source Code】FPGA-based Ethernet design tutorial and program source code
- [ESP32-Audio-Kit Audio Development Board Review] Part 3: Comments on the Installation of the Development Environment
- Caibo International Customer Service Life Quotes
- Are there any good high current sensor chips now?