This article is a mirror article of machine translation, please click here to jump to the original article.

View: 7554|Reply: 0

Zigbee Journey (5): Several important CC2430 basic experiments - serial communication

[Copy link]
Posted on 10/30/2014 11:18:18 PM | | | |
1. Connecting the upper and lower levels

In a wireless sensing network, the CC2430 needs to send the collected data to the host computer (i.e., PC) for processing, and the host computer needs to send control information to the CC2430. All of this is inseparable from the transmission of information between the two. In this section, we will learn how to implement serial port communication between the PC and the CC2430.

The CC2430 includes 2 serial communication interfacesUSART0andUSART1, each serial port includes two modes:UART(asynchronous) mode,SPI(synchronous) mode, this section only deals with UART mode).

2. Serial port communication experiment(1) Introduction to the experiment

Realize the communication between the development board and the PC: The PC sends a certain string to the CC2430, and the CC2430 returns this string to the PC after receiving it.

(2) Experimental preparation

Before starting to write code, you need to set up the hardware facilities: correct connection + install the USB serial driver.

Two hardware connections are required:

    The JTAG port of the CC2430 development board → the debugger → the USB port of the PC(For debugging and downloading programs)

    Serial port of CC2430 development board → USB port of PC(For data communication between PC and CC2430)

Then you need to install the USB to serial driver (Download address

In order to send data to the serial port, a serial port debugging tool (Download address)。

(3) Program flow chart

(4) Experimental source code and analysis/*
  Experimental description: UART0, baud rate 115200bps, PC sends a string to CC2430 (ending with @ character), CC2430 returns the string after receiving it
*/

#include

unsigned charrecv_buf[300] = {0};
unsigned charrecv_count =0;


/*系统时钟初始化
-------------------------------------------------------*/
voidxtal_init(void)
{
  SLEEP &= ~0x04;            //都上电
  while(! (SLEEP &0x40));     //晶体振荡器开启且稳定
  CLKCON &= ~0x47;            Choose a 32MHz crystal oscillator
  SLEEP |=0x04;
}

/*UART0通信初始化
-------------------------------------------------------*/
voidUart0Init(unsigned charStopBits,unsigned charParity)
{
   PERCFG&= ~0x01;                  Select UART0 as the first optional position, that is, RXD to P0.2 and TXD to P0.3
   P0SEL |=  0x0C;                  //初始化UART0端口,设置P0.2与P0.3为外部设备IO口

   U0CSR =0xC0;                    Set to UART mode and enable the receiver
   
   U0GCR = 11;
   U0BAUD = 216;                     Set the UART0 baud rate to 115200bps, as for why it is 216 and 11, please refer to the CC2430 Chinese manual
   
   U0UCR |= StopBits| Parity;        //设置停止位与奇偶校验
}

/*UART0发送数据
-------------------------------------------------------*/
void  Uart0Send(unsigned chardata)
{
  while(U0CSR&0x01);   //等待UART空闲时发送数据
  U0DBUF = data;
}

/*UART0发送字符串
-------------------------------------------------------*/
voidUart0SendString(unsigned char*s)
{
  while(*s !=0)         //依次发送字符串s中的每个字符
    Uart0Send(*s++);
}

/*UART0接受数据
-------------------------------------------------------*/
unsigned charUart0Receive(void)
{
  unsigned chardata;
  while(! (U0CSR&0x04));//查询是否收到数据,否则继续等待
  data=U0DBUF;         //提取接收到的数据
  returndata;         
}

/*主函数
-------------------------------------------------------*/
voidmain(void)
{
  unsigned chari,b;
  
  xtal_init();

  Uart0Init(0x00,0x00);  //初始化UART0,设置1个停止位,无奇偶校验
  Uart0SendString("Please Input string ended with '@'!");

  recv_count =0;
  
  while(1)
  {
    while(1)                           
    {
      b = Uart0Receive(); UART
      if(b=='@')break;                 If '@' is received, it jumps out of the loop and outputs the string

      recv_buf[recv_count] = b;        If it is not '@', continue to add characters to the character array recv_buf[]
      recv_count++;
    }
    for(i=0; i<recv_count; i++)="" [color="rgb(0," 136,="" 0)]="" output string[="" i][="" color]
      Uart0Send(recv_buf);
   
    Uart0SendString("");
    recv_count =0;                     //重置
  }
}

First, configure the I/O port corresponding to USART0: pass the pairPECFRG.0Set UART0 to optional position 1, i.e., P0.2 for RXD and P0.3 for TXD. Then configure P0.2 and P0.3 as external device I/O.

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

Then send a string to the PC: Please Input string ended with'@'! , and then use while(1) to keep trying to get every character received. When this character does not do it'@', it means that the input is not complete, and continue to add this character to the character array recv_buf; This character is just right'@', the input is complete, so the loop is sent to the PC in order for each character in the recv_buf to be sent to the PC, while resetting the recv_count.

(5) Experimental results

First, complete the hardware connection, open the serial port debugging tool, and configure the parameters as shown below:

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

Then enter "Hello" in the send textbox below the serial port debugging tool, as shown below:

After clicking "Send", you may wonder why the CC2430 does not return what you typed, because you did not end in @.

We type "Zigbee!@" again, click "Send" and the result looks like this:

The expected content "Hello Zigbee!" will appear. It's over! This is the end of the experiment~

3. Conclusion

This article describes the communication between the CC2430 development board and the host computer. Now that we have the basics of serial communication, let's learn about ADC (analog-to-digital conversion) single sampling in the next section. With ADC, we can collect the value of the temperature sensor on the development board and send the temperature value to the PC through the serial port to display it.






Previous:Zigbee Journey (4): Several important CC2430 basic experiments - timer interruption
Next:Zigbee Journey (6): Several important CC2430 basic experiments - ADC single sampling
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com