Zigbee Tour (V): Several important CC2430 basic experiments-serial communication __zigbee

Source: Internet
Author: User
ZigBee Tour (v): Several important CC2430 basic experiments--serial communication first, the connecting link

In the wireless sensor network, the CC2430 needs to send the collected data to the host computer (ie pc), while the host computer needs to transmit the control information to the CC2430. All this is inseparable from the transmission of information between the two. In this section, we will learn how to realize the serial communication between PC and CC2430.

CC2430 includes 2 serial communication interfaces USART0 and USART1, each serial port consists of two modes: UART (asynchronous) mode, SPI (sync) mode, this section only covers UART mode). second, serial communication experiment (1) Introduction to the experiment

Realize the communication between the Development Board and PC: The PC sends a string to CC2430 and CC2430 returns this string to the PC after receiving it. (2) Experimental preparation

Before you start coding, you need to build your hardware: Properly connect + install USB drive.

Hardware wiring requires two:

CC2430 Development Board Jtag port → Debugger →pc USB port (for program debugging, download)

CC2430 Development Board serial Port →PC USB port (for PC and CC2430 data communication)

Then you need to install USB to the serial port driver (download address)

In order to send data to the serial port, you need a serial debugging tool (download address). (3) Program flow chart

(4) Experimental source and analysis/*
Experiment instruction: UART0, baud rate 115200bps,pc machine sends string to CC2430 (ends with @ character), CC2430 returns the string when it is received
*/

#include <ioCC2430.h>

unsignedCharRECV_BUF [300] = {0};
unsignedCharRecv_count = 0;


/* System Clock initialization
-------------------------------------------------------*/
voidXtal_init (void)
{
Sleep &= ~ 0x04; All on the electricity.
while( !      (Sleep & 0x40)); Crystal oscillator Open and stable
Clkcon &= ~ 0x47; Select 32MHz Crystal Oscillator
Sleep |= 0x04;
}

/*UART0 Communication Initialization
-------------------------------------------------------*/
voidUart0init (unsignedCharStopBits,unsigned CharParity)
{
Percfg &= ~ 0x01; Select UART0 as optional position one, that is RXD connect P0.2,txd connect P0.3
P0sel |= 0x0C; Initialize the UART0 port and set P0.2 and P0.3 as external device IO ports

U0CSR = 0xc0; Set to UART mode and enable receiver

U0GCR = 1 1;
U0baud = 216; Set UART0 baud rate to 115200bps, as for why 216 and 11, can consult CC2430 Chinese manual

U0UCR |= stopbits|         Parity; Set stop bit and parity
}

/*uart0 Send data
-------------------------------------------------------*/
voidUart0send (unsignedCharData
{
while(U0CSR & 0x01); Wait for UART to send data when idle
U0DBUF = data;
}

/*uart0 Send string
-------------------------------------------------------*/
voidUart0sendstring (unsignedChar*s)
{
while(*s!= 0)//send each character in the string s sequentially
Uart0send (*s + +);
}

/*uart0 Accept Data
-------------------------------------------------------*/
unsignedCharUart0receive (void)
{
unsignedCharData
while( !  (U0CSR & 0x04)); Query whether or not to receive data, or continue to wait
data = U0dbuf; Extract the Received data
returnData
}

/* Main function
-------------------------------------------------------*/
voidMainvoid)
{
unsignedCharI, B;

Xtal_init ();

Uart0init (0x00, 0x00); Initialize UART0, set 1 stop bits, no parity
Uart0sendstring ("Please Input string ended with ' @ '!) \ r \ n ");

Recv_count = 0;

while(1)
{
while(1)
{
b = uart0receive (); Uart
if(b = = ' @ ') Break; If receive ' @ ', then jump out of loop, output string

Recv_buf [Recv_count] = b; If not ' @ ', continue adding characters 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 USART0 corresponding I/O port: by pecfrg.0 Zero set UART0 is optional location 1, that is rxd corresponding p0.2,txd corresponding P0.3. Then configure P0.2 and P0.3 for external device I/O.

Then select UART mode and enable the receiver. Then configure the USART0 parameters: Baud rate 115200, no parity, stop bit is 1.

Then send a string to the PC: please Input string ended with ' @ '. , and then use while (1) to constantly try to get each character received. When this character is not ' @ ', indicates that the completion has not yet been entered, continues to add the character to the character array recv_buf, and when this character is exactly ' @ ', the input completes, so stepping out of the loop sends each character in the Recv_buf to the PC in order, resetting the Recv_count. (5) Experimental results

First completes the hardware connection, opens the Serial Port debugging tool, the configuration parameter is as follows:

Click on "Open serial port", and then start IAR debugging, let the program run up, you will find the Serial debugging tool in the Receive box appears in the expected string:

Then, in the Send textbox below the Serial debugging tool, enter "Hello" as follows:

After clicking "Send", you may wonder why CC2430 will not return what you entered, because you are not using the @ end.

We enter "zigbee!@" again and click "Send", and the results are as follows:

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.