ZigBee journey (V): several important cc2430 Basic Experiments-Serial Communication

Source: Internet
Author: User
Document directory
  • (1) experiment Overview
  • (2) test preparation
  • (3) program flowchart
  • (4) experiment source code and Analysis
  • (5) Experiment Results

I. Connecting

In a wireless sensor network, cc2430 sends the collected data to the upper computer (PC) for processing, and the upper computer needs to send control information to cc2430. All of this is inseparable from the transfer of information between the two. In this section, we will learn how to implement serial communication between PC and cc2430.

Cc2430 includes two serial communication interfacesUsart0AndUsart1Each serial port has two modes:UART(Asynchronous) mode,SPI(Synchronous) mode. This section only applies to UART mode ).

Ii. Serial Communication Experiment (1) experiment Introduction

Communication between the Development Board and the PC: The PC sends a string to cc2430, Which is returned to the PC after receiving the string.

(2) test preparation

Before writing the code, you need to set up the hardware: Correct Connection + install the USB conversion driver.

Two hardware connections are required:

    JTAG port of cc2430 Development Board → debugger → USB port of PC(For program debugging and downloading)

    Cc2430 Development Board serial port → pc usb port(For data communication between PC and cc2430)

Then you need to install the USB to serial drive ()

To send data to the serial port, a serial port debugging tool () is also required ().

(3) program flowchart

(4) experiment source code and analysis /*
Lab Description: uart0, baud rate 115200bps. The PC sends a string to cc2430 (ended with the @ character). After receiving the string from cc2430, the string is returned.
*/

# Include <iocc2430.h>

Unsigned char recv_buf [300] = {0 };
Unsigned char recv_count = 0;

/* System clock Initialization
-------------------------------------------------------*/
Void xtal_init (void)
{
Sleep & = ~ 0x04; // all powered on
While (! (Sleep & 0x40); // The crystal oscillator is enabled and stable.
Clkcon & = ~ 0x47; // select a 32 MHz Crystal Oscillator
Sleep | = 0x04;
}

/* Uart0 communication Initialization
-------------------------------------------------------*/
Void uart0init (unsigned char stopbits, unsigned char parity)
{
Percfg & = ~ 0x01; // select uart0 as the optional position 1, that is, rxd is connected to limit 2, txd is connected to limit 3
P0sel | = 0x0c; // initialize port uart0 and set Port 2 and Port 3 as the external device IO port

U0csr = 0xc0; // set it to UART mode and enable the receiver

U0gcr = 11;
U0baud = 216; // set the baud rate of uart0 to 115200bps. For details about why it is 216 and 11, refer to the cc2430 Chinese manual.

U0ucr | = stopbits | parity; // sets the stop bit and parity.
}

/* Uart0 sends data
-------------------------------------------------------*/
Void uart0send (unsigned char data)
{
While (u0csr & 0x01); // send data when the UART is idle
U0dbuf = data;
}

/* Uart0 sending string
-------------------------------------------------------*/
Void uart0sendstring (unsigned char * s)
{
While (* s! = 0) // send each character in string s in sequence
Uart0send (* s ++ );
}

/* Uart0 receives data
-------------------------------------------------------*/
Unsigned char uart0receive (void)
{
Unsigned char data;
While (! (U0csr & 0x04); // query whether data is received. Otherwise, wait.
Data = u0dbuf; // extract the received data
Return data;
}

/* Main Function
-------------------------------------------------------*/
Void main (void)
{
Unsigned char I, B;

Xtal_init ();

Uart0init (0x00); // initialize uart0 and set one stop bit Without parity
Uart0sendstring ("Please input string ended '@'! \ R \ n ");

Recv_count = 0;

While (1)
{
While (1)
{
B = uart0receive (); UART
If (B = '@') break; // If '@' is received, the loop jumps out and the string is output.

Recv_buf [recv_count] = B; // if it is not '@', add a character to the character array recv_buf []
Recv_count ++;
}
For (I = 0; I <recv_count; I ++) // output string
Uart0send (recv_buf [I]);

Uart0sendstring ("\ n ");
Recv_count = 0; // Reset
}
}

First, configure the I/O port corresponding to usart0:Pecfrg.0Set uart0 to the optional position 1, that is, rxd corresponds to limit 2 and txd corresponds to limit 3. Configure route 2 and Route 3 as the external device I/O.

Select the UART mode and enable the receiver. Configure the usart0 parameter: baud rate 115200, no parity check, stop bit is 1.

Then, send a string to the PC: Please input string ended '@'!, Then we use while (1) to constantly try to get every character received. When this character is not '@', it indicates that the input is not complete. Add the character to the character array recv_buf. When this character is exactly, this indicates that the input is complete. Therefore, every character in recv_buf is sent to the PC in order and recv_count is reset.

(5) Experiment Results

First, complete the hardware connection and open the serial port debugging tool. The configuration parameters are as follows:

Click "open serial port" and start IAR debugging to run the program. You will find the expected string in the receiving box on the serial port debugging tool:

Enter "hello" in the send textbox below the serial port debugging tool, as shown below:

After clicking "send", you may wonder why cc2430 does not return the content you entered, because you didn't end.

Enter "ZigBee!" again! @ ", Click" send ", and the result is as follows:

The expected content "Hello ZigBee!" appears !" Now! The experiment ends ~

Iii. Conclusion

This article describes the communication between the cc2430 Development Board and the host computer. With the basics of serial communication, let's take a single sample of ADC (analog-to-digital conversion) in the next section. With ADC, we can collect the temperature sensor value on the Development Board and send the temperature value to the PC through the serial port.

Next section: ZigBee journey (6): several important cc2430 Basic Experiments-ADC single sampling (unfinished)

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.