ZigBee journey (ii): The first cc2430 Program-led flashing Experiment

Source: Internet
Author: User
Document directory
  • (1) create a workspace and a project
  • (2) Configure general options
  • (3) linker settings
  • (4) debugger settings:
  • (1) create a program file
  • (2) Introduce header files
  • (3) define led pins
  • (4) Main Function
  • (5) code Overview
I. Connecting

In the previous article "ZigBee journey (1): setting up the ground", we briefly introduced Zigbee and the establishment of its development environment. Okay, now the tools are complete, and a problem arises: how can we use these software and hardware to compile a program that can run?

This article explains how to configure IAR to develop a cc2430-based program and how to use it.IARCompile and debug the program online.

Ii. IAR Configuration

IAR is a powerful embedded development platform that supports many types of chips. Each project in IAR can have its own configuration, including the device type, heap/stack, linker, and debugger.

(1) create a workspace and a project

Create a folder named ledtest. Open IAR and choose File> New> workspace from the main menu to create a new work area.

Select project-> Create new project-> Empty Project, click OK, and save the project file to the ledtest folder named ledtest. EWP (for example ).

(2) Configure general options

Target setting: Device: cc2430;

Code mode: near;

Data Model: large;

Calling convention: xdata stack reetrant

Data Pointer settings: Number of dptrs: 1

Stack/heap settings: xdata stack size: 0x1ff

(3) linker settings

Linker command file: Select lnk51ew_cc2430.xcl


(4) debugger settings:

Driver: Texas Instruments)
Device description file: cc2430.ddf

Now, the IAR configuration for this experiment has basically ended. The encoding implementation is described below.

3. Programming (1) creating a program file

Select File> New> file to create the main. c file.

(2) Introduce header files

The cc2430-based program must contain a reference to iocc2430.h. This file defines address ing of various special function registers (SFR) of cc2430.

# Include <iocc2430.h> // introduce the header file corresponding to cc2430 (including the definition of each SFr)

This file is built in IAR (similar to stdio. h). Right-click this line of code and selectOpen "iocc2430.h"To view all the contents of this header file.

(3) define led pins

View the circuit diagram of the Development Board as follows:

We can see that led1 ~ 4 pin p1_0 ~ P4_0 control. Therefore, led1, led2, led3, and led4 can be defined as p1_0, p2_0, p3_0, and p4_0.

# Define led1 p1_0 // define led1 as p1_0 Port Control
# Define led2 p1_1 // define led2 as p1_1 Port Control
# Define led3 p1_2 // define led3 as p1_2 Port Control
# Define led4 p1_3 // define led4 as p1_3 Port Control
(4) Main Function

Next, write the main function.

First, use p1.0 ~ Before p1.4, you need to set the port's working mode and input/output direction, which involves two SFr: p1sel and p1dir.

P1sel = 0x00; // set P1 to a common I/O port
P1dir | = 0x0f; // set p1.0 p1.1 p1.2 p1.3 as output ZigBee tips

Cc2430 hasP0_0 ~ P0_7,P1_0 ~ P1_7,P2_0 ~ P2_7A total of 21 I/O Ports. They can be used as common digital I/O, and can be used to connect ADC, timing/counter, usart, and other peripheral Io.

Three registers are available in SFR of cc2430 for configuring these Io ports:

Pxsel(X is 0/1/2): P0/P1/P2 port function Selection

0: General Digital Io, 1: Peripheral Io, default: 0

Pxdir(X 0/1/2): P0/P1/P2 port direction

0: input, 1: output. The default value is 0.

Pxindium(X is 0/1): P0/P1 port input mode

0: Pull up/drop down; 1: three States; default value: 0

It must be configured when I/O port is used. If the default value is used, the default value is used.

Then initialize four LED lights and set them to "completely off:

Led1 = 1;
Led2 = 1;
Led3 = 1;
Led4 = 1;

Finally, write the code for flashing the LED light:

Led1 = 0; // led1 flashes
Delay (10 );
Led1 = 1;
Delay (10 );

Led2 = 0; // led2 flashes
Delay (10 );
Led2 = 1;
Delay (10 );

Led3 = 0; // led3 flashes
Delay (10 );
Led3 = 1;
Delay (10 );

Led4 = 0; // led4 flashes
Delay (10 );
Led4 = 1;
Delay (10 );

Here, a latency subfunction delay (unsigned char N) is involved ):

Void delay (unsigned char N)

{
Unsigned char I;
Unsigned Int J;
For (I = 0; I <n; I ++)
For (j = 1; j ++)
;
}

(5) code Overview

Merge the above code into a whole, as shown below:

// Introduce the header file
# Include <iocc2430.h> // introduce the header file corresponding to cc2430 (including the definition of each SFr)

// Define the LED pin
# Define led1 p1_0 // define led1 as p1_0 Port Control
# Define led2 p1_1 // define led2 as p1_1 Port Control
# Define led3 p1_2 // define led3 as p1_2 Port Control
# Define led4 p1_3 // define led4 as a p1_3 port control // a delay subroutine
Void delay (unsigned char N)

{
Unsigned char I;
Unsigned Int J;
For (I = 0; I <n; I ++)
For (j = 1; j ++)
;
}

Void main (void)
{
P1sel = 0x00; // set P1 to a common I/O port
P1dir | = 0x0f; // set p1.0 p1.1 p1.2 p1.3 as output

Led1 = 1; // initialization, all four LED lights go out
Led2 = 1;
Led3 = 1;
Led4 = 1;

While (1) // start the loop
{
Led1 = 0; // led1 flashes
Delay (10 );
Led1 = 1;
Delay (10 );

Led2 = 0; // led2 flashes
Delay (10 );
Led2 = 1;
Delay (10 );

Led3 = 0; // led3 flashes
Delay (10 );
Led3 = 1;
Delay (10 );

Led4 = 0; // led4 flashes
Delay (10 );
Led4 = 1;
Delay (10 );
}
}

OK. The code for this small experiment is compiled. Isn't it very simple ~

Iv. Compilation and debugging

Select project-> make to compile the Code. If the code is successfully compiled, the following output is displayed:

PressZigBee Development Board → debugger → pc usb InterfaceConnect to the ZigBee device sequentially, and select project-> Debug. The program will be automatically downloaded to the Development Board.

Then select debug-> go to start the program. Then, you will see four LED lights flashing in turn! Although this is a simple little experiment that cannot be simple, but when I successfully implement it, I am still a little excited ~ Haha!

V. Conclusion

Based on the "LED light flashing experiment", this article introduces the entire implementation process from the IAR configuration, program code compilation, and compilation and Debugging Processes. In the next article, we will introduce several basic experiments developed by cc2430 based on the basic process of the development program.Timer,Serial Communication,Ad ing,System sleep and watchdogWait, so stay tuned!

Next article: ZigBee journey (III): several important cc2430 Basic Experiments-External interruptions

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.