MSP430+nRF24L01 Program

Publisher:yunhuiLatest update time:2015-06-18 Source: 51heiKeywords:MSP430  nRF24L01 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
***************************************************
Source: Internet
Rewritten by: bluefeel
Time: 10-03-13
Unit: Guilin University of Technology
/******************************************************/
#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
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;
/***************************************************/
/**************************************************
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
}
/***************************************************/
/**************************************************
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);
    }
}
/***************************************************/
/**************************************************
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
}
/******************************************************/
/**************************************************
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
}
/******************************************************/
/**************************************************
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
}
/******************************************************/
/**************************************************
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]
/******************************************************/
/**************************************************
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
}
/******************************************************/
/******************************************************
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
}
/******************************************************/
/**************************************************
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;
}
/******************************************************/
/**************************************************
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);
}
/******************************************************/
/**************************************************
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;
        }
    }
}
/******************************************************/
/******************************************************
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
        }
    }
}
/******************************************************/
Keywords:MSP430  nRF24L01 Reference address:MSP430+nRF24L01 Program

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 and implementation of programmable filter based on MSP430 and MAX262
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]
Design of wireless remote control pointer based on MSP430F149
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]
MSP430 Comprehensive Solution for Medical Electronics
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号