|
1. Connecting the upper and lower levels In the previous articleZigbee Journey (1): Breaking the Grounds, we briefly introduce Zigbee and its development environment. OK, now that the tools are complete, a question arises: how to use these software and hardware to write a program that can run? This article is basically to answer the above questions: Take the small experiment of "LED light flashing" as an example to introduce how to configure IAR to be suitable for developing CC2430-based programs, and how to use itIARWrite and debug programs online. 2. IAR configuration IAR is a powerful embedded development platform that supports a wide variety of chips. Each project in IAR can have its own configuration, including device type, stack/stack, linker, debugger, etc. (1) Create a new Workspace and Project First, create a new folder ledtest. Open IAR and select the main menu File -> New -> Workspace to create a new workspace. Select Project -> Create New Project -> Empty Project, click OK, and save the project file to the folder ledtest, named ledtest.ewp (as shown below).
(2) Configure General Options Target setting: Device: CC2430; Code Mode:Near; Data model:Large; Calling convention:XDATA stack reetrant
Data pointer setting: Number of DPTRs: 1
Stack/Heap setting: XDATA stack size: 0x1FF
(3) Linker settings Linker command file: Select lnk51ew_cc2430.xcl
(4) Debugger settings: Driver: Texas Instruments (This experiment is a real machine debugging, so choose TI; If other programs want to use the IAR simulator, you can choose Simulator) Device Description file:CC2430.ddf
At this point, the IAR configuration for this experiment is basically over, and the following is an introduction to its coding implementation. 3. Writing program code(1) Create a new procedural document Select File->New->File and create a new file main.c. (2) Introduce header files CC2430-based programs must include a reference to ioCC2430.h, which defines the address mapping of the CC2430's various special function registers (SFRs). #include //引入CC2430所对应的头文件(包含各SFR的定义)
This file is built into IAR (similar to stdio.h), hover over this line of code, right-click, selectOPen "ioCC2430.h"to see the full contents of this header file. (3) Define the LED pins View the development board circuit diagram as follows:
It can be seen that LED1~4 is controlled by pins P1_0~P4_0 respectively, so LED1, LED2, LED3, and LED4 can be defined as pins P1_0, P2_0, P3_0, and P4_0 respectively. #define LED1 P1_0 // Define LED1 as the 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, start writing the main function. First of all, before using the P1.0~P1.4 port, it is necessary to set its working mode and input/output direction, which involves two SFRs: P1SEL and P1DIR. P1SEL = 0x00; Set P1 to the normal I/O port P1DIR |= 0x0F; Set P1.0, P1.1, P1.2, P1.3 as the output
Zigbee Tips The CC2430 has:P0_0 ~ P0_7 , P1_0~P1_7 , P2_0~P2_7A total of 21 I/O ports. They can be used as general-purpose digital I/Os or can be used to connect peripheral IOs such as ADCs, timing/counters, or USARTs. There are three classes of registers in the CC2430's SFR that can be used to configure these IO ports: ①PxSEL(x is 0/1/2) :P 0/P1/P2 port function selection 0: Universal numeric IO, 1: Peripheral IO, the default is 0 ②PxDIR(x is 0/1/2) :P 0/P1/P2 port direction 0: input, 1: output, the default is 0 ③PxINP(x is 0/1) :P 0/P1 port input mode 0: Pull up/pull down, 1: Three states, the default is 0 It needs to be configured when using the IO port, and if it is default, the default value of the system is taken. Then initialize the 4 LEDs and set them to all extinguish: led1 = 1; led2 = 1; led3 = 1; led4 = 1;
Finally, write the LED light flashing effect code: led1 = 0; LED1 flashes Delay(10); led1 = 1; Delay(10);
led2 = 0; LED2 flashes Delay(10); led2 = 1; Delay(10);
led3 = 0; LED3 flashing Delay(10); led3 = 1; Delay(10);
led4 = 0; LED4 flashing Delay(10); led4 = 1; Delay(10);
It involves a delay subfunction Delay(unsigned char n): void Delay(unsigned char n) { unsigned char i; unsigned int j; for(i = 0; i < n; i++) for(j = 1; j; j++) ; }
(5) Code overview Combine the above code into a single whole, as follows: //引入头文件
#include //引入CC2430所对应的头文件(包含各SFR的定义)
//定义LED引脚
#define led1 P1_0 //定义LED1为P1_0口控制
#define led2 P1_1 //定义LED2为P1_1口控制
#define led3 P1_2 //定义LED3为P1_2口控制
#define led4 P1_3 //定义LED4为P1_3口控制 //延时子程序
voidDelay(unsigned charn) {
unsigned chari;
unsigned intj;
for(i =0; i < n; i++)
for(j =1; j; j++) ;
}
voidmain(void)
{ P1SEL =0x00; Set P1 to the normal I/O port P1DIR |=0x0F; Set P1.0, P1.1, P1.2, P1.3 as the output
led1 =1; //初始化,4个led灯全熄 led2 =1; led3 =1; led4 =1;
while(1) //开始循环 { led1 =0; //led1闪烁 Delay(10); led1 =1; Delay(10);
led2 =0; //led2闪烁 Delay(10); led2 =1; Delay(10);
led3 =0; //led3闪烁 Delay(10); led3 =1; Delay(10);
led4 =0; //led4闪烁 Delay(10); led4 =1; Delay(10); }
}
OK, the code for this small experiment has been written, isn't it very simple, hehe~ 4. Compilation and debugging Select Project -> Make, compile the code, and if successful, the following output will appear:
PressZigbee Development Board → Debugger → USB interface for PCand then select Project -> Debug, and the program will be automatically downloaded to the board. Then select Debug -> Go to start the program, and you will see 4 LED lights flashing in turn! Although this is a small experiment that is too simple to be simple, when the author successfully realized it, I was still a little excited~ hehe! 5. Conclusion Based on the "LED light flashing experiment", this paper introduces the whole implementation process from the configuration of IAR, the writing of program code, and the process of compilation and debugging. In the next article, based on understanding the basic process of developing the program, we will introduce several basic experiments for CC2430 developmentTimer、Serial communication、AD Conversion、Systematic sleep as well as watchdogsetc., so stay tuned!
|